Search in sources :

Example 66 with RuleTerminalNode

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

the class AddRuleTest method testSplitOneBeforeCreatedSegment.

@Test
public void testSplitOneBeforeCreatedSegment() 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 D(1));
    wm.insert(new D(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[2]);
    assertNull(smems[3]);
    SegmentMemory sm = smems[1];
    assertEquals(1, sm.getPos());
    assertEquals(2, sm.getSegmentPosMaskBit());
    assertEquals(2, pm1.getLinkedSegmentMask());
    PathMemory pm3 = (PathMemory) wm.getNodeMemory(rtn3);
    SegmentMemory[] smemsP3 = pm3.getSegmentMemories();
    assertEquals(3, smemsP3.length);
    assertNull(smemsP3[0]);
    assertNull(smemsP3[2]);
    sm = smems[1];
    assertEquals(1, sm.getPos());
    assertEquals(2, sm.getSegmentPosMaskBit());
    assertEquals(2, 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[3]);
    assertNull(smems[4]);
    sm = smems[2];
    assertEquals(2, sm.getPos());
    assertEquals(4, sm.getSegmentPosMaskBit());
    assertEquals(4, pm1.getLinkedSegmentMask());
    smems = pm3.getSegmentMemories();
    assertEquals(4, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
    assertNull(smems[3]);
    sm = smems[2];
    assertEquals(2, sm.getPos());
    assertEquals(4, sm.getSegmentPosMaskBit());
    assertEquals(4, 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) Arrays.asList(java.util.Arrays.asList) 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 67 with RuleTerminalNode

use of org.drools.core.reteoo.RuleTerminalNode 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;) 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 D(1));
    wm.insert(new D(2));
    wm.insert(new D(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;) D(1;) D(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.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 68 with RuleTerminalNode

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

the class AddRuleTest 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());
    kbase1.addPackages(buildKnowledgePackage("r2", "   A() B() C() D() E()\n"));
    ObjectTypeNode eotn = getObjectTypeNode(kbase1, E.class);
    JoinNode eNode = (JoinNode) eotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) eNode.getSinkPropagator().getLastLeftTupleSink();
    PathMemory pm = (PathMemory) wm.getNodeMemory(rtn);
    SegmentMemory sm = pm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(4, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r2", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) 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) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 69 with RuleTerminalNode

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

the class RemoveRuleTest method testPathMemorySizeAfterSegmentMerge.

@Test
public void testPathMemorySizeAfterSegmentMerge() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;) B(1;)\n");
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)\n"));
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    // trigger segment initialization
    wm.insert(new A(1));
    wm.insert(new B(1));
    wm.fireAllRules();
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    assertEquals(2, wm.getNodeMemory(rtn1).getSegmentMemories().length);
    assertEquals(2, wm.getNodeMemory(rtn2).getSegmentMemories().length);
    kbase1.removeRule("org.kie", "r2");
    assertEquals(1, wm.getNodeMemory(rtn1).getSegmentMemories().length);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)69 Test (org.junit.Test)54 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)38 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)30 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)28 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 KieBase (org.kie.api.KieBase)23 SegmentMemory (org.drools.core.reteoo.SegmentMemory)18 JoinNode (org.drools.core.reteoo.JoinNode)16 PathMemory (org.drools.core.reteoo.PathMemory)16 ArrayList (java.util.ArrayList)14 ClassObjectType (org.drools.core.base.ClassObjectType)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 List (java.util.List)12 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)11 WorkingMemory (org.drools.core.WorkingMemory)10 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)9 FactHandle (org.kie.api.runtime.rule.FactHandle)9 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)9 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)8