Search in sources :

Example 1 with BooleanExpression

use of io.atlasmap.expression.internal.BooleanExpression in project atlasmap by atlasmap.

the class IF method create.

@Override
public Expression create(List<Expression> args) throws ParseException {
    if (args.size() != 3) {
        throw new ParseException("IF expects 3 arguments.");
    }
    BooleanExpression conditional = BooleanExpression.asBooleanExpression(args.get(0));
    Expression trueExpression = args.get(1);
    Expression falseExpression = args.get(2);
    return (ctx) -> {
        if (conditional.matches(ctx)) {
            return trueExpression.evaluate(ctx);
        } else {
            return falseExpression.evaluate(ctx);
        }
    };
}
Also used : List(java.util.List) Expression(io.atlasmap.expression.Expression) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) BaseFunctionFactory(io.atlasmap.core.BaseFunctionFactory) ParseException(io.atlasmap.expression.parser.ParseException) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) Expression(io.atlasmap.expression.Expression) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) ParseException(io.atlasmap.expression.parser.ParseException)

Example 2 with BooleanExpression

use of io.atlasmap.expression.internal.BooleanExpression 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 3 with BooleanExpression

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

Aggregations

Expression (io.atlasmap.expression.Expression)3 BooleanExpression (io.atlasmap.expression.internal.BooleanExpression)3 ParseException (io.atlasmap.expression.parser.ParseException)3 BaseFunctionFactory (io.atlasmap.core.BaseFunctionFactory)2 Field (io.atlasmap.v2.Field)2 List (java.util.List)2 AtlasPath (io.atlasmap.core.AtlasPath)1 ExpressionContext (io.atlasmap.expression.ExpressionContext)1 AtlasModelFactory (io.atlasmap.v2.AtlasModelFactory)1 AtlasModelFactory.wrapWithField (io.atlasmap.v2.AtlasModelFactory.wrapWithField)1 FieldGroup (io.atlasmap.v2.FieldGroup)1 Arrays (java.util.Arrays)1