Search in sources :

Example 31 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class DefaultAtlasExpressionProcessor method processExpression.

/**
 * Processes the expression.
 * @param session session
 * @param expression expression
 */
public static void processExpression(DefaultAtlasSession session, String expression) {
    if (expression == null || expression.trim().isEmpty()) {
        return;
    }
    try {
        Map<String, Field> sourceFieldMap = new HashMap<>();
        Field parent = session.head().getSourceField();
        if (parent != null && !AtlasUtil.isEmpty(parent.getDocId()) && !AtlasUtil.isEmpty(parent.getPath())) {
            sourceFieldMap.put(parent.getDocId() + ":" + parent.getPath(), parent);
        }
        // Anonymous FieldGroup is just a wrapping, peel it off
        if (parent instanceof FieldGroup && AtlasUtil.isEmpty(parent.getPath())) {
            FieldGroup parentGroup = FieldGroup.class.cast(parent);
            for (Field child : parentGroup.getField()) {
                if (!(AtlasUtil.isEmpty(child.getDocId()) && AtlasUtil.isEmpty(child.getPath()))) {
                    sourceFieldMap.put(child.getDocId() + ":" + child.getPath(), child);
                }
            }
        }
        Expression parsedExpression = Expression.parse(expression, DefaultAtlasFunctionResolver.getInstance());
        Object answer = parsedExpression.evaluate((path) -> {
            if (path == null || path.isEmpty()) {
                return null;
            }
            try {
                Field f = sourceFieldMap.get(path);
                if (f == null) {
                    return null;
                }
                AtlasModule sourceModule;
                Map<String, AtlasModule> sourceModules = session.getAtlasContext().getSourceModules();
                if (f instanceof ConstantField) {
                    sourceModule = sourceModules.get(AtlasConstants.CONSTANTS_DOCUMENT_ID);
                } else if (f instanceof PropertyField) {
                    sourceModule = sourceModules.get(AtlasConstants.PROPERTIES_SOURCE_DOCUMENT_ID);
                } else {
                    String[] splitted = path.split(":", 2);
                    sourceModule = sourceModules.get(splitted[0]);
                }
                if (sourceModule == null) {
                    throw new ExpressionException(String.format("Module for the path '%s' is not found", path));
                }
                session.head().setSourceField(f);
                sourceModule.readSourceValue(session);
                return session.head().getSourceField();
            } catch (Exception e) {
                throw new ExpressionException(e);
            }
        });
        if (answer instanceof Field) {
            session.head().setSourceField((Field) answer);
        } else {
            Field from = session.head().getSourceField();
            SimpleField to = new SimpleField();
            AtlasModelFactory.copyField(from, to, false);
            to.setValue(answer);
            session.head().setSourceField(to);
        }
    } catch (Exception e) {
        AtlasUtil.addAudit(session, expression, String.format("Expression processing error [%s]: %s", expression, e.getMessage()), AuditStatus.ERROR, null);
        if (LOG.isDebugEnabled()) {
            LOG.debug("", e);
        }
    }
}
Also used : HashMap(java.util.HashMap) FieldGroup(io.atlasmap.v2.FieldGroup) ConstantField(io.atlasmap.v2.ConstantField) ExpressionException(io.atlasmap.expression.ExpressionException) ExpressionException(io.atlasmap.expression.ExpressionException) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) AtlasModule(io.atlasmap.spi.AtlasModule) PropertyField(io.atlasmap.v2.PropertyField) Expression(io.atlasmap.expression.Expression) SimpleField(io.atlasmap.v2.SimpleField)

Example 32 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class DefaultAtlasFieldActionService method extractNestedListValuesForExpressionAction.

private void extractNestedListValuesForExpressionAction(Field field, List<Object> fieldValues) {
    if (!(field instanceof FieldGroup)) {
        fieldValues.add(field.getValue());
        return;
    }
    FieldGroup fieldGroup = (FieldGroup) field;
    if (fieldGroup == null || fieldGroup.getField() == null || fieldGroup.getField().isEmpty()) {
        return;
    }
    List<Field> fields = null;
    // passed in to Expression.
    while (fieldGroup.getPath() == null || fieldGroup.getPath().isEmpty()) {
        if (fieldGroup.getField().size() == 1 && (fieldGroup.getField().get(0) instanceof FieldGroup)) {
            fieldGroup = (FieldGroup) fieldGroup.getField().get(0);
        } else {
            fields = fieldGroup.getField();
            break;
        }
    }
    if (fields == null) {
        fields = new LinkedList<>();
        fields.add(fieldGroup);
    }
    doExtractValuesForExpressionAction(fields, fieldValues);
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup)

Example 33 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class ISEMPTY method create.

@Override
public Expression create(List<Expression> args) throws ParseException {
    if (args.size() != 1) {
        throw new ParseException("ISEMPTY expects 1 argument.");
    }
    final Expression arg = args.get(0);
    return new BooleanExpression() {

        public Field evaluate(ExpressionContext ctx) throws ExpressionException {
            Field f = arg.evaluate(ctx);
            Object value = f == null ? null : f.getValue();
            if (value == null || value.toString().isEmpty()) {
                return wrapWithField(Boolean.TRUE);
            }
            return wrapWithField(Boolean.FALSE);
        }

        public boolean matches(ExpressionContext ctx) throws ExpressionException {
            Object answer = evaluate(ctx).getValue();
            return answer != null && answer == Boolean.TRUE;
        }
    };
}
Also used : Field(io.atlasmap.v2.Field) AtlasModelFactory.wrapWithField(io.atlasmap.v2.AtlasModelFactory.wrapWithField) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) Expression(io.atlasmap.expression.Expression) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) ExpressionContext(io.atlasmap.expression.ExpressionContext) ParseException(io.atlasmap.expression.parser.ParseException)

Example 34 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class ExpressionFieldActionTest method testISEMPTY.

@Test
public void testISEMPTY() throws Exception {
    Expression action = new Expression();
    action.setExpression("IF(ISEMPTY(${0}), 'empty', 'not empty')");
    assertEquals("empty", ExpressionFieldAction.process(action, Arrays.asList((Object) null)));
    assertEquals("empty", ExpressionFieldAction.process(action, Arrays.asList("")));
    assertEquals("not empty", ExpressionFieldAction.process(action, Arrays.asList(" ")));
}
Also used : Expression(io.atlasmap.v2.Expression) Test(org.junit.jupiter.api.Test)

Example 35 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class ExpressionFieldActionTest method testNumberField.

@Test
public void testNumberField() throws Exception {
    Expression action = new Expression();
    Number integer = 1;
    action.setExpression("IF(${0} == 1, '1', 'not 1')");
    assertEquals("1", ExpressionFieldAction.process(action, Arrays.asList(integer)));
    BigInteger bigInt = new BigInteger("1");
    action.setExpression("IF(${0} == 1, 'bigint 1', 'not bigint 1')");
    assertEquals("bigint 1", ExpressionFieldAction.process(action, Arrays.asList(bigInt)));
    BigDecimal bigDec = new BigDecimal("1");
    action.setExpression("IF(${0} == 1, 'bigdecimal 1', 'not bigdecimal 1')");
    assertEquals("bigdecimal 1", ExpressionFieldAction.process(action, Arrays.asList(bigDec)));
}
Also used : Expression(io.atlasmap.v2.Expression) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

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