Search in sources :

Example 1 with ProcessContext

use of io.automatiko.engine.api.runtime.process.ProcessContext in project automatiko-engine by automatiko-io.

the class SubProcessNode method internalSubProcess.

public void internalSubProcess(Collection<io.automatiko.engine.api.workflow.Process> availableProcesses) {
    io.automatiko.engine.api.workflow.Process process = (io.automatiko.engine.api.workflow.Process<Map<String, Object>>) availableProcesses.stream().filter(p -> p.id().equals(processId)).findFirst().orElse(null);
    if (process == null) {
        return;
    }
    this.subProcessFactory = new SubProcessFactory<Object>() {

        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public Map<String, Object> bind(ProcessContext ctx) {
            Map<String, Object> parameters = new HashMap<String, Object>();
            for (Iterator<DataAssociation> iterator = getInAssociations().iterator(); iterator.hasNext(); ) {
                DataAssociation mapping = iterator.next();
                Object parameterValue = null;
                if (mapping.getTransformation() != null) {
                    Transformation transformation = mapping.getTransformation();
                    DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                    if (transformer != null) {
                        parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(ctx, mapping));
                    }
                } else if (mapping.getAssignments() == null || mapping.getAssignments().isEmpty()) {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getSources().get(0));
                    if (variableScopeInstance != null) {
                        parameterValue = variableScopeInstance.getVariable(mapping.getSources().get(0));
                    } else {
                        try {
                            ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) ctx.getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
                            parameterValue = evaluator.evaluate(mapping.getSources().get(0), new NodeInstanceResolverFactory((NodeInstance) ctx.getNodeInstance()));
                        } catch (Throwable t) {
                            parameterValue = VariableUtil.resolveVariable(mapping.getSources().get(0), ctx.getNodeInstance());
                            if (parameterValue != null) {
                                parameters.put(mapping.getTarget(), parameterValue);
                            }
                        }
                    }
                } else {
                    mapping.getAssignments().stream().forEach(a -> handleAssignment(ctx, parameters, a));
                }
                if (parameterValue != null) {
                    parameters.put(mapping.getTarget(), parameterValue);
                }
            }
            return parameters;
        }

        @Override
        public ProcessInstance createInstance(Object model) {
            Model data = (Model) process.createModel();
            data.fromMap((Map<String, Object>) model);
            return process.createInstance(data);
        }

        @Override
        public void unbind(ProcessContext ctx, Object model) {
            Map<String, Object> result = ((Model) model).toMap();
            for (Iterator<DataAssociation> iterator = getOutAssociations().iterator(); iterator.hasNext(); ) {
                DataAssociation mapping = iterator.next();
                if (mapping.getTransformation() != null) {
                    Transformation transformation = mapping.getTransformation();
                    DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                    if (transformer != null) {
                        Map<String, Object> dataSet = new HashMap<String, Object>();
                        if (ctx.getNodeInstance().getNodeInstanceContainer() instanceof CompositeContextNodeInstance) {
                            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) ctx.getNodeInstance().getNodeInstanceContainer()).getContextInstance(VariableScope.VARIABLE_SCOPE);
                            if (variableScopeInstance != null) {
                                dataSet.putAll(variableScopeInstance.getVariables());
                            }
                        }
                        dataSet.putAll(result);
                        Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
                        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
                        if (variableScopeInstance != null && parameterValue != null) {
                            variableScopeInstance.setVariable(ctx.getNodeInstance(), mapping.getTarget(), parameterValue);
                        }
                    }
                } else {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((NodeInstance) ctx.getNodeInstance()).resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
                    if (variableScopeInstance != null) {
                        Object value = result.get(mapping.getSources().get(0));
                        if (value == null) {
                            try {
                                value = MVEL.eval(mapping.getSources().get(0), result);
                            } catch (Throwable t) {
                            // do nothing
                            }
                        }
                        variableScopeInstance.setVariable(ctx.getNodeInstance(), mapping.getTarget(), value);
                    } else {
                        String output = mapping.getSources().get(0);
                        String target = mapping.getTarget();
                        Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
                        if (matcher.find()) {
                            String paramName = matcher.group(1);
                            String expression = paramName + " = " + output;
                            Serializable compiled = MVEL.compileExpression(expression);
                            MVEL.executeExpression(compiled, result);
                        }
                    }
                }
            }
        }

        @SuppressWarnings("unchecked")
        @Override
        public void abortInstance(String instanceId) {
            process.instances().findById(instanceId).ifPresent(pi -> {
                try {
                    ((ProcessInstance<?>) pi).abort();
                } catch (IllegalArgumentException e) {
                // ignore it as this might be thrown in case of canceling already aborted instance
                }
            });
        }

        private void handleAssignment(ProcessContext ctx, Map<String, Object> output, Assignment assignment) {
            AssignmentAction action = (AssignmentAction) assignment.getMetaData("Action");
            try {
                WorkItemImpl workItem = new WorkItemImpl();
                action.execute(workItem, ctx);
                output.putAll(workItem.getParameters());
            } catch (WorkItemExecutionError e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException("unable to execute Assignment", e);
            }
        }
    };
}
Also used : VariableUtil(io.automatiko.engine.workflow.base.instance.impl.util.VariableUtil) Context(io.automatiko.engine.workflow.base.core.Context) HashMap(java.util.HashMap) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) ArrayList(java.util.ArrayList) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) ProcessContext(io.automatiko.engine.api.runtime.process.ProcessContext) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) Matcher(java.util.regex.Matcher) AbstractContext(io.automatiko.engine.workflow.base.core.context.AbstractContext) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) Map(java.util.Map) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) CompositeContextNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance) LinkedList(java.util.LinkedList) Connection(io.automatiko.engine.api.definition.process.Connection) NodeInstance(io.automatiko.engine.workflow.process.instance.NodeInstance) Iterator(java.util.Iterator) Model(io.automatiko.engine.api.Model) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) Collection(java.util.Collection) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) Serializable(java.io.Serializable) DataTransformerRegistry(io.automatiko.engine.workflow.base.core.impl.DataTransformerRegistry) List(java.util.List) PatternConstants(io.automatiko.engine.workflow.util.PatternConstants) ContextContainerImpl(io.automatiko.engine.workflow.base.core.impl.ContextContainerImpl) Mappable(io.automatiko.engine.workflow.base.core.context.variable.Mappable) AssignmentAction(io.automatiko.engine.workflow.base.instance.impl.AssignmentAction) Collections(java.util.Collections) MVEL(org.mvel2.MVEL) Serializable(java.io.Serializable) Matcher(java.util.regex.Matcher) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) ProcessContext(io.automatiko.engine.api.runtime.process.ProcessContext) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) Iterator(java.util.Iterator) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) CompositeContextNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance) AssignmentAction(io.automatiko.engine.workflow.base.instance.impl.AssignmentAction) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) Model(io.automatiko.engine.api.Model) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) HashMap(java.util.HashMap) Map(java.util.Map) CompositeContextNodeInstance(io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance) NodeInstance(io.automatiko.engine.workflow.process.instance.NodeInstance)

