Search in sources :

Example 11 with AccumulateContext

use of org.drools.core.reteoo.AccumulateNode.AccumulateContext in project drools by kiegroup.

the class AddRemoveRule method processLeftTuples.

/**
 * Populates the SegmentMemory with staged LeftTuples. If the parent is not a Beta or From node, it iterates up to find the first node with memory. If necessary
 * It traverses to the LiaNode's ObjectTypeNode. It then iterates the LeftTuple chains, where an existing LeftTuple is staged
 * as delete. Or a new LeftTuple is created and staged as an insert.
 */
private static void processLeftTuples(LeftTupleNode node, InternalWorkingMemory wm, boolean insert, Rule rule) {
    // *** if you make a fix here, it most likely needs to be in PhreakActivationIteratorToo ***
    // Must iterate up until a node with memory is found, this can be followed to find the LeftTuples
    // which provide the potential peer of the tuple being added or removed
    Memory memory = wm.getNodeMemories().peekNodeMemory(node);
    if (memory == null || memory.getSegmentMemory() == null) {
        // segment has never been initialized, which means the rule(s) have never been linked and thus no Tuples to fix
        return;
    }
    SegmentMemory sm = memory.getSegmentMemory();
    while (NodeTypeEnums.LeftInputAdapterNode != node.getType()) {
        if (NodeTypeEnums.isBetaNode(node)) {
            BetaMemory bm;
            if (NodeTypeEnums.AccumulateNode == node.getType()) {
                AccumulateMemory am = (AccumulateMemory) memory;
                bm = am.getBetaMemory();
                FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
                Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
                for (; lt != null; lt = (LeftTuple) it.next(lt)) {
                    AccumulateContext accctx = (AccumulateContext) lt.getContextObject();
                    visitChild(accctx.getResultLeftTuple(), insert, wm, rule);
                }
            } else if (NodeTypeEnums.ExistsNode == node.getType()) {
                bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
                // done off the RightTupleMemory, as exists only have unblocked tuples on the left side
                FastIterator it = bm.getRightTupleMemory().fullFastIterator();
                RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it);
                for (; rt != null; rt = (RightTuple) it.next(rt)) {
                    for (LeftTuple lt = rt.getBlocked(); lt != null; lt = lt.getBlockedNext()) {
                        visitChild(wm, insert, rule, it, lt);
                    }
                }
            } else {
                bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
                FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
                Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
                visitChild(wm, insert, rule, it, lt);
            }
            return;
        } else if (NodeTypeEnums.FromNode == node.getType()) {
            FromMemory fm = (FromMemory) wm.getNodeMemory((MemoryFactory) node);
            TupleMemory ltm = fm.getBetaMemory().getLeftTupleMemory();
            FastIterator it = ltm.fullFastIterator();
            for (LeftTuple lt = (LeftTuple) ltm.getFirst(null); lt != null; lt = (LeftTuple) it.next(lt)) {
                visitChild(lt, insert, wm, rule);
            }
            return;
        }
        if (sm.getRootNode() == node) {
            sm = wm.getNodeMemory((MemoryFactory<Memory>) node.getLeftTupleSource()).getSegmentMemory();
        }
        node = node.getLeftTupleSource();
    }
    // No beta or from nodes, so must retrieve LeftTuples from the LiaNode.
    // This is done by scanning all the LeftTuples referenced from the FactHandles in the ObjectTypeNode
    LeftInputAdapterNode lian = (LeftInputAdapterNode) node;
    ObjectSource os = lian.getObjectSource();
    while (os.getType() != NodeTypeEnums.ObjectTypeNode) {
        os = os.getParentObjectSource();
    }
    ObjectTypeNode otn = (ObjectTypeNode) os;
    final ObjectTypeNodeMemory omem = wm.getNodeMemory(otn);
    if (omem == null) {
        // no OTN memory yet, i.e. no inserted matching objects, so no Tuples to process
        return;
    }
    Iterator<InternalFactHandle> it = omem.iterator();
    while (it.hasNext()) {
        InternalFactHandle fh = it.next();
        fh.forEachLeftTuple(lt -> {
            LeftTuple nextLt = lt.getHandleNext();
            // Each lt is for a different lian, skip any lian not associated with the rule. Need to use lt parent (souce) not child to check the lian.
            if (lt.getTupleSource().isAssociatedWith(rule)) {
                visitChild(lt, insert, wm, rule);
                if (lt.getHandlePrevious() != null) {
                    lt.getHandlePrevious().setHandleNext(nextLt);
                }
                if (nextLt != null) {
                    nextLt.setHandlePrevious(lt.getHandlePrevious());
                }
            }
        });
    }
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) 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) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) MemoryFactory(org.drools.core.common.MemoryFactory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ObjectSource(org.drools.core.reteoo.ObjectSource) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle) Tuple(org.drools.core.spi.Tuple) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 12 with AccumulateContext

