Search in sources :

Example 31 with Variable

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

the class EventNodeInstance method signalEvent.

public void signalEvent(String type, Object event) {
    if ("timerTriggered".equals(type)) {
        TimerInstance timerInstance = (TimerInstance) event;
        if (timerInstance.getId().equals(slaTimerId)) {
            handleSLAViolation();
        }
    } else if (("slaViolation:" + getId()).equals(type)) {
        handleSLAViolation();
    } else {
        String variableName = getEventNode().getVariableName();
        if (!getEventNode().getOutAssociations().isEmpty()) {
            for (DataAssociation association : getEventNode().getOutAssociations()) {
                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 {}", getEventNode().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) {
                EventTransformer transformer = getEventNode().getEventTransformer();
                if (transformer != null) {
                    event = transformer.transformEvent(event);
                }
                variableScopeInstance.setVariable(this, variableName, event);
            } else {
                String output = "event";
                Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(variableName);
                if (matcher.find()) {
                    String paramName = matcher.group(1);
                    String expression = VariableUtil.transformDotNotation(paramName, output);
                    NodeInstanceResolverFactory resolver = new NodeInstanceResolverFactory(this);
                    resolver.addExtraParameters(Collections.singletonMap("event", 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 {}", variableName);
                    logger.warn("when trying to complete start node {}", getEventNode().getName());
                    logger.warn("Continuing without setting variable.");
                }
            }
        }
        triggerCompleted();
    }
}
Also used : VariableUtil(io.automatiko.engine.workflow.base.instance.impl.util.VariableUtil) Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) Date(java.util.Date) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) DataType(io.automatiko.engine.api.workflow.datatype.DataType) HashMap(java.util.HashMap) ProcessContext(io.automatiko.engine.workflow.base.core.context.ProcessContext) NamedDataType(io.automatiko.engine.api.workflow.NamedDataType) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) Matcher(java.util.regex.Matcher) JobsService(io.automatiko.engine.api.jobs.JobsService) BaseEventDescription(io.automatiko.engine.api.workflow.BaseEventDescription) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) Map(java.util.Map) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) EventListener(io.automatiko.engine.api.runtime.process.EventListener) ExtendedNodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.ExtendedNodeInstanceImpl) VARIABLE_SCOPE(io.automatiko.engine.workflow.base.core.context.variable.VariableScope.VARIABLE_SCOPE) NodeInstance(io.automatiko.engine.api.runtime.process.NodeInstance) NodeInstanceState(io.automatiko.engine.api.runtime.process.NodeInstanceState) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) Set(java.util.Set) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) EMPTY_EVENT_LISTENER(io.automatiko.engine.workflow.process.instance.impl.DummyEventListener.EMPTY_EVENT_LISTENER) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) Serializable(java.io.Serializable) TimerInstance(io.automatiko.engine.services.time.TimerInstance) ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) PatternConstants(io.automatiko.engine.workflow.util.PatternConstants) Entry(java.util.Map.Entry) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) EventDescription(io.automatiko.engine.api.workflow.EventDescription) AssignmentAction(io.automatiko.engine.workflow.base.instance.impl.AssignmentAction) WorkflowProcessInstance(io.automatiko.engine.workflow.process.instance.WorkflowProcessInstance) Collections(java.util.Collections) MVEL(org.mvel2.MVEL) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) Serializable(java.io.Serializable) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) TimerInstance(io.automatiko.engine.services.time.TimerInstance) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) Matcher(java.util.regex.Matcher) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) NamedDataType(io.automatiko.engine.api.workflow.NamedDataType)

Example 32 with Variable

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

the class EventNodeInstance method getEventDescriptions.

@Override
public Set<EventDescription<?>> getEventDescriptions() {
    NamedDataType dataType = null;
    if (getEventNode().getVariableName() != null) {
        Map<String, Object> dataOutputs = (Map<String, Object>) getEventNode().getMetaData().get("DataOutputs");
        if (dataOutputs != null) {
            for (Entry<String, Object> dOut : dataOutputs.entrySet()) {
                dataType = new NamedDataType(dOut.getKey(), dOut.getValue());
            }
        } else {
            VariableScope variableScope = (VariableScope) getEventNode().getContext(VariableScope.VARIABLE_SCOPE);
            Variable variable = variableScope.findVariable(getEventNode().getVariableName());
            dataType = new NamedDataType(variable.getName(), variable.getType());
        }
    }
    return Collections.singleton(new BaseEventDescription(getEventType(), getNodeDefinitionId(), getNodeName(), "signal", getId(), getProcessInstance().getId(), dataType));
}
Also used : NamedDataType(io.automatiko.engine.api.workflow.NamedDataType) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) BaseEventDescription(io.automatiko.engine.api.workflow.BaseEventDescription) HashMap(java.util.HashMap) Map(java.util.Map) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 33 with Variable

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

