Search in sources :

Example 1 with BaseAccumulation

use of org.drools.core.reteoo.AccumulateNode.BaseAccumulation in project drools by kiegroup.

the class PhreakAccumulateNode method removePreviousMatchesForRightTuple.

private void removePreviousMatchesForRightTuple(final AccumulateNode accNode, final Accumulate accumulate, final RightTuple rightTuple, final ReteEvaluator reteEvaluator, final AccumulateMemory memory, final LeftTuple firstChild, final TupleSets<LeftTuple> trgLeftTuples) {
    for (LeftTuple match = firstChild; match != null; ) {
        final LeftTuple next = match.getRightParentNext();
        final LeftTuple leftTuple = match.getLeftParent();
        final BaseAccumulation accctx = (BaseAccumulation) leftTuple.getContextObject();
        removeMatch(accNode, accumulate, rightTuple, match, reteEvaluator, memory, accctx, true);
        if (leftTuple.getStagedType() == LeftTuple.NONE) {
            trgLeftTuples.addUpdate(leftTuple);
        }
        match = next;
    }
}
Also used : BaseAccumulation(org.drools.core.reteoo.AccumulateNode.BaseAccumulation) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 2 with BaseAccumulation

use of org.drools.core.reteoo.AccumulateNode.BaseAccumulation in project drools by kiegroup.

the class PhreakAccumulateNode method doRightInserts.

private void doRightInserts(AccumulateNode accNode, AccumulateMemory am, ReteEvaluator reteEvaluator, TupleSets<RightTuple> srcRightTuples, 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();
    if (srcRightTuples.getInsertSize() > 32 && rtm instanceof AbstractHashTable) {
        ((AbstractHashTable) rtm).ensureCapacity(srcRightTuples.getInsertSize());
    }
    boolean tupleMemoryEnabled = accNode.isLeftTupleMemoryEnabled();
    for (RightTuple rightTuple = srcRightTuples.getInsertFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        boolean useTupleMemory = tupleMemoryEnabled || RuleNetworkEvaluator.useLeftMemory(accNode, rightTuple);
        if (useTupleMemory || !accNode.isRightInputIsRiaNode()) {
            // If tuple memory is off, it will still be when it is not a subnetwork.
            rtm.add(rightTuple);
        }
        if (accNode.isRightInputIsRiaNode() || (ltm != null && ltm.size() > 0)) {
            constraints.updateFromFactHandle(contextEntry, reteEvaluator, rightTuple.getFactHandleForEvaluation());
            FastIterator leftIt = accNode.getLeftIterator(ltm);
            for (LeftTuple leftTuple = accNode.getFirstLeftTuple(rightTuple, ltm, leftIt); leftTuple != null; leftTuple = (LeftTuple) leftIt.next(leftTuple)) {
                if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
                    final BaseAccumulation accctx = (BaseAccumulation) leftTuple.getContextObject();
                    addMatch(accNode, accumulate, leftTuple, rightTuple, null, null, reteEvaluator, am, accctx, true, false);
                    // so any existing leftTuples we know are updates, but only add if not already added
                    if (leftTuple.getStagedType() == LeftTuple.NONE) {
                        trgLeftTuples.addUpdate(leftTuple);
                    }
                }
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetFactHandle(contextEntry);
}
Also used : AbstractHashTable(org.drools.core.util.AbstractHashTable) 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) 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 3 with BaseAccumulation

use of org.drools.core.reteoo.AccumulateNode.BaseAccumulation 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)

Example 4 with BaseAccumulation

use of org.drools.core.reteoo.AccumulateNode.BaseAccumulation 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 5 with BaseAccumulation

use of org.drools.core.reteoo.AccumulateNode.BaseAccumulation in project drools by kiegroup.

the class PhreakAccumulateNode method doRightUpdatesProcessChildren.

private void doRightUpdatesProcessChildren(AccumulateNode accNode, AccumulateMemory am, ReteEvaluator reteEvaluator, BetaMemory bm, BetaConstraints constraints, Accumulate accumulate, FastIterator leftIt, RightTuple rightTuple, LeftTuple childLeftTuple, LeftTuple leftTuple, 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) leftIt.next(leftTuple)) {
            if (constraints.isAllowedCachedRight(bm.getContext(), leftTuple)) {
                if (leftTuple.getStagedType() == LeftTuple.NONE) {
                    trgLeftTuples.addUpdate(leftTuple);
                }
                final BaseAccumulation accctx = (BaseAccumulation) leftTuple.getContextObject();
                // add a new match
                addMatch(accNode, accumulate, leftTuple, rightTuple, null, null, reteEvaluator, am, accctx, true, false);
            }
        }
    } else {
        // in the same bucket, so iterate and compare
        for (; leftTuple != null; leftTuple = (LeftTuple) leftIt.next(leftTuple)) {
            if (constraints.isAllowedCachedRight(bm.getContext(), leftTuple)) {
                if (leftTuple.getStagedType() == LeftTuple.NONE) {
                    trgLeftTuples.addUpdate(leftTuple);
                }
                final BaseAccumulation accctx = (BaseAccumulation) leftTuple.getContextObject();
                LeftTuple temp;
                if (childLeftTuple != null && childLeftTuple.getLeftParent() == leftTuple) {
                    temp = childLeftTuple.getRightParentNext();
                    // we must re-add this to ensure deterministic iteration
                    childLeftTuple.reAddLeft();
                    removeMatch(accNode, accumulate, rightTuple, childLeftTuple, reteEvaluator, am, accctx, true);
                    childLeftTuple = temp;
                }
                // add a new match
                addMatch(accNode, accumulate, leftTuple, rightTuple, null, childLeftTuple, reteEvaluator, am, accctx, true, false);
            } else if (childLeftTuple != null && childLeftTuple.getLeftParent() == leftTuple) {
                if (leftTuple.getStagedType() == LeftTuple.NONE) {
                    trgLeftTuples.addUpdate(leftTuple);
                }
                LeftTuple temp = childLeftTuple.getRightParentNext();
                final BaseAccumulation accctx = (BaseAccumulation) leftTuple.getContextObject();
                // FIXME This will be really slow, if it re-accumulates on the same LeftTuple (MDP)
                // remove the match
                removeMatch(accNode, accumulate, rightTuple, childLeftTuple, reteEvaluator, am, accctx, true);
                childLeftTuple = temp;
            }
        // else do nothing, was false before and false now.
        }
    }
}
Also used : BaseAccumulation(org.drools.core.reteoo.AccumulateNode.BaseAccumulation) LeftTuple(org.drools.core.reteoo.LeftTuple)

Aggregations

BaseAccumulation (org.drools.core.reteoo.AccumulateNode.BaseAccumulation)7 LeftTuple (org.drools.core.reteoo.LeftTuple)7 TupleMemory (org.drools.core.reteoo.TupleMemory)5 Accumulate (org.drools.core.rule.Accumulate)5 BetaMemory (org.drools.core.reteoo.BetaMemory)4 RightTuple (org.drools.core.reteoo.RightTuple)4 BetaConstraints (org.drools.core.common.BetaConstraints)3 AccumulateContextEntry (org.drools.core.reteoo.AccumulateNode.AccumulateContextEntry)3 ContextEntry (org.drools.core.rule.ContextEntry)3 FastIterator (org.drools.core.util.FastIterator)3 AbstractHashTable (org.drools.core.util.AbstractHashTable)1