Search in sources :

Example 46 with LeftTuple

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

the class PhreakJoinNode 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();
        if (leftTuple.getMemory() != null) {
            // it may have been staged and never actually added
            ltm.remove(leftTuple);
        }
        if (leftTuple.getFirstChild() != null) {
            LeftTuple childLeftTuple = leftTuple.getFirstChild();
            while (childLeftTuple != null) {
                LeftTuple nextChild = childLeftTuple.getHandleNext();
                RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                childLeftTuple = nextChild;
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory)

Example 47 with LeftTuple

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

the class PhreakJoinNode method doRightUpdatesProcessChildren.

public LeftTuple doRightUpdatesProcessChildren(LeftTuple childLeftTuple, LeftTuple leftTuple, RightTuple rightTuple, TupleSets<LeftTuple> stagedLeftTuples, ContextEntry[] contextEntry, BetaConstraints constraints, LeftTupleSink sink, FastIterator it, TupleSets<LeftTuple> trgLeftTuples) {
    if (childLeftTuple == null) {
        // we had no children before, but there is a bucket to potentially match, so try as normal assert
        for (; leftTuple != null; leftTuple = (LeftTuple) it.next(leftTuple)) {
            if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                // ignore, as it will get processed via left iteration. Children cannot be processed twice
                continue;
            }
            if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, null, sink, true);
            }
        }
    } else {
        // in the same bucket, so iterate and compare
        for (; leftTuple != null; leftTuple = (LeftTuple) it.next(leftTuple)) {
            if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                // ignore, as it will get processed via left iteration. Children cannot be processed twice
                continue;
            }
            if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                // insert, childLeftTuple is not updated
                if (childLeftTuple == null || childLeftTuple.getLeftParent() != leftTuple) {
                    insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, childLeftTuple, sink, true);
                } else {
                    // update, childLeftTuple is updated
                    childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                    updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);
                    LeftTuple nextChildLeftTuple = childLeftTuple.getRightParentNext();
                    childLeftTuple.reAddLeft();
                    childLeftTuple = nextChildLeftTuple;
                }
            } else if (childLeftTuple != null && childLeftTuple.getLeftParent() == leftTuple) {
                // delete, childLeftTuple is updated
                childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                LeftTuple nextChild = childLeftTuple.getRightParentNext();
                RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                childLeftTuple = nextChild;
            }
        }
    }
    return childLeftTuple;
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 48 with LeftTuple

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

the class PhreakJoinNode method doLeftUpdatesProcessChildren.

public LeftTuple doLeftUpdatesProcessChildren(LeftTuple childLeftTuple, LeftTuple leftTuple, RightTuple rightTuple, TupleSets<LeftTuple> stagedLeftTuples, ContextEntry[] contextEntry, BetaConstraints constraints, LeftTupleSink sink, FastIterator it, TupleSets<LeftTuple> trgLeftTuples) {
    if (childLeftTuple == null) {
        // we had no children before, but there is a bucket to potentially match, so try as normal assert
        for (; rightTuple != null; rightTuple = (RightTuple) it.next(rightTuple)) {
            if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandle())) {
                insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, null, sink, true);
            }
        }
    } else {
        // in the same bucket, so iterate and compare
        for (; rightTuple != null; rightTuple = (RightTuple) it.next(rightTuple)) {
            if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandle())) {
                // insert, childLeftTuple is not updated
                if (childLeftTuple == null || childLeftTuple.getRightParent() != rightTuple) {
                    insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, childLeftTuple, null, sink, true);
                } else {
                    // update, childLeftTuple is updated
                    childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
                    updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);
                    LeftTuple nextChildLeftTuple = childLeftTuple.getHandleNext();
                    childLeftTuple.reAddRight();
                    childLeftTuple = nextChildLeftTuple;
                }
            } else if (childLeftTuple != null && childLeftTuple.getRightParent() == rightTuple) {
                // delete, childLeftTuple is updated
                LeftTuple nextChild = childLeftTuple.getHandleNext();
                RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                childLeftTuple = nextChild;
            }
        }
    }
    return childLeftTuple;
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 49 with LeftTuple

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

the class PhreakNotNode method doLeftUpdates.

public void doLeftUpdates(NotNode notNode, 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 = notNode.getRawConstraints();
    boolean leftUpdateOptimizationAllowed = notNode.isLeftUpdateOptimizationAllowed();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        FastIterator rightIt = notNode.getRightIterator(rtm);
        RightTuple firstRightTuple = notNode.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()) {
                    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;
                }
            }
            LeftTuple childLeftTuple = leftTuple.getFirstChild();
            if (leftTuple.getBlocker() != null) {
                // blocked
                if (childLeftTuple != null) {
                    // blocked, with previous children, so must have not been previously blocked, so retract
                    // no need to remove, as we removed at the start
                    // to be matched against, as it's now blocked
                    // we have the righttuple, so use it for the pctx
                    childLeftTuple.setPropagationContext(leftTuple.getBlocker().getPropagationContext());
                    RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                }
            // else: it's blocked now and no children so blocked before, thus do nothing
            } else if (childLeftTuple == null) {
                // not blocked, with no children, must have been previously blocked so assert
                insertChildLeftTuple(sink, trgLeftTuples, ltm, leftTuple, leftTuple.getPropagationContext(), true);
            } else {
                updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);
                // not blocked, with children, so wasn't previous blocked and still isn't so modify
                // add to memory so other fact handles can attempt to match
                ltm.add(leftTuple);
                childLeftTuple.reAddLeft();
            }
        }
        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 50 with LeftTuple

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

the class PhreakNotNode method doLeftInserts.

public void doLeftInserts(NotNode notNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(notNode, leftTuple);
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        // This method will also remove rightTuples that are from subnetwork where no leftmemory use used
        RuleNetworkEvaluator.findLeftTupleBlocker(notNode, rtm, contextEntry, constraints, leftTuple, useLeftMemory);
        if (leftTuple.getBlocker() == null) {
            insertChildLeftTuple(sink, trgLeftTuples, ltm, leftTuple, leftTuple.getPropagationContext(), useLeftMemory);
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) 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

LeftTuple (org.drools.core.reteoo.LeftTuple)125 RightTuple (org.drools.core.reteoo.RightTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)37 InternalFactHandle (org.drools.core.common.InternalFactHandle)34 FastIterator (org.drools.core.util.FastIterator)22 BetaConstraints (org.drools.core.common.BetaConstraints)21 BetaMemory (org.drools.core.reteoo.BetaMemory)21 ContextEntry (org.drools.core.rule.ContextEntry)20 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)16 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)13 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 Test (org.junit.Test)13 KieSession (org.kie.api.runtime.KieSession)13 Tuple (org.drools.core.spi.Tuple)12 WorkingMemory (org.drools.core.WorkingMemory)11 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)11 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)10 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 ArrayList (java.util.ArrayList)8