Search in sources :

Example 6 with NodeMemories

use of org.drools.core.common.NodeMemories 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";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.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) KieBase(org.kie.api.KieBase) 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) 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)

Example 7 with NodeMemories

use of org.drools.core.common.NodeMemories in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeNodeMemories.

private static void writeNodeMemories(MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _ksb) throws IOException {
    InternalWorkingMemory wm = context.getWorkingMemory();
    NodeMemories memories = wm.getNodeMemories();
    // so we iterate over all of them and process only those that require it
    for (BaseNode baseNode : context.getSinks().values()) {
        Memory memory = memories.peekNodeMemory(baseNode);
        if (memory != null) {
            ProtobufMessages.NodeMemory _node = null;
            switch(memory.getNodeType()) {
                case NodeTypeEnums.QueryElementNode:
                    {
                        _node = writeQueryElementNodeMemory(baseNode.getId(), memory, wm);
                        break;
                    }
            }
            if (_node != null) {
                // not all node memories require serialization
                _ksb.addNodeMemory(_node);
            }
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) BaseNode(org.drools.core.common.BaseNode) NodeMemories(org.drools.core.common.NodeMemories)

Example 8 with NodeMemories

use of org.drools.core.common.NodeMemories in project drools by kiegroup.

the class NotTest method getBlockerFactHandle.

private InternalFactHandle getBlockerFactHandle(KieSession ksession) {
    ObjectTypeNode otn = getObjectTypeNode(ksession.getKieBase(), Person.class);
    BetaNode notNode = (BetaNode) ((AlphaNode) otn.getSinks()[0]).getSinks()[0];
    StatefulKnowledgeSessionImpl ksessionImpl = (StatefulKnowledgeSessionImpl) ksession;
    NodeMemories nodeMemories = ksessionImpl.getNodeMemories();
    BetaMemory betaMemory = (BetaMemory) nodeMemories.getNodeMemory(notNode, ksessionImpl);
    TupleMemory rightTupleMemory = betaMemory.getRightTupleMemory();
    Tuple[] tuples = (Tuple[]) rightTupleMemory.toArray();
    for (int i = 0; i < tuples.length; i++) {
        RightTupleImpl tuple = (RightTupleImpl) tuples[i];
        if (tuple.getBlocked() != null) {
            return tuple.getFactHandle();
        }
    }
    fail("Cannot find blocker in BetaMemory");
    return null;
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) NodeMemories(org.drools.core.common.NodeMemories) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) AlphaNode(org.drools.core.reteoo.AlphaNode) TupleMemory(org.drools.core.reteoo.TupleMemory) Tuple(org.drools.core.spi.Tuple)

Example 9 with NodeMemories

use of org.drools.core.common.NodeMemories 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)

Example 10 with NodeMemories

use of org.drools.core.common.NodeMemories in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedSingleRuleNoSharing.

@Test
public void testPopulatedSingleRuleNoSharing() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    wm.insert(new A(1));
    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();
    kbase.addPackages(buildKnowledgePackage("r1", "   A() B() C(object == 2) D() E()\n"));
    List list = new ArrayList();
    wm.setGlobal("list", list);
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    wm.fireAllRules();
    BetaMemory bMem = (BetaMemory) sm.getNodeMemories().get(1);
    assertEquals(1, bMem.getLeftTupleMemory().size());
    assertEquals(1, bMem.getRightTupleMemory().size());
    BetaMemory eMem = (BetaMemory) sm.getNodeMemories().get(4);
    assertEquals(1, eMem.getLeftTupleMemory().size());
    assertEquals(1, eMem.getRightTupleMemory().size());
    NodeMemories nms = wm.getNodeMemories();
    assertEquals(12, countNodeMemories(nms));
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(1, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    kbase.removeRule("org.kie", "r1");
    // still has OTN
    assertEquals(6, countNodeMemories(nms));
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) 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) NodeMemories(org.drools.core.common.NodeMemories) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

NodeMemories (org.drools.core.common.NodeMemories)10 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)9 Memory (org.drools.core.common.Memory)7 Test (org.junit.Test)7 BetaMemory (org.drools.core.reteoo.BetaMemory)6 SegmentMemory (org.drools.core.reteoo.SegmentMemory)6 LeftTuple (org.drools.core.reteoo.LeftTuple)5 KieSession (org.kie.api.runtime.KieSession)5 FactHandle (org.kie.api.runtime.rule.FactHandle)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)3 KieHelper (org.kie.internal.utils.KieHelper)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BaseNode (org.drools.core.common.BaseNode)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 JoinNode (org.drools.core.reteoo.JoinNode)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)2 ObjectTypeNodeMemory (org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)2