Search in sources :

Example 1 with ExpressionEvaluator

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

the class TaskInputJqAssignmentAction method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(WorkItem workItem, ProcessContext context) throws Exception {
    Object sdata = context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
    Map<String, Object> vars = new HashMap<>();
    vars.put("workflowdata", sdata);
    if (context.getVariable("$CONST") != null) {
        vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
    }
    if (inputFilterExpression != null) {
        sdata = evaluator.evaluate(inputFilterExpression, vars);
    }
    if (context.getNodeInstance() instanceof ContextableInstance) {
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ContextableInstance) context.getNodeInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
        variableScopeInstance.setVariable(JsonVariableScope.WORKFLOWDATA_KEY, sdata);
    }
    if (paramNames != null && !paramNames.isEmpty()) {
        vars = new HashMap<>();
        vars.put("workflowdata", sdata);
        if (context.getVariable("$CONST") != null) {
            vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
        }
        for (String name : paramNames) {
            Object param = workItem.getParameter(name);
            if (param != null) {
                param = evaluator.evaluate(param.toString(), vars);
                ((WorkItemImpl) workItem).setParameter(name, param);
            }
        }
    } else {
        ((WorkItemImpl) workItem).setParameter("Parameter", sdata);
    }
}
Also used : ContextableInstance(io.automatiko.engine.workflow.base.instance.ContextableInstance) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 2 with ExpressionEvaluator

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

the class TaskOutputJqAssignmentAction method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(WorkItem workItem, ProcessContext context) throws Exception {
    JsonNode sdata = (JsonNode) workItem.getResult(JsonVariableScope.WORKFLOWDATA_KEY);
    if (outputFilterExpression != null) {
        ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
        Map<String, Object> vars = new HashMap<>();
        vars.put("workflowdata", sdata);
        Map<String, Object> variables = new HashMap<>();
        if (context.getVariable("$CONST") != null) {
            variables.put("CONST", context.getVariable("$CONST"));
        }
        vars.put("workflow_variables", variables);
        sdata = (JsonNode) evaluator.evaluate(outputFilterExpression, vars);
        if (scopeFilter != null) {
            variables.put("v", sdata);
            sdata = (JsonNode) evaluator.evaluate(scopeFilter + "=$v", vars);
        }
    }
    ObjectMapper mapper = new ObjectMapper();
    if (sdata != null) {
        Object updated = mapper.readerForUpdating(context.getVariable(JsonVariableScope.WORKFLOWDATA_KEY)).readValue(sdata);
        context.setVariable(JsonVariableScope.WORKFLOWDATA_KEY, updated);
    }
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ExpressionEvaluator

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

the class VariableUtil method resolveVariable.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String resolveVariable(String s, NodeInstance nodeInstance) {
    if (s == null) {
        return null;
    }
    Map<String, String> replacements = new HashMap<String, String>();
    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
    while (matcher.find()) {
        String paramName = matcher.group(1);
        String replacementKey = paramName;
        String defaultValue = "";
        if (paramName.contains(":")) {
            String[] items = paramName.split(":");
            paramName = items[0];
            defaultValue = items[1];
        }
        if (replacements.get(paramName) == null) {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((io.automatiko.engine.workflow.process.instance.NodeInstance) nodeInstance).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) nodeInstance.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    Object variableValue = evaluator.evaluate(paramName, new NodeInstanceResolverFactory((io.automatiko.engine.workflow.process.instance.NodeInstance) nodeInstance));
                    String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
                    replacements.put(replacementKey, variableValueString);
                } catch (Throwable t) {
                }
            }
        }
    }
    for (Map.Entry<String, String> replacement : replacements.entrySet()) {
        s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
    }
    return s;
}
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) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with ExpressionEvaluator

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

the class InputJqAssignmentAction method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(WorkItem workItem, ProcessContext context) throws Exception {
    Object sdata = context.getProcessInstance().getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
    if (inputFilterExpression != null) {
        ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) context.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
        Map<String, Object> vars = new HashMap<>();
        vars.put("workflowdata", sdata);
        if (context.getVariable("$CONST") != null) {
            vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
        }
        sdata = evaluator.evaluate(inputFilterExpression, vars);
    }
    if (context.getNodeInstance() instanceof ContextableInstance) {
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ContextableInstance) context.getNodeInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
        variableScopeInstance.setVariable(JsonVariableScope.WORKFLOWDATA_KEY, sdata);
    }
}
Also used : ContextableInstance(io.automatiko.engine.workflow.base.instance.ContextableInstance) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 5 with ExpressionEvaluator

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

the class ProcessImpl method addImports.

public void addImports(Collection<String> imports) {
    this.imports.addAll(imports);
    ExpressionEvaluator evaluator = (ExpressionEvaluator) getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
    evaluator.addImports(imports);
}
Also used : ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

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