Search in sources :

Example 11 with RightTuple

use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.

the class PhreakAccumulateNode method doLeftUpdates.

public void doLeftUpdates(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    Accumulate accumulate = accNode.getAccumulate();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = accNode.getRawConstraints();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        FastIterator rightIt = accNode.getRightIterator(rtm);
        RightTuple rightTuple = accNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
        LeftTuple childLeftTuple = leftTuple.getFirstChild();
        // if rightTuple is null, we assume there was a bucket change and that bucket is empty
        if (childLeftTuple != null && rtm.isIndexed() && !rightIt.isFullIterator() && (rightTuple == null || (rightTuple.getMemory() != childLeftTuple.getRightParent().getMemory()))) {
            // our index has changed, so delete all the previous matchings
            removePreviousMatchesForLeftTuple(accumulate, leftTuple, wm, am, accctx, true);
            // null so the next check will attempt matches for new bucket
            childLeftTuple = null;
        }
        // we can't do anything if RightTupleMemory is empty
        if (rightTuple != null) {
            doLeftUpdatesProcessChildren(accNode, am, wm, bm, accumulate, constraints, rightIt, leftTuple, accctx, rightTuple, childLeftTuple);
        }
        leftTuple.clearStaged();
        trgLeftTuples.addUpdate(leftTuple);
        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 12 with RightTuple

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

use of org.drools.core.reteoo.RightTuple 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 14 with RightTuple

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

use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.

the class PhreakExistsNode method doLeftDeletes.

public void doLeftDeletes(BetaMemory bm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        RightTuple blocker = leftTuple.getBlocker();
        if (blocker == null) {
            if (leftTuple.getMemory() != null) {
                // it may have been staged and never actually added
                ltm.remove(leftTuple);
            }
        } else {
            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);
            }
            blocker.removeBlocked(leftTuple);
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : 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)

Aggregations

RightTuple (org.drools.core.reteoo.RightTuple)60 LeftTuple (org.drools.core.reteoo.LeftTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)30 FastIterator (org.drools.core.util.FastIterator)26 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 BetaMemory (org.drools.core.reteoo.BetaMemory)20 BetaConstraints (org.drools.core.common.BetaConstraints)19 ContextEntry (org.drools.core.rule.ContextEntry)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)13 Test (org.junit.Test)13 Tuple (org.drools.core.spi.Tuple)11 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)9 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)8 BetaNode (org.drools.core.reteoo.BetaNode)7 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)7 ArrayList (java.util.ArrayList)6 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)6 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 Accumulate (org.drools.core.rule.Accumulate)6