Search in sources :

Example 31 with WorkItemManager

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

the class FlowTest method testInclusiveParallelExclusiveSplitNoLoop.

@Test
public void testInclusiveParallelExclusiveSplitNoLoop() 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) {
            logger.info(event.getNodeInstance().getNodeName());
            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", 0);
    ProcessInstance processInstance = ksession.startProcess("Process_1", params);
    assertProcessInstanceCompleted(processInstance);
    assertEquals(12, nodeInstanceExecutionCounter.size());
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("Start"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("XORGateway-converging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("ANDGateway-diverging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("ORGateway-diverging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("testWI3"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("testWI2"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("ORGateway-converging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("Script"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("XORGateway-diverging"));
    assertEquals(2, (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 32 with WorkItemManager

use of org.kie.api.runtime.process.WorkItemManager 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 33 with WorkItemManager

use of org.kie.api.runtime.process.WorkItemManager 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 34 with WorkItemManager

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

the class FlowTest method testInclusiveParallelExclusiveSplitNoLoopAsync.

@Test
public void testInclusiveParallelExclusiveSplitNoLoopAsync() 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", 0);
    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(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(1, (int) nodeInstanceExecutionCounter.get("XORGateway-converging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("ANDGateway-diverging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("ORGateway-diverging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("testWI3"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("testWI2"));
    assertEquals(2, (int) nodeInstanceExecutionCounter.get("ORGateway-converging"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("Script"));
    assertEquals(1, (int) nodeInstanceExecutionCounter.get("XORGateway-diverging"));
    assertEquals(2, (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 35 with WorkItemManager

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

the class OneProcessPerThreadTest method testMultiThreadProcessInstanceWorkItem.

@Test
public void testMultiThreadProcessInstanceWorkItem() throws Exception {
    final ConcurrentHashMap<Long, Long> workItems = new ConcurrentHashMap<Long, Long>();
    try {
        final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("BPMN2-MultiThreadServiceProcess.bpmn"), ResourceType.BPMN2);
        KieBase kbase = kbuilder.newKieBase();
        KieSession ksession = createStatefulKnowledgeSession(kbase);
        ksession.getWorkItemManager().registerWorkItemHandler("Log", new WorkItemHandler() {

            public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
                Long threadId = (Long) workItem.getParameter("id");
                workItems.put(workItem.getProcessInstanceId(), threadId);
            }

            public void abortWorkItem(WorkItem arg0, WorkItemManager arg1) {
            }
        });
        startThreads(ksession);
        assertEquals(THREAD_COUNT, workItems.size());
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Should not raise any exception: " + t.getMessage());
    }
    int i = 0;
    while (started.get() > done.get()) {
        logger.info("{} > {}", started, done);
        Thread.sleep(10 * 1000);
        if (++i > 10) {
            fail("Not all threads completed.");
        }
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

WorkItemManager (org.kie.api.runtime.process.WorkItemManager)102 Test (org.junit.Test)86 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)56 TestWorkItemManager (org.jbpm.process.workitem.core.TestWorkItemManager)36 WorkItem (org.kie.api.runtime.process.WorkItem)31 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)27 HashMap (java.util.HashMap)25 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)25 KieBase (org.kie.api.KieBase)23 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)20 KieSession (org.kie.api.runtime.KieSession)15 Map (java.util.Map)9 DefaultWorkItemManager (org.drools.core.process.instance.impl.DefaultWorkItemManager)8 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)8 AsyncWorkItemHandler (org.jbpm.executor.impl.wih.AsyncWorkItemHandler)7 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)7 QueryContext (org.kie.api.runtime.query.QueryContext)6 Document (org.w3c.dom.Document)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ArrayList (java.util.ArrayList)5