Search in sources :

Example 26 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class StateBasedNodeInstance method mapDynamicOutputData.

protected void mapDynamicOutputData(Map<String, Object> results) {
    if (results != null && !results.isEmpty()) {
        VariableScope variableScope = (VariableScope) ((ContextContainer) getProcessInstance().getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE);
        for (Entry<String, Object> result : results.entrySet()) {
            String variableName = result.getKey();
            Variable variable = variableScope.findVariable(variableName);
            if (variable != null) {
                variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), variableName, result.getValue());
                variableScopeInstance.setVariable(this, variableName, result.getValue());
            }
        }
    }
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 27 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class JoinInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("An ActionNode only accepts default incoming connections!");
    }
    triggerTime = new Date();
    final Join join = getJoin();
    switch(join.getType()) {
        case Join.TYPE_XOR:
            triggerCompleted();
            break;
        case Join.TYPE_AND:
            Integer count = (Integer) this.triggers.get(from.getNodeId());
            if (count == null) {
                this.triggers.put(from.getNodeId(), 1);
            } else {
                this.triggers.put(from.getNodeId(), count.intValue() + 1);
            }
            if (checkAllActivated()) {
                decreaseAllTriggers();
                triggerCompleted();
            }
            break;
        case Join.TYPE_DISCRIMINATOR:
            boolean triggerCompleted = triggers.isEmpty();
            triggers.put(from.getNodeId(), new Integer(1));
            if (checkAllActivated()) {
                resetAllTriggers();
            }
            if (triggerCompleted) {
                triggerCompleted();
            }
            break;
        case Join.TYPE_N_OF_M:
            count = (Integer) this.triggers.get(from.getNodeId());
            if (count == null) {
                this.triggers.put(from.getNodeId(), 1);
            } else {
                this.triggers.put(from.getNodeId(), count.intValue() + 1);
            }
            int counter = 0;
            for (final Connection connection : getJoin().getDefaultIncomingConnections()) {
                if (this.triggers.get(connection.getFrom().getId()) != null) {
                    counter++;
                }
            }
            String n = join.getN();
            Integer number = null;
            if (n.startsWith("#{") && n.endsWith("}")) {
                n = n.substring(2, n.length() - 1);
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, n);
                if (variableScopeInstance == null) {
                    throw new IllegalArgumentException("Could not find variable " + n + " when executing join.");
                }
                Object value = variableScopeInstance.getVariable(n);
                if (value instanceof Number) {
                    number = ((Number) value).intValue();
                } else {
                    throw new IllegalArgumentException("Variable " + n + " did not return a number when executing join: " + value);
                }
            } else {
                number = Integer.parseInt(n);
            }
            if (counter >= number) {
                resetAllTriggers();
                NodeInstanceContainer nodeInstanceContainer = (NodeInstanceContainer) getNodeInstanceContainer();
                cancelRemainingDirectFlows(nodeInstanceContainer, getJoin());
                triggerCompleted();
            }
            break;
        case Join.TYPE_OR:
            NodeInstanceContainer nodeInstanceContainer = (NodeInstanceContainer) getNodeInstanceContainer();
            boolean activePathExists = existsActiveDirectFlow(nodeInstanceContainer, getJoin());
            if (!activePathExists) {
                triggerCompleted();
            }
            break;
        default:
            throw new IllegalArgumentException("Illegal join type " + join.getType());
    }
}
Also used : NodeInstanceContainer(io.automatiko.engine.api.runtime.process.NodeInstanceContainer) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) Connection(io.automatiko.engine.api.definition.process.Connection) Join(io.automatiko.engine.workflow.process.core.node.Join) Date(java.util.Date)

Example 28 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class RuleSetNodeInstance method evaluateParameters.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Map<String, Object> evaluateParameters(RuleSetNode ruleSetNode) {
    Map<String, Object> replacements = new HashMap<>();
    for (Iterator<DataAssociation> iterator = ruleSetNode.getInAssociations().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) {
                Object parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(association));
                replacements.put(association.getTarget(), parameterValue);
            }
        } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
            Object parameterValue = null;
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getSources().get(0));
            if (variableScopeInstance != null) {
                parameterValue = variableScopeInstance.getVariable(association.getSources().get(0));
            } else {
                try {
                    ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                    parameterValue = evaluator.evaluate(association.getSources().get(0), new NodeInstanceResolverFactory(this));
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", association.getSources().get(0));
                    logger.error("when trying to execute RuleSetNode {}", ruleSetNode.getName());
                    logger.error("Continuing without setting parameter.");
                }
            }
            replacements.put(association.getTarget(), parameterValue);
        }
    }
    for (Map.Entry<String, Object> entry : ruleSetNode.getParameters().entrySet()) {
        if (entry.getValue() instanceof String) {
            Object value = resolveVariable(entry.getValue());
            replacements.put(entry.getKey(), value);
        }
    }
    return replacements;
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) HashMap(java.util.HashMap) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) 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) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance 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 30 with VariableScopeInstance

use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.

the class EventNodeInstance method resolveVariable.

private String resolveVariable(String s) {
    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(replacementKey) == null) {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
            if (variableScopeInstance != null) {
                Object variableValue = variableScopeInstance.getVariable(paramName);
                String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
                replacements.put(replacementKey, variableValueString);
            }
        }
    }
    for (Map.Entry<String, String> replacement : replacements.entrySet()) {
        s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
    }
    return s;
}
Also used : VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)44 HashMap (java.util.HashMap)26 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)20 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)18 Map (java.util.Map)16 Matcher (java.util.regex.Matcher)12 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)11 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)11 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)10 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)9 Transformation (io.automatiko.engine.workflow.process.core.node.Transformation)9 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)7 ArrayList (java.util.ArrayList)7 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)6 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)6 Serializable (java.io.Serializable)6 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)5 Process (io.automatiko.engine.api.definition.process.Process)4 DataType (io.automatiko.engine.api.workflow.datatype.DataType)4 Context (io.automatiko.engine.workflow.base.core.Context)4