Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Merge pull request #213 from metafacture/207-separateFixInterpretatio…
Browse files Browse the repository at this point in the history
…nFromRecordTransformation

Separate Fix expression interpretation from record data transformation.
  • Loading branch information
blackwinter authored Apr 25, 2022
2 parents 90cbde4 + 40c9dce commit b7c4462
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 132 deletions.
11 changes: 3 additions & 8 deletions metafix/src/main/java/org/metafacture/metafix/FixBind.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.metafacture.metafix;

import org.metafacture.metafix.api.FixContext;
import org.metafacture.metafix.fix.Expression;

import java.util.List;
import java.util.Map;
Expand All @@ -26,8 +25,7 @@ public enum FixBind implements FixContext {

list {
@Override
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final List<Expression> expressions) {
final RecordTransformer recordTransformer = metafix.getRecordTransformer();
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
final String scopeVariable = options.get("var");
Value.asList(record.get(options.get("path")), a -> {
for (int i = 0; i < a.size(); ++i) {
Expand All @@ -36,7 +34,7 @@ public void execute(final Metafix metafix, final Record record, final List<Strin
// with var -> keep full record in scope, add the var:
if (scopeVariable != null) {
record.put(scopeVariable, value);
recordTransformer.process(expressions);
recordTransformer.transform(record);
record.remove(scopeVariable);
}
// w/o var -> use the currently bound value as the record:
Expand All @@ -48,10 +46,7 @@ public void execute(final Metafix metafix, final Record record, final List<Strin
final Record scopeRecord = new Record();
scopeRecord.addAll(h);

recordTransformer.setRecord(scopeRecord);
recordTransformer.process(expressions);
recordTransformer.setRecord(record);

recordTransformer.transform(scopeRecord);
a.set(index, new Value(scopeRecord));
})
// TODO: bind to arrays (if that makes sense) and strings (access with '.')
Expand Down
8 changes: 1 addition & 7 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package org.metafacture.metafix;

import org.metafacture.metafix.api.FixFunction;
import org.metafacture.metafix.fix.Fix;
import org.metafacture.metamorph.api.Maps;
import org.metafacture.metamorph.maps.FileMap;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
Expand All @@ -52,9 +50,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
// TODO: Catmandu load path
final String includePath = metafix.resolvePath(includeFile);

final RecordTransformer recordTransformer = metafix.getRecordTransformer();
recordTransformer.setRecord(recordTransformer.transformRecord(
INCLUDE_FIX.computeIfAbsent(includePath, FixStandaloneSetup::parseFix)));
metafix.getRecordTransformer(includePath).transform(record);
}
},
nothing {
Expand Down Expand Up @@ -499,6 +495,4 @@ public void apply(final Metafix metafix, final Record record, final List<String>

private static final Random RANDOM = new Random();

private static final Map<String, Fix> INCLUDE_FIX = new HashMap<>();

}
64 changes: 37 additions & 27 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.metafacture.framework.helpers.DefaultStreamReceiver;
import org.metafacture.mangling.StreamFlattener;
import org.metafacture.metafix.fix.Expression;
import org.metafacture.metafix.fix.Fix;
import org.metafacture.metamorph.api.Maps;

import org.slf4j.Logger;
Expand All @@ -48,8 +47,8 @@
import java.util.function.BiConsumer;

/**
* Transforms a data stream sent via the {@link StreamReceiver} interface. Use
* {@link RecordTransformer} to transform based on a Fix DSL description.
* Transforms a data stream sent via the {@link StreamReceiver} interface. Uses
* {@link RecordTransformer} to transform records based on a Fix DSL description.
*
* @author Markus Michael Geipel (Metamorph)
* @author Christoph Böhme (Metamorph)
Expand All @@ -73,12 +72,12 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps { // checkstyle
private final Deque<Integer> entityCountStack = new LinkedList<>();
private final List<Closeable> resources = new ArrayList<>();
private final List<Expression> expressions = new ArrayList<>();
private final Map<String, RecordTransformer> fixCache = new HashMap<>();
private final Map<String, Map<String, String>> maps = new HashMap<>();
private final Map<String, String> vars = new HashMap<>();
private final RecordTransformer recordTransformer = new RecordTransformer(this);
private final RecordTransformer recordTransformer;
private final StreamFlattener flattener = new StreamFlattener();

private Fix fix;
private List<Value> entities = new ArrayList<>();
private Record currentRecord = new Record();
private StreamReceiver outputStreamReceiver;
Expand All @@ -88,37 +87,27 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps { // checkstyle
private int entityCount;

public Metafix() {
flattener.setReceiver(new DefaultStreamReceiver() {

@Override
public void literal(final String name, final String value) {
final String[] split = Value.split(name);
addValue(split[split.length - 1], new Value(value, name));
// TODO could this help with https://github.com/metafacture/metafacture-fix/issues/147?
// TODO use full path here to insert only once?
// new FixPath(name).insertInto(currentRecord, InsertMode.APPEND, new Value(value));
}
});
this(NO_VARS);
}

public Metafix(final Map<String, String> newVars) {
this();
vars.putAll(newVars);
init(newVars);
recordTransformer = null;
}

public Metafix(final String fixDef) throws FileNotFoundException {
this(fixDef, NO_VARS);
}

public Metafix(final String fixDef, final Map<String, String> vars) throws FileNotFoundException {
this(vars);
init(vars);

if (isFixFile(fixDef)) {
fixFile = fixDef;
fix = FixStandaloneSetup.parseFix(fixDef);
recordTransformer = getRecordTransformer(fixDef);
}
else {
fix = FixStandaloneSetup.parseFix(new StringReader(fixDef));
recordTransformer = getRecordTransformer(new StringReader(fixDef));
}
}

Expand All @@ -127,8 +116,25 @@ public Metafix(final Reader fixDef) {
}

public Metafix(final Reader fixDef, final Map<String, String> vars) {
this(vars);
fix = FixStandaloneSetup.parseFix(fixDef);
init(vars);
recordTransformer = getRecordTransformer(fixDef);
}

private void init(final Map<String, String> newVars) {
flattener.setReceiver(new DefaultStreamReceiver() {

@Override
public void literal(final String name, final String value) {
final String[] split = Value.split(name);
addValue(split[split.length - 1], new Value(value, name));
// TODO could this help with https://github.com/metafacture/metafacture-fix/issues/147?
// TODO use full path here to insert only once?
// new FixPath(name).insertInto(currentRecord, InsertMode.APPEND, new Value(value));
}

});

vars.putAll(newVars);
}

/*package-private*/ static boolean isFixFile(final String fixDef) {
Expand All @@ -149,8 +155,12 @@ public String resolvePath(final String path) {
}
}

public RecordTransformer getRecordTransformer() {
return recordTransformer;
public RecordTransformer getRecordTransformer(final String fixDef) {
return fixCache.computeIfAbsent(fixDef, k -> new RecordTransformer(this, FixStandaloneSetup.parseFix(k)));
}

private RecordTransformer getRecordTransformer(final Reader fixDef) {
return new RecordTransformer(this, FixStandaloneSetup.parseFix(fixDef));
}

public List<Expression> getExpressions() {
Expand All @@ -177,8 +187,8 @@ public void endRecord() {
throw new IllegalStateException(ENTITIES_NOT_BALANCED);
}
flattener.endRecord();
LOG.debug("End record, walking fix: {}", currentRecord);
currentRecord = recordTransformer.transform(fix);
LOG.debug("End record, walking Fix: {}", currentRecord);
recordTransformer.transform(currentRecord);
if (!currentRecord.getReject()) {
outputStreamReceiver.startRecord(recordIdentifier);
LOG.debug("Sending results to {}", outputStreamReceiver);
Expand Down
Loading

0 comments on commit b7c4462

Please sign in to comment.