Example 2 with ProcessContext

use of io.automatiko.engine.api.runtime.process.ProcessContext in project automatiko-engine by automatiko-io.

the class CompensationTest method createNestedCompensationEventSubProcessProcess.

private ExecutableProcess createNestedCompensationEventSubProcessProcess(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);
    NodeCreator<StartNode> startNodeCreator = new NodeCreator<StartNode>(process, StartNode.class);
    NodeCreator<WorkItemNode> workItemNodeCreator = new NodeCreator<WorkItemNode>(process, WorkItemNode.class);
    NodeCreator<CompositeContextNode> compNodeCreator = new NodeCreator<CompositeContextNode>(process, CompositeContextNode.class);
    NodeCreator<EndNode> endNodeCreator = new NodeCreator<EndNode>(process, EndNode.class);
    // outer process
    CompositeContextNode compositeNode = compNodeCreator.createNode("sub0");
    {
        StartNode startNode = startNodeCreator.createNode("start0");
        WorkItemNode workItemNode = workItemNodeCreator.createNode("work0-pre");
        workItemNode.getWork().setName(workItemNames[0]);
        connect(startNode, workItemNode);
        connect(workItemNode, compositeNode);
        EndNode endNode = endNodeCreator.createNode("end0");
        connect(compositeNode, endNode);
    }
    // 1rst level nested subprocess (contains compensation visibility scope)
    CompositeContextNode compensationScopeContainerNode = compositeNode;
    {
        startNodeCreator.setNodeContainer(compositeNode);
        workItemNodeCreator.setNodeContainer(compositeNode);
        compNodeCreator.setNodeContainer(compositeNode);
        endNodeCreator.setNodeContainer(compositeNode);
        StartNode startNode = startNodeCreator.createNode("start1");
        CompositeContextNode subCompNode = compNodeCreator.createNode("sub1");
        connect(startNode, subCompNode);
        WorkItemNode workItemNode = workItemNodeCreator.createNode("work1-post");
        workItemNode.getWork().setName(workItemNames[2]);
        connect(subCompNode, workItemNode);
        EndNode endNode = endNodeCreator.createNode("end1");
        connect(workItemNode, endNode);
        compositeNode = subCompNode;
    }
    // 2nd level nested subprocess
    {
        startNodeCreator.setNodeContainer(compositeNode);
        workItemNodeCreator.setNodeContainer(compositeNode);
        endNodeCreator.setNodeContainer(compositeNode);
        StartNode startNode = startNodeCreator.createNode("start2");
        WorkItemNode workItemNode = workItemNodeCreator.createNode("work2");
        workItemNode.getWork().setName(workItemNames[1]);
        connect(startNode, workItemNode);
        EndNode endNode = endNodeCreator.createNode("end2");
        connect(workItemNode, endNode);
    }
    // 3nd level nested event subprocess in 2nd level subprocess
    {
        NodeCreator<EventSubProcessNode> espNodeCreator = new NodeCreator<EventSubProcessNode>(compositeNode, EventSubProcessNode.class);
        EventSubProcessNode espNode = espNodeCreator.createNode("eventSub2");
        startNodeCreator.setNodeContainer(espNode);
        endNodeCreator.setNodeContainer(espNode);
        NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(espNode, ActionNode.class);
        EventTypeFilter eventFilter = new NonAcceptingEventTypeFilter();
        eventFilter.setType("Compensation");
        espNode.addEvent(eventFilter);
        addCompensationScope(espNode, compensationScopeContainerNode, (String) compositeNode.getMetaData("UniqueId"));
        StartNode startNode = startNodeCreator.createNode("start3*");
        ActionNode actionNode = actionNodeCreator.createNode("action3*");
        actionNode.setName("Execute");
        ProcessAction action = new ConsequenceAction("java", null);
        action.setMetaData("Action", new Action() {

            public void execute(ProcessContext context) throws Exception {
                eventList.add("Executed action");
            }
        });
        actionNode.setAction(action);
        connect(startNode, actionNode);
        EndNode endNode = endNodeCreator.createNode("end3*");
        connect(actionNode, endNode);
    }
    return process;
}
Also used : ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) Action(io.automatiko.engine.workflow.base.instance.impl.Action) ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) CompositeContextNode(io.automatiko.engine.workflow.process.core.node.CompositeContextNode) EventSubProcessNode(io.automatiko.engine.workflow.process.core.node.EventSubProcessNode) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) NonAcceptingEventTypeFilter(io.automatiko.engine.workflow.base.core.event.NonAcceptingEventTypeFilter) ProcessContext(io.automatiko.engine.api.runtime.process.ProcessContext) NonAcceptingEventTypeFilter(io.automatiko.engine.workflow.base.core.event.NonAcceptingEventTypeFilter) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter) 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 3 with ProcessContext

