Search in sources :

Example 11 with ContextEntry

use of org.drools.core.rule.ContextEntry in project drools by kiegroup.

the class PhreakJoinNode method doLeftUpdates.

public void doLeftUpdates(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = joinNode.getRawConstraints();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        FastIterator it = joinNode.getRightIterator(rtm);
        RightTuple rightTuple = joinNode.getFirstRightTuple(leftTuple, rtm, null, it);
        // if rightTuple is null, we assume there was a bucket change and that bucket is empty
        if (rtm.isIndexed() && !it.isFullIterator()) {
            // our index has changed, so delete all the previous propagations
            for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; ) {
                LeftTuple nextChild = childLeftTuple.getHandleNext();
                if (rightTuple == null || rightTuple.getMemory() != childLeftTuple.getRightParent().getMemory()) {
                    RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                }
                childLeftTuple = nextChild;
            }
        }
        // we can't do anything if RightTupleMemory is empty
        if (rightTuple != null) {
            doLeftUpdatesProcessChildren(leftTuple.getFirstChild(), leftTuple, rightTuple, stagedLeftTuples, contextEntry, constraints, sink, it, trgLeftTuples);
        }
        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) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 12 with ContextEntry

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

use of org.drools.core.rule.ContextEntry 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)

Example 14 with ContextEntry

use of org.drools.core.rule.ContextEntry in project drools by kiegroup.

the class NotNodeLeftTuple method getAccumulatedObjects.

@Override
public Collection<Object> getAccumulatedObjects() {
    if (NodeTypeEnums.ExistsNode != getTupleSink().getType()) {
        return Collections.emptyList();
    }
    BetaNode betaNode = ((BetaNode) getTupleSink());
    BetaConstraints constraints = betaNode.getRawConstraints();
    InternalWorkingMemory wm = getFactHandle().getEntryPoint().getInternalWorkingMemory();
    BetaMemory bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) getTupleSink());
    TupleMemory rtm = bm.getRightTupleMemory();
    FastIterator it = betaNode.getRightIterator(rtm);
    ContextEntry[] contextEntry = bm.getContext();
    constraints.updateFromTuple(contextEntry, wm, this);
    Collection<Object> result = new ArrayList<>();
    for (RightTuple rightTuple = betaNode.getFirstRightTuple(this, rtm, null, it); rightTuple != null; ) {
        RightTuple nextRight = (RightTuple) it.next(rightTuple);
        InternalFactHandle fh = rightTuple.getFactHandleForEvaluation();
        if (constraints.isAllowedCachedLeft(contextEntry, fh)) {
            result.add(fh.getObject());
        }
        rightTuple = nextRight;
    }
    return result;
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) ArrayList(java.util.ArrayList) MemoryFactory(org.drools.core.common.MemoryFactory) ContextEntry(org.drools.core.rule.ContextEntry) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 15 with ContextEntry

use of org.drools.core.rule.ContextEntry in project drools by kiegroup.

the class PhreakAccumulateNode method doRightUpdates.

public void doRightUpdates(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleMemory ltm = bm.getLeftTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = accNode.getRawConstraints();
    Accumulate accumulate = accNode.getAccumulate();
    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        if (ltm != null && ltm.size() > 0) {
            LeftTuple childLeftTuple = rightTuple.getFirstChild();
            FastIterator leftIt = accNode.getLeftIterator(ltm);
            LeftTuple leftTuple = accNode.getFirstLeftTuple(rightTuple, ltm, leftIt);
            constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
            // We assume a bucket change if leftTuple == null
            if (childLeftTuple != null && ltm.isIndexed() && !leftIt.isFullIterator() && (leftTuple == null || (leftTuple.getMemory() != childLeftTuple.getLeftParent().getMemory()))) {
                // our index has changed, so delete all the previous matches
                removePreviousMatchesForRightTuple(accNode, accumulate, rightTuple, wm, am, childLeftTuple, trgLeftTuples);
                // null so the next check will attempt matches for new bucket
                childLeftTuple = null;
            }
            // if LeftTupleMemory is empty, there are no matches to modify
            if (leftTuple != null) {
                if (leftTuple.getStagedType() == LeftTuple.NONE) {
                    trgLeftTuples.addUpdate(leftTuple);
                }
                doRightUpdatesProcessChildren(accNode, am, wm, bm, constraints, accumulate, leftIt, rightTuple, childLeftTuple, leftTuple, trgLeftTuples);
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) BetaMemory(org.drools.core.reteoo.BetaMemory) FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry) Accumulate(org.drools.core.rule.Accumulate)

Aggregations

BetaConstraints (org.drools.core.common.BetaConstraints)21 ContextEntry (org.drools.core.rule.ContextEntry)21 LeftTuple (org.drools.core.reteoo.LeftTuple)20 RightTuple (org.drools.core.reteoo.RightTuple)18 TupleMemory (org.drools.core.reteoo.TupleMemory)18 FastIterator (org.drools.core.util.FastIterator)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)12 BetaMemory (org.drools.core.reteoo.BetaMemory)6 Accumulate (org.drools.core.rule.Accumulate)4 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)3 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)2 DataProvider (org.drools.core.spi.DataProvider)2 PropagationContext (org.drools.core.spi.PropagationContext)2 AbstractHashTable (org.drools.core.util.AbstractHashTable)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1