Search in sources :

Example 1 with Expression

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

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

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

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

the class ExpressionFieldAction method process.

/**
 * Processes expression field action.
 * @param action action model
 * @param args expression arguments
 * @return processed
 * @throws ExpressionException expression processing error
 */
@Deprecated
@AtlasActionProcessor
public static Object process(io.atlasmap.v2.Expression action, List<Object> args) throws ExpressionException {
    if (action.getExpression() == null || action.getExpression().trim().isEmpty()) {
        return null;
    }
    Expression parsedExpression = Expression.parse(action.getExpression(), DefaultAtlasFunctionResolver.getInstance());
    Field answer = parsedExpression.evaluate((index) -> {
        try {
            return wrapWithField(args.get(Integer.parseInt(index)));
        } catch (Throwable e) {
            throw new ExpressionException("Invalid variable: " + index);
        }
    });
    return unwrapField(answer);
}
Also used : Field(io.atlasmap.v2.Field) AtlasModelFactory.unwrapField(io.atlasmap.v2.AtlasModelFactory.unwrapField) AtlasModelFactory.wrapWithField(io.atlasmap.v2.AtlasModelFactory.wrapWithField) Expression(io.atlasmap.expression.Expression) ExpressionException(io.atlasmap.expression.ExpressionException) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor)

Example 5 with Expression

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

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