Search in sources :

Example 6 with Memory

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

the class AddRemoveRule method splitNodeMemories.

private static void splitNodeMemories(SegmentMemory sm1, SegmentMemory sm2, int pos) {
    LinkedList<Memory> smNodeMemories1 = sm1.getNodeMemories();
    LinkedList<Memory> smNodeMemories2 = sm2.getNodeMemories();
    Memory mem = smNodeMemories1.getFirst();
    int nodePosMask = 1;
    for (int i = 0, length = smNodeMemories1.size(); i < length; i++) {
        Memory next = mem.getNext();
        if (i > pos) {
            smNodeMemories1.remove(mem);
            smNodeMemories2.add(mem);
            mem.setSegmentMemory(sm2);
            // correct the NodePosMaskBit
            if (mem instanceof SegmentNodeMemory) {
                ((SegmentNodeMemory) mem).setNodePosMaskBit(nodePosMask);
            }
            nodePosMask = nodePosMask << 1;
        }
        mem = next;
    }
}
Also used : Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory)

Example 7 with Memory

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

the class AddRemoveRule method flushStagedTuples.

private static void flushStagedTuples(TerminalNode tn, PathMemory pmem, PathEndNodes pathEndNodes, InternalWorkingMemory wm) {
    // first flush the subject rule, then flush any staging lists that are part of a merge
    if (pmem.isInitialized()) {
        new RuleNetworkEvaluator().evaluateNetwork(pmem, pmem.getRuleAgendaItem().getRuleExecutor(), wm);
    }
    // With the removing rules being flushed, we need to check any splits that will be merged, to see if they need flushing
    // Beware that flushing a higher up node, might again cause lower nodes to have more staged items. So track flushed items
    // incase they need to be reflushed
    List<Flushed> flushed = new ArrayList<Flushed>();
    for (LeftTupleNode node : pathEndNodes.subjectSplits) {
        if (!isSplit(node, tn)) {
            // check if the split is there even without the processed rule
            Memory mem = wm.getNodeMemories().peekNodeMemory(node);
            if (mem != null) {
                SegmentMemory smem = mem.getSegmentMemory();
                if (!smem.isEmpty()) {
                    for (SegmentMemory childSmem = smem.getFirst(); childSmem != null; childSmem = childSmem.getNext()) {
                        if (!childSmem.getStagedLeftTuples().isEmpty()) {
                            PathMemory childPmem = childSmem.getPathMemories().get(0);
                            flushed.add(new Flushed(childSmem, childPmem));
                            forceFlushLeftTuple(childPmem, childSmem, wm, childSmem.getStagedLeftTuples().takeAll());
                        }
                    }
                }
            }
        }
    }
    // need to ensure that there is one full iteration, without any flushing. To avoid one flush causing populat of another already flushed segment
    int flushCount = 1;
    while (!flushed.isEmpty() && flushCount != 0) {
        flushCount = 0;
        for (Flushed path : flushed) {
            if (!path.segmentMemory.getStagedLeftTuples().isEmpty()) {
                flushCount++;
                forceFlushLeftTuple(pmem, path.segmentMemory, wm, path.segmentMemory.getStagedLeftTuples().takeAll());
            }
        }
    }
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ArrayList(java.util.ArrayList) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) PathMemory(org.drools.core.reteoo.PathMemory)

Example 8 with Memory

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

the class AddRemoveRule method removeNewPaths.

private static void removeNewPaths(InternalWorkingMemory wm, List<PathMemory> pmems) {
    Set<Integer> visitedNodes = new HashSet<Integer>();
    for (PathMemory pmem : pmems) {
        LeftTupleSink tipNode = (LeftTupleSink) pmem.getPathEndNode();
        LeftTupleNode child = tipNode;
        LeftTupleNode parent = tipNode.getLeftTupleSource();
        while (true) {
            if (child.getAssociationsSize() == 1 && NodeTypeEnums.isBetaNode(child)) {
                // If this is a beta node, it'll delete all the right input data
                deleteRightInputData((LeftTupleSink) child, wm);
            }
            if (parent != null && parent.getAssociationsSize() != 1 && child.getAssociationsSize() == 1) {
                // all right input data must be propagated
                if (!visitedNodes.contains(child.getId())) {
                    Memory mem = wm.getNodeMemories().peekNodeMemory(parent);
                    if (mem != null && mem.getSegmentMemory() != null) {
                        SegmentMemory sm = mem.getSegmentMemory();
                        if (sm.getFirst() != null) {
                            SegmentMemory childSm = wm.getNodeMemories().peekNodeMemory(child).getSegmentMemory();
                            sm.remove(childSm);
                        }
                    }
                }
            } else {
                Memory mem = wm.getNodeMemories().peekNodeMemory(child);
                // The root of each segment
                if (mem != null) {
                    SegmentMemory sm = mem.getSegmentMemory();
                    if (sm != null && sm.getPathMemories().contains(pmem)) {
                        mem.getSegmentMemory().removePathMemory(pmem);
                    }
                }
            }
            if (parent == null) {
                break;
            }
            visitedNodes.add(child.getId());
            child = parent;
            parent = parent.getLeftTupleSource();
        }
    }
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) HashSet(java.util.HashSet) PathMemory(org.drools.core.reteoo.PathMemory)

Example 9 with Memory

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

the class AddRemoveRule method insertPeerLeftTuple.

/**
 * Create all missing peers
 */
private static LeftTuple insertPeerLeftTuple(LeftTuple lt, LeftTupleSinkNode node, InternalWorkingMemory wm) {
    LeftInputAdapterNode.LiaNodeMemory liaMem = null;
    if (node.getLeftTupleSource().getType() == NodeTypeEnums.LeftInputAdapterNode) {
        liaMem = wm.getNodeMemory(((LeftInputAdapterNode) node.getLeftTupleSource()));
    }
    LeftTuple peer = node.createPeer(lt);
    Memory memory = wm.getNodeMemories().peekNodeMemory(node);
    if (memory == null || memory.getSegmentMemory() == null) {
        throw new IllegalStateException("Defensive Programming: this should not be possilbe, as the addRule code should init child segments if they are needed ");
    }
    if (liaMem == null) {
        memory.getSegmentMemory().getStagedLeftTuples().addInsert(peer);
    } else {
        // If parent is Lian, then this must be called, so that any linking or unlinking can be done.
        LeftInputAdapterNode.doInsertSegmentMemory(wm, true, liaMem, memory.getSegmentMemory(), peer, node.getLeftTupleSource().isStreamMode());
    }
    return peer;
}
Also used : Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 10 with Memory

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

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