Search in sources :

Example 26 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class PropertyHandler method start.

@SuppressWarnings("unchecked")
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    final String id = attrs.getValue("id");
    final String name = attrs.getValue("name");
    final String itemSubjectRef = attrs.getValue("itemSubjectRef");
    Object parent = parser.getParent();
    if (parent instanceof ContextContainer) {
        ContextContainer contextContainer = (ContextContainer) parent;
        VariableScope variableScope = (VariableScope) contextContainer.getDefaultContext(VariableScope.VARIABLE_SCOPE);
        List variables = variableScope.getVariables();
        Variable variable = new Variable();
        variable.setId(id);
        // if name is given use it as variable name instead of id
        if (name != null && name.length() > 0) {
            variable.setName(name);
            variable.setMetaData(name, variable.getName());
        } else {
            variable.setName(id);
        }
        variable.setMetaData("ItemSubjectRef", itemSubjectRef);
        variable.setMetaData(id, variable.getName());
        variables.add(variable);
        ((ProcessBuildData) parser.getData()).setMetaData("Variable", variable);
        return variable;
    }
    return new Variable();
}
Also used : ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) ProcessBuildData(io.automatiko.engine.workflow.compiler.xml.ProcessBuildData) List(java.util.List) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 27 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class XmlBPMNProcessDumper method visitVariableScope.

private void visitVariableScope(VariableScope variableScope, String prefix, StringBuilder xmlDump, Set<String> dumpedItemDefs) {
    if (variableScope != null && !variableScope.getVariables().isEmpty()) {
        int variablesAdded = 0;
        for (Variable variable : variableScope.getVariables()) {
            String itemDefId = (String) variable.getMetaData("ItemSubjectRef");
            if (itemDefId == null) {
                itemDefId = prefix + variable.getName();
            }
            if (itemDefId != null && !dumpedItemDefs.add(itemDefId.intern())) {
                continue;
            }
            if (!visitedVariables.add(variable.getName())) {
                continue;
            }
            ++variablesAdded;
            xmlDump.append("  <itemDefinition id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(itemDefId) + "\" ");
            if (variable.getType() != null && !"java.lang.Object".equals(variable.getType().getStringType())) {
                xmlDump.append("structureRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(variable.getType().getStringType()) + "\" ");
            }
            xmlDump.append("/>" + EOL);
        }
        if (variablesAdded > 0) {
            xmlDump.append(EOL);
        }
    }
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) Constraint(io.automatiko.engine.workflow.process.core.Constraint)

Example 28 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class WorkItemNodeInstance method triggerCompleted.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void triggerCompleted(WorkItem workItem) {
    this.workItem = workItem;
    WorkItemNode workItemNode = getWorkItemNode();
    if (workItemNode != null && workItem.getState() == WorkItem.ABORTED) {
        cancel();
        continueToNextNode(io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE, workItemNode);
        return;
    } else if (workItemNode != null && workItem.getState() == WorkItem.COMPLETED) {
        validateWorkItemResultVariable(getProcessInstance().getProcessName(), workItemNode.getOutAssociations(), workItem);
        for (Iterator<DataAssociation> iterator = getWorkItemNode().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(workItem.getParameters());
                    dataSet.putAll(workItem.getResults());
                    Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), parameterValue);
                        variableScopeInstance.setVariable(this, association.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", association.getTarget());
                        logger.warn("when trying to complete Work Item {}", workItem.getName());
                        logger.warn("Continuing without setting variable.");
                    }
                    if (parameterValue != null) {
                        ((WorkItemImpl) workItem).setParameter(association.getTarget(), parameterValue);
                    }
                }
            } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
                if (variableScopeInstance != null) {
                    Object value = workItem.getResult(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 WorkItemResolverFactory(workItem));
                        } 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") && !dataType.getStringType().endsWith("Object") && value instanceof String) {
                        value = dataType.readValue((String) value);
                    } else {
                        variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), 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(workItem.getResults());
                        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());
                        logger.warn("when trying to complete Work Item {}", workItem.getName());
                        logger.warn("Continuing without setting variable.");
                    }
                }
            } else {
                try {
                    association.getAssignments().forEach(this::handleAssignment);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    // handle dynamic nodes
    if (getNode() == null) {
        setMetaData("NodeType", workItem.getName());
        mapDynamicOutputData(workItem.getResults());
    }
    triggerCompleted();
}
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) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) Matcher(java.util.regex.Matcher) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) WorkItemHandlerNotFoundException(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemHandlerNotFoundException) ProcessWorkItemHandlerException(io.automatiko.engine.api.runtime.process.ProcessWorkItemHandlerException) WorkflowRuntimeException(io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException) WorkItemResolverFactory(io.automatiko.engine.workflow.process.instance.impl.WorkItemResolverFactory) WorkflowRuntimeException(io.automatiko.engine.workflow.process.instance.WorkflowRuntimeException) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) WorkItemNode(io.automatiko.engine.workflow.process.core.node.WorkItemNode) Iterator(java.util.Iterator) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) NamedDataType(io.automatiko.engine.api.workflow.NamedDataType) GroupedNamedDataType(io.automatiko.engine.api.workflow.GroupedNamedDataType) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class StartNodeInstance method signalEvent.

