Search in sources :

Example 31 with SegmentMemory

use of org.drools.core.reteoo.SegmentMemory in project drools by kiegroup.

the class PhreakNotNodeTest method setupNotNode.

private void setupNotNode(String operator) {
    buildContext = createContext();
    notNode = (NotNode) BetaNodeBuilder.create(NodeTypeEnums.NotNode, buildContext).setLeftType(A.class).setBinding("object", "$object").setRightType(B.class).setConstraint("object", operator, "$object").build();
    sinkNode = (JoinNode) BetaNodeBuilder.create(NodeTypeEnums.JoinNode, buildContext).build();
    notNode.addTupleSink(sinkNode);
    wm = ((StatefulKnowledgeSessionImpl) buildContext.getKnowledgeBase().newKieSession());
    bm = (BetaMemory) wm.getNodeMemory(notNode);
    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(sinkNode);
    SegmentMemory smem = new SegmentMemory(notNode);
    bm.setSegmentMemory(smem);
    SegmentMemory childSmem = new SegmentMemory(sinkNode);
    bm1.setSegmentMemory(childSmem);
    smem.add(childSmem);
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) BetaMemory(org.drools.core.reteoo.BetaMemory)

Example 32 with SegmentMemory

use of org.drools.core.reteoo.SegmentMemory in project drools by kiegroup.

the class RemoveRuleTest method testSplitTwoBeforeCreatedSegment.

@Test
public void testSplitTwoBeforeCreatedSegment() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n");
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;)\n"));
    kbase1.addPackages(buildKnowledgePackage("r4", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) \n"));
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new E(1));
    wm.insert(new E(2));
    wm.flushPropagations();
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    RuleTerminalNode rtn3 = getRtn("org.kie.r3", kbase1);
    RuleTerminalNode rtn4 = getRtn("org.kie.r4", kbase1);
    PathMemory pm1 = (PathMemory) wm.getNodeMemory(rtn1);
    SegmentMemory[] smems = pm1.getSegmentMemories();
    assertEquals(4, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
    assertNull(smems[3]);
    SegmentMemory sm = smems[2];
    assertEquals(2, sm.getPos());
    assertEquals(4, sm.getSegmentPosMaskBit());
    assertEquals(4, pm1.getLinkedSegmentMask());
    kbase1.addPackages(buildKnowledgePackage("r5", "   A(1;)  A(2;) B(1;) B(2;) \n"));
    smems = pm1.getSegmentMemories();
    assertEquals(5, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
    assertNull(smems[2]);
    sm = smems[3];
    assertEquals(3, sm.getPos());
    assertEquals(8, sm.getSegmentPosMaskBit());
    assertEquals(8, pm1.getLinkedSegmentMask());
    RuleTerminalNode rtn5 = getRtn("org.kie.r5", kbase1);
    PathMemory pm5 = (PathMemory) wm.getNodeMemory(rtn5);
    smems = pm5.getSegmentMemories();
    assertEquals(2, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 33 with SegmentMemory

use of org.drools.core.reteoo.SegmentMemory in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedRuleMidwayShare.

@Test
public void testPopulatedRuleMidwayShare() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B() C(1;) D() E()\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.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B() C(2;) D() E()\n"));
    wm.fireAllRules();
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) bNode.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(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertEquals(6, list.size());
    kbase1.removeRule("org.kie", "r2");
    assertEquals(10, countNodeMemories(wm.getNodeMemories()));
    assertNull(sm.getFirst());
    // c1SMem repoints back to original Smem
    assertSame(sm, c1Mem.getSegmentMemory());
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(6)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(7, list.size());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) 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) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 34 with SegmentMemory

use of org.drools.core.reteoo.SegmentMemory in project drools by kiegroup.

the class RemoveRuleTest 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.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   a:A() B() eval(1==1) eval(1==1) C(2;) \n"));
    wm.fireAllRules();
    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(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertEquals(6, list.size());
    kbase1.removeRule("org.kie", "r2");
    assertEquals(8, countNodeMemories(wm.getNodeMemories()));
    assertNull(sm.getFirst());
    // c1SMem repoints back to original Smem
    assertSame(sm, c1Mem.getSegmentMemory());
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(6)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(7, list.size());
}
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) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 35 with SegmentMemory

use of org.drools.core.reteoo.SegmentMemory in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedSharedToRtn.

@Test
public void testPopulatedSharedToRtn() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B() C() D() E()\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 B(1));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   A() B() C() D() E()\n"));
    wm.fireAllRules();
    assertEquals(8, countNodeMemories(wm.getNodeMemories()));
    assertEquals(4, list.size());
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    PathMemory pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    PathMemory pmem2 = (PathMemory) wm.getNodeMemory(rtn2);
    SegmentMemory[] smems1 = pmem1.getSegmentMemories();
    SegmentMemory[] smems2 = pmem2.getSegmentMemories();
    assertEquals(2, smems1.length);
    assertEquals(2, smems2.length);
    assertSame(smems1[0], smems2[0]);
    assertNotSame(smems1[1], smems2[1]);
    SegmentMemory sm = smems1[0];
    assertEquals(smems1[1], sm.getFirst());
    JoinNode eNode1 = (JoinNode) rtn1.getLeftTupleSource();
    JoinNode eNode2 = (JoinNode) rtn2.getLeftTupleSource();
    assertSame(eNode1, eNode2);
    pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    kbase1.removeRule("org.kie", "r2");
    System.out.println("---");
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    assertNull(sm.getFirst());
    pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    smems1 = pmem1.getSegmentMemories();
    assertEquals(1, smems1.length);
    assertSame(sm, smems1[0]);
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(4)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(5, list.size());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

SegmentMemory (org.drools.core.reteoo.SegmentMemory)67 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)43 PathMemory (org.drools.core.reteoo.PathMemory)30 Test (org.junit.Test)30 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)27 BetaMemory (org.drools.core.reteoo.BetaMemory)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)23 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)22 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)18 JoinNode (org.drools.core.reteoo.JoinNode)18 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)18 ArrayList (java.util.ArrayList)16 List (java.util.List)15 Memory (org.drools.core.common.Memory)14 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)12 LeftTuple (org.drools.core.reteoo.LeftTuple)12 Match (org.kie.api.runtime.rule.Match)11 RiaNodeMemory (org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory)10 KieBase (org.kie.api.KieBase)10 Arrays.asList (java.util.Arrays.asList)9