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 testSubnetworkNoSharing() throws Exception {
    KnowledgeBase kbase = buildKnowledgeBase(" 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];

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

    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn1 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];

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

    // LiaNode and Rule are in same segment
    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(liaNode, smem.getRootNode());
    assertEquals(rtn1, smem.getTipNode());
    assertNull(smem.getNext());
    assertNull(smem.getFirst());

    SegmentMemory bSmem =
        wm.getNodeMemory(bNode).getSegmentMemory(); // it's nested inside of smem, so lookup from wm
    assertEquals(bNode, bSmem.getRootNode());
    assertEquals(riaNode, bSmem.getTipNode());

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(notNode);
    assertEquals(bSmem, bm.getSubnetworkSegmentMemory()); // check subnetwork ref was made
  }