Search in sources :

Example 36 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class ProcessDynamicNodeTest method testAddDynamicWorkItem.

@Test
public void testAddDynamicWorkItem() {
    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=\"ruleflow\" id=\"org.drools.dynamic\" package-name=\"org.drools\" >\n" + "\n" + "  <header>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <dynamic id=\"2\" name=\"DynamicNode\" >\n" + "      <nodes>\n" + "        <actionNode id=\"1\" name=\"Action\" >\n" + "          <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Action\");</action>\n" + "        </actionNode>\n" + "      </nodes>\n" + "      <connections>\n" + "      </connections>\n" + "      <in-ports/>\n" + "      <out-ports/>\n" + "    </dynamic>\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" + "</process>");
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    KieRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    // start a new process instance
    ProcessInstance processInstance = (ProcessInstance) ksession.startProcess("org.drools.dynamic");
    DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next();
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("TaskName", "Dynamic Task");
    assertNull(handler.getWorkItem());
    assertEquals(0, dynamicContext.getNodeInstances().size());
    DynamicUtils.addDynamicWorkItem(dynamicContext, ksession, "Human Task", parameters);
    assertNotNull(handler.getWorkItem());
    assertEquals(1, dynamicContext.getNodeInstances().size());
    logger.close();
}
Also used : TestWorkItemHandler(org.jbpm.integrationtests.handler.TestWorkItemHandler) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) HashMap(java.util.HashMap) Reader(java.io.Reader) StringReader(java.io.StringReader) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 37 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class ProcessExceptionHandlerTest method testCompositeNodeExceptionHandlerTriggerNode.

