Ejemplo n.º 1
0
  @Test
  public void tesShareInSubnetwork() throws Exception {
    KnowledgeBase kbase =
        buildKnowledgeBase("   A() \n", "   A() B() C() \n", "   A()  not ( B() and C() ) \n");

    ReteooWorkingMemoryInterface wm =
        ((StatefulKnowledgeSessionImpl) kbase.newStatefulKnowledgeSession()).session;

    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);

    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];

    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[1];

    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[2];
    RuleTerminalNode rtn3 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];

    wm.insert(new A());
    wm.insert(new B());
    wm.insert(new C());

    // LiaNode  is in it's own segment
    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(liaNode, smem.getRootNode());
    assertEquals(liaNode, smem.getTipNode());

    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());

    SegmentMemory bSmem = rtnSmem1.getNext();
    assertEquals(bNode, bSmem.getRootNode());
    assertEquals(cNode, bSmem.getTipNode());

    assertNull(bSmem.getFirst()); // segment is not initialized yet

    wm.fireAllRules();

    SegmentMemory rtn2Smem = bSmem.getFirst();
    assertEquals(rtn2, rtn2Smem.getRootNode());
    assertEquals(rtn2, rtn2Smem.getTipNode());

    SegmentMemory riaSmem = rtn2Smem.getNext();
    assertEquals(riaNode, riaSmem.getRootNode());
    assertEquals(riaNode, riaSmem.getTipNode());

    SegmentMemory notSmem = bSmem.getNext();
    assertEquals(notNode, notSmem.getRootNode());
    assertEquals(rtn3, notSmem.getTipNode());
  }
Ejemplo n.º 2
0
  @Test
  public void testMultiSharedPattern() throws Exception {
    KnowledgeBase kbase = buildKnowledgeBase("   A() \n", "   A() B() \n", "   A() B() C() \n");

    ReteooWorkingMemoryInterface wm =
        ((StatefulKnowledgeSessionImpl) kbase.newStatefulKnowledgeSession()).session;

    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);

    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn2 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];

    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn3 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];

    wm.insert(new A());
    wm.insert(new B());
    wm.insert(new C());

    // LiaNode  is in it's own segment
    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(liaNode, smem.getRootNode());
    assertEquals(liaNode, smem.getTipNode());

    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());

    SegmentMemory bSmem = rtnSmem1.getNext();
    assertEquals(bNode, bSmem.getRootNode());
    assertEquals(bNode, bSmem.getTipNode());

    // child segment is not yet initialised, so null
    assertNull(bSmem.getFirst());

    // there is no next
    assertNull(bSmem.getNext());

    wm.fireAllRules(); // child segments should now be initialised

    SegmentMemory rtnSmem2 = bSmem.getFirst();
    assertEquals(rtn2, rtnSmem2.getRootNode());
    assertEquals(rtn2, rtnSmem2.getTipNode());

    SegmentMemory cSmem = rtnSmem2.getNext();
    assertEquals(cNode, cSmem.getRootNode());
    assertEquals(rtn3, cSmem.getTipNode()); // note rtn3 is in the same segment as C
  }