Search in sources :

Example 6 with FastIterator

use of org.drools.core.util.FastIterator in project drools by kiegroup.

the class PhreakAccumulateNode method doLeftInserts.

public void doLeftInserts(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    Accumulate accumulate = accNode.getAccumulate();
    BetaMemory bm = am.getBetaMemory();
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = accNode.getRawConstraints();
    boolean leftTupleMemoryEnabled = accNode.isLeftTupleMemoryEnabled();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        boolean useLeftMemory = leftTupleMemoryEnabled || RuleNetworkEvaluator.useLeftMemory(accNode, leftTuple);
        if (useLeftMemory) {
            ltm.add(leftTuple);
        }
        AccumulateContext accresult = new AccumulateContext();
        leftTuple.setContextObject(accresult);
        accresult.context = accumulate.createContext();
        accumulate.init(am.workingMemoryContext, accresult.context, leftTuple, wm);
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        FastIterator rightIt = accNode.getRightIterator(rtm);
        for (RightTuple rightTuple = accNode.getFirstRightTuple(leftTuple, rtm, null, rightIt); rightTuple != null; ) {
            RightTuple nextRightTuple = (RightTuple) rightIt.next(rightTuple);
            if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandleForEvaluation())) {
                // add a match
                addMatch(accNode, accumulate, leftTuple, rightTuple, null, null, wm, am, accresult, useLeftMemory);
                if (!useLeftMemory && accNode.isRightInputIsRiaNode()) {
                    // RIAN with no left memory must have their right tuples removed
                    rtm.remove(rightTuple);
                }
            }
            rightTuple = nextRightTuple;
        }
        leftTuple.clearStaged();
        trgLeftTuples.addInsert(leftTuple);
        constraints.resetTuple(contextEntry);
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) BetaMemory(org.drools.core.reteoo.BetaMemory) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) Accumulate(org.drools.core.rule.Accumulate)

Example 7 with FastIterator

use of org.drools.core.util.FastIterator in project drools by kiegroup.

the class PhreakExistsNode method doLeftUpdates.

