use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class DefaultAtlasExpressionProcessorTest method testFilterSelect.
@Test
public void testFilterSelect() throws Exception {
FieldGroup source = populateComplexCollectionSourceField(null, AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID, "foo");
String expression = String.format("SELECT(FILTER(${%s:/testPathfoo<>}, ${/value} != 'foo1'), ${/value})", AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID, AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID);
recreateSession();
session.head().setSourceField(source);
DefaultAtlasExpressionProcessor.processExpression(session, expression);
assertFalse(session.hasErrors(), printAudit(session));
assertEquals(FieldGroup.class, session.head().getSourceField().getClass());
FieldGroup fieldGroup = (FieldGroup) session.head().getSourceField();
assertEquals("/testPathfoo<>/value", fieldGroup.getPath());
assertEquals(9, fieldGroup.getField().size());
Field child = fieldGroup.getField().get(0);
assertEquals("/testPathfoo<0>/value", child.getPath());
assertEquals("foo0", child.getValue());
child = fieldGroup.getField().get(1);
assertEquals("/testPathfoo<1>/value", child.getPath());
assertEquals("foo2", child.getValue());
}
use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class DefaultAtlasExpressionProcessorTest method testRepeatCount.
@Test
public void testRepeatCount() throws Exception {
Field source = populateSourceField(null, AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID, FieldType.STRING, "foo");
populateComplexCollectionSourceField(null, AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID, "count");
String expression = String.format("REPEAT(COUNT(${%s:/testPathcount<>/value}), ${%s:/testPathfoo})", AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID, AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID);
recreateSession();
FieldGroup wrapper = new FieldGroup();
wrapper.getField().add((Field) reader.sources.get("/testPathcount<>/value"));
wrapper.getField().add(source);
session.head().setSourceField(wrapper);
DefaultAtlasExpressionProcessor.processExpression(session, expression);
assertFalse(session.hasErrors(), printAudit(session));
assertEquals(FieldGroup.class, session.head().getSourceField().getClass());
FieldGroup fieldGroup = (FieldGroup) session.head().getSourceField();
assertEquals("/testPathfoo<>", fieldGroup.getPath());
assertEquals(10, fieldGroup.getField().size());
Field child = fieldGroup.getField().get(0);
assertEquals("/testPathfoo<0>", child.getPath());
assertNull(child.getIndex());
assertEquals("foo", child.getValue());
child = fieldGroup.getField().get(1);
assertEquals("/testPathfoo<1>", child.getPath());
assertNull(child.getIndex());
assertEquals("foo", child.getValue());
child = fieldGroup.getField().get(9);
assertEquals("/testPathfoo<9>", child.getPath());
assertNull(child.getIndex());
assertEquals("foo", child.getValue());
}
use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class DefaultAtlasPreviewContextTest method testProcessPreviewSourceFieldGroup.
@Test
public void testProcessPreviewSourceFieldGroup() throws AtlasException {
Mapping m = new Mapping();
Field source1 = new SimpleField();
source1.setFieldType(FieldType.STRING);
source1.setPath("/one");
source1.setIndex(0);
source1.setValue("one");
Field source2 = new SimpleField();
source2.setFieldType(FieldType.STRING);
source2.setPath("/two");
source2.setIndex(1);
source2.setValue("two");
FieldGroup group = new FieldGroup();
group.getField().add(source1);
group.getField().add(source2);
Expression action = new Expression();
action.setExpression("${0} + ' and ' + ${1}");
group.setActions(new ArrayList<>());
group.getActions().add(action);
m.setInputFieldGroup(group);
Field target = new SimpleField();
target.setFieldType(FieldType.STRING);
m.getOutputField().add(target);
previewContext.processPreview(m);
target = m.getOutputField().get(0);
assertEquals("one and two", target.getValue());
}
use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class DefaultAtlasContext method processSourceFieldMapping.
/**
* Processes source fields.
* @param session session
*/
protected void processSourceFieldMapping(DefaultAtlasSession session) {
try {
Mapping mapping = session.head().getMapping();
if (mapping.getInputFieldGroup() != null) {
if (mapping.getExpression() != null) {
session.head().setSourceField(mapping.getInputFieldGroup());
DefaultAtlasExpressionProcessor.processExpression(session, mapping.getExpression());
} else {
processSourceFieldGroup(session, mapping.getInputFieldGroup());
}
} else if (mapping.getInputField() != null && !mapping.getInputField().isEmpty()) {
if (mapping.getExpression() != null) {
FieldGroup sourceFieldGroup = new FieldGroup();
sourceFieldGroup.getField().addAll(mapping.getInputField());
session.head().setSourceField(sourceFieldGroup);
DefaultAtlasExpressionProcessor.processExpression(session, mapping.getExpression());
} else {
List<Field> sourceFields = mapping.getInputField();
applyCopyToActions(sourceFields, mapping);
processSourceFields(session, sourceFields);
}
} else {
session.head().addAudit(AuditStatus.WARN, null, String.format("Mapping does not contain expression or at least one source field: alias=%s desc=%s", mapping.getAlias(), mapping.getDescription()));
}
} catch (Exception t) {
Field sourceField = session.head().getSourceField();
String docId = sourceField != null ? sourceField.getDocId() : null;
String path = sourceField != null ? sourceField.getPath() : null;
session.head().addAudit(AuditStatus.ERROR, sourceField, String.format("Unexpected exception is thrown while reading source field: %s", t.getMessage()));
if (LOG.isDebugEnabled()) {
LOG.error("", t);
}
}
}
use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class DefaultAtlasFunctionResolver method resolve.
@Override
public Expression resolve(final String name, List<Expression> args) throws ParseException {
String functionName = name.toUpperCase();
FunctionFactory f = functions.get(functionName);
if (f != null) {
return f.create(args);
} else {
// lookup action
return (ctx) -> {
List<Field> arguments = new ArrayList<>();
for (Expression arg : args) {
arguments.add(arg.evaluate(ctx));
}
Object valueForTypeEvaluation = null;
if (arguments.isEmpty()) {
return null;
} else {
valueForTypeEvaluation = arguments.get(arguments.size() - 1);
}
ActionProcessor actionProcessor = fieldActionService.findActionProcessor(name, valueForTypeEvaluation);
if (actionProcessor != null) {
Map<String, Object> actionParameters = new HashMap<>();
ActionParameters actionDetailParameters = actionProcessor.getActionDetail().getParameters();
if (actionDetailParameters != null && actionDetailParameters.getParameter() != null) {
for (ActionParameter parameter : actionDetailParameters.getParameter()) {
if (!arguments.isEmpty()) {
Object parameterValue = arguments.remove(0).getValue();
actionParameters.put(parameter.getName(), parameterValue);
} else {
throw new IllegalArgumentException(String.format("The transformation '%s' expects more parameters. The parameter '%s' is missing", name, parameter.getName()));
}
}
}
if (arguments.isEmpty()) {
throw new IllegalArgumentException(String.format("The transformation '%s' expects more arguments", name));
}
FieldGroup fields = new FieldGroup();
fields.getField().addAll(arguments);
return fieldActionService.buildAndProcessAction(actionProcessor, actionParameters, fields);
} else {
throw new IllegalArgumentException(String.format("The expression function or transformation '%s' was not found", name));
}
};
}
}
Aggregations