Search in sources :

Example 66 with Expression

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());
}
Also used : PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup) Test(org.junit.jupiter.api.Test)

Example 67 with Expression

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());
}
Also used : PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup) Test(org.junit.jupiter.api.Test)

Example 68 with Expression

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());
}
Also used : SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) Expression(io.atlasmap.v2.Expression) Mapping(io.atlasmap.v2.Mapping) SimpleField(io.atlasmap.v2.SimpleField) Test(org.junit.jupiter.api.Test)

Example 69 with Expression

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);
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) PropertyField(io.atlasmap.v2.PropertyField) ConstantField(io.atlasmap.v2.ConstantField) FieldGroup(io.atlasmap.v2.FieldGroup) CustomMapping(io.atlasmap.v2.CustomMapping) BaseMapping(io.atlasmap.v2.BaseMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasException(io.atlasmap.api.AtlasException)

Example 70 with Expression

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));
            }
        };
    }
}
Also used : ActionParameters(io.atlasmap.v2.ActionParameters) FieldGroup(io.atlasmap.v2.FieldGroup) Expression(io.atlasmap.expression.Expression) FunctionResolver(io.atlasmap.expression.FunctionResolver) HashMap(java.util.HashMap) ServiceLoader(java.util.ServiceLoader) ParseException(io.atlasmap.expression.parser.ParseException) ArrayList(java.util.ArrayList) List(java.util.List) FunctionFactory(io.atlasmap.spi.FunctionFactory) Field(io.atlasmap.v2.Field) Map(java.util.Map) ActionProcessor(io.atlasmap.spi.ActionProcessor) ActionParameter(io.atlasmap.v2.ActionParameter) FieldGroup(io.atlasmap.v2.FieldGroup) ActionParameter(io.atlasmap.v2.ActionParameter) Expression(io.atlasmap.expression.Expression) ActionParameters(io.atlasmap.v2.ActionParameters) ArrayList(java.util.ArrayList) List(java.util.List) ActionProcessor(io.atlasmap.spi.ActionProcessor) HashMap(java.util.HashMap) Map(java.util.Map) FunctionFactory(io.atlasmap.spi.FunctionFactory)

Aggregations

Test (org.junit.jupiter.api.Test)34 Expression (org.apache.commons.jexl2.Expression)28 Expression (io.atlasmap.v2.Expression)26 JexlContext (org.apache.commons.jexl2.JexlContext)25 JexlEngine (org.apache.commons.jexl2.JexlEngine)22 Field (io.atlasmap.v2.Field)21 FieldGroup (io.atlasmap.v2.FieldGroup)20 MapContext (org.apache.commons.jexl2.MapContext)20 SimpleField (io.atlasmap.v2.SimpleField)16 Test (org.testng.annotations.Test)13 PropertyField (io.atlasmap.v2.PropertyField)9 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)7 Expression (org.eclipse.xtext.resource.bug385636.Expression)7 Expression (org.kie.workbench.common.dmn.api.definition.v1_1.Expression)7 Expression (io.atlasmap.expression.Expression)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 ParseException (io.atlasmap.expression.parser.ParseException)4 Action (io.atlasmap.v2.Action)4 InformationItem (org.kie.workbench.common.dmn.api.definition.v1_1.InformationItem)4