コード例 #1
0
  @Test
  public void testLiaNodeInitialisation() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    SegmentUtilities.createSegmentMemory(liaNode, wm);
    liaNode.assertObject((InternalFactHandle) wm.insert("str"), context, wm);

    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n1
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    n1.assertObject((InternalFactHandle) wm.insert("str"), context, wm);

    liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
  }
コード例 #2
0
  @Test
  public void testNestedCorelatedRulesWithForall() throws Exception {

    PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(
            FirstOrderLogicTest.class.getResourceAsStream(
                "test_NestedCorrelatedRulesWithForall.drl")));

    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    ruleBase.addPackage(builder.getPackage());
    ruleBase = SerializationHelper.serializeObject(ruleBase);
    StatefulSession session = ruleBase.newStatefulSession();

    List list1 = new ArrayList();
    List list2 = new ArrayList();
    List list3 = new ArrayList();
    List list4 = new ArrayList();

    session.setGlobal("list1", list1);
    session.setGlobal("list2", list2);
    session.setGlobal("list3", list3);
    session.setGlobal("list4", list4);

    SpecialString first42 = new SpecialString("42");
    SpecialString second42 = new SpecialString("42");
    SpecialString world = new SpecialString("World");

    // System.out.println( "Inserting ..." );

    session.insert(world);
    session.insert(first42);
    session.insert(second42);

    // System.out.println( "Done." );

    // System.out.println( "Firing rules ..." );

    // check all lists are empty
    assertTrue(list1.isEmpty());
    assertTrue(list2.isEmpty());
    assertTrue(list3.isEmpty());
    assertTrue(list4.isEmpty());

    session.fireAllRules();

    // System.out.println( "Done." );

    // check first list is populated correctly
    assertEquals(0, list1.size());

    // check second list is populated correctly
    assertEquals(0, list2.size());

    // check third list is populated correctly
    assertEquals(1, list3.size());

    // check fourth list is populated correctly
    assertEquals(0, list4.size());
  }
コード例 #3
0
ファイル: LargeRuleBase.java プロジェクト: henry-liu/drools
  private static void smallBlobCompile() throws DroolsParserException, IOException, Exception {

    /* love you */ long time = System.currentTimeMillis();
    PackageBuilder b = new PackageBuilder();
    b.addPackageFromDrl(new StringReader(getHeader()));
    for (int i = 0; i < RULE_COUNT; i++) {
      String name = "x" + i;
      int status = i;

      String r = getTemplate2(name, i, status);
      b.addPackageFromDrl(new StringReader(r));
      if (i % 1000 == 0) System.err.println("Rule #" + i);
    }

    assertFalse(b.getErrors().toString(), b.hasErrors());

    System.err.println("Time taken for compiling: " + (System.currentTimeMillis() - time));
    time = System.currentTimeMillis();

    Package p = b.getPackage();
    RuleBase rb = RuleBaseFactory.newRuleBase();

    rb.addPackage(p);

    System.err.println("Time taken rete building: " + (System.currentTimeMillis() - time));
  }
コード例 #4
0
  @Test
  public void testAllLinkedInWithNotNodesOnly() {
    setUp(NOT_NODE);

    assertEquals(NotNode.class, n3.getClass()); // make sure it created NotNodes

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n3);
    createSegmentMemory(n3, wm);
    assertTrue(bm.getSegmentMemory().isSegmentLinked()); // not nodes start off linked

    DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1"); // unlinked after first assertion
    n3.assertObject(f1, context, wm);

    // this doesn't unlink on the assertObject, as the node's memory must be processed. So use the
    // helper method the main network evaluator uses.
    PhreakNotNode.unlinkNotNodeOnRightInsert((NotNode) n3, bm, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n3.retractRightTuple(f1.getFirstRightTuple(), context, wm);
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
    // assertFalse( bm.getSegmentMemory().isSigmentLinked() ); // check retraction unlinks again
  }