@Test
public void testCompositeNodeExceptionHandlerTriggerNode() {
    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.exception\" 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" + "    <composite id=\"2\" name=\"Composite\" >\n" + "      <variables>\n" + "        <variable name=\"SomeVar\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "          <value>SomeValue</value>\n" + "        </variable>\n" + "        <variable name=\"FaultVariable\" >\n" + "          <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        </variable>\n" + "      </variables>\n" + "      <exceptionHandlers>\n" + "        <exceptionHandler faultName=\"MyFault\" type=\"action\" faultVariable=\"FaultVariable\" >\n" + "          <action type=\"expression\" name=\"Trigger\" dialect=\"java\" >context.getProcessInstance().signalEvent(\"MyEvent\", null);</action>\n" + "        </exceptionHandler>\n" + "      </exceptionHandlers>\n" + "      <nodes>\n" + "        <fault id=\"1\" name=\"Fault\" faultName=\"MyFault\" faultVariable=\"SomeVar\" />\n" + "        <eventNode id=\"2\" name=\"Event\" >\n" + "          <eventFilters>\n" + "            <eventFilter type=\"eventType\" eventType=\"MyEvent\" />\n" + "          </eventFilters>\n" + "        </eventNode>\n" + "        <actionNode id=\"3\" name=\"Action\" >\n" + "          <action type=\"expression\" dialect=\"java\" >list.add(context.getVariable(\"FaultVariable\"));</action>\n" + "        </actionNode>\n" + "      </nodes>\n" + "      <connections>\n" + "        <connection from=\"2\" to=\"3\" />\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=\"3\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + "      </out-ports>\n" + "    </composite>\n" + "    <end id=\"3\" name=\"End\" />" + "  </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 session = createKieSession(builder.getPackages());
    List<String> list = new ArrayList<String>();
    session.setGlobal("list", list);
    ProcessInstance processInstance = (ProcessInstance) session.startProcess("org.drools.exception");
    assertEquals(1, list.size());
    assertEquals("SomeValue", list.get(0));
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 38 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class ProcessForEachTest method testForEachNullList.

@Test
public void testForEachNullList() {
    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=\"ForEach\" id=\"org.drools.ForEach\" package-name=\"org.drools\" >\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"myList\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"collection\" >\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=\"2\" name=\"ForEach\" variableName=\"item\" collectionExpression=\"collection\" >\n" + "      <nodes>\n" + "    <actionNode id=\"1\" name=\"Action\" >\n" + "        <action type=\"expression\" dialect=\"mvel\" >myList.add(item);</action>\n" + "    </actionNode>\n" + "      </nodes>\n" + "      <connections>\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=\"1\" 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=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "</process>");
    builder.addRuleFlow(source);
    KieSession workingMemory = createKieSession(builder.getPackages());
    List<String> myList = new ArrayList<String>();
    workingMemory.setGlobal("myList", myList);
    ProcessInstance processInstance = (ProcessInstance) workingMemory.startProcess("org.drools.ForEach");
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 39 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class ProcessForEachTest method testForEachLargeList.

@Test
public void testForEachLargeList() {
    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=\"ForEach\" id=\"org.drools.ForEach\" package-name=\"org.drools\" >\n" + "  <header>\n" + "    <variables>\n" + "      <variable name=\"collection\" >\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=\"2\" name=\"ForEach\" variableName=\"item\" collectionExpression=\"collection\" >\n" + "      <nodes>\n" + "        <workItem id=\"1\" name=\"Log\" >\n" + "          <work name=\"Log\" >\n" + "            <parameter name=\"Message\" >\n" + "              <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "            </parameter>\n" + "          </work>\n" + "          <mapping type=\"in\" from=\"item\" to=\"Message\" />" + "        </workItem>\n" + "      </nodes>\n" + "      <connections>\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=\"1\" 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=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "</process>");
    builder.addRuleFlow(source);
    KieSession workingMemory = createKieSession(builder.getPackages());
    final List<String> myList = new ArrayList<String>();
    workingMemory.getWorkItemManager().registerWorkItemHandler("Log", new WorkItemHandler() {

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            String message = (String) workItem.getParameter("Message");
            myList.add(message);
            manager.completeWorkItem(workItem.getId(), null);
        }

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    List<String> collection = new ArrayList<String>();
    for (int i = 0; i < 10000; i++) {
        collection.add(i + "");
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("collection", collection);
    ProcessInstance processInstance = (ProcessInstance) workingMemory.startProcess("org.drools.ForEach", params);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals(10000, myList.size());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 40 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class ProcessForEachTest method testForEachCancel.

@Test
public void testForEachCancel() {
    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=\"ForEach\" id=\"org.drools.ForEach\" package-name=\"org.jbpm\" >\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"myList\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"collection\" >\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=\"2\" name=\"ForEach\" variableName=\"item\" collectionExpression=\"collection\" >\n" + "      <nodes>\n" + "    <subProcess id=\"1\" name=\"SubProcess\" processId=\"org.drools.subflow\" independent=\"false\" />\n" + "      </nodes>\n" + "      <connections>\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=\"1\" 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=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "</process>");
    builder.addRuleFlow(source);
    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.subflow\" package-name=\"org.jbpm\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Person\" />\n" + "    </imports>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <milestone id=\"2\" name=\"Event Wait\" >\n" + "      <constraint type=\"rule\" dialect=\"mvel\" >Person( )</constraint>" + "    </milestone>\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());
    List<String> collection = new ArrayList<String>();
    collection.add("one");
    collection.add("two");
    collection.add("three");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("collection", collection);
    ProcessInstance processInstance = (ProcessInstance) workingMemory.startProcess("org.drools.ForEach", params);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    assertEquals(4, workingMemory.getProcessInstances().size());
    processInstance.setState(ProcessInstance.STATE_ABORTED);
    assertEquals(0, workingMemory.getProcessInstances().size());
}
Also used : HashMap(java.util.HashMap) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

ProcessInstance (org.jbpm.process.instance.ProcessInstance)49 KieSession (org.kie.api.runtime.KieSession)34 StringReader (java.io.StringReader)33 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)33 Test (org.junit.Test)33 Reader (java.io.Reader)30 ArrayList (java.util.ArrayList)26 HashMap (java.util.HashMap)16 TestWorkItemHandler (org.jbpm.integrationtests.handler.TestWorkItemHandler)10 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 DroolsError (org.drools.compiler.compiler.DroolsError)6 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)6 Person (org.jbpm.integrationtests.test.Person)5 WorkItem (org.kie.api.runtime.process.WorkItem)5 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)5 KieRuntime (org.kie.api.runtime.KieRuntime)4 WorkItemHandlerNotFoundException (org.drools.core.WorkItemHandlerNotFoundException)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 WorkItem (org.drools.core.process.instance.WorkItem)3 Message (org.jbpm.integrationtests.test.Message)3