Search in sources :

Example 76 with SegmentMemory

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

the class SegmentCreationTest method testBranchCEMultipleSegments.

@Test
public void testBranchCEMultipleSegments() throws Exception {
    KieBase kbase = buildKnowledgeBase(// r1
    "   $a : A() \n", "   $a : A() \n" + "   if ( $a != null ) do[t1] \n" + // r2
    "   B() \n", "   $a : A() \n" + "   if ( $a != null ) do[t1] \n" + "   B() \n" + // r3
    "   C() \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    ConditionalBranchNode cen1Node = (ConditionalBranchNode) liaNode.getSinkPropagator().getSinks()[1];
    JoinNode bNode = (JoinNode) cen1Node.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn3 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    FactHandle bFh = wm.insert(new LinkingTest.B());
    FactHandle cFh = wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    BetaMemory bNodeBm = (BetaMemory) wm.getNodeMemory(bNode);
    SegmentMemory bNodeSmem = bNodeBm.getSegmentMemory();
    // no beta nodes before branch CE, so never unlinks
    assertEquals(0, bNodeSmem.getAllLinkedMaskTest());
    assertEquals(2, bNodeSmem.getLinkedNodeMask());
    PathMemory pmemr2 = (PathMemory) wm.getNodeMemory(rtn2);
    assertEquals(1, pmemr2.getAllLinkedMaskTest());
    assertEquals(2, pmemr2.getLinkedSegmentMask());
    assertEquals(3, pmemr2.getSegmentMemories().length);
    assertFalse(pmemr2.isRuleLinked());
    PathMemory pmemr3 = (PathMemory) wm.getNodeMemory(rtn3);
    // notice only the first segment links
    assertEquals(1, pmemr3.getAllLinkedMaskTest());
    assertEquals(3, pmemr3.getSegmentMemories().length);
    assertFalse(pmemr3.isRuleLinked());
    BetaMemory cNodeBm = (BetaMemory) wm.getNodeMemory(cNode);
    SegmentMemory cNodeSmem = cNodeBm.getSegmentMemory();
    assertEquals(1, cNodeSmem.getAllLinkedMaskTest());
    assertEquals(1, cNodeSmem.getLinkedNodeMask());
    wm.insert(new LinkingTest.A());
    wm.flushPropagations();
    assertTrue(pmemr2.isRuleLinked());
    assertTrue(pmemr3.isRuleLinked());
    // retract B does not unlink the rule
    wm.delete(bFh);
    // retract C does not unlink the rule
    wm.delete(cFh);
    wm.flushPropagations();
    // b segment never unlinks, as it has no impact on path unlinking anyway
    assertEquals(3, pmemr2.getLinkedSegmentMask());
    assertTrue(pmemr2.isRuleLinked());
    // b segment never unlinks, as it has no impact on path unlinking anyway
    assertEquals(3, pmemr3.getLinkedSegmentMask());
    assertTrue(pmemr3.isRuleLinked());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ConditionalBranchNode(org.drools.core.reteoo.ConditionalBranchNode) BetaMemory(org.drools.core.reteoo.BetaMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 77 with SegmentMemory

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

the class SegmentCreationTest method testSingleSharedEmptyLhs.

@Test
public void testSingleSharedEmptyLhs() throws Exception {
    KieBase kbase = buildKnowledgeBase(" ", " ");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, InitialFactImpl.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[1];
    wm.insert(new LinkingTest.A());
    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());
    // each RTN is in it's own segment
    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());
    SegmentMemory rtnSmem2 = rtnSmem1.getNext();
    assertEquals(rtn2, rtnSmem2.getRootNode());
    assertEquals(rtn2, rtnSmem2.getTipNode());
}
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 78 with SegmentMemory

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

the class SegmentCreationTest method testSingleSharedPattern.

@Test
public void testSingleSharedPattern() throws Exception {
    KieBase kbase = buildKnowledgeBase("   A() \n", "   A() \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];
    RuleTerminalNode rtn2 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[1];
    wm.insert(new LinkingTest.A());
    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());
    // each RTN is in it's own segment
    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());
    SegmentMemory rtnSmem2 = rtnSmem1.getNext();
    assertEquals(rtn2, rtnSmem2.getRootNode());
    assertEquals(rtn2, rtnSmem2.getTipNode());
}
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 79 with SegmentMemory

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

the class SegmentCreationTest method testSinglePattern.

@Test
public void testSinglePattern() throws Exception {
    KieBase kbase = buildKnowledgeBase("   A() \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.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 80 with SegmentMemory

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

the class MemoryLeakTest method testBetaMemoryLeakOnFactDelete.

@Test
public void testBetaMemoryLeakOnFactDelete() {
    // DROOLS-913
    String drl = "rule R1 when\n" + "    $a : Integer(this == 1)\n" + "    $b : String()\n" + "    $c : Integer(this == 2)\n" + "then \n" + "end\n" + "rule R2 when\n" + "    $a : Integer(this == 1)\n" + "    $b : String()\n" + "    $c : Integer(this == 3)\n" + "then \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    FactHandle fh1 = ksession.insert(1);
    FactHandle fh2 = ksession.insert(3);
    FactHandle fh3 = ksession.insert("test");
    ksession.fireAllRules();
    ksession.delete(fh1);
    ksession.delete(fh2);
    ksession.delete(fh3);
    ksession.fireAllRules();
    NodeMemories nodeMemories = ((InternalWorkingMemory) ksession).getNodeMemories();
    for (int i = 0; i < nodeMemories.length(); i++) {
        Memory memory = nodeMemories.peekNodeMemory(i);
        if (memory != null && memory.getSegmentMemory() != null) {
            SegmentMemory segmentMemory = memory.getSegmentMemory();
            System.out.println(memory);
            LeftTuple deleteFirst = memory.getSegmentMemory().getStagedLeftTuples().getDeleteFirst();
            if (segmentMemory.getRootNode() instanceof JoinNode) {
                BetaMemory bm = (BetaMemory) segmentMemory.getNodeMemories().getFirst();
                assertEquals(0, bm.getLeftTupleMemory().size());
            }
            System.out.println(deleteFirst);
            assertNull(deleteFirst);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FactHandle(org.kie.api.runtime.rule.FactHandle) Memory(org.drools.core.common.Memory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) JoinNode(org.drools.core.reteoo.JoinNode) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) NodeMemories(org.drools.core.common.NodeMemories) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) 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