Search in sources :

Example 51 with TupleMemory

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

the class PhreakAccumulateNode method doRightUpdates.

private void doRightUpdates(AccumulateNode accNode, AccumulateMemory am, ReteEvaluator reteEvaluator, 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, reteEvaluator, 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, reteEvaluator, 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) {
                doRightUpdatesProcessChildren(accNode, am, reteEvaluator, 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) AccumulateContextEntry(org.drools.core.reteoo.AccumulateNode.AccumulateContextEntry) ContextEntry(org.drools.core.rule.ContextEntry) Accumulate(org.drools.core.rule.Accumulate)

Example 52 with TupleMemory

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

the class PhreakAccumulateNode method doRightDeletes.

private void doRightDeletes(AccumulateNode accNode, AccumulateMemory am, ReteEvaluator reteEvaluator, 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 BaseAccumulation accctx = (BaseAccumulation) leftTuple.getContextObject();
                    // FIXME This will be really slow, if it re-accumulates on the same LeftTuple (MDP)
                    removeMatch(accNode, accumulate, rightTuple, match, reteEvaluator, am, accctx, true);
                    if (leftTuple.getStagedType() == LeftTuple.NONE) {
                        trgLeftTuples.addUpdate(leftTuple);
                    }
                    match = nextLeft;
                }
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
}
Also used : BaseAccumulation(org.drools.core.reteoo.AccumulateNode.BaseAccumulation) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) Accumulate(org.drools.core.rule.Accumulate)

Example 53 with TupleMemory

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

the class PhreakAccumulateNode method doLeftDeletes.

private void doLeftDeletes(AccumulateNode accNode, AccumulateMemory am, ReteEvaluator reteEvaluator, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleMemory ltm = bm.getLeftTupleMemory();
    Accumulate accumulate = accNode.getAccumulate();
    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);
            BaseAccumulation accctx = (BaseAccumulation) leftTuple.getContextObject();
            leftTuple.setContextObject(null);
            removePreviousMatchesForLeftTuple(accumulate, leftTuple, reteEvaluator, am, accctx, false);
            propagateDelete(trgLeftTuples, stagedLeftTuples, accctx);
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : BetaMemory(org.drools.core.reteoo.BetaMemory) BaseAccumulation(org.drools.core.reteoo.AccumulateNode.BaseAccumulation) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) Accumulate(org.drools.core.rule.Accumulate)

Example 54 with TupleMemory

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

the class PhreakAccumulateNode method doLeftUpdates.

private void doLeftUpdates(AccumulateNode accNode, AccumulateMemory am, ReteEvaluator reteEvaluator, 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();
        BaseAccumulation accctx = (BaseAccumulation) leftTuple.getContextObject();
        if (accNode.isRightInputIsRiaNode()) {
            // This is a subnetwork, do not process further. As all matches will processed
            // by the right updates. This is to avoid double iteration (first right side iteration
            // then left side iteration) or for the join to find matching tuple chains, which it previously
            // did via subsumption checking.
            leftTuple.clearStaged();
            trgLeftTuples.addUpdate(leftTuple);
            leftTuple = next;
            continue;
        }
        constraints.updateFromTuple(contextEntry, reteEvaluator, leftTuple);
        FastIterator rightIt = accNode.getRightIterator(rtm);
        RightTuple rightTuple = accNode.getFirstRightTuple(leftTuple, rtm, 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, reteEvaluator, 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, reteEvaluator, 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) BaseAccumulation(org.drools.core.reteoo.AccumulateNode.BaseAccumulation) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) AccumulateContextEntry(org.drools.core.reteoo.AccumulateNode.AccumulateContextEntry) ContextEntry(org.drools.core.rule.ContextEntry) Accumulate(org.drools.core.rule.Accumulate)

Example 55 with TupleMemory

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

the class PhreakAccumulateNode method doLeftInserts.

private void doLeftInserts(AccumulateNode accNode, AccumulateMemory am, ReteEvaluator reteEvaluator, 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);
        }
        BaseAccumulation accresult = initAccumulationContext(am, reteEvaluator, accumulate, leftTuple);
        if (accNode.isRightInputIsRiaNode()) {
            // This is a subnetwork, do not process further. As all matches will processed
            // by the right insert. This is to avoid double iteration (first right side iteration
            // then left side iteration) or for the join to find matching tuple chains, which it previously
            // did via subsumption checking.
            leftTuple.clearStaged();
            trgLeftTuples.addInsert(leftTuple);
            leftTuple = next;
            continue;
        }
        constraints.updateFromTuple(contextEntry, reteEvaluator, leftTuple);
        FastIterator rightIt = accNode.getRightIterator(rtm);
        for (RightTuple rightTuple = accNode.getFirstRightTuple(leftTuple, rtm, 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, reteEvaluator, am, accresult, useLeftMemory, true);
            }
            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) BaseAccumulation(org.drools.core.reteoo.AccumulateNode.BaseAccumulation) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) AccumulateContextEntry(org.drools.core.reteoo.AccumulateNode.AccumulateContextEntry) ContextEntry(org.drools.core.rule.ContextEntry) Accumulate(org.drools.core.rule.Accumulate)

Aggregations

TupleMemory (org.drools.core.reteoo.TupleMemory)66 LeftTuple (org.drools.core.reteoo.LeftTuple)59 RightTuple (org.drools.core.reteoo.RightTuple)52 FastIterator (org.drools.core.util.FastIterator)40 BetaConstraints (org.drools.core.common.BetaConstraints)36 ContextEntry (org.drools.core.rule.ContextEntry)36 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)23 BetaMemory (org.drools.core.reteoo.BetaMemory)23 Accumulate (org.drools.core.rule.Accumulate)12 Tuple (org.drools.core.spi.Tuple)9 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)8 ArrayList (java.util.ArrayList)6 BetaNode (org.drools.core.reteoo.BetaNode)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)5 BaseAccumulation (org.drools.core.reteoo.AccumulateNode.BaseAccumulation)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 PhreakPropagationContext (org.drools.core.common.PhreakPropagationContext)4 AccumulateContextEntry (org.drools.core.reteoo.AccumulateNode.AccumulateContextEntry)4 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)4