Search in sources :

Example 1 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.wm;
    NodeMemories memories = wm.getNodeMemories();
    // so we iterate over all of them and process only those that require it
    for (BaseNode baseNode : context.sinks.values()) {
        Memory memory = memories.peekNodeMemory(baseNode);
        if (memory != null) {
            ProtobufMessages.NodeMemory _node = null;
            switch(memory.getNodeType()) {
                case NodeTypeEnums.AccumulateNode:
                    {
                        _node = writeAccumulateNodeMemory(baseNode.getId(), memory);
                        break;
                    }
                case NodeTypeEnums.RightInputAdaterNode:
                    {
                        _node = writeRIANodeMemory(baseNode.getId(), baseNode, memories);
                        break;
                    }
                case NodeTypeEnums.FromNode:
                case NodeTypeEnums.ReactiveFromNode:
                    {
                        _node = writeFromNodeMemory(baseNode.getId(), memory);
                        break;
                    }
                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) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) BaseNode(org.drools.core.common.BaseNode) NodeMemories(org.drools.core.common.NodeMemories)

Example 2 with NodeMemories

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

the class Misc2Test method testBetaMemoryLeakOnSegmentUnlinking.

@Test
@Ignore
public void testBetaMemoryLeakOnSegmentUnlinking() {
    // DROOLS-915
    String drl = "rule R1 when\n" + "    $a : Integer(this == 1)\n" + "    $b : String()\n" + "    $c : Integer(this == 2)\n" + "    $d : Integer(this == 3)\n" + "then \n" + "end\n" + "rule R2 when\n" + "    $a : Integer(this == 1)\n" + "    $b : String()\n" + "then \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    FactHandle fh1 = ksession.insert(1);
    FactHandle fh2 = ksession.insert(2);
    FactHandle fh3 = ksession.insert(3);
    FactHandle fhtest = ksession.insert("test");
    ksession.fireAllRules();
    ksession.delete(fh3);
    ksession.fireAllRules();
    ksession.delete(fh1);
    ksession.delete(fh2);
    ksession.delete(fhtest);
    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();
            System.out.println(deleteFirst);
            assertNull(deleteFirst);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Memory(org.drools.core.common.Memory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) NodeMemories(org.drools.core.common.NodeMemories) LeftTuple(org.drools.core.reteoo.LeftTuple) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 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 X(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    kbase.addPackages(buildKnowledgePackage("r1", "   A() B() C(object == 2) X() 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.kiesession.rulebase.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 4 with NodeMemories

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

the class BetaMemoryLeakOnDeleteTest method testBetaMemoryLeakOnFactDelete.

@Test
public void testBetaMemoryLeakOnFactDelete() {
    final 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" + "then \n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final FactHandle fh1 = ksession.insert(1);
    final FactHandle fh2 = ksession.insert("test");
    ksession.fireAllRules();
    ksession.delete(fh1);
    ksession.delete(fh2);
    ksession.fireAllRules();
    final NodeMemories nodeMemories = ((InternalWorkingMemory) ksession).getNodeMemories();
    for (int i = 0; i < nodeMemories.length(); i++) {
        final Memory memory = nodeMemories.peekNodeMemory(i);
        if (memory != null && memory.getSegmentMemory() != null) {
            final LeftTuple deleteFirst = memory.getSegmentMemory().getStagedLeftTuples().getDeleteFirst();
            assertThat(deleteFirst).isNull();
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) FactHandle(org.kie.api.runtime.rule.FactHandle) Memory(org.drools.core.common.Memory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) NodeMemories(org.drools.core.common.NodeMemories) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 5 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)

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