Search in sources :

Example 86 with LeftTuple

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

the class PhreakAccumulateNode method removePreviousMatchesForLeftTuple.

private static void removePreviousMatchesForLeftTuple(final Accumulate accumulate, final LeftTuple leftTuple, final InternalWorkingMemory workingMemory, final AccumulateMemory memory, final AccumulateContext accctx, boolean reInit) {
    for (LeftTuple match = leftTuple.getFirstChild(); match != null; ) {
        LeftTuple next = match.getHandleNext();
        match.unlinkFromRightParent();
        match.unlinkFromLeftParent();
        match = next;
    }
    if (reInit) {
        // since there are no more matches, the following call will just re-initialize the accumulation
        accumulate.init(memory.workingMemoryContext, accctx.context, leftTuple, workingMemory);
    }
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 87 with LeftTuple

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

Example 88 with LeftTuple

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

the class PhreakAccumulateNode method doNode.

public void doNode(AccumulateNode accNode, LeftTupleSink sink, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
    // order of left and right operations is to minimise wasted of innefficient joins.
    // We need to collect which leftTuple where updated, so that we can
    // add their result tuple to the real target tuples later
    TupleSets<LeftTuple> tempLeftTuples = new TupleSetsImpl<LeftTuple>();
    if (srcLeftTuples.getDeleteFirst() != null) {
        // use the real target here, as dealing direct with left tuples
        doLeftDeletes(accNode, am, wm, srcLeftTuples, trgLeftTuples, stagedLeftTuples);
    }
    if (srcRightTuples.getDeleteFirst() != null) {
        doRightDeletes(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcRightTuples.getUpdateFirst() != null) {
        RuleNetworkEvaluator.doUpdatesReorderRightMemory(bm, srcRightTuples);
        doRightUpdates(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcLeftTuples.getUpdateFirst() != null) {
        RuleNetworkEvaluator.doUpdatesReorderLeftMemory(bm, srcLeftTuples);
        doLeftUpdates(accNode, am, wm, srcLeftTuples, tempLeftTuples);
    }
    if (srcRightTuples.getInsertFirst() != null) {
        doRightInserts(accNode, am, wm, srcRightTuples, tempLeftTuples);
    }
    if (srcLeftTuples.getInsertFirst() != null) {
        doLeftInserts(accNode, am, wm, srcLeftTuples, tempLeftTuples);
    }
    Accumulate accumulate = accNode.getAccumulate();
    // LeftTuple retracts are already on the trgLeftTuples
    for (LeftTuple leftTuple = tempLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
        leftTuple.clearStaged();
        leftTuple = next;
    }
    for (LeftTuple leftTuple = tempLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
        leftTuple.clearStaged();
        leftTuple = next;
    }
    srcRightTuples.resetAll();
    srcLeftTuples.resetAll();
}
Also used : TupleSetsImpl(org.drools.core.common.TupleSetsImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) Accumulate(org.drools.core.rule.Accumulate)

Example 89 with LeftTuple

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

the class PhreakAccumulateNode method addMatch.

private static void addMatch(final AccumulateNode accNode, final Accumulate accumulate, final LeftTuple leftTuple, final RightTuple rightTuple, final LeftTuple currentLeftChild, final LeftTuple currentRightChild, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx, final boolean useLeftMemory) {
    LeftTuple tuple = leftTuple;
    InternalFactHandle handle = rightTuple.getFactHandle();
    if (accNode.isUnwrapRightObject()) {
        // if there is a subnetwork, handle must be unwrapped
        tuple = (LeftTuple) rightTuple;
        handle = rightTuple.getFactHandleForEvaluation();
    }
    if (handle.isExpired()) {
        return;
    }
    accctx.setPropagationContext(rightTuple.getPropagationContext());
    accumulate.accumulate(am.workingMemoryContext, accctx.context, tuple, handle, wm);
    // in sequential mode, we don't need to keep record of matched tuples
    if (useLeftMemory) {
        // linking left and right by creating a new left tuple
        accNode.createLeftTuple(leftTuple, rightTuple, currentLeftChild, currentRightChild, accNode, true);
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 90 with LeftTuple

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

the class PhreakAccumulateNode method doRightDeletes.

public void doRightDeletes(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, 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 AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
                    removeMatch(accNode, accumulate, rightTuple, match, wm, am, accctx, true);
                    if (leftTuple.getStagedType() == LeftTuple.NONE) {
                        trgLeftTuples.addUpdate(leftTuple);
                    }
                    match = nextLeft;
                }
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
}
Also used : RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) Accumulate(org.drools.core.rule.Accumulate)

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