public void doLeftUpdates(ExistsNode existsNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = existsNode.getRawConstraints();
    boolean leftUpdateOptimizationAllowed = existsNode.isLeftUpdateOptimizationAllowed();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        FastIterator rightIt = existsNode.getRightIterator(rtm);
        RightTuple firstRightTuple = existsNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
        // If in memory, remove it, because we'll need to add it anyway if it's not blocked, to ensure iteration order
        RightTuple blocker = leftTuple.getBlocker();
        if (blocker == null) {
            if (leftTuple.getMemory() != null) {
                // memory can be null, if blocker was deleted in same do loop
                ltm.remove(leftTuple);
            }
        } else {
            // check if we changed bucket
            if (rtm.isIndexed() && !rightIt.isFullIterator()) {
                // if newRightTuple is null, we assume there was a bucket change and that bucket is empty
                if (firstRightTuple == null || firstRightTuple.getMemory() != blocker.getMemory()) {
                    // we changed bucket, so blocker no longer blocks
                    blocker.removeBlocked(leftTuple);
                    blocker = null;
                }
            }
        }
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        if (!leftUpdateOptimizationAllowed && blocker != null) {
            blocker.removeBlocked(leftTuple);
            blocker = null;
        }
        // if we where not blocked before (or changed buckets), or the previous blocker no longer blocks, then find the next blocker
        if (blocker == null || !constraints.isAllowedCachedLeft(contextEntry, blocker.getFactHandleForEvaluation())) {
            if (blocker != null) {
                // remove previous blocker if it exists, as we know it doesn't block any more
                blocker.removeBlocked(leftTuple);
            }
            // find first blocker, because it's a modify, we need to start from the beginning again
            for (RightTuple newBlocker = firstRightTuple; newBlocker != null; newBlocker = (RightTuple) rightIt.next(newBlocker)) {
                if (constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandleForEvaluation())) {
                    leftTuple.setBlocker(newBlocker);
                    newBlocker.addBlocked(leftTuple);
                    break;
                }
            }
        }
        if (leftTuple.getBlocker() == null) {
            // not blocked
            // add to memory so other fact handles can attempt to match
            ltm.add(leftTuple);
            if (leftTuple.getFirstChild() != null) {
                // no need to update pctx, as no right available, and pctx will exist on a parent LeftTuple anyway
                RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(leftTuple.getFirstChild(), trgLeftTuples, stagedLeftTuples);
            }
        // with no previous children. do nothing.
        } else if (leftTuple.getFirstChild() == null) {
            // blocked, with no previous children, insert
            insertChildLeftTuple(sink, trgLeftTuples, leftTuple, leftTuple.getBlocker().getPropagationContext(), true);
        } else {
            // blocked, with previous children, modify
            LeftTuple childLeftTuple = leftTuple.getFirstChild();
            while (childLeftTuple != null) {
                childLeftTuple.setPropagationContext(leftTuple.getBlocker().getPropagationContext());
                updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);
                childLeftTuple.reAddRight();
                childLeftTuple = childLeftTuple.getHandleNext();
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 8 with FastIterator

use of org.drools.core.util.FastIterator in project drools by kiegroup.

the class PhreakExistsNode method doRightInserts.

public void doRightInserts(ExistsNode existsNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = existsNode.getRawConstraints();
    for (RightTuple rightTuple = srcRightTuples.getInsertFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        rtm.add(rightTuple);
        if (ltm != null && ltm.size() > 0) {
            FastIterator it = existsNode.getLeftIterator(ltm);
            constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
            for (LeftTuple leftTuple = existsNode.getFirstLeftTuple(rightTuple, ltm, it); leftTuple != null; ) {
                // preserve next now, in case we remove this leftTuple
                LeftTuple temp = (LeftTuple) it.next(leftTuple);
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    leftTuple = temp;
                    continue;
                }
                // we know that only unblocked LeftTuples are  still in the memory
                if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                    leftTuple.setBlocker(rightTuple);
                    rightTuple.addBlocked(leftTuple);
                    ltm.remove(leftTuple);
                    insertChildLeftTuple(sink, trgLeftTuples, leftTuple, rightTuple.getPropagationContext(), true);
                }
                leftTuple = temp;
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 9 with FastIterator

use of org.drools.core.util.FastIterator in project drools by kiegroup.

the class PhreakExistsNode method doRightDeletes.

public void doRightDeletes(ExistsNode existsNode, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory rtm = bm.getRightTupleMemory();
    TupleMemory ltm = bm.getLeftTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = existsNode.getRawConstraints();
    for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        FastIterator it = existsNode.getRightIterator(rtm);
        boolean useComparisonIndex = rtm.getIndexType().isComparison();
        RightTuple rootBlocker = useComparisonIndex ? null : (RightTuple) it.next(rightTuple);
        if (rightTuple.getMemory() != null) {
            // it may have been staged and never actually added
            rtm.remove(rightTuple);
        }
        if (rightTuple.getBlocked() != null) {
            for (LeftTuple leftTuple = rightTuple.getBlocked(); leftTuple != null; ) {
                LeftTuple temp = leftTuple.getBlockedNext();
                leftTuple.clearBlocker();
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    leftTuple = temp;
                    continue;
                }
                constraints.updateFromTuple(contextEntry, wm, leftTuple);
                if (useComparisonIndex) {
                    rootBlocker = (RightTuple) rtm.getFirst(leftTuple);
                }
                // we know that older tuples have been checked so continue previously
                for (RightTuple newBlocker = rootBlocker; newBlocker != null; newBlocker = (RightTuple) it.next(newBlocker)) {
                    if (constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandleForEvaluation())) {
                        leftTuple.setBlocker(newBlocker);
                        newBlocker.addBlocked(leftTuple);
                        break;
                    }
                }
                if (leftTuple.getBlocker() == null) {
                    // was previous blocked and not in memory, so add
                    ltm.add(leftTuple);
                    LeftTuple childLeftTuple = leftTuple.getFirstChild();
                    if (childLeftTuple != null) {
                        childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                        RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                    }
                }
                leftTuple = temp;
            }
        }
        rightTuple.setBlocked(null);
        rightTuple.clearStaged();
        rightTuple = next;
    }
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 10 with FastIterator

use of org.drools.core.util.FastIterator in project drools by kiegroup.

the class PhreakExistsNode method doRightUpdates.

public void doRightUpdates(ExistsNode existsNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = existsNode.getRawConstraints();
    boolean iterateFromStart = existsNode.isIndexedUnificationJoin() || rtm.getIndexType().isComparison();
    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        if (ltm != null && ltm.size() > 0) {
            FastIterator leftIt = existsNode.getLeftIterator(ltm);
            LeftTuple firstLeftTuple = existsNode.getFirstLeftTuple(rightTuple, ltm, leftIt);
            constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
            // first process non-blocked tuples, as we know only those ones are in the left memory.
            for (LeftTuple leftTuple = firstLeftTuple; leftTuple != null; ) {
                // preserve next now, in case we remove this leftTuple
                LeftTuple temp = (LeftTuple) leftIt.next(leftTuple);
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    leftTuple = temp;
                    continue;
                }
                // we know that only unblocked LeftTuples are  still in the memory
                if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                    leftTuple.setBlocker(rightTuple);
                    rightTuple.addBlocked(leftTuple);
                    // this is now blocked so remove from memory
                    ltm.remove(leftTuple);
                    // subclasses like ForallNotNode might override this propagation
                    insertChildLeftTuple(sink, trgLeftTuples, leftTuple, rightTuple.getPropagationContext(), true);
                }
                leftTuple = temp;
            }
        }
        LeftTuple firstBlocked = rightTuple.getTempBlocked();
        if (firstBlocked != null) {
            RightTuple rootBlocker = rightTuple.getTempNextRightTuple();
            if (rootBlocker == null) {
                iterateFromStart = true;
            }
            FastIterator rightIt = existsNode.getRightIterator(rtm);
            // iterate all the existing previous blocked LeftTuples
            for (LeftTuple leftTuple = firstBlocked; leftTuple != null; ) {
                LeftTuple temp = leftTuple.getBlockedNext();
                // must null these as we are re-adding them to the list
                leftTuple.clearBlocker();
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    // but need to add it back into list first
                    leftTuple.setBlocker(rightTuple);
                    rightTuple.addBlocked(leftTuple);
                    leftTuple = temp;
                    continue;
                }
                constraints.updateFromTuple(contextEntry, wm, leftTuple);
                if (iterateFromStart) {
                    rootBlocker = existsNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
                }
                // we know that older tuples have been checked so continue next
                for (RightTuple newBlocker = rootBlocker; newBlocker != null; newBlocker = (RightTuple) rightIt.next(newBlocker)) {
                    // There may be UPDATE RightTuples too, but that's ok. They've already been re-added to the correct bucket, safe to be reprocessed.
                    if (leftTuple.getStagedType() != LeftTuple.DELETE && newBlocker.getStagedType() != LeftTuple.DELETE && constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandleForEvaluation())) {
                        leftTuple.setBlocker(newBlocker);
                        newBlocker.addBlocked(leftTuple);
                        break;
                    }
                }
                if (leftTuple.getBlocker() == null) {
                    // was previous blocked and not in memory, so add
                    if (ltm != null) {
                        ltm.add(leftTuple);
                    }
                    LeftTuple childLeftTuple = leftTuple.getFirstChild();
                    if (childLeftTuple != null) {
                        childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                        RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                    }
                }
                leftTuple = temp;
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Aggregations

FastIterator (org.drools.core.util.FastIterator)40 RightTuple (org.drools.core.reteoo.RightTuple)27 LeftTuple (org.drools.core.reteoo.LeftTuple)24 TupleMemory (org.drools.core.reteoo.TupleMemory)21 BetaConstraints (org.drools.core.common.BetaConstraints)18 ContextEntry (org.drools.core.rule.ContextEntry)18 Tuple (org.drools.core.spi.Tuple)12 BetaMemory (org.drools.core.reteoo.BetaMemory)11 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)9 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)8 ArrayList (java.util.ArrayList)5 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)4 ObjectSource (org.drools.core.reteoo.ObjectSource)4 HashMap (java.util.HashMap)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 AccumulateNode (org.drools.core.reteoo.AccumulateNode)3 BetaNode (org.drools.core.reteoo.BetaNode)3