Search in sources :

Example 96 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() X() 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 X(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   A() B() C() X() 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.kiesession.rulebase.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 97 with SegmentMemory

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

the class RemoveRuleTest method testPopulatedSharedLiaNode.

@Test
public void testPopulatedSharedLiaNode() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B(1;) C() X() 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 B(2));
    wm.insert(new C(1));
    wm.insert(new X(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(3, list.size());
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B(2;) C() X() E()\n"));
    wm.fireAllRules();
    assertEquals(17, countNodeMemories(wm.getNodeMemories()));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode b1Node = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode b2Node = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink();
    JoinNode c1Node = (JoinNode) b1Node.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory b1Mem = (BetaMemory) wm.getNodeMemory(b1Node);
    assertSame(sm.getFirst(), b1Mem.getSegmentMemory());
    assertEquals(3, b1Mem.getLeftTupleMemory().size());
    assertEquals(1, b1Mem.getRightTupleMemory().size());
    BetaMemory b2Mem = (BetaMemory) wm.getNodeMemory(b2Node);
    SegmentMemory b2Smem = sm.getFirst().getNext();
    assertSame(b2Smem, b2Mem.getSegmentMemory());
    assertEquals(3, b2Mem.getLeftTupleMemory().size());
    assertEquals(1, b2Mem.getRightTupleMemory().size());
    assertEquals(6, list.size());
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(b1Mem.getSegmentMemory(), c1Mem.getSegmentMemory());
    assertNotSame(b1Mem.getSegmentMemory(), b2Mem.getSegmentMemory());
    wm.fireAllRules();
    assertEquals(6, list.size());
    assertEquals(17, countNodeMemories(wm.getNodeMemories()));
    kbase1.removeRule("org.kie", "r2");
    assertEquals(12, countNodeMemories(wm.getNodeMemories()));
    assertSame(sm, b1Mem.getSegmentMemory());
    assertSame(sm, c1Mem.getSegmentMemory());
    assertNull(sm.getFirst());
    assertEquals(3, b1Mem.getLeftTupleMemory().size());
    assertEquals(1, b1Mem.getRightTupleMemory().size());
    // SegmentMemory b2Smem =  sm.getFirst().remove();
    assertSame(b2Smem, b2Mem.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.kiesession.rulebase.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 98 with SegmentMemory

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

the class ScenarioTest method setupJoinNode.

public void setupJoinNode() {
    buildContext = createContext();
    joinNode = (JoinNode) BetaNodeBuilder.create(NodeTypeEnums.JoinNode, buildContext).setLeftType(A.class).setBinding("object", "$object").setRightType(B.class).setConstraint("object", "!=", "$object").build();
    sinkNode = (JoinNode) BetaNodeBuilder.create(NodeTypeEnums.JoinNode, buildContext).build();
    joinNode.addTupleSink(sinkNode);
    wm = (InternalWorkingMemory) KnowledgeBaseFactory.newKnowledgeBase(buildContext.getRuleBase()).newKieSession();
    bm = (BetaMemory) wm.getNodeMemory(joinNode);
    bm0 = (BetaMemory) wm.getNodeMemory(sinkNode);
    smem = new SegmentMemory(joinNode);
    bm.setSegmentMemory(smem);
    smem0 = new SegmentMemory(sinkNode);
    bm0.setSegmentMemory(smem0);
    smem.add(smem0);
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory)

Example 99 with SegmentMemory

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

the class AddRuleTest method testPopulatedSingleRuleNoSharing.

@Test
public void testPopulatedSingleRuleNoSharing() {
    KieServices ks = KieServices.get();
    KieContainer kieContainer = KieUtil.getKieContainerFromDrls(kieBaseTestConfiguration, KieSessionTestConfiguration.STATEFUL_PSEUDO);
    InternalWorkingMemory wm = (InternalWorkingMemory) kieContainer.newKieSession();
    wm.insert(new A(1));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new X(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    String rule = buildKnowledgePackageDrl("r1", "   A() B() C(object == 2) X() E()\n");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "testPopulatedSingleRuleNoSharing", "2.0.0");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, KieSessionTestConfiguration.STATEFUL_PSEUDO, new HashMap<>(), rule);
    kieContainer.updateToVersion(releaseId2);
    List<Match> list = new ArrayList<>();
    wm.setGlobal("list", list);
    ObjectTypeNode aotn = getObjectTypeNode(wm.getKnowledgeBase().getRete(), A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    LiaNodeMemory lm = wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    wm.fireAllRules();
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(1, list.size());
    assertEquals("r1", list.get(0).getRule().getName());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieContainer(org.kie.api.runtime.KieContainer) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 100 with SegmentMemory

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

the class AddRuleTest method testSplitOnCreatedSegment.

@Test
public void testSplitOnCreatedSegment() throws Exception {
    // this test splits D1 and D2 on the later add rule
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) X(1;) X(2;) E(1;) E(2;)\n");
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) X(1;) X(2;) E(1;) E(2;)\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) X(1;) X(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 X(1));
    wm.insert(new X(2));
    wm.insert(new X(3));
    wm.flushPropagations();
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    PathMemory pm1 = (PathMemory) wm.getNodeMemory(rtn1);
    assertEquals(2, pm1.getLinkedSegmentMask());
    SegmentMemory[] smems = pm1.getSegmentMemories();
    assertEquals(4, smems.length);
    assertNull(smems[0]);
    assertNull(smems[2]);
    assertNull(smems[3]);
    SegmentMemory sm = smems[1];
    assertEquals(1, sm.getPos());
    assertEquals(2, sm.getSegmentPosMaskBit());
    kbase1.addPackages(buildKnowledgePackage("r5", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) X(1;) X(3;)\n"));
    wm.fireAllRules();
    assertEquals(6, pm1.getLinkedSegmentMask());
    smems = pm1.getSegmentMemories();
    assertEquals(5, smems.length);
    assertNull(smems[0]);
    assertNull(smems[3]);
    assertNull(smems[4]);
    sm = smems[1];
    assertEquals(1, sm.getPos());
    assertEquals(2, sm.getSegmentPosMaskBit());
    sm = smems[2];
    assertEquals(2, sm.getPos());
    assertEquals(4, sm.getSegmentPosMaskBit());
    RuleTerminalNode rtn5 = getRtn("org.kie.r5", kbase1);
    PathMemory pm5 = (PathMemory) wm.getNodeMemory(rtn5);
    assertEquals(6, pm5.getLinkedSegmentMask());
    smems = pm5.getSegmentMemories();
    assertEquals(3, smems.length);
    assertNull(smems[0]);
    sm = smems[1];
    assertEquals(1, sm.getPos());
    assertEquals(2, sm.getSegmentPosMaskBit());
    sm = smems[2];
    assertEquals(2, sm.getPos());
    assertEquals(4, sm.getSegmentPosMaskBit());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

SegmentMemory (org.drools.core.reteoo.SegmentMemory)122 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)75 Test (org.junit.Test)60 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)50 PathMemory (org.drools.core.reteoo.PathMemory)49 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)44 BetaMemory (org.drools.core.reteoo.BetaMemory)42 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)42 JoinNode (org.drools.core.reteoo.JoinNode)37 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)36 ArrayList (java.util.ArrayList)31 List (java.util.List)29 KieBase (org.kie.api.KieBase)24 LeftTuple (org.drools.core.reteoo.LeftTuple)23 Memory (org.drools.core.common.Memory)22 Match (org.kie.api.runtime.rule.Match)22 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)18 Arrays.asList (java.util.Arrays.asList)17 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)17 QueryElementNodeMemory (org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory)14