the class WorkItemTest method getWorkItemProcess.

private ExecutableProcess getWorkItemProcess(String processId, String workName) {
    ExecutableProcess process = new ExecutableProcess();
    process.setId(processId);
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("UserName");
    variable.setType(new StringDataType());
    variables.add(variable);
    variable = new Variable();
    variable.setName("Person");
    variable.setType(new ObjectDataType(Person.class));
    variables.add(variable);
    variable = new Variable();
    variable.setName("MyObject");
    variable.setType(new ObjectDataType());
    variables.add(variable);
    variable = new Variable();
    variable.setName("Number");
    variable.setType(new IntegerDataType());
    variables.add(variable);
    process.getVariableScope().setVariables(variables);
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    WorkItemNode workItemNode = new WorkItemNode();
    workItemNode.setName("workItemNode");
    workItemNode.setId(2);
    workItemNode.addInMapping("Comment", "Person.name");
    workItemNode.addInMapping("Attachment", "MyObject");
    workItemNode.addOutMapping("Result", "MyObject");
    workItemNode.addOutMapping("Result.length()", "Number");
    Work work = new WorkImpl();
    work.setName(workName);
    Set<ParameterDefinition> parameterDefinitions = new HashSet<ParameterDefinition>();
    ParameterDefinition parameterDefinition = new ParameterDefinitionImpl("ActorId", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    parameterDefinition = new ParameterDefinitionImpl("Content", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    parameterDefinition = new ParameterDefinitionImpl("Comment", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    work.setParameterDefinitions(parameterDefinitions);
    work.setParameter("ActorId", "#{UserName}");
    work.setParameter("Content", "#{Person.name}");
    workItemNode.setWork(work);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    connect(startNode, workItemNode);
    connect(workItemNode, endNode);
    process.addNode(startNode);
    process.addNode(workItemNode);
    process.addNode(endNode);
    return process;
}
Also used : StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) IntegerDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.IntegerDataType) ArrayList(java.util.ArrayList) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) ParameterDefinitionImpl(io.automatiko.engine.workflow.base.core.impl.ParameterDefinitionImpl) StringDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.StringDataType) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) WorkItemNode(io.automatiko.engine.workflow.process.core.node.WorkItemNode) Work(io.automatiko.engine.workflow.base.core.Work) WorkImpl(io.automatiko.engine.workflow.base.core.impl.WorkImpl) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) Person(io.automatiko.engine.workflow.process.test.Person) ParameterDefinition(io.automatiko.engine.workflow.base.core.ParameterDefinition) HashSet(java.util.HashSet)

Example 34 with Variable

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

the class CompensationTest method createCompensationBoundaryEventProcess.