コード例 #5
0
  @Test
  public void testAllLinkedInWithExistsNodesOnly() {
    setUp(EXISTS_NODE);

    assertEquals(ExistsNode.class, n3.getClass()); // make sure it created ExistsNodes

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1");
    n3.assertObject(f1, context, wm);

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n3);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n4.assertObject(f1, context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n5.assertObject(f1, context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n6.assertObject(f1, context, wm);
    assertTrue(
        bm.getSegmentMemory()
            .isSegmentLinked()); // only after all 4 nodes are populated, is the segment linked in

    n6.retractRightTuple(f1.getLastRightTuple(), context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked()); // check retraction unlinks again
  }
コード例 #6
0
ファイル: LargeRuleBase.java プロジェクト: henry-liu/drools
  private static void bigBlobCompile() throws DroolsParserException, IOException, Exception {
    StringBuilder buf = new StringBuilder();
    buf.append(getHeader());

    for (int i = 0; i < RULE_COUNT; i++) {
      String name = "x" + i;
      int status = i;

      String r = getTemplate1(name, status);
      buf.append(r);
    }

    /* love you */ long time = System.currentTimeMillis();

    DrlParser ps = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr pkg = ps.parse(new StringReader(buf.toString()));

    System.err.println("Time taken for parsing: " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    PackageBuilder b = new PackageBuilder();
    b.addPackage(pkg);
    assertFalse(b.getErrors().toString(), b.hasErrors());

    System.err.println("Time taken for compiling: " + (System.currentTimeMillis() - time));
    time = System.currentTimeMillis();

    Package p = b.getPackage();
    RuleBase rb = RuleBaseFactory.newRuleBase();

    rb.addPackage(p);

    System.err.println("Time taken rete building: " + (System.currentTimeMillis() - time));

    File f = new File("foo.rulebase");
    if (f.exists()) f.delete();

    time = System.currentTimeMillis();
    ObjectOutput out = new DroolsObjectOutputStream(new FileOutputStream(f));
    out.writeObject(rb);
    out.flush();
    out.close();
    System.err.println("Time taken serializing rulebase: " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
    RuleBase rb_ = (RuleBase) in.readObject();
    System.err.println(
        "Time taken de-serializing rulebase: " + (System.currentTimeMillis() - time));
  }
コード例 #7
0
  @Test
  public void testLiaNodeLinking() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));

    SegmentUtilities.createSegmentMemory(liaNode, wm);

    InternalFactHandle fh1 = (InternalFactHandle) wm.insert("str1");
    n1.assertObject(fh1, context, wm);

    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());

    // still unlinked
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());

    // now linked
    InternalFactHandle fh2 = (InternalFactHandle) wm.insert("str2");
    liaNode.assertObject(fh2, context, wm);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    // test unlink after one retract
    liaNode.retractLeftTuple(fh2.getFirstLeftTuple(), context, wm);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());

    // check counter, after multiple asserts
    InternalFactHandle fh3 = (InternalFactHandle) wm.insert("str3");
    InternalFactHandle fh4 = (InternalFactHandle) wm.insert("str4");
    liaNode.assertObject(fh3, context, wm);
    liaNode.assertObject(fh4, context, wm);

    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    liaNode.retractLeftTuple(fh3.getFirstLeftTuple(), context, wm);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    liaNode.retractLeftTuple(fh4.getFirstLeftTuple(), context, wm);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
  }
コード例 #8
0
 public KnowledgeBase newKnowledgeBase(String kbaseId) {
   return new KnowledgeBaseImpl(RuleBaseFactory.newRuleBase(kbaseId));
 }
コード例 #9
0
  @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());
  }
