Search in sources :

Example 16 with NodeInstanceResolverFactory

use of io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory in project automatiko-engine by automatiko-io.

the class RuleSetNodeInstance method getSourceParameters.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Map<String, Object> getSourceParameters(DataAssociation association) {
    Map<String, Object> parameters = new HashMap<>();
    for (String sourceParam : association.getSources()) {
        Object parameterValue = null;
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, sourceParam);
        if (variableScopeInstance != null) {
            parameterValue = variableScopeInstance.getVariable(sourceParam);
        } else {
            try {
                ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                parameterValue = evaluator.evaluate(sourceParam, new NodeInstanceResolverFactory(this));
            } catch (Throwable t) {
                logger.warn("Could not find variable scope for variable {}", sourceParam);
            }
        }
        if (parameterValue != null) {
            parameters.put(sourceParam, parameterValue);
        }
    }
    return parameters;
}
Also used : VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator)

Example 17 with NodeInstanceResolverFactory

use of io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory 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)

Example 18 with NodeInstanceResolverFactory

use of io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory 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)

Aggregations

VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)18 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)18 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)16 HashMap (java.util.HashMap)15 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)9 Map (java.util.Map)9 Matcher (java.util.regex.Matcher)9 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)8 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)7 Transformation (io.automatiko.engine.workflow.process.core.node.Transformation)7 Serializable (java.io.Serializable)5 ArrayList (java.util.ArrayList)5 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)4 DataType (io.automatiko.engine.api.workflow.datatype.DataType)4 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)4 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)4 ProcessInstance (io.automatiko.engine.workflow.base.instance.ProcessInstance)4 AssignmentAction (io.automatiko.engine.workflow.base.instance.impl.AssignmentAction)4 Context (io.automatiko.engine.workflow.base.core.Context)3 ContextContainer (io.automatiko.engine.workflow.base.core.ContextContainer)3