Search in sources :

Example 41 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class FlowTest method testInclusiveSplitAndJoin.

@Test
public void testInclusiveSplitAndJoin() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-InclusiveSplitAndJoin.bpmn2");
    ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", 15);
    ProcessInstance processInstance = ksession.startProcess("com.sample.test", params);
    List<WorkItem> activeWorkItems = workItemHandler.getWorkItems();
    assertEquals(2, activeWorkItems.size());
    ksession = restoreSession(ksession, true);
    for (WorkItem wi : activeWorkItems) {
        ksession.getWorkItemManager().completeWorkItem(wi.getId(), null);
    }
    assertProcessInstanceFinished(processInstance, ksession);
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

Example 42 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class FlowTest method testInclusiveParallelExclusiveSplitLoopAsync.

@Test
public void testInclusiveParallelExclusiveSplitLoopAsync() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-InclusiveNestedInParallelNestedInExclusive.bpmn2");
    ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("testWI", handler);
    ksession.getWorkItemManager().registerWorkItemHandler("testWI2", new SystemOutWorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            Integer x = (Integer) workItem.getParameter("input1");
            x++;
            Map<String, Object> results = new HashMap<String, Object>();
            results.put("output1", x);
            manager.completeWorkItem(workItem.getId(), results);
        }
    });
    final Map<String, Integer> nodeInstanceExecutionCounter = new HashMap<String, Integer>();
    ksession.addEventListener(new DefaultProcessEventListener() {

        @Override
        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            Integer value = nodeInstanceExecutionCounter.get(event.getNodeInstance().getNodeName());
            if (value == null) {
                value = new Integer(0);
            }
            value++;
            nodeInstanceExecutionCounter.put(event.getNodeInstance().getNodeName(), value);
        }
    });
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", -1);
    ProcessInstance processInstance = ksession.startProcess("Process_1", params);
    assertProcessInstanceActive(processInstance);
    List<WorkItem> workItems = handler.getWorkItems();
    assertNotNull(workItems);
    assertEquals(2, workItems.size());
    // complete work items within OR gateway
    for (WorkItem workItem : workItems) {
        ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    }
    assertProcessInstanceActive(processInstance);
    workItems = handler.getWorkItems();
    assertNotNull(workItems);
    assertEquals(2, workItems.size());
    // complete work items within OR gateway
    for (WorkItem workItem : workItems) {
        ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    }
    assertProcessInstanceActive(processInstance);
    workItems = handler.getWorkItems();
    assertNotNull(workItems);
    assertEquals(1, workItems.size());
    // complete last workitem after AND gateway
    for (WorkItem workItem : workItems) {
        ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    }
    assertProcessInstanceCompleted(processInstance);
    assertEquals(12, nodeInstanceExecutionCounter.size());
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("Start"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("XORGateway-converging"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("ANDGateway-diverging"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("ORGateway-diverging"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("testWI3"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("testWI2"));
    assertEquals(4, (int) nodeInstanceExecutionCounter.get("ORGateway-converging"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("Script"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("XORGateway-diverging"));
    assertEquals(4, (int) nodeInstanceExecutionCounter.get("ANDGateway-converging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("testWI6"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("End"));
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) HashMap(java.util.HashMap) WorkItem(org.kie.api.runtime.process.WorkItem) KieBase(org.kie.api.KieBase) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 43 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class FlowTest method testMultiInstanceLoopCharacteristicsProcessWithORGateway.

@Test
public void testMultiInstanceLoopCharacteristicsProcessWithORGateway() throws Exception {
    KieBase kbase = createKnowledgeBaseWithoutDumper("BPMN2-MultiInstanceLoopCharacteristicsProcessWithORgateway.bpmn2");
    ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    Map<String, Object> params = new HashMap<String, Object>();
    List<Integer> myList = new ArrayList<Integer>();
    myList.add(12);
    myList.add(15);
    params.put("list", myList);
    ProcessInstance processInstance = ksession.startProcess("MultiInstanceLoopCharacteristicsProcess", params);
    List<WorkItem> workItems = workItemHandler.getWorkItems();
    assertEquals(4, workItems.size());
    Collection<NodeInstance> nodeInstances = ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances();
    assertEquals(1, nodeInstances.size());
    NodeInstance nodeInstance = nodeInstances.iterator().next();
    assertTrue(nodeInstance instanceof ForEachNodeInstance);
    Collection<NodeInstance> nodeInstancesChild = ((ForEachNodeInstance) nodeInstance).getNodeInstances();
    assertEquals(2, nodeInstancesChild.size());
    for (NodeInstance child : nodeInstancesChild) {
        assertTrue(child instanceof CompositeContextNodeInstance);
        assertEquals(2, ((CompositeContextNodeInstance) child).getNodeInstances().size());
    }
    ksession.getWorkItemManager().completeWorkItem(workItems.get(0).getId(), null);
    ksession.getWorkItemManager().completeWorkItem(workItems.get(1).getId(), null);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    nodeInstances = ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances();
    assertEquals(1, nodeInstances.size());
    nodeInstance = nodeInstances.iterator().next();
    assertTrue(nodeInstance instanceof ForEachNodeInstance);
    if (isPersistence()) {
        // when persistence is used there is slightly different behaviour of ContextNodeInstance
        // it's already tested by SimplePersistenceBPMNProcessTest.testMultiInstanceLoopCharacteristicsProcessWithORGateway
        nodeInstancesChild = ((ForEachNodeInstance) nodeInstance).getNodeInstances();
        assertEquals(1, nodeInstancesChild.size());
        Iterator<NodeInstance> childIterator = nodeInstancesChild.iterator();
        assertTrue(childIterator.next() instanceof CompositeContextNodeInstance);
        ksession.getWorkItemManager().completeWorkItem(workItems.get(2).getId(), null);
        ksession.getWorkItemManager().completeWorkItem(workItems.get(3).getId(), null);
        assertProcessInstanceFinished(processInstance, ksession);
    } else {
        nodeInstancesChild = ((ForEachNodeInstance) nodeInstance).getNodeInstances();
        assertEquals(2, nodeInstancesChild.size());
        Iterator<NodeInstance> childIterator = nodeInstancesChild.iterator();
        assertTrue(childIterator.next() instanceof CompositeContextNodeInstance);
        assertTrue(childIterator.next() instanceof ForEachJoinNodeInstance);
        ksession.getWorkItemManager().completeWorkItem(workItems.get(2).getId(), null);
        ksession.getWorkItemManager().completeWorkItem(workItems.get(3).getId(), null);
        assertProcessInstanceFinished(processInstance, ksession);
    }
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ForEachJoinNodeInstance(org.jbpm.workflow.instance.node.ForEachNodeInstance.ForEachJoinNodeInstance) WorkItem(org.kie.api.runtime.process.WorkItem) KieBase(org.kie.api.KieBase) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) ForEachNodeInstance(org.jbpm.workflow.instance.node.ForEachNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance) ForEachJoinNodeInstance(org.jbpm.workflow.instance.node.ForEachNodeInstance.ForEachJoinNodeInstance) ForEachNodeInstance(org.jbpm.workflow.instance.node.ForEachNodeInstance) Test(org.junit.Test)

Example 44 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class FlowTest method testInclusiveParallelExclusiveSplitLoop.

@Test
public void testInclusiveParallelExclusiveSplitLoop() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-InclusiveNestedInParallelNestedInExclusive.bpmn2");
    ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("testWI", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("testWI2", new SystemOutWorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            Integer x = (Integer) workItem.getParameter("input1");
            x++;
            Map<String, Object> results = new HashMap<String, Object>();
            results.put("output1", x);
            manager.completeWorkItem(workItem.getId(), results);
        }
    });
    final Map<String, Integer> nodeInstanceExecutionCounter = new HashMap<String, Integer>();
    ksession.addEventListener(new DefaultProcessEventListener() {

        @Override
        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            Integer value = nodeInstanceExecutionCounter.get(event.getNodeInstance().getNodeName());
            if (value == null) {
                value = new Integer(0);
            }
            value++;
            nodeInstanceExecutionCounter.put(event.getNodeInstance().getNodeName(), value);
        }
    });
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", -1);
    ProcessInstance processInstance = ksession.startProcess("Process_1", params);
    assertProcessInstanceCompleted(processInstance);
    assertEquals(12, nodeInstanceExecutionCounter.size());
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("Start"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("XORGateway-converging"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("ANDGateway-diverging"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("ORGateway-diverging"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("testWI3"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("testWI2"));
    assertEquals(4, (int) nodeInstanceExecutionCounter.get("ORGateway-converging"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("Script"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("XORGateway-diverging"));
    assertEquals(4, (int) nodeInstanceExecutionCounter.get("ANDGateway-converging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("testWI6"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("End"));
}
Also used : HashMap(java.util.HashMap) WorkItem(org.kie.api.runtime.process.WorkItem) KieBase(org.kie.api.KieBase) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 45 with WorkItem

use of org.kie.api.runtime.process.WorkItem in project jbpm by kiegroup.

the class FlowTest method testInclusiveSplitAndJoinWithTimer.

@Test(timeout = 10000)
public void testInclusiveSplitAndJoinWithTimer() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 2);
    KieBase kbase = createKnowledgeBase("BPMN2-InclusiveSplitAndJoinWithTimer.bpmn2");
    ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(countDownListener);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", 15);
    ProcessInstance processInstance = ksession.startProcess("com.sample.test", params);
    List<WorkItem> activeWorkItems = workItemHandler.getWorkItems();
    assertEquals(1, activeWorkItems.size());
    ksession.getWorkItemManager().completeWorkItem(activeWorkItems.get(0).getId(), null);
    countDownListener.waitTillCompleted();
    assertProcessInstanceActive(processInstance);
    activeWorkItems = workItemHandler.getWorkItems();
    assertEquals(2, activeWorkItems.size());
    ksession.getWorkItemManager().completeWorkItem(activeWorkItems.get(0).getId(), null);
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(activeWorkItems.get(1).getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

Aggregations

WorkItem (org.kie.api.runtime.process.WorkItem)174 Test (org.junit.Test)139 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)127 KieBase (org.kie.api.KieBase)110 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)91 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)87 HashMap (java.util.HashMap)72 KieSession (org.kie.api.runtime.KieSession)48 WorkItemManager (org.kie.api.runtime.process.WorkItemManager)33 ArrayList (java.util.ArrayList)29 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)27 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)26 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)22 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)20 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)14 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)13 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)13 Map (java.util.Map)11 StringReader (java.io.StringReader)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)10