Search in sources :

Example 1 with ParseException

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

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

use of io.atlasmap.expression.parser.ParseException in project atlasmap by atlasmap.

the class Expression method parse.

/**
 * Parses the expression text.
 * @param expessionText expression text
 * @param functionResolver function resolver
 * @return result
 * @throws ExpressionException unexpected error
 */
static Expression parse(String expessionText, FunctionResolver functionResolver) throws ExpressionException {
    if (functionResolver == null) {
        functionResolver = (name, args) -> {
            throw new ParseException("Function not found: " + name);
        };
    }
    Object result = CACHE.get(expessionText);
    if (result instanceof ExpressionException) {
        throw (ExpressionException) result;
    } else if (result instanceof Expression) {
        return (Expression) result;
    } else {
        String actual = expessionText;
        try {
            Parser parser = new Parser(new StringReader(actual));
            parser.functionResolver = functionResolver;
            Expression e = parser.parse();
            CACHE.put(expessionText, e);
            return e;
        } catch (Throwable e) {
            ExpressionException fe = new ExpressionException(actual, e);
            CACHE.put(expessionText, fe);
            throw fe;
        }
    }
}
Also used : StringReader(java.io.StringReader) ParseException(io.atlasmap.expression.parser.ParseException) Parser(io.atlasmap.expression.parser.Parser)

Example 4 with ParseException

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

Example 5 with ParseException

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

ParseException (io.atlasmap.expression.parser.ParseException)6 Expression (io.atlasmap.expression.Expression)5 Field (io.atlasmap.v2.Field)4 List (java.util.List)4 BaseFunctionFactory (io.atlasmap.core.BaseFunctionFactory)3 BooleanExpression (io.atlasmap.expression.internal.BooleanExpression)3 FieldGroup (io.atlasmap.v2.FieldGroup)3 AtlasPath (io.atlasmap.core.AtlasPath)2 AtlasModelFactory (io.atlasmap.v2.AtlasModelFactory)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 ExpressionContext (io.atlasmap.expression.ExpressionContext)1 FunctionResolver (io.atlasmap.expression.FunctionResolver)1 Parser (io.atlasmap.expression.parser.Parser)1 ActionProcessor (io.atlasmap.spi.ActionProcessor)1 FunctionFactory (io.atlasmap.spi.FunctionFactory)1 ActionParameter (io.atlasmap.v2.ActionParameter)1 ActionParameters (io.atlasmap.v2.ActionParameters)1 AtlasModelFactory.wrapWithField (io.atlasmap.v2.AtlasModelFactory.wrapWithField)1 StringReader (java.io.StringReader)1