Search in sources :

Example 1 with Person

use of org.jbpm.integrationtests.test.Person in project jbpm by kiegroup.

the class ProcessMarshallingTest method testMarshallingProcessInstancesAndGlobals.

@Test
@SuppressWarnings("unchecked")
public void testMarshallingProcessInstancesAndGlobals() throws Exception {
    String rule = "package org.test;\n";
    rule += "import org.jbpm.integrationtests.test.Person\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "  ruleflow-group \"hello\"\n";
    rule += "when\n";
    rule += "    $p : Person( ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";
    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" + "  </header>\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <ruleSet id=\"2\" name=\"Hello\" ruleFlowGroup=\"hello\" />\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>";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(process)), ResourceType.DRF);
    KieSession ksession = createKieSession(kbuilder.getKnowledgePackages().toArray(new KiePackage[0]));
    ksession.getEnvironment().set("org.jbpm.rule.task.waitstate", true);
    List<Object> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    Person p = new Person("bobba fet", 32);
    ksession.insert(p);
    ksession.startProcess("org.test.ruleflow");
    assertEquals(1, ksession.getProcessInstances().size());
    ksession = JbpmSerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals(1, ksession.getProcessInstances().size());
    ksession.fireAllRules();
    assertEquals(1, ((List<Object>) ksession.getGlobal("list")).size());
    assertEquals(p, ((List<Object>) ksession.getGlobal("list")).get(0));
    assertEquals(0, ksession.getProcessInstances().size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.jbpm.integrationtests.test.Person) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 2 with Person

use of org.jbpm.integrationtests.test.Person in project jbpm by kiegroup.

the class ProcessMilestoneTest method testMilestoneWithProcessInstanceConstraint.

@Test
public void testMilestoneWithProcessInstanceConstraint() {
    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.milestone\" package-name=\"org.jbpm\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Person\" />\n" + "      <import name=\"org.jbpm.integrationtests.ProcessMilestoneTest.ProcessUtils\" />\n" + "    </imports>\n" + "    <variables>\n" + "      <variable name=\"name\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <milestone id=\"2\" name=\"Milestone\" >\n" + "      <constraint type=\"rule\" dialect=\"mvel\" >processInstance: org.jbpm.ruleflow.instance.RuleFlowProcessInstance()\n" + "Person( name == (ProcessUtils.getValue(processInstance, \"name\")) )</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);
    for (DroolsError error : builder.getErrors().getErrors()) {
        logger.error(error.toString());
    }
    KieSession workingMemory = createKieSession(builder.getPackages());
    Person john = new Person("John Doe", 20);
    Person jane = new Person("Jane Doe", 20);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", john.getName());
    ProcessInstance processInstanceJohn = (ProcessInstance) workingMemory.startProcess("org.drools.milestone", params);
    workingMemory.insert(processInstanceJohn);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstanceJohn.getState());
    params = new HashMap<String, Object>();
    params.put("name", jane.getName());
    ProcessInstance processInstanceJane = (ProcessInstance) workingMemory.startProcess("org.drools.milestone", params);
    workingMemory.insert(processInstanceJane);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstanceJane.getState());
    workingMemory.insert(jane);
    workingMemory.fireAllRules();
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstanceJohn.getState());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstanceJane.getState());
    workingMemory.insert(john);
    workingMemory.fireAllRules();
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstanceJohn.getState());
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) HashMap(java.util.HashMap) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Person(org.jbpm.integrationtests.test.Person) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with Person

use of org.jbpm.integrationtests.test.Person in project jbpm by kiegroup.

the class ProcessMilestoneTest method testMilestone.

