Search in sources :

Example 11 with Memory

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

the class RightInputAdapterNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    RightInputAdapterNode an = (RightInputAdapterNode) node;
    DefaultNodeInfo ni = info.getNodeInfo(node);
    BetaNode betaNode = (BetaNode) an.getObjectSinkPropagator().getSinks()[0];
    Memory childMemory = info.getSession().getNodeMemory(betaNode);
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
        bm = ((AccumulateMemory) childMemory).getBetaMemory();
    } else {
        bm = (BetaMemory) childMemory;
    }
    ni.setMemoryEnabled(true);
    ni.setTupleMemorySize(bm.getRightTupleMemory().size());
    ni.setCreatedFactHandles(bm.getRightTupleMemory().size());
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) Memory(org.drools.core.common.Memory) BetaMemory(org.drools.core.reteoo.BetaMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 12 with Memory

use of org.drools.core.common.Memory 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 13 with Memory

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

the class PassiveQueryTest method testPassiveQueryUsingSegmentPropagator.

@Test
public void testPassiveQueryUsingSegmentPropagator() throws Exception {
    String str = "global java.util.List list\n" + "query Q (Integer i)\n" + "    String( this == i.toString() )\n" + "end\n" + "rule R1a @Propagation(IMMEDIATE) when\n" + "    $i : Integer()\n" + "    ?Q( $i; )\n" + "then\n" + "    list.add( \"R1a\" );\n" + "end\n" + "rule R1b @Propagation(IMMEDIATE) when\n" + "    $i : Integer()\n" + "    ?Q( $i; )\n" + "    Long( intValue == $i )\n" + "then\n" + "    list.add( \"R1b\" );\n" + "end\n" + "rule R2 when\n" + "    $i : Integer()\n" + "    ?Q( $i; )\n" + "then\n" + "    list.add( \"R2\" );\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(1L);
    FactHandle fh = ksession.insert(1);
    ksession.insert("1");
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode lia = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        if (Integer.class == otn.getObjectType().getValueType().getClassType()) {
            lia = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
            break;
        }
    }
    LeftTupleSink[] sinks = lia.getSinkPropagator().getSinks();
    QueryElementNode q1 = (QueryElementNode) sinks[0];
    QueryElementNode q2 = (QueryElementNode) sinks[1];
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    wm.flushPropagations();
    Memory memory1 = wm.getNodeMemory(q1);
    assertTrue(memory1.getSegmentMemory().getStagedLeftTuples().isEmpty());
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("R2", list.get(0));
    list.clear();
    ksession.delete(fh);
    ksession.insert(1);
    ksession.fireAllRules();
    assertEquals(3, list.size());
}
Also used : Rete(org.drools.core.reteoo.Rete) FactHandle(org.kie.api.runtime.rule.FactHandle) Memory(org.drools.core.common.Memory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) QueryElementNode(org.drools.core.reteoo.QueryElementNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 14 with Memory

use of org.drools.core.common.Memory 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 15 with Memory

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

the class ProtobufOutputMarshaller method writeRIANodeMemory.

private static ProtobufMessages.NodeMemory writeRIANodeMemory(final int nodeId, final BaseNode node, final NodeMemories memories) {
    RightInputAdapterNode riaNode = (RightInputAdapterNode) node;
    ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
    BetaNode betaNode = (BetaNode) sinks[0];
    Memory betaMemory = memories.peekNodeMemory(betaNode);
    if (betaMemory == null) {
        return null;
    }
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
        bm = ((AccumulateMemory) betaMemory).getBetaMemory();
    } else {
        bm = (BetaMemory) betaMemory;
    }
    // for RIA nodes, we need to store the ID of the created handles
    bm.getRightTupleMemory().iterator();
    if (bm.getRightTupleMemory().size() > 0) {
        ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria = ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder();
        final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();
        // iterates over all propagated handles and assert them to the new sink
        for (RightTuple entry = (RightTuple) it.next(); entry != null; entry = (RightTuple) it.next()) {
            LeftTuple leftTuple = entry instanceof LeftTuple ? // with phreak the entry is always both a right and a left tuple
            (LeftTuple) entry : // this is necessary only for reteoo
            (LeftTuple) entry.getFactHandle().getObject();
            InternalFactHandle handle = (InternalFactHandle) leftTuple.getFactHandle();
            if (handle == null) {
                continue;
            }
            FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
            _ria.addContext(ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setResultHandle(_handle).build());
        }
        return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.RIA).setRia(_ria.build()).build();
    }
    return null;
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) 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) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) ObjectSink(org.drools.core.reteoo.ObjectSink) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Aggregations

Memory (org.drools.core.common.Memory)22 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)21 BetaMemory (org.drools.core.reteoo.BetaMemory)19 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)18 SegmentMemory (org.drools.core.reteoo.SegmentMemory)17 PathMemory (org.drools.core.reteoo.PathMemory)15 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)14 RiaNodeMemory (org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory)12 TupleMemory (org.drools.core.reteoo.TupleMemory)12 ObjectTypeNodeMemory (org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)11 SegmentNodeMemory (org.drools.core.reteoo.SegmentNodeMemory)9 LeftTuple (org.drools.core.reteoo.LeftTuple)8 QueryElementNodeMemory (org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory)8 ConditionalBranchMemory (org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory)6 EvalMemory (org.drools.core.reteoo.EvalConditionNode.EvalMemory)6 TimerNodeMemory (org.drools.core.reteoo.TimerNode.TimerNodeMemory)6 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)5 LeftTupleNode (org.drools.core.reteoo.LeftTupleNode)5 NodeMemories (org.drools.core.common.NodeMemories)4 BetaNode (org.drools.core.reteoo.BetaNode)4