public void signalEvent(String type, Object event) {
    boolean hidden = false;
    if (getNode().getMetaData().get(HIDDEN) != null) {
        hidden = true;
    }
    InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
    if (!hidden) {
        runtime.getProcessEventSupport().fireBeforeNodeTriggered(this, runtime);
    }
    if (event != null) {
        String variableName = (String) getStartNode().getMetaData("TriggerMapping");
        if (!getStartNode().getOutAssociations().isEmpty()) {
            for (DataAssociation association : getStartNode().getOutAssociations()) {
                // } else
                if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null) {
                        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") && !dataType.getStringType().endsWith("Object") && event instanceof String) {
                            event = dataType.readValue((String) event);
                        } else {
                            variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), event);
                        }
                        variableScopeInstance.setVariable(this, association.getTarget(), event);
                    } 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(Collections.singletonMap(association.getSources().get(0), event));
                            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());
                            logger.warn("when trying to complete start node {}", getStartNode().getName());
                            logger.warn("Continuing without setting variable.");
                        }
                    }
                } else {
                    Object data = event;
                    association.getAssignments().stream().forEach(assignment -> handleAssignment(assignment, data));
                }
            }
        } else {
            if (variableName != null) {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, variableName);
                if (variableScopeInstance == null) {
                    throw new IllegalArgumentException("Could not find variable for start node: " + variableName);
                }
                EventTransformer transformer = getStartNode().getEventTransformer();
                if (transformer != null) {
                    event = transformer.transformEvent(event);
                }
                variableScopeInstance.setVariable(this, variableName, event);
            }
        }
    }
    VariableScope variableScope = (VariableScope) ((ContextContainer) getProcessInstance().getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE);
    for (Variable var : variableScope.getVariables()) {
        if (var.getMetaData(Variable.DEFAULT_VALUE) != null && variableScopeInstance.getVariable(var.getName()) == null) {
            Object value = runtime.getVariableInitializer().initialize(getProcessInstance().getProcess(), var, variableScopeInstance.getVariables());
            variableScope.validateVariable(getProcessInstance().getProcess().getName(), var.getName(), value);
            variableScopeInstance.setVariable(var.getName(), value);
        }
    }
    triggerCompleted();
    if (!hidden) {
        runtime.getProcessEventSupport().fireAfterNodeTriggered(this, runtime);
    }
}
Also used : Serializable(java.io.Serializable) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) Matcher(java.util.regex.Matcher) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 30 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable 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)

Aggregations

Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)57 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)27 Map (java.util.Map)22 HashMap (java.util.HashMap)18 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)16 ObjectDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)16 ArrayList (java.util.ArrayList)14 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 Matcher (java.util.regex.Matcher)12 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)11 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)10 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)9 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)9 ActionNode (io.automatiko.engine.workflow.process.core.node.ActionNode)9 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)9 List (java.util.List)9 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)8 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)8 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)8 ExecutableProcess (io.automatiko.engine.workflow.process.executable.core.ExecutableProcess)8