use of io.automatiko.engine.api.runtime.process.ProcessContext in project automatiko-engine by automatiko-io.

the class ProcessEventSupportTest method testProcessEventListener.

@Test
public void testProcessEventListener() throws Exception {
    // create a simple package with one process to test the events
    ExecutableProcess process = new ExecutableProcess();
    process.setId("org.company.core.process.event");
    process.setName("Event Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Print");
    ProcessAction action = new ConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Executed action");
        }
    });
    actionNode.setAction(action);
    actionNode.setId(2);
    process.addNode(actionNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    process.addNode(endNode);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    InternalProcessRuntime processRuntime = new ProcessRuntimeImpl(Collections.singletonMap(process.getId(), process));
    final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
    final ProcessEventListener processEventListener = new ProcessEventListener() {

        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void afterProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void afterProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }
    };
    processRuntime.addEventListener(processEventListener);
    // execute the process
    processRuntime.startProcess("org.company.core.process.event");
    assertEquals(16, processEventList.size());
    assertEquals("org.company.core.process.event", ((ProcessStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(1)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(2)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(3)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(4)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(5)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(6)).getNodeInstance().getNodeName());
    assertEquals("org.company.core.process.event", ((ProcessCompletedEvent) processEventList.get(7)).getProcessInstance().getProcessId());
    assertEquals("org.company.core.process.event", ((ProcessCompletedEvent) processEventList.get(8)).getProcessInstance().getProcessId());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(9)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(10)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(11)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(12)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(13)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(14)).getNodeInstance().getNodeName());
    assertEquals("org.company.core.process.event", ((ProcessStartedEvent) processEventList.get(15)).getProcessInstance().getProcessId());
}
Also used : ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) Action(io.automatiko.engine.workflow.base.instance.impl.Action) ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ProcessEvent(io.automatiko.engine.api.event.process.ProcessEvent) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) ProcessRuntimeImpl(io.automatiko.engine.workflow.base.instance.ProcessRuntimeImpl) ArrayList(java.util.ArrayList) ProcessStartedEvent(io.automatiko.engine.api.event.process.ProcessStartedEvent) ConnectionImpl(io.automatiko.engine.workflow.process.core.impl.ConnectionImpl) ProcessCompletedEvent(io.automatiko.engine.api.event.process.ProcessCompletedEvent) ProcessContext(io.automatiko.engine.api.runtime.process.ProcessContext) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) ProcessNodeTriggeredEvent(io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) ProcessNodeLeftEvent(io.automatiko.engine.api.event.process.ProcessNodeLeftEvent) AbstractBaseTest(io.automatiko.engine.workflow.test.util.AbstractBaseTest) Test(org.junit.jupiter.api.Test)

