Search in sources :

Example 6 with Expression

use of io.atlasmap.expression.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 7 with Expression

use of io.atlasmap.expression.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)

Aggregations

Expression (io.atlasmap.expression.Expression)7 Field (io.atlasmap.v2.Field)6 ParseException (io.atlasmap.expression.parser.ParseException)5 FieldGroup (io.atlasmap.v2.FieldGroup)4 List (java.util.List)4 BaseFunctionFactory (io.atlasmap.core.BaseFunctionFactory)3 BooleanExpression (io.atlasmap.expression.internal.BooleanExpression)3 AtlasPath (io.atlasmap.core.AtlasPath)2 ExpressionException (io.atlasmap.expression.ExpressionException)2 AtlasModelFactory (io.atlasmap.v2.AtlasModelFactory)2 AtlasModelFactory.wrapWithField (io.atlasmap.v2.AtlasModelFactory.wrapWithField)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 ExpressionContext (io.atlasmap.expression.ExpressionContext)1 FunctionResolver (io.atlasmap.expression.FunctionResolver)1 ActionProcessor (io.atlasmap.spi.ActionProcessor)1 AtlasActionProcessor (io.atlasmap.spi.AtlasActionProcessor)1 AtlasModule (io.atlasmap.spi.AtlasModule)1 FunctionFactory (io.atlasmap.spi.FunctionFactory)1