@Test public void test3() throws Exception { String process1 = "<?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" + " <imports>\n" + " <import name=\"org.jbpm.integrationtests.test.Person\" />\n" + " </imports>\n" + " <swimlanes>\n" + " <swimlane name=\"swimlane\" />\n" + " </swimlanes>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <end id=\"4\" name=\"End\" />\n" + " <split id=\"5\" name=\"AND\" type=\"1\" />\n" + " <subProcess id=\"6\" name=\"SubProcess\" processId=\"com.sample.subflow\" />\n" + " <actionNode id=\"7\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Executing action 1\");</action>\n" + " </actionNode>\n" + " <join id=\"8\" name=\"AND\" type=\"1\" />\n" + " <actionNode id=\"9\" name=\"Action\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Executing action 2\");</action>\n" + " </actionNode>\n" + " <ruleSet id=\"10\" name=\"RuleSet\" ruleFlowGroup=\"flowgroup\" />\n" + " <milestone id=\"11\" name=\"Event Wait\" >\n" + " <constraint type=\"rule\" dialect=\"mvel\" >Person( )</constraint>\n" + " </milestone>\n" + " <workItem id=\"12\" name=\"Log\" >\n" + " <work name=\"Log\" >\n" + " <parameter name=\"Message\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>This is a log message</value>\n" + " </parameter>\n" + " </work>\n" + " </workItem>\n" + " <composite id=\"13\" name=\"CompositeNode\" >\n" + " <variables>\n" + " <variable name=\"x\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>x-value</value>\n" + " </variable>\n" + " </variables>\n" + " <nodes>\n" + " <humanTask id=\"1\" name=\"Human Task\" swimlane=\"swimlane\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>John Doe</value>\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something !</value>\n" + " </parameter>\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " </work>\n" + " </humanTask>\n" + " <humanTask id=\"2\" name=\"Human Task\" swimlane=\"swimlane\" >\n" + " <work name=\"Human Task\" >\n" + " <parameter name=\"ActorId\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something else !</value>\n" + " </parameter>\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " </work>\n" + " <mapping type=\"in\" from=\"x\" to=\"Priority\" />\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" + " </composite>\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"9\" to=\"4\" />\n" + " <connection from=\"1\" to=\"5\" />\n" + " <connection from=\"5\" to=\"6\" />\n" + " <connection from=\"5\" to=\"7\" />\n" + " <connection from=\"7\" to=\"8\" />\n" + " <connection from=\"6\" to=\"8\" />\n" + " <connection from=\"10\" to=\"8\" />\n" + " <connection from=\"11\" to=\"8\" />\n" + " <connection from=\"12\" to=\"8\" />\n" + " <connection from=\"13\" to=\"8\" />\n" + " <connection from=\"8\" to=\"9\" />\n" + " <connection from=\"5\" to=\"10\" />\n" + " <connection from=\"5\" to=\"11\" />\n" + " <connection from=\"5\" to=\"12\" />\n" + " <connection from=\"5\" to=\"13\" />\n" + " </connections>\n" + "\n" + "</process>\n"; final PackageBuilder builder = new PackageBuilder(); builder.addProcessFromXml(new StringReader(process1)); String process2 = "<?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=\"com.sample.subflow\" package-name=\"com.sample\" >\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>\n" + " </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>\n"; builder.addProcessFromXml(new StringReader(process2)); String rule = "package com.sample\n" + "import org.jbpm.integrationtests.test.Person;\n" + "rule \"Hello\" ruleflow-group \"flowgroup\"\n" + " when\n" + " then\n" + " System.out.println( \"Hello\" );\n" + "end"; builder.addPackageFromDrl(new StringReader(rule)); final Package pkg = builder.getPackage(); final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(pkg); StatefulSession session = ruleBase.newStatefulSession(); TestWorkItemHandler handler1 = new TestWorkItemHandler(); session.getWorkItemManager().registerWorkItemHandler("Log", handler1); TestWorkItemHandler handler2 = new TestWorkItemHandler(); session.getWorkItemManager().registerWorkItemHandler("Human Task", handler2); session.startProcess("com.sample.ruleflow"); assertEquals(2, session.getProcessInstances().size()); assertTrue(handler1.getWorkItem() != null); long workItemId = handler2.getWorkItem().getId(); assertTrue(workItemId != -1); session = getSerialisedStatefulSession(session); session.getWorkItemManager().registerWorkItemHandler("Human Task", handler2); assertEquals(2, session.getProcessInstances().size()); handler2.reset(); session.getWorkItemManager().completeWorkItem(workItemId, null); assertTrue(handler2.getWorkItem() != null); assertEquals("John Doe", handler2.getWorkItem().getParameter("ActorId")); assertEquals("x-value", handler2.getWorkItem().getParameter("Priority")); session.getWorkItemManager().completeWorkItem(handler1.getWorkItem().getId(), null); session.getWorkItemManager().completeWorkItem(handler2.getWorkItem().getId(), null); session.insert(new Person()); session.fireAllRules(); assertEquals(0, session.getProcessInstances().size()); }
@Test public void testVariablePersistenceMarshallingStrategies() 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=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + " <header>\n" + " <variables>\n" + " <variable name=\"myVariable\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>OldValue</value>\n" + " </variable>\n" + " <variable name=\"myPerson\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.integrationtests.test.Person\"/>\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <workItem id=\"2\" name=\"Email\" >\n" + " <work name=\"Report\" >\n" + " <parameter name=\"Subject\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Mail</value>\n" + " </parameter>\n" + " <parameter name=\"Subject\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Mail</value>\n" + " </parameter>\n" + " </work>\n" + " </workItem>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\"/>\n" + " <connection from=\"2\" to=\"3\"/>\n" + " </connections>\n" + "</process>"; final PackageBuilder builder = new PackageBuilder(); builder.addProcessFromXml(new StringReader(process)); final Package pkg = builder.getPackage(); final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(pkg); StatefulSession session = ruleBase.newStatefulSession(); TestWorkItemHandler handler = new TestWorkItemHandler(); session.getWorkItemManager().registerWorkItemHandler("Report", handler); Map<String, Object> variables = new HashMap<String, Object>(); variables.put("myVariable", "ThisIsMyValue"); Person myPerson = new Person("Nikola Tesla", 156); variables.put("myPerson", myPerson); session.startProcess("org.test.ruleflow", variables); assertEquals(1, session.getProcessInstances().size()); assertTrue(handler.getWorkItem() != null); session = getSerialisedStatefulSession(session); assertEquals(1, session.getProcessInstances().size()); VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstance) session.getProcessInstances().iterator().next()) .getContextInstance(VariableScope.VARIABLE_SCOPE); assertEquals("ThisIsMyValue", variableScopeInstance.getVariable("myVariable")); assertEquals(myPerson, variableScopeInstance.getVariable("myPerson")); session.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null); assertEquals(0, session.getProcessInstances().size()); }
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.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>John Doe</value>\n" + " </variable>\n" + " <variable name=\"Person\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.Person\" />\n" + " </variable>\n" + " <variable name=\"MyObject\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </variable>\n" + " <variable name=\"Number\" >\n" + " <type name=\"org.drools.core.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.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>#{UserName}</value>\n" + " </parameter>\n" + " <parameter name=\"Content\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>#{Person.name}</value>\n" + " </parameter>\n" + " <parameter name=\"TaskName\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>Do something</value>\n" + " </parameter>\n" + " <parameter name=\"Priority\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Comment\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " </parameter>\n" + " <parameter name=\"Attachment\" >\n" + " <type name=\"org.drools.core.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); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); 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")); }