Search in sources :

Example 26 with ExpressionEvaluator

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

the class SubProcessNodeInstance method handleOutMappings.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void handleOutMappings(ProcessInstance processInstance) {
    VariableScopeInstance subProcessVariableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
    SubProcessNode subProcessNode = getSubProcessNode();
    if (subProcessNode != null) {
        for (Iterator<io.automatiko.engine.workflow.process.core.node.DataAssociation> iterator = subProcessNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
            io.automatiko.engine.workflow.process.core.node.DataAssociation mapping = iterator.next();
            if (mapping.getTransformation() != null) {
                Transformation transformation = mapping.getTransformation();
                DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                if (transformer != null) {
                    Map<String, Object> dataSet = new HashMap<String, Object>();
                    if (getNodeInstanceContainer() instanceof CompositeContextNodeInstance) {
                        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) getNodeInstanceContainer()).getContextInstance(VariableScope.VARIABLE_SCOPE);
                        if (variableScopeInstance != null) {
                            dataSet.putAll(variableScopeInstance.getVariables());
                        }
                    }
                    dataSet.putAll(subProcessVariableScopeInstance.getVariables());
                    Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.setVariable(this, mapping.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", mapping.getTarget());
                        logger.warn("Continuing without setting variable.");
                    }
                }
            } else {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
                if (variableScopeInstance != null) {
                    Object value = subProcessVariableScopeInstance.getVariable(mapping.getSources().get(0));
                    if (value == null) {
                        try {
                            ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                            value = evaluator.evaluate(mapping.getSources().get(0), new VariableScopeResolverFactory(subProcessVariableScopeInstance));
                        } catch (Throwable t) {
                        // do nothing
                        }
                    }
                    variableScopeInstance.setVariable(this, mapping.getTarget(), value);
                } else {
                    String output = mapping.getSources().get(0);
                    String target = mapping.getTarget();
                    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
                    if (matcher.find()) {
                        String paramName = matcher.group(1);
                        String expression = paramName + " = " + output;
                        VariableScopeResolverFactory resolver = new VariableScopeResolverFactory(subProcessVariableScopeInstance);
                        resolver.addExtraParameters(((VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE)).getVariables());
                        Serializable compiled = MVEL.compileExpression(expression);
                        MVEL.executeExpression(compiled, resolver);
                    } else {
                        logger.error("Could not find variable scope for variable {}", mapping.getTarget());
                        logger.error("when trying to complete SubProcess node {}", getSubProcessNode().getName());
                        logger.error("Continuing without setting variable.");
                    }
                }
            }
        }
    } else {
        // handle dynamic sub processes without data output mapping
        mapDynamicOutputData(subProcessVariableScopeInstance.getVariables());
    }
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) Serializable(java.io.Serializable) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) VariableScopeResolverFactory(io.automatiko.engine.workflow.process.instance.impl.VariableScopeResolverFactory) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) SubProcessNode(io.automatiko.engine.workflow.process.core.node.SubProcessNode)

Example 27 with ExpressionEvaluator

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

the class ForEachNodeInstance method evaluateCollectionExpression.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Collection<?> evaluateCollectionExpression(String collectionExpression) {
    Object collection;
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, collectionExpression);
    if (variableScopeInstance != null) {
        collection = variableScopeInstance.getVariable(collectionExpression);
    } else {
        try {
            ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
            collection = evaluator.evaluate(collectionExpression, new NodeInstanceResolverFactory(this));
        } catch (Throwable t) {
            throw new IllegalArgumentException("Could not find collection " + collectionExpression);
        }
    }
    if (collection == null) {
        return Collections.emptyList();
    }
    if (collection instanceof Collection<?>) {
        return (Collection<?>) collection;
    }
    if (collection.getClass().isArray()) {
        List<Object> list = new ArrayList<>();
        Collections.addAll(list, (Object[]) collection);
        return list;
    }
    throw new IllegalArgumentException("Unexpected collection type: " + collection.getClass());
}
Also used : VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 28 with ExpressionEvaluator

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

the class WorkflowProcessInstanceImpl method resolveVariable.

@SuppressWarnings({ "unchecked", "rawtypes" })
private String resolveVariable(String s, VariableResolverFactory factory) {
    Map<String, String> replacements = new HashMap<>();
    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
    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) {
            Object variableValue = getVariable(paramName);
            if (variableValue != null) {
                replacements.put(replacementKey, variableValue.toString());
            } else {
                try {
                    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((io.automatiko.engine.workflow.process.core.WorkflowProcess) getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    variableValue = evaluator.evaluate(paramName, factory);
                    String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
                    replacements.put(replacementKey, variableValueString);
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", paramName);
                }
            }
        }
    }
    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) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with ExpressionEvaluator

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

the class DefaultVariableInitializer method defaultValue.

protected Object defaultValue(Process process, String valueExpression, Variable definition, Map<String, Object> data) {
    try {
        Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(valueExpression);
        if (matcher.find()) {
            String paramName = matcher.group(1);
            String defaultValue = null;
            if (paramName.contains(":")) {
                String[] items = paramName.split(":");
                paramName = items[0];
                defaultValue = items[1];
            }
            ExpressionEvaluator<?> evaluator = (ExpressionEvaluator<?>) ((WorkflowProcess) process).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
            Object result = evaluator.evaluate(paramName, data);
            return result == null ? defaultValue : result;
        } else {
            return definition.getType().readValue(valueExpression);
        }
    } catch (Throwable e) {
        return null;
    }
}
Also used : Matcher(java.util.regex.Matcher) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 30 with ExpressionEvaluator

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

the class OutputJqAssignmentAction method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(WorkItem workItem, ProcessContext context) throws Exception {
    Object sdata = context.getVariable(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);
        if (context.getVariable("$CONST") != null) {
            vars.put("workflow_variables", Collections.singletonMap("CONST", context.getVariable("$CONST")));
        }
        sdata = evaluator.evaluate(outputFilterExpression, vars);
    }
    ObjectNode workflowData = (ObjectNode) context.getProcessInstance().getVariable(JsonVariableScope.WORKFLOWDATA_KEY);
    ObjectMapper mapper = new ObjectMapper();
    if (!workflowData.equals(sdata)) {
        Object updated = mapper.readerForUpdating(workflowData).readValue((JsonNode) sdata);
        context.getProcessInstance().setVariable(JsonVariableScope.WORKFLOWDATA_KEY, updated);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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