use of org.drools.core.reteoo.AccumulateNode.AccumulateContext in project drools by kiegroup.

the class PhreakAccumulateNode method removePreviousMatchesForRightTuple.

private static void removePreviousMatchesForRightTuple(final AccumulateNode accNode, final Accumulate accumulate, final RightTuple rightTuple, final InternalWorkingMemory workingMemory, final AccumulateMemory memory, final LeftTuple firstChild, final TupleSets<LeftTuple> trgLeftTuples) {
    for (LeftTuple match = firstChild; match != null; ) {
        final LeftTuple next = match.getRightParentNext();
        final LeftTuple leftTuple = match.getLeftParent();
        final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
        removeMatch(accNode, accumulate, rightTuple, match, workingMemory, memory, accctx, true);
        if (leftTuple.getStagedType() == LeftTuple.NONE) {
            trgLeftTuples.addUpdate(leftTuple);
        }
        match = next;
    }
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext)

Example 13 with AccumulateContext

use of org.drools.core.reteoo.AccumulateNode.AccumulateContext in project drools by kiegroup.

the class PhreakAccumulateNode method doRightDeletes.

public void doRightDeletes(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory rtm = am.getBetaMemory().getRightTupleMemory();
    Accumulate accumulate = accNode.getAccumulate();
    for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        if (rightTuple.getMemory() != null) {
            // it may have been staged and never actually added
            rtm.remove(rightTuple);
            if (rightTuple.getFirstChild() != null) {
                LeftTuple match = rightTuple.getFirstChild();
                while (match != null) {
                    LeftTuple nextLeft = match.getRightParentNext();
                    LeftTuple leftTuple = match.getLeftParent();
                    final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
                    removeMatch(accNode, accumulate, rightTuple, match, wm, am, accctx, true);
                    if (leftTuple.getStagedType() == LeftTuple.NONE) {
                        trgLeftTuples.addUpdate(leftTuple);
                    }
                    match = nextLeft;
                }
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
}
Also used : RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) Accumulate(org.drools.core.rule.Accumulate)

Aggregations

AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)13 LeftTuple (org.drools.core.reteoo.LeftTuple)12 BetaMemory (org.drools.core.reteoo.BetaMemory)7 RightTuple (org.drools.core.reteoo.RightTuple)7 TupleMemory (org.drools.core.reteoo.TupleMemory)7 FastIterator (org.drools.core.util.FastIterator)7 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 Accumulate (org.drools.core.rule.Accumulate)5 Tuple (org.drools.core.spi.Tuple)4 BetaConstraints (org.drools.core.common.BetaConstraints)3 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)3 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)3 ObjectSource (org.drools.core.reteoo.ObjectSource)3 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)3 ContextEntry (org.drools.core.rule.ContextEntry)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 AccumulateNode (org.drools.core.reteoo.AccumulateNode)2 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)2 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)2 ObjectTypeNodeMemory (org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)2