Search in sources :

Example 11 with WorkflowProcess

use of io.automatiko.engine.workflow.process.core.WorkflowProcess in project automatiko-engine by automatiko-io.

the class RuleSetNodeInstance method processOutputs.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void processOutputs(Map<String, Object> inputs, Map<String, Object> objects) {
    logger.debug("Rules evaluation results {}", objects);
    RuleSetNode ruleSetNode = getRuleSetNode();
    if (ruleSetNode != null) {
        for (Iterator<DataAssociation> iterator = ruleSetNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
            DataAssociation association = iterator.next();
            if (association.getTransformation() != null) {
                Transformation transformation = association.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(inputs);
                    dataSet.putAll(objects);
                    Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.setVariable(this, association.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", association.getTarget());
                        logger.warn("Continuing without setting variable.");
                    }
                }
            } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                if (variableScopeInstance != null) {
                    Object value = objects.get(association.getSources().get(0));
                    if (value == null) {
                        try {
                            ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                            value = evaluator.evaluate(association.getSources().get(0), new MapVariableResolverFactory(objects));
                        } catch (Throwable t) {
                        // do nothing
                        }
                    }
                    Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
                    DataType dataType = varDef.getType();
                    // exclude java.lang.Object as it is considered unknown type
                    if (!dataType.getStringType().endsWith("java.lang.Object") && value instanceof String) {
                        value = dataType.readValue((String) value);
                    }
                    variableScopeInstance.setVariable(this, association.getTarget(), value);
                } else {
                    String output = association.getSources().get(0);
                    String target = association.getTarget();
                    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
                    if (matcher.find()) {
                        String paramName = matcher.group(1);
                        String expression = VariableUtil.transformDotNotation(paramName, output);
                        NodeInstanceResolverFactory resolver = new NodeInstanceResolverFactory(this);
                        resolver.addExtraParameters(objects);
                        Serializable compiled = MVEL.compileExpression(expression);
                        MVEL.executeExpression(compiled, resolver);
                        String varName = VariableUtil.nameFromDotNotation(paramName);
                        variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, varName);
                        variableScopeInstance.setVariable(this, varName, variableScopeInstance.getVariable(varName));
                    } else {
                        logger.warn("Could not find variable scope for variable {}", association.getTarget());
                    }
                }
            }
        }
    }
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) Serializable(java.io.Serializable) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) RuleSetNode(io.automatiko.engine.workflow.process.core.node.RuleSetNode) 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) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess)

Aggregations

WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)11 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)8 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)7 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)7 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)7 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)7 HashMap (java.util.HashMap)7 Transformation (io.automatiko.engine.workflow.process.core.node.Transformation)6 List (java.util.List)6 Process (io.automatiko.engine.api.definition.process.Process)4 Assignment (io.automatiko.engine.workflow.process.core.node.Assignment)4 CompositeContextNode (io.automatiko.engine.workflow.process.core.node.CompositeContextNode)4 Connection (io.automatiko.engine.api.definition.process.Connection)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