Example 4 with ProcessContext

use of io.automatiko.engine.api.runtime.process.ProcessContext in project automatiko-engine by automatiko-io.

the class ProcessEventSupportTest method testProcessEventListenerWithEndEvent.

@Test
public void testProcessEventListenerWithEndEvent() throws Exception {
    ExecutableProcess process = new ExecutableProcess();
    process.setId("org.company.core.process.event");
    process.setName("Event Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Print");
    ProcessAction action = new ConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Executed action");
        }
    });
    actionNode.setAction(action);
    actionNode.setId(2);
    process.addNode(actionNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    endNode.setTerminate(false);
    process.addNode(endNode);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    InternalProcessRuntime processRuntime = new ProcessRuntimeImpl(Collections.singletonMap(process.getId(), process));
    final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
    final ProcessEventListener processEventListener = new ProcessEventListener() {

        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void afterProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void afterProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }
    };
    processRuntime.addEventListener(processEventListener);
    // execute the process
    processRuntime.startProcess("org.company.core.process.event");
    assertEquals(14, processEventList.size());
    assertEquals("org.company.core.process.event", ((ProcessStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(1)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(2)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(3)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(4)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(5)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(6)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(7)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(8)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(9)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(10)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(11)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(12)).getNodeInstance().getNodeName());
    assertEquals("org.company.core.process.event", ((ProcessStartedEvent) processEventList.get(13)).getProcessInstance().getProcessId());
}
Also used : ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) Action(io.automatiko.engine.workflow.base.instance.impl.Action) ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ProcessEvent(io.automatiko.engine.api.event.process.ProcessEvent) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) ProcessRuntimeImpl(io.automatiko.engine.workflow.base.instance.ProcessRuntimeImpl) ArrayList(java.util.ArrayList) ProcessStartedEvent(io.automatiko.engine.api.event.process.ProcessStartedEvent) ConnectionImpl(io.automatiko.engine.workflow.process.core.impl.ConnectionImpl) ProcessCompletedEvent(io.automatiko.engine.api.event.process.ProcessCompletedEvent) ProcessContext(io.automatiko.engine.api.runtime.process.ProcessContext) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) ProcessNodeTriggeredEvent(io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) ProcessNodeLeftEvent(io.automatiko.engine.api.event.process.ProcessNodeLeftEvent) AbstractBaseTest(io.automatiko.engine.workflow.test.util.AbstractBaseTest) Test(org.junit.jupiter.api.Test)

Example 5 with ProcessContext

use of io.automatiko.engine.api.runtime.process.ProcessContext in project automatiko-engine by automatiko-io.

the class ProcessEventSupportTest method testProcessEventListenerWithEvent.