コード例 #10
0
  @Test
  public void test6() 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"
            + "    </header>\n"
            + "\n"
            + "    <nodes>\n"
            + "      <start id=\"1\" name=\"Start\" />\n"
            + "      <timerNode id=\"4\" name=\"Timer\" delay=\"200\" />\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";
    final PackageBuilder builder = new PackageBuilder();
    builder.addProcessFromXml(new StringReader(process));
    final Package pkg = builder.getPackage();
    final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    ruleBase.addPackage(pkg);

    final StatefulSession session = ruleBase.newStatefulSession();

    new Thread(
            new Runnable() {
              public void run() {
                session.fireUntilHalt();
              }
            })
        .start();

    session.startProcess("com.sample.ruleflow", null);
    assertEquals(1, session.getProcessInstances().size());

    StatefulKnowledgeSession ksession =
        new StatefulKnowledgeSessionImpl((ReteooWorkingMemory) session);
    Marshaller marshaller = MarshallerFactory.newMarshaller(ksession.getKieBase());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshall(baos, ksession);
    byte[] b1 = baos.toByteArray();
    session.halt();
    session.dispose();
    Thread.sleep(400);

    ByteArrayInputStream bais = new ByteArrayInputStream(b1);
    final StatefulSession session2 =
        (StatefulSession) ((StatefulKnowledgeSessionImpl) marshaller.unmarshall(bais)).session;

    new Thread(
            new Runnable() {
              public void run() {
                session2.fireUntilHalt();
              }
            })
        .start();

    Thread.sleep(100);

    assertEquals(0, session2.getProcessInstances().size());
    session2.halt();
  }
コード例 #11
0
  @Test
  public void test5() 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"
            + "    </header>\n"
            + "\n"
            + "    <nodes>\n"
            + "      <start id=\"1\" name=\"Start\" />\n"
            + "      <timerNode id=\"4\" name=\"Timer\" delay=\"200\" />\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";
    final PackageBuilder builder = new PackageBuilder();
    builder.addProcessFromXml(new StringReader(process));
    final Package pkg = builder.getPackage();
    final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    ruleBase.addPackage(pkg);

    final StatefulSession session = ruleBase.newStatefulSession();

    new Thread(
            new Runnable() {
              public void run() {
                session.fireUntilHalt();
              }
            })
        .start();

    session.startProcess("com.sample.ruleflow", null);

    assertEquals(1, session.getProcessInstances().size());
    session.halt();

    final StatefulSession session2 = getSerialisedStatefulSession(session);

    new Thread(
            new Runnable() {
              public void run() {
                session2.fireUntilHalt();
              }
            })
        .start();

    Thread.sleep(400);

    assertEquals(0, session2.getProcessInstances().size());

    session2.halt();
  }
コード例 #12
0
  @Test
  public void test4() 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.drools.core.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.drools.core.process.core.datatype.impl.type.StringDataType\" />\n"
            + "              </parameter>\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: #{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.drools.core.process.core.datatype.impl.type.StringDataType\" />\n"
            + "              </parameter>\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: #{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";
    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();

    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 = getSerialisedStatefulSession(session);

    for (WorkItem workItem : handler.getWorkItems()) {
      session.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    }
    assertEquals(0, session.getProcessInstances().size());
  }
コード例 #13
0
  @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());
  }
コード例 #14
0
 public KnowledgeBase newKnowledgeBase(String kbaseId, KieBaseConfiguration conf) {
   return new KnowledgeBaseImpl(
       RuleBaseFactory.newRuleBase(kbaseId, (RuleBaseConfiguration) conf));
 }
コード例 #15
0
 public KnowledgeBase newKnowledgeBase() {
   return new KnowledgeBaseImpl(RuleBaseFactory.newRuleBase());
 }
