Search in sources :

Example 71 with Expression

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

the class FILTER method create.

@Override
public Expression create(List<Expression> args) throws ParseException {
    if (args.size() != 2) {
        throw new ParseException("FILTER expects 2 arguments.");
    }
    Expression parentExpression = args.get(0);
    BooleanExpression filterExpression = BooleanExpression.asBooleanExpression(args.get(1));
    return (ctx) -> {
        Field parent = (Field) parentExpression.evaluate(ctx);
        List<Field> collection = parent instanceof FieldGroup ? ((FieldGroup) parent).getField() : Arrays.asList(parent);
        FieldGroup filtered = AtlasModelFactory.createFieldGroupFrom(parent, true);
        int index = 0;
        for (Field f : collection) {
            if (filterExpression.matches((subCtx) -> {
                return AtlasPath.extractChildren(f, subCtx);
            })) {
                adjustRootCollectionIndex(f, index);
                index++;
                filtered.getField().add(f);
            }
        }
        return filtered;
    };
}
Also used : AtlasModelFactory(io.atlasmap.v2.AtlasModelFactory) Arrays(java.util.Arrays) List(java.util.List) FieldGroup(io.atlasmap.v2.FieldGroup) Expression(io.atlasmap.expression.Expression) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) Field(io.atlasmap.v2.Field) AtlasPath(io.atlasmap.core.AtlasPath) BaseFunctionFactory(io.atlasmap.core.BaseFunctionFactory) ParseException(io.atlasmap.expression.parser.ParseException) Field(io.atlasmap.v2.Field) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) Expression(io.atlasmap.expression.Expression) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) FieldGroup(io.atlasmap.v2.FieldGroup) List(java.util.List) ParseException(io.atlasmap.expression.parser.ParseException)

Example 72 with Expression

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

the class SELECT method create.

@Override
public Expression create(List<Expression> args) throws ParseException {
    if (args.size() != 2) {
        throw new ParseException("SELECT expects 2 arguments.");
    }
    Expression parentExpression = args.get(0);
    Expression selectExpression = args.get(1);
    return (ctx) -> {
        Field parent = parentExpression.evaluate(ctx);
        List<Field> collection = parent instanceof FieldGroup ? ((FieldGroup) parent).getField() : Arrays.asList(parent);
        List<Field> selected = new ArrayList<>();
        final FieldGroup answer = AtlasModelFactory.createFieldGroupFrom(parent, true);
        answer.setPath(AtlasModelFactory.GENERATED_PATH);
        for (Field f : collection) {
            Field fs = (Field) selectExpression.evaluate((subCtx) -> {
                if (subCtx != null && AtlasModelFactory.GENERATED_PATH.equals(answer.getPath())) {
                    answer.setPath(parent.getPath() + (subCtx.startsWith(AtlasPath.PATH_SEPARATOR) ? subCtx : (AtlasPath.PATH_SEPARATOR + subCtx)));
                }
                return AtlasPath.extractChildren(f, subCtx);
            });
            selected.add(fs);
        }
        if (selected.size() == 1) {
            return selected.get(0);
        }
        answer.getField().addAll(selected);
        return answer;
    };
}
Also used : AtlasModelFactory(io.atlasmap.v2.AtlasModelFactory) Arrays(java.util.Arrays) List(java.util.List) FieldGroup(io.atlasmap.v2.FieldGroup) Expression(io.atlasmap.expression.Expression) Field(io.atlasmap.v2.Field) AtlasPath(io.atlasmap.core.AtlasPath) BaseFunctionFactory(io.atlasmap.core.BaseFunctionFactory) ParseException(io.atlasmap.expression.parser.ParseException) ArrayList(java.util.ArrayList) Field(io.atlasmap.v2.Field) Expression(io.atlasmap.expression.Expression) FieldGroup(io.atlasmap.v2.FieldGroup) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(io.atlasmap.expression.parser.ParseException)

Example 73 with Expression

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

the class ExpressionFieldActionTest method testNull.

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

Example 74 with Expression

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

the class ExpressionFieldActionTest method testAdd.

/*
     * operators
     */
@Test
public void testAdd() throws Exception {
    Expression action = new Expression();
    action.setExpression("${0} + ${1}");
    assertEquals(110, ExpressionFieldAction.process(action, Arrays.asList(10, 100)));
    assertEquals("foobar", ExpressionFieldAction.process(action, Arrays.asList("foo", "bar")));
}
Also used : Expression(io.atlasmap.v2.Expression) Test(org.junit.jupiter.api.Test)

Example 75 with Expression

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

the class ExpressionFieldActionTest method testAddNestedAction.

@Test
public void testAddNestedAction() throws Exception {
    Expression action = new Expression();
    action.setExpression("IF(ADD(${0}, ${1}, ${2}) == ${3}, 'pass', 'fail')");
    assertEquals("pass", ExpressionFieldAction.process(action, Arrays.asList(1, 2, 3, 6)));
}
Also used : Expression(io.atlasmap.v2.Expression) 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