@Test
public void testProcessEventListenerWithEvent() throws Exception {
    ExecutableProcess process = new ExecutableProcess();
    process.setId("org.company.core.process.event");
    process.setName("Event Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Print");
    ProcessAction action = new ConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Executed action");
        }
    });
    actionNode.setAction(action);
    actionNode.setId(2);
    process.addNode(actionNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EventNode eventNode = new EventNode();
    eventNode.setName("Event");
    eventNode.setId(3);
    List<EventFilter> filters = new ArrayList<EventFilter>();
    EventTypeFilter filter = new EventTypeFilter();
    filter.setType("signal");
    filters.add(filter);
    eventNode.setEventFilters(filters);
    process.addNode(eventNode);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, eventNode, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(4);
    process.addNode(endNode);
    new ConnectionImpl(eventNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    InternalProcessRuntime processRuntime = new ProcessRuntimeImpl(Collections.singletonMap(process.getId(), process));
    final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
    final ProcessEventListener processEventListener = new ProcessEventListener() {

        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void afterProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void afterProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
        }

        public void beforeProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
        }

        public void beforeVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
        }
    };
    processRuntime.addEventListener(processEventListener);
    // execute the process
    ProcessInstance pi = processRuntime.startProcess("org.company.core.process.event");
    pi.signalEvent("signal", null);
    assertEquals(20, processEventList.size());
    assertEquals("org.company.core.process.event", ((ProcessStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(1)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(2)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(3)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(4)).getNodeInstance().getNodeName());
    assertEquals("Event", ((ProcessNodeTriggeredEvent) processEventList.get(5)).getNodeInstance().getNodeName());
    assertEquals("Event", ((ProcessNodeTriggeredEvent) processEventList.get(6)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(7)).getNodeInstance().getNodeName());
    assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(8)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(9)).getNodeInstance().getNodeName());
    assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(10)).getNodeInstance().getNodeName());
    assertEquals("org.company.core.process.event", ((ProcessStartedEvent) processEventList.get(11)).getProcessInstance().getProcessId());
    assertEquals("Event", ((ProcessNodeLeftEvent) processEventList.get(12)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(13)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(14)).getNodeInstance().getNodeName());
    assertEquals("org.company.core.process.event", ((ProcessCompletedEvent) processEventList.get(15)).getProcessInstance().getProcessId());
    assertEquals("org.company.core.process.event", ((ProcessCompletedEvent) processEventList.get(16)).getProcessInstance().getProcessId());
    assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(17)).getNodeInstance().getNodeName());
    assertEquals("Event", ((ProcessNodeLeftEvent) processEventList.get(19)).getNodeInstance().getNodeName());
    assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(18)).getNodeInstance().getNodeName());
}
Also used : ProcessVariableChangedEvent(io.automatiko.engine.api.event.process.ProcessVariableChangedEvent) Action(io.automatiko.engine.workflow.base.instance.impl.Action) ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) ArrayList(java.util.ArrayList) ConnectionImpl(io.automatiko.engine.workflow.process.core.impl.ConnectionImpl) ProcessContext(io.automatiko.engine.api.runtime.process.ProcessContext) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter) ProcessNodeTriggeredEvent(io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent) ProcessNodeLeftEvent(io.automatiko.engine.api.event.process.ProcessNodeLeftEvent) ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) ProcessEvent(io.automatiko.engine.api.event.process.ProcessEvent) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) ProcessRuntimeImpl(io.automatiko.engine.workflow.base.instance.ProcessRuntimeImpl) ProcessStartedEvent(io.automatiko.engine.api.event.process.ProcessStartedEvent) EventFilter(io.automatiko.engine.workflow.base.core.event.EventFilter) ProcessCompletedEvent(io.automatiko.engine.api.event.process.ProcessCompletedEvent) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) InternalProcessRuntime(io.automatiko.engine.workflow.base.instance.InternalProcessRuntime) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) AbstractBaseTest(io.automatiko.engine.workflow.test.util.AbstractBaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

ProcessContext (io.automatiko.engine.api.runtime.process.ProcessContext)10 Action (io.automatiko.engine.workflow.base.instance.impl.Action)9 ProcessAction (io.automatiko.engine.workflow.process.core.ProcessAction)9 ActionNode (io.automatiko.engine.workflow.process.core.node.ActionNode)9 ConsequenceAction (io.automatiko.engine.workflow.process.core.impl.ConsequenceAction)8 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)7 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)7 ExecutableProcess (io.automatiko.engine.workflow.process.executable.core.ExecutableProcess)7 ArrayList (java.util.ArrayList)7 Test (org.junit.jupiter.api.Test)6 ProcessCompletedEvent (io.automatiko.engine.api.event.process.ProcessCompletedEvent)5 ProcessEventListener (io.automatiko.engine.api.event.process.ProcessEventListener)5 ProcessNodeLeftEvent (io.automatiko.engine.api.event.process.ProcessNodeLeftEvent)5 ProcessNodeTriggeredEvent (io.automatiko.engine.api.event.process.ProcessNodeTriggeredEvent)5 ProcessStartedEvent (io.automatiko.engine.api.event.process.ProcessStartedEvent)5 ProcessVariableChangedEvent (io.automatiko.engine.api.event.process.ProcessVariableChangedEvent)5 EventTypeFilter (io.automatiko.engine.workflow.base.core.event.EventTypeFilter)5 InternalProcessRuntime (io.automatiko.engine.workflow.base.instance.InternalProcessRuntime)5 ProcessRuntimeImpl (io.automatiko.engine.workflow.base.instance.ProcessRuntimeImpl)5 ConnectionImpl (io.automatiko.engine.workflow.process.core.impl.ConnectionImpl)5