private ExecutableProcess createCompensationBoundaryEventProcess(String processId, String[] workItemNames, final List<String> eventList) throws Exception {
    ExecutableProcess process = new ExecutableProcess();
    process.setAutoComplete(true);
    process.setId(processId);
    process.setName("CESP Process");
    process.setMetaData("Compensation", true);
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("event");
    ObjectDataType personDataType = new ObjectDataType(java.lang.String.class);
    variable.setType(personDataType);
    variables.add(variable);
    process.getVariableScope().setVariables(variables);
    NodeCreator<StartNode> startNodeCreator = new NodeCreator<StartNode>(process, StartNode.class);
    NodeCreator<EndNode> endNodeCreator = new NodeCreator<EndNode>(process, EndNode.class);
    NodeCreator<WorkItemNode> workItemNodeCreator = new NodeCreator<WorkItemNode>(process, WorkItemNode.class);
    NodeCreator<BoundaryEventNode> boundaryNodeCreator = new NodeCreator<BoundaryEventNode>(process, BoundaryEventNode.class);
    NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(process, ActionNode.class);
    // Create process
    StartNode startNode = startNodeCreator.createNode("start");
    Node lastNode = startNode;
    WorkItemNode[] workItemNodes = new WorkItemNode[3];
    for (int i = 0; i < 3; ++i) {
        workItemNodes[i] = workItemNodeCreator.createNode("work" + (i + 1));
        workItemNodes[i].getWork().setName(workItemNames[i]);
        connect(lastNode, workItemNodes[i]);
        lastNode = workItemNodes[i];
    }
    EndNode endNode = endNodeCreator.createNode("end");
    connect(workItemNodes[2], endNode);
    // Compensation (boundary event) handlers
    for (int i = 0; i < 3; ++i) {
        createBoundaryEventCompensationHandler(process, workItemNodes[i], eventList, "" + i + 1);
    }
    return process;
}
Also used : StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) CompositeNode(io.automatiko.engine.workflow.process.core.node.CompositeNode) CompositeContextNode(io.automatiko.engine.workflow.process.core.node.CompositeContextNode) BoundaryEventNode(io.automatiko.engine.workflow.process.core.node.BoundaryEventNode) Node(io.automatiko.engine.workflow.process.core.Node) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) EventSubProcessNode(io.automatiko.engine.workflow.process.core.node.EventSubProcessNode) WorkItemNode(io.automatiko.engine.workflow.process.core.node.WorkItemNode) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) ArrayList(java.util.ArrayList) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) BoundaryEventNode(io.automatiko.engine.workflow.process.core.node.BoundaryEventNode) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) NodeCreator(io.automatiko.engine.workflow.process.test.NodeCreator) WorkItemNode(io.automatiko.engine.workflow.process.core.node.WorkItemNode) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess)

Example 35 with Variable

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

the class AbstractNodeHandler method findVariable.

/**
 * Finds the right variable by its name to make sure that when given as id it
 * will be also matched
 *
 * @param variableName name or id of the variable
 * @param parser parser instance
 * @return returns found variable name or given 'variableName' otherwise
 */
protected String findVariable(String variableName, final ExtensibleXmlParser parser) {
    if (variableName == null) {
        return null;
    }
    List<?> parents = parser.getParents();
    for (Object parent : parents) {
        if (parent instanceof ContextContainer) {
            ContextContainer contextContainer = (ContextContainer) parent;
            VariableScope variableScope = (VariableScope) contextContainer.getDefaultContext(VariableScope.VARIABLE_SCOPE);
            return variableScope.getVariables().stream().filter(v -> v.matchByIdOrName(variableName)).map(v -> v.getName()).findFirst().orElse(variableName);
        }
    }
    return variableName;
}
Also used : Text(org.w3c.dom.Text) COMPLETION_CONDITION(io.automatiko.engine.workflow.process.executable.core.Metadata.COMPLETION_CONDITION) Handler(io.automatiko.engine.workflow.compiler.xml.Handler) ItemDefinition(io.automatiko.engine.workflow.bpmn2.core.ItemDefinition) LoggerFactory(org.slf4j.LoggerFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) Definitions(io.automatiko.engine.workflow.bpmn2.core.Definitions) XmlDumper(io.automatiko.engine.workflow.compiler.xml.XmlDumper) NodeContainer(io.automatiko.engine.workflow.process.core.NodeContainer) HashMap(java.util.HashMap) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) ProcessBuildData(io.automatiko.engine.workflow.compiler.xml.ProcessBuildData) FloatDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.FloatDataType) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IntegerDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.IntegerDataType) Error(io.automatiko.engine.workflow.bpmn2.core.Error) BooleanDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.BooleanDataType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) Map(java.util.Map) Attributes(org.xml.sax.Attributes) Node(io.automatiko.engine.workflow.process.core.Node) Association(io.automatiko.engine.workflow.bpmn2.core.Association) ForEachNode(io.automatiko.engine.workflow.process.core.node.ForEachNode) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) Signal(io.automatiko.engine.workflow.bpmn2.core.Signal) ExtendedNodeImpl(io.automatiko.engine.workflow.process.core.impl.ExtendedNodeImpl) ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) Set(java.util.Set) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) ClassUtils.constructClass(io.automatiko.engine.workflow.compiler.util.ClassUtils.constructClass) List(java.util.List) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) SequenceFlow(io.automatiko.engine.workflow.bpmn2.core.SequenceFlow) ExtensibleXmlParser(io.automatiko.engine.workflow.compiler.xml.ExtensibleXmlParser) SAXException(org.xml.sax.SAXException) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) BaseAbstractHandler(io.automatiko.engine.workflow.compiler.xml.BaseAbstractHandler) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) StringDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.StringDataType) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) Lane(io.automatiko.engine.workflow.bpmn2.core.Lane) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) 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