@Test
public void testMilestone() {
    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.milestone\" package-name=\"org.jbpm\" version=\"1\" >\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=\"Milestone\" >\n" + "      <constraint type=\"rule\" dialect=\"mvel\" >Person( name == \"John Doe\" )</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());
    ProcessInstance processInstance = (ProcessInstance) workingMemory.startProcess("org.drools.milestone");
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    workingMemory.insert(new Person("Jane Doe", 20));
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    workingMemory.insert(new Person("John Doe", 50));
    workingMemory.fireAllRules();
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Person(org.jbpm.integrationtests.test.Person) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with Person

use of org.jbpm.integrationtests.test.Person in project jbpm by kiegroup.

the class ProcessSplitTest method testSplitWithProcessInstanceConstraint2.

@Test
public void testSplitWithProcessInstanceConstraint2() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<process xmlns=\"http://drools.org/drools-5.0/process\"" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"" + "         type=\"RuleFlow\" name=\"ruleflow\" id=\"org.jbpm.process-split\" package-name=\"org.jbpm\" >" + "" + "  <header>" + "    <imports>" + "      <import name=\"org.jbpm.integrationtests.test.Person\" />" + "      <import name=\"org.kie.api.runtime.process.WorkflowProcessInstance\" />" + "    </imports>" + "    <globals>" + "      <global identifier=\"list\" type=\"java.util.List\" />" + "    </globals>" + "    <variables>\n" + "      <variable name=\"name\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>" + "" + "  <nodes>" + "    <actionNode id=\"2\" name=\"Action\" >" + "        <action type=\"expression\" dialect=\"mvel\" >insert(kcontext.getProcessInstance());</action>" + "    </actionNode>" + "    <split id=\"4\" name=\"Split\" type=\"2\" >" + "      <constraints>" + "        <constraint toNodeId=\"8\" toType=\"DROOLS_DEFAULT\" priority=\"2\" type=\"rule\" dialect=\"mvel\" >eval(true)</constraint>" + "        <constraint toNodeId=\"6\" toType=\"DROOLS_DEFAULT\" name=\"constraint\" priority=\"1\" type=\"rule\" dialect=\"mvel\" >processInstance: WorkflowProcessInstance()" + "Person( name == ( processInstance.getVariable(\"name\") ) )</constraint>" + "      </constraints>" + "    </split>" + "    <end id=\"8\" name=\"End\" />" + "    <actionNode id=\"6\" name=\"Action\" >" + "        <action type=\"expression\" dialect=\"mvel\" >list.add(kcontext.getProcessInstance().getId());</action>" + "    </actionNode>" + "    <start id=\"1\" name=\"Start\" />" + "    <end id=\"3\" name=\"End\" />" + "  </nodes>" + "  <connections>" + "    <connection from=\"1\" to=\"2\" />" + "    <connection from=\"2\" to=\"4\" />" + "    <connection from=\"4\" to=\"8\" />" + "    <connection from=\"4\" to=\"6\" />" + "    <connection from=\"6\" to=\"3\" />" + "  </connections>" + "" + "</process>");
    kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
    for (KnowledgeBuilderError error : kbuilder.getErrors()) {
        logger.error(error.toString());
    }
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kpkgs);
    KieSession ksession = kbase.newKieSession();
    List<Long> list = new ArrayList<Long>();
    ksession.setGlobal("list", list);
    Person john = new Person("John Doe", 20);
    Person jane = new Person("Jane Doe", 20);
    Person julie = new Person("Julie Doe", 20);
    ksession.insert(john);
    ksession.insert(jane);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", john.getName());
    ProcessInstance processInstance1 = ksession.startProcess("org.jbpm.process-split", params);
    params = new HashMap<String, Object>();
    params.put("name", jane.getName());
    ProcessInstance processInstance2 = ksession.startProcess("org.jbpm.process-split", params);
    params = new HashMap<String, Object>();
    params.put("name", julie.getName());
    ProcessInstance processInstance3 = ksession.startProcess("org.jbpm.process-split", params);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance1.getState());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance2.getState());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance3.getState());
    assertEquals(2, list.size());
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Person(org.jbpm.integrationtests.test.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 5 with Person

use of org.jbpm.integrationtests.test.Person in project jbpm by kiegroup.

the class ProcessStateTest method testDelayedStateConstraint.

@Test
public void testDelayedStateConstraint() {
    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.state\" package-name=\"org.jbpm\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Person\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <state id=\"2\" >\n" + "      <constraints>\n" + "        <constraint toNodeId=\"3\" name=\"one\" >\n" + "            Person( age &gt; 21 )" + "        </constraint>" + "       <constraint toNodeId=\"4\" name=\"two\" >\n" + "           Person( age &lt;= 21 )" + "        </constraint>" + "      </constraints>\n" + "    </state>\n" + "    <actionNode id=\"3\" name=\"ActionNode1\" >\n" + "      <action type=\"expression\" dialect=\"java\" >list.add(\"1\");</action>\n" + "    </actionNode>\n" + "    <end id=\"4\" name=\"End\" />\n" + "    <actionNode id=\"5\" name=\"ActionNode2\" >\n" + "      <action type=\"expression\" dialect=\"java\" >list.add(\"2\");</action>\n" + "    </actionNode>\n" + "    <end id=\"6\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "    <connection from=\"3\" to=\"4\" />\n" + "    <connection from=\"2\" to=\"5\" />\n" + "    <connection from=\"5\" to=\"6\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ProcessInstance processInstance = ksession.startProcess("org.drools.state");
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    assertTrue(list.isEmpty());
    Person person = new Person("John Doe", 30);
    ksession.insert(person);
    ksession.fireAllRules();
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals(1, list.size());
    assertEquals("1", list.get(0));
}
Also used : 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) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Person(org.jbpm.integrationtests.test.Person) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

Person (org.jbpm.integrationtests.test.Person)24 Test (org.junit.Test)24 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)23 KieSession (org.kie.api.runtime.KieSession)23 StringReader (java.io.StringReader)21 Reader (java.io.Reader)14 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 RuleFlowProcessInstance (org.jbpm.ruleflow.instance.RuleFlowProcessInstance)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 TestWorkItemHandler (org.jbpm.integrationtests.handler.TestWorkItemHandler)6 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)6 ProcessInstance (org.jbpm.process.instance.ProcessInstance)5 KieBase (org.kie.api.KieBase)5 ReaderResource (org.drools.core.io.impl.ReaderResource)3 KiePackage (org.kie.api.definition.KiePackage)3 DroolsError (org.drools.compiler.compiler.DroolsError)2 Ignore (org.junit.Ignore)2