Search in sources :

Example 6 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class SegmentCreationTest method tesSubnetworkAfterShare.

@Test
public void tesSubnetworkAfterShare() throws Exception {
    KieBase kbase = buildKnowledgeBase("   A() \n", "   A()  not ( B() and C() ) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[0];
    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[2];
    RuleTerminalNode rtn2 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];
    wm.insert(new LinkingTest.A());
    wm.insert(new LinkingTest.B());
    wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    // 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(riaNode, bSmem.getTipNode());
    SegmentMemory notSmem = bSmem.getNext();
    assertEquals(notNode, notSmem.getRootNode());
    assertEquals(rtn2, notSmem.getTipNode());
    // child segment is not yet initialised, so null
    assertNull(bSmem.getFirst());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 7 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class SegmentCreationTest method testSingleEmptyLhs.

@Test
public void testSingleEmptyLhs() throws Exception {
    KieBase kbase = buildKnowledgeBase(" ");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, InitialFactImpl.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    wm.insert(new LinkingTest.A());
    wm.flushPropagations();
    // LiaNode and Rule are in same segment
    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(liaNode, smem.getRootNode());
    assertEquals(rtn, smem.getTipNode());
    assertNull(smem.getNext());
    assertNull(smem.getFirst());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 8 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class SegmentCreationTest method testSubnetworkNoSharing.

@Test
public void testSubnetworkNoSharing() throws Exception {
    KieBase kbase = buildKnowledgeBase(" A()  not ( B() and C() ) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().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 LinkingTest.A());
    wm.insert(new LinkingTest.B());
    wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    // 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());
    assertNull(smem.getNext());
    smem = smem.getFirst();
    // it's nested inside of smem, so lookup from wm
    SegmentMemory bSmem = wm.getNodeMemory(bNode).getSegmentMemory();
    assertEquals(smem, bSmem);
    assertEquals(bNode, bSmem.getRootNode());
    assertEquals(riaNode, bSmem.getTipNode());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(notNode);
    assertEquals(bm.getSegmentMemory(), smem.getNext());
    // check subnetwork ref was made
    assertEquals(bSmem, bm.getRiaRuleMemory().getSegmentMemory());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 9 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class SegmentCreationTest method testMultiSharedPattern.

@Test
public void testMultiSharedPattern() throws Exception {
    KieBase kbase = buildKnowledgeBase("   A() \n", "   A() B() \n", "   A() B() C() \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().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 LinkingTest.A());
    wm.insert(new LinkingTest.B());
    wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    // 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());
    // child segments should now be initialised
    wm.fireAllRules();
    wm.flushPropagations();
    SegmentMemory rtnSmem2 = bSmem.getFirst();
    assertEquals(rtn2, rtnSmem2.getRootNode());
    assertEquals(rtn2, rtnSmem2.getTipNode());
    SegmentMemory cSmem = rtnSmem2.getNext();
    assertEquals(cNode, cSmem.getRootNode());
    // note rtn3 is in the same segment as C
    assertEquals(rtn3, cSmem.getTipNode());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) KieBase(org.kie.api.KieBase) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 10 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class AddRuleTest method testPopulatedRuleWithEvals.

@Test
public void testPopulatedRuleWithEvals() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a:A() B() eval(1==1) eval(1==1) C(1;) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a:A() B() eval(1==1) eval(1==1) C(2;) \n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) e2.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals(3, c1Mem.getLeftTupleMemory().size());
    assertEquals(1, c1Mem.getRightTupleMemory().size());
    BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem = sm.getFirst().getNext();
    assertSame(c2Smem, c2Mem.getSegmentMemory());
    assertEquals(0, c2Mem.getLeftTupleMemory().size());
    assertEquals(0, c2Mem.getRightTupleMemory().size());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertEquals(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertEquals(6, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r1", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals(3, ((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals(2, ((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    assertEquals(1, ((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)29 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)26 Test (org.junit.Test)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)22 SegmentMemory (org.drools.core.reteoo.SegmentMemory)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)15 JoinNode (org.drools.core.reteoo.JoinNode)15 ArrayList (java.util.ArrayList)11 List (java.util.List)11 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)11 BetaMemory (org.drools.core.reteoo.BetaMemory)10 KieBase (org.kie.api.KieBase)9 Match (org.kie.api.runtime.rule.Match)8 NotNode (org.drools.core.reteoo.NotNode)5 FactHandle (org.kie.api.runtime.rule.FactHandle)5 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)5 Arrays.asList (java.util.Arrays.asList)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)4