Search in sources :

Example 16 with ExpressionEvaluator

use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.

the class RuleSetNodeInstance method resolveVariable.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object resolveVariable(Object s) {
    if (s instanceof String) {
        Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher((String) s);
        while (matcher.find()) {
            String paramName = matcher.group(1);
            String defaultValue = null;
            if (paramName.contains(":")) {
                String[] items = paramName.split(":");
                paramName = items[0];
                defaultValue = items[1];
            }
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
            if (variableScopeInstance != null) {
                Object variableValue = variableScopeInstance.getVariable(paramName);
                if (variableValue != null) {
                    return variableValue;
                } else {
                    return defaultValue;
                }
            } else {
                try {
                    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    Object variableValue = evaluator.evaluate(paramName, new NodeInstanceResolverFactory(this));
                    if (variableValue != null) {
                        return variableValue;
                    } else {
                        return defaultValue;
                    }
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", paramName);
                }
            }
        }
    }
    return s;
}
Also used : VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) Matcher(java.util.regex.Matcher) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 17 with ExpressionEvaluator

use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.

the class ServerlessFunctions method expression.

public static Object expression(ProcessContext context, String expression, String inputFilter, String outputFilter, String scope) {
    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
    Map<String, Object> vars = new HashMap<>();
    Map<String, Object> variables = new HashMap<>();
    vars.put("workflowdata", context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY));
    if (context.getVariable("$CONST") != null) {
        variables.put("CONST", context.getVariable("$CONST"));
        vars.put("workflow_variables", variables);
    }
    if (inputFilter != null) {
        Object filteredInput = evaluator.evaluate(inputFilter, vars);
        vars.put("workflowdata", filteredInput);
    }
    Object content = evaluator.evaluate(expression, vars);
    if (outputFilter != null) {
        vars.put("workflowdata", content);
        content = evaluator.evaluate(outputFilter, vars);
    }
    JsonNode outcome;
    if (scope != null) {
        vars.put("workflowdata", context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY));
        variables.put("v", content);
        vars.put("workflow_variables", variables);
        outcome = (JsonNode) evaluator.evaluate(scope + "=$v", vars);
    } else {
        outcome = (JsonNode) context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
    }
    try {
        Object updated = mapper.readerForUpdating(context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY)).readValue(outcome);
        context.setVariable(JsonVariableScope.WORKFLOWDATA_KEY, updated);
        return content;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 18 with ExpressionEvaluator

use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.

the class ServerlessFunctions method expressionAsString.

public static String expressionAsString(ProcessContext context, String expression) {
    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
    Map<String, Object> vars = new HashMap<>();
    vars.put("workflowdata", context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY));
    if (context.getVariable("$CONST") != null) {
        vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
    }
    Object content = evaluator.evaluate(expression, vars);
    if (content == null) {
        return null;
    }
    if (content instanceof TextNode) {
        return ((TextNode) content).asText();
    }
    return content.toString();
}
Also used : HashMap(java.util.HashMap) TextNode(com.fasterxml.jackson.databind.node.TextNode) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 19 with ExpressionEvaluator

use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.

the class ServerlessFunctions method variableExpression.

public static Object variableExpression(ProcessContext context, String expression) {
    if (expression.startsWith("${") || expression.startsWith("\"${")) {
        ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
        Map<String, Object> vars = new HashMap<>();
        vars.put("workflowdata", context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY));
        if (context.getVariable("$CONST") != null) {
            vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
        }
        Object content = evaluator.evaluate(unwrapExpression(expression), vars);
        return content;
    } else {
        return fromJson(expression);
    }
}
Also used : HashMap(java.util.HashMap) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 20 with ExpressionEvaluator

use of io.automatiko.engine.api.expression.ExpressionEvaluator in project automatiko-engine by automatiko-io.

the class MvelNodeInstanceVariableExpression method evaluate.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <T> T evaluate(String expression, ProcessContext context, Class<T> clazz) {
    Map<String, Object> replacements = new HashMap<>();
    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(expression);
    while (matcher.find()) {
        String paramName = matcher.group(1);
        String replacementKey = paramName;
        String defaultValue = null;
        if (paramName.contains(":")) {
            String[] items = paramName.split(":");
            paramName = items[0];
            defaultValue = items[1];
        }
        if (replacements.get(paramName) == null) {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) context.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
            if (variableScopeInstance != null) {
                Object variableValue = variableScopeInstance.getVariable(paramName);
                String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
                replacements.put(replacementKey, variableValueString);
            } else {
                try {
                    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    Object variableValue = evaluator.evaluate(paramName, resolverFactory((NodeInstance) context.getNodeInstance()));
                    replacements.put(replacementKey, variableValue == null ? defaultValue : variableValue);
                } catch (Throwable t) {
                    replacements.put(replacementKey, defaultValue);
                }
            }
        }
    }
    for (Map.Entry<String, Object> replacement : replacements.entrySet()) {
        expression = expression.replace("#{" + replacement.getKey() + "}", replacement.getValue().toString());
    }
    if (expression == null) {
        return null;
    }
    if (clazz.isInstance(expression)) {
        return clazz.cast(expression);
    }
    if (clazz == String.class) {
        return (T) expression.toString();
    }
    throw new IllegalStateException("Variable expression evaluated to non compatible type with " + clazz);
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstance(io.automatiko.engine.workflow.process.instance.NodeInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)31 HashMap (java.util.HashMap)25 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)20 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)16 Matcher (java.util.regex.Matcher)12 Map (java.util.Map)11 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)9 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)8 Transformation (io.automatiko.engine.workflow.process.core.node.Transformation)8 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)7 ArrayList (java.util.ArrayList)5 ContextableInstance (io.automatiko.engine.workflow.base.instance.ContextableInstance)4 WorkItemImpl (io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl)4 Serializable (java.io.Serializable)4 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)3 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)3 Context (io.automatiko.engine.workflow.base.core.Context)3 ContextContainer (io.automatiko.engine.workflow.base.core.ContextContainer)3 DataTransformerRegistry (io.automatiko.engine.workflow.base.core.impl.DataTransformerRegistry)3 ProcessInstance (io.automatiko.engine.workflow.base.instance.ProcessInstance)3