Search in sources :

Example 81 with WorkItem

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

the class ProcessActionTest method testOnEntryExit.

@Test
public void testOnEntryExit() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <workItem id=\"2\" name=\"HumanTask\" >\n" + "      <work name=\"Human Task\" >\n" + "        <parameter name=\"ActorId\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>John Doe</value>\n" + "        </parameter>\n" + "        <parameter name=\"TaskName\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>Do something</value>\n" + "        </parameter>\n" + "        <parameter name=\"Priority\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        </parameter>\n" + "        <parameter name=\"Comment\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        </parameter>\n" + "      </work>\n" + "      <onEntry>\n" + "        <action type=\"expression\" name=\"Print\" dialect=\"mvel\" >list.add(\"Executing on entry action\");</action>\n" + "      </onEntry>\n" + "      <onExit>\n" + "        <action type=\"expression\" name=\"Print\" dialect=\"java\" >list.add(\"Executing on exit action1\");</action>\n" + "        <action type=\"expression\" name=\"Print\" dialect=\"java\" >list.add(\"Executing on exit action2\");</action>\n" + "      </onExit>\n" + "    </workItem>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(new ReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ProcessInstance processInstance = ksession.startProcess("org.drools.actions");
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    assertEquals(1, list.size());
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertEquals(3, list.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : TestWorkItemHandler(org.jbpm.integrationtests.handler.TestWorkItemHandler) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ReaderResource(org.drools.core.io.impl.ReaderResource) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 82 with WorkItem

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

the class ProcessHumanTaskTest method testHumanTask.

@Test
public void testHumanTask() {
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.humantask\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <humanTask id=\"2\" name=\"HumanTask\" >\n" + "      <work name=\"Human Task\" >\n" + "        <parameter name=\"ActorId\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>John Doe</value>\n" + "        </parameter>\n" + "        <parameter name=\"TaskName\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>Do something</value>\n" + "        </parameter>\n" + "        <parameter name=\"Priority\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        </parameter>\n" + "        <parameter name=\"Comment\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        </parameter>\n" + "      </work>\n" + "    </humanTask>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    builder.addRuleFlow(source);
    KieSession workingMemory = createKieSession(builder.getPackages());
    TestWorkItemHandler handler = new TestWorkItemHandler();
    workingMemory.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    ProcessInstance processInstance = (ProcessInstance) workingMemory.startProcess("org.drools.humantask");
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    workingMemory.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : TestWorkItemHandler(org.jbpm.integrationtests.handler.TestWorkItemHandler) StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 83 with WorkItem

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

the class ProcessMarshallingTest method testMarshallingWithMultipleHumanTasks.

@Test
public void testMarshallingWithMultipleHumanTasks() throws Exception {
    String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "  xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "  xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "  type=\"RuleFlow\" name=\"ruleflow\" id=\"com.sample.ruleflow\" package-name=\"com.sample\" >\n" + "\n" + "    <header>\n" + "      <variables>\n" + "        <variable name=\"list\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.ObjectDataType\" className=\"java.util.List\" />\n" + "        </variable>\n" + "      </variables>\n" + "    </header>\n" + "\n" + "    <nodes>\n" + "      <forEach id=\"4\" name=\"ForEach\" variableName=\"item\" collectionExpression=\"list\" >\n" + "        <nodes>\n" + "          <humanTask id=\"1\" name=\"Human Task\" >\n" + "            <work name=\"Human Task\" >\n" + "              <parameter name=\"Comment\" >\n" + "                <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "              </parameter>\n" + "              <parameter name=\"ActorId\" >\n" + "                <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "              </parameter>\n" + "              <parameter name=\"Priority\" >\n" + "                <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "              </parameter>\n" + "              <parameter name=\"TaskName\" >\n" + "                <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "                <value>Do something: #{item}</value>\n" + "              </parameter>\n" + "            </work>\n" + "          </humanTask>\n" + "          <humanTask id=\"2\" name=\"Human Task Again\" >\n" + "            <work name=\"Human Task\" >\n" + "              <parameter name=\"Comment\" >\n" + "                <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "              </parameter>\n" + "              <parameter name=\"ActorId\" >\n" + "                <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "              </parameter>\n" + "              <parameter name=\"Priority\" >\n" + "                <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "              </parameter>\n" + "              <parameter name=\"TaskName\" >\n" + "                <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "                <value>Do something else: #{item}</value>\n" + "              </parameter>\n" + "            </work>\n" + "          </humanTask>\n" + "        </nodes>\n" + "        <connections>\n" + "          <connection from=\"1\" to=\"2\" />\n" + "        </connections>\n" + "        <in-ports>\n" + "          <in-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeInType=\"DROOLS_DEFAULT\" />\n" + "        </in-ports>\n" + "        <out-ports>\n" + "          <out-port type=\"DROOLS_DEFAULT\" nodeId=\"2\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + "        </out-ports>\n" + "      </forEach>\n" + "      <start id=\"1\" name=\"Start\" />\n" + "      <end id=\"3\" name=\"End\" />\n" + "    </nodes>\n" + "\n" + "    <connections>\n" + "      <connection from=\"1\" to=\"4\" />\n" + "      <connection from=\"4\" to=\"3\" />\n" + "    </connections>\n" + "\n" + "</process>\n";
    builder.addProcessFromXml(new StringReader(process));
    KieSession session = createKieSession(builder.getPackages());
    TestListWorkItemHandler handler = new TestListWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    List<String> list = new ArrayList<String>();
    list.add("one");
    list.add("two");
    list.add("three");
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("list", list);
    session.startProcess("com.sample.ruleflow", parameters);
    assertEquals(1, session.getProcessInstances().size());
    assertEquals(3, handler.getWorkItems().size());
    // session = getSerialisedStatefulSession( session );
    // session.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    List<WorkItem> workItems = new ArrayList<WorkItem>(handler.getWorkItems());
    handler.reset();
    for (WorkItem workItem : workItems) {
        session.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    }
    assertEquals(1, session.getProcessInstances().size());
    assertEquals(3, handler.getWorkItems().size());
    session = getSerialisedStatefulKnowledgeSession(session);
    for (WorkItem workItem : handler.getWorkItems()) {
        session.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    }
    assertEquals(0, session.getProcessInstances().size());
}
Also used : HashMap(java.util.HashMap) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 84 with WorkItem

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

the class ProcessWorkItemTest method testWorkItem.

@Test
public void testWorkItem() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <variables>\n" + "      <variable name=\"UserName\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        <value>John Doe</value>\n" + "      </variable>\n" + "      <variable name=\"Person\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.integrationtests.test.Person\" />\n" + "      </variable>\n" + "      <variable name=\"MyObject\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "      </variable>\n" + "      <variable name=\"Number\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.IntegerDataType\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <workItem id=\"2\" name=\"HumanTask\" >\n" + "      <work name=\"Human Task\" >\n" + "        <parameter name=\"ActorId\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>#{UserName}</value>\n" + "        </parameter>\n" + "        <parameter name=\"Content\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>#{Person.name}</value>\n" + "        </parameter>\n" + "        <parameter name=\"TaskName\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>Do something</value>\n" + "        </parameter>\n" + "        <parameter name=\"Priority\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        </parameter>\n" + "        <parameter name=\"Comment\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        </parameter>\n" + "        <parameter name=\"Attachment\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        </parameter>\n" + "      </work>\n" + "      <mapping type=\"in\" from=\"MyObject\" to=\"Attachment\" />" + "      <mapping type=\"in\" from=\"Person.name\" to=\"Comment\" />" + "      <mapping type=\"out\" from=\"Result\" to=\"MyObject\" />" + "      <mapping type=\"out\" from=\"Result.length()\" to=\"Number\" />" + "    </workItem>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("UserName", "John Doe");
    Person person = new Person();
    person.setName("John Doe");
    parameters.put("Person", person);
    WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("org.drools.actions", parameters);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("John Doe", workItem.getParameter("ActorId"));
    assertEquals("John Doe", workItem.getParameter("Content"));
    assertEquals("John Doe", workItem.getParameter("Comment"));
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    parameters = new HashMap<String, Object>();
    parameters.put("UserName", "Jane Doe");
    parameters.put("MyObject", "SomeString");
    person = new Person();
    person.setName("Jane Doe");
    parameters.put("Person", person);
    processInstance = (WorkflowProcessInstance) ksession.startProcess("org.drools.actions", parameters);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("Jane Doe", workItem.getParameter("ActorId"));
    assertEquals("SomeString", workItem.getParameter("Attachment"));
    assertEquals("Jane Doe", workItem.getParameter("Content"));
    assertEquals("Jane Doe", workItem.getParameter("Comment"));
    Map<String, Object> results = new HashMap<String, Object>();
    results.put("Result", "SomeOtherString");
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), results);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals("SomeOtherString", processInstance.getVariable("MyObject"));
    assertEquals(15, processInstance.getVariable("Number"));
}
Also used : TestWorkItemHandler(org.jbpm.integrationtests.handler.TestWorkItemHandler) HashMap(java.util.HashMap) Reader(java.io.Reader) StringReader(java.io.StringReader) WorkItem(org.kie.api.runtime.process.WorkItem) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) Person(org.jbpm.integrationtests.test.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 85 with WorkItem

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

the class ProcessServiceEJBIntegrationTest method testStartProcessAndGetWorkItems.

@Test
public void testStartProcessAndGetWorkItems() {
    assertNotNull(deploymentService);
    KModuleDeploymentUnit deploymentUnit = new KModuleDeploymentUnit(GROUP_ID, ARTIFACT_ID, VERSION);
    deploymentService.deploy(deploymentUnit);
    units.add(deploymentUnit);
    boolean isDeployed = deploymentService.isDeployed(deploymentUnit.getIdentifier());
    assertTrue(isDeployed);
    assertNotNull(processService);
    long processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
    assertNotNull(processInstanceId);
    ProcessInstance pi = processService.getProcessInstance(processInstanceId);
    assertNotNull(pi);
    Collection<NodeInstanceDesc> activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstanceId, new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    assertEquals("Write a Document", activeNodes.iterator().next().getName());
    List<WorkItem> wis = processService.getWorkItemByProcessInstance(processInstanceId);
    assertNotNull(wis);
    assertEquals(1, wis.size());
    assertEquals("Human Task", wis.get(0).getName());
    assertEquals("Write a Document", wis.get(0).getParameter("NodeName"));
    processService.abortProcessInstance(processInstanceId);
    pi = processService.getProcessInstance(processInstanceId);
    assertNull(pi);
}
Also used : ProcessInstance(org.kie.api.runtime.process.ProcessInstance) QueryContext(org.kie.api.runtime.query.QueryContext) KModuleDeploymentUnit(org.jbpm.kie.services.impl.KModuleDeploymentUnit) NodeInstanceDesc(org.jbpm.services.api.model.NodeInstanceDesc) 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