Search in sources :

Example 26 with LeftTuple

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

the class AddRemoveRule method flushLeftTupleIfNecessary.

public static boolean flushLeftTupleIfNecessary(InternalWorkingMemory wm, SegmentMemory sm, LeftTuple leftTuple, boolean streamMode, short stagedType) {
    PathMemory pmem = streamMode ? sm.getPathMemories().get(0) : sm.getFirstDataDrivenPathMemory();
    if (pmem == null) {
        return false;
    }
    TupleSets<LeftTuple> leftTupleSets = new TupleSetsImpl<LeftTuple>();
    if (leftTuple != null) {
        switch(stagedType) {
            case Tuple.INSERT:
                leftTupleSets.addInsert(leftTuple);
                break;
            case Tuple.DELETE:
                leftTupleSets.addDelete(leftTuple);
                break;
            case Tuple.UPDATE:
                leftTupleSets.addUpdate(leftTuple);
                break;
        }
    }
    forceFlushLeftTuple(pmem, sm, wm, leftTupleSets);
    if (pmem.isDataDriven() && pmem.getNodeType() == NodeTypeEnums.RightInputAdaterNode) {
        for (PathEndNode pnode : pmem.getPathEndNode().getPathEndNodes()) {
            if (pnode instanceof TerminalNode) {
                PathMemory outPmem = wm.getNodeMemory((TerminalNode) pnode);
                if (outPmem.isDataDriven()) {
                    SegmentMemory outSmem = outPmem.getSegmentMemories()[0];
                    if (outSmem != null) {
                        forceFlushLeftTuple(outPmem, outSmem, wm, new TupleSetsImpl<LeftTuple>());
                    }
                }
            }
        }
    }
    return true;
}
Also used : TupleSetsImpl(org.drools.core.common.TupleSetsImpl) SegmentMemory(org.drools.core.reteoo.SegmentMemory) PathEndNode(org.drools.core.reteoo.PathEndNode) AbstractTerminalNode(org.drools.core.reteoo.AbstractTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) LeftTuple(org.drools.core.reteoo.LeftTuple) PathMemory(org.drools.core.reteoo.PathMemory)

Example 27 with LeftTuple

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

the class AddRemoveRule method insertPeerLeftTuple.

/**
 * Create all missing peers
 */
private static LeftTuple insertPeerLeftTuple(LeftTuple lt, LeftTupleSinkNode node, InternalWorkingMemory wm) {
    LeftInputAdapterNode.LiaNodeMemory liaMem = null;
    if (node.getLeftTupleSource().getType() == NodeTypeEnums.LeftInputAdapterNode) {
        liaMem = wm.getNodeMemory(((LeftInputAdapterNode) node.getLeftTupleSource()));
    }
    LeftTuple peer = node.createPeer(lt);
    Memory memory = wm.getNodeMemories().peekNodeMemory(node);
    if (memory == null || memory.getSegmentMemory() == null) {
        throw new IllegalStateException("Defensive Programming: this should not be possilbe, as the addRule code should init child segments if they are needed ");
    }
    if (liaMem == null) {
        memory.getSegmentMemory().getStagedLeftTuples().addInsert(peer);
    } else {
        // If parent is Lian, then this must be called, so that any linking or unlinking can be done.
        LeftInputAdapterNode.doInsertSegmentMemory(wm, true, liaMem, memory.getSegmentMemory(), peer, node.getLeftTupleSource().isStreamMode());
    }
    return peer;
}
Also used : Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 28 with LeftTuple

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

the class PhreakAccumulateNode method doRightUpdatesProcessChildren.

private void doRightUpdatesProcessChildren(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, 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 AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
                // add a new match
                addMatch(accNode, accumulate, leftTuple, rightTuple, null, null, wm, am, accctx, true);
            }
        }
    } 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 AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
                LeftTuple temp = null;
                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, wm, am, accctx, true);
                    childLeftTuple = temp;
                }
                // add a new match
                addMatch(accNode, accumulate, leftTuple, rightTuple, null, childLeftTuple, wm, am, accctx, true);
                if (temp != null) {
                    childLeftTuple = temp;
                }
            } else if (childLeftTuple != null && childLeftTuple.getLeftParent() == leftTuple) {
                if (leftTuple.getStagedType() == LeftTuple.NONE) {
                    trgLeftTuples.addUpdate(leftTuple);
                }
                LeftTuple temp = childLeftTuple.getRightParentNext();
                final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
                // remove the match
                removeMatch(accNode, accumulate, rightTuple, childLeftTuple, wm, am, accctx, true);
                childLeftTuple = temp;
            }
        // else do nothing, was false before and false now.
        }
    }
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext)

Example 29 with LeftTuple

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

the class PhreakAccumulateNode method reaccumulateForLeftTuple.

private static void reaccumulateForLeftTuple(final AccumulateNode accNode, final Accumulate accumulate, final LeftTuple leftTuple, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx) {
    accumulate.init(am.workingMemoryContext, accctx.context, leftTuple, wm);
    for (LeftTuple childMatch = leftTuple.getFirstChild(); childMatch != null; childMatch = childMatch.getHandleNext()) {
        RightTuple rightTuple = childMatch.getRightParent();
        InternalFactHandle childHandle = rightTuple.getFactHandle();
        LeftTuple tuple = leftTuple;
        if (accNode.isUnwrapRightObject()) {
            // if there is a subnetwork, handle must be unwrapped
            tuple = (LeftTuple) rightTuple;
            childHandle = rightTuple.getFactHandleForEvaluation();
        }
        accumulate.accumulate(am.workingMemoryContext, accctx.context, tuple, childHandle, wm);
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple)

Example 30 with LeftTuple

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

the class PhreakAccumulateNode method removeMatch.

/**
 * Removes a match between left and right tuple
 */
private static void removeMatch(final AccumulateNode accNode, final Accumulate accumulate, final RightTuple rightTuple, final LeftTuple match, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx, final boolean reaccumulate) {
    // save the matching tuple
    LeftTuple leftTuple = match.getLeftParent();
    // removing link between left and right
    match.unlinkFromLeftParent();
    match.unlinkFromRightParent();
    // if there is a subnetwork, we need to unwrap the object from inside the tuple
    InternalFactHandle handle = rightTuple.getFactHandle();
    LeftTuple tuple = leftTuple;
    if (accNode.isUnwrapRightObject()) {
        tuple = (LeftTuple) rightTuple;
        handle = rightTuple.getFactHandleForEvaluation();
    }
    if (accumulate.supportsReverse()) {
        // just reverse this single match
        accumulate.reverse(am.workingMemoryContext, accctx.context, tuple, handle, wm);
    } else {
        // otherwise need to recalculate all matches for the given leftTuple
        if (reaccumulate) {
            reaccumulateForLeftTuple(accNode, accumulate, leftTuple, wm, am, accctx);
        }
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

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