示例#1
0
  @Test
  @SuppressWarnings("unchecked")
  public void test1() 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);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    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 = SerializationHelper.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());
  }
  @Test
  public void noProcessInstancesLeftWithPreTxKSessionAndRollback() throws Exception {
    long[] notProcess = new long[4];

    StatefulKnowledgeSession ksession = reloadKnowledgeSession();

    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();

    notProcess[0] =
        ksession.createProcessInstance("org.jbpm.processinstance.helloworld", null).getId();
    notProcess[1] =
        ksession.createProcessInstance("org.jbpm.processinstance.helloworld", null).getId();

    ut.rollback();
    // Validate that proc inst mgr cache is also flushed on rollback
    assertEquals(0, ksession.getProcessInstances().size());

    ksession = reloadKnowledgeSession(ksession);
    assertEquals(0, ksession.getProcessInstances().size());
    ksession.dispose();

    assertProcessInstancesNotExist(notProcess);
  }
  @Test
  public void create2ProcessInstancesInsideTransaction() throws Exception {
    long[] processId = new long[2];

    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();

    StatefulKnowledgeSession ksession = reloadKnowledgeSession();
    processId[0] =
        ksession.createProcessInstance("org.jbpm.processinstance.helloworld", null).getId();
    processId[1] =
        ksession.createProcessInstance("org.jbpm.processinstance.helloworld", null).getId();
    assertEquals(2, ksession.getProcessInstances().size());

    // process instance manager cache flushed on tx
    ut.commit();
    assertEquals(0, ksession.getProcessInstances().size());

    ksession = reloadKnowledgeSession(ksession);
    assertEquals(0, ksession.getProcessInstances().size());
    ksession.dispose();

    assertProcessInstancesExist(processId);
  }
 @Test
 public void getEmptyProcessInstances() throws Exception {
   StatefulKnowledgeSession ksession = reloadKnowledgeSession();
   assertEquals(0, ksession.getProcessInstances().size());
   ksession.dispose();
 }