コード例 #16
0
  public void setUp(int... type) {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf);
    buildContext = new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());

    PropagationContextFactory pctxFactory =
        ruleBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    context =
        pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null);

    MockTupleSource mockTupleSource = new MockTupleSource(9);

    rule1 = new Rule("rule1");
    rule2 = new Rule("rule2");
    rule3 = new Rule("rule3");

    ObjectTypeNode otn =
        new ObjectTypeNode(2, null, new ClassObjectType(String.class), buildContext);
    liaNode = new LeftInputAdapterNode(3, otn, buildContext);

    // 3, 4, 5, 6 are in same shared segment
    n1 = createBetaNode(10, type[0], liaNode);
    n2 = createBetaNode(11, type[1], n1);
    RuleTerminalNode rtn1 = new RuleTerminalNode(18, n2, rule1, rule1.getLhs(), 0, buildContext);
    rtn1.attach();

    n3 = createBetaNode(12, type[2], n1);
    n4 = createBetaNode(13, type[3], n3);
    n5 = createBetaNode(14, type[4], n4);
    n6 = createBetaNode(15, type[5], n5);
    RuleTerminalNode rtn2 = new RuleTerminalNode(19, n6, rule2, rule2.getLhs(), 0, buildContext);
    rtn2.attach();

    n7 = createBetaNode(16, type[6], n6);
    n8 = createBetaNode(17, type[7], n7);
    RuleTerminalNode rtn3 = new RuleTerminalNode(20, n8, rule3, rule3.getLhs(), 0, buildContext);
    rtn3.attach();

    // n1 -> n2 -> r1
    //  \
    //   n3 -> n4 -> n5 -> n6 -> r2
    //                      \
    //                      n7 -> n8 -> r3

    n1.getAssociations().put(rule1, null);
    n1.getAssociations().put(rule2, null);
    n1.getAssociations().put(rule3, null);
    n2.getAssociations().put(rule1, null);
    n2.getAssociations().put(rule2, null);
    n2.getAssociations().put(rule3, null);

    n3.getAssociations().put(rule2, null);
    n3.getAssociations().put(rule3, null);
    n4.getAssociations().put(rule2, null);
    n4.getAssociations().put(rule3, null);
    n5.getAssociations().put(rule2, null);
    n5.getAssociations().put(rule3, null);
    n6.getAssociations().put(rule2, null);
    n6.getAssociations().put(rule3, null);

    n7.getAssociations().put(rule3, null);
    n8.getAssociations().put(rule3, null);
  }
コード例 #17
0
  @Test
  public void tesMultiNodeSegmentDifferentInitialisationPoints() {
    setUp(JOIN_NODE);
    // Initialise from n3
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    createSegmentMemory(n3, wm);

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n4
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    bm = createSegmentMemory(n4, wm);

    bm = (BetaMemory) wm.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n5
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    createSegmentMemory(n5, wm);

    bm = (BetaMemory) wm.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n6
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    createSegmentMemory(n6, wm);

    bm = (BetaMemory) wm.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
  }
コード例 #18
0
 public KnowledgeBase newKnowledgeBase(KieBaseConfiguration conf) {
   return new KnowledgeBaseImpl(RuleBaseFactory.newRuleBase((RuleBaseConfiguration) conf));
 }
コード例 #19
0
  @Test
  public void testSingleNodeinSegment() {

    rule1 = new Rule("rule1");
    rule2 = new Rule("rule2");
    rule3 = new Rule("rule3");

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf);
    BuildContext buildContext =
        new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());

    MockObjectSource mockObjectSource = new MockObjectSource(8);
    MockTupleSource mockTupleSource = new MockTupleSource(9);

    // n2 is only node in it's segment
    ObjectTypeNode otn =
        new ObjectTypeNode(2, null, new ClassObjectType(String.class), buildContext);
    BetaNode n1 =
        new JoinNode(
            10,
            new LeftInputAdapterNode(3, otn, buildContext),
            mockObjectSource,
            new EmptyBetaConstraints(),
            buildContext);
    BetaNode n2 = new JoinNode(11, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n3 = new JoinNode(12, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n4 = new JoinNode(13, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n5 = new JoinNode(14, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext);

    n1.addAssociation(rule1, null);
    n1.addAssociation(rule2, null);
    n1.addAssociation(rule3, null);

    n2.addAssociation(rule2, null);
    n2.addAssociation(rule3, null);

    n3.addAssociation(rule1, null);
    n4.addAssociation(rule2, null);
    n5.addAssociation(rule3, null);

    mockObjectSource.attach();
    mockTupleSource.attach();
    n1.attach();
    n2.attach();
    n3.attach();
    n4.attach();
    n5.attach();

    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    createSegmentMemory(n2, wm);

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n2);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(1, bm.getSegmentMemory().getAllLinkedMaskTest());
  }