Search in sources :

Example 1 with RightTuple

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

the class PhreakNotNode method doRightDeletes.

public void doRightDeletes(NotNode notNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();
    for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        FastIterator it = notNode.getRightIterator(rtm);
        // assign now, so we can remove from memory before doing any possible propagations
        boolean useComparisonIndex = rtm.getIndexType().isComparison();
        RightTuple rootBlocker = useComparisonIndex ? null : (RightTuple) it.next(rightTuple);
        if (rightTuple.getMemory() != null) {
            // it may have been staged and never actually added
            rtm.remove(rightTuple);
        }
        if (rightTuple.getBlocked() != null) {
            for (LeftTuple leftTuple = rightTuple.getBlocked(); leftTuple != null; ) {
                LeftTuple temp = leftTuple.getBlockedNext();
                leftTuple.clearBlocker();
                if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
                    // ignore, as it will get processed via left iteration. Children cannot be processed twice
                    leftTuple = temp;
                    continue;
                }
                constraints.updateFromTuple(contextEntry, wm, leftTuple);
                if (useComparisonIndex) {
                    rootBlocker = (RightTuple) rtm.getFirst(leftTuple);
                }
                // we know that older tuples have been checked so continue next
                for (RightTuple newBlocker = rootBlocker; newBlocker != null; newBlocker = (RightTuple) it.next(newBlocker)) {
                    if (constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandleForEvaluation())) {
                        leftTuple.setBlocker(newBlocker);
                        newBlocker.addBlocked(leftTuple);
                        break;
                    }
                }
                if (leftTuple.getBlocker() == null) {
                    // was previous blocked and not in memory, so add
                    insertChildLeftTuple(sink, trgLeftTuples, ltm, leftTuple, rightTuple.getPropagationContext(), true);
                }
                leftTuple = temp;
            }
        }
        rightTuple.setBlocked(null);
        rightTuple.clearStaged();
        rightTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) 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 2 with RightTuple

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

the class RuleNetworkEvaluator method doRiaNode2.

private void doRiaNode2(InternalWorkingMemory wm, TupleSets<LeftTuple> srcTuples, RightInputAdapterNode riaNode) {
    ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
    BetaNode betaNode = (BetaNode) sinks[0];
    BetaMemory bm;
    Memory nodeMem = wm.getNodeMemory(betaNode);
    if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
        bm = ((AccumulateMemory) nodeMem).getBetaMemory();
    } else {
        bm = (BetaMemory) nodeMem;
    }
    TupleSets<RightTuple> rightTuples = bm.getStagedRightTuples();
    // Build up iteration array for other sinks
    BetaNode[] bns = null;
    BetaMemory[] bms = null;
    int length = sinks.length;
    if (length > 1) {
        bns = new BetaNode[sinks.length - 1];
        bms = new BetaMemory[sinks.length - 1];
        for (int i = 1; i < length; i++) {
            bns[i - 1] = (BetaNode) sinks[i];
            Memory nodeMem2 = wm.getNodeMemory(bns[i - 1]);
            if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
                bms[i - 1] = ((AccumulateMemory) nodeMem2).getBetaMemory();
            } else {
                bms[i - 1] = (BetaMemory) nodeMem2;
            }
        }
    }
    // subtract one, as first is not in the array;
    length--;
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getInsertFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (bm.getStagedRightTuples().isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addInsert(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                subnetworkTuple = riaNode.createPeer(subnetworkTuple);
                bms[i].getStagedRightTuples().addInsert(subnetworkTuple);
            }
        }
        subnetworkTuple = next;
    }
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getDeleteFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (rightTuples.isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        switch(subnetworkTuple.getStagedTypeOnRight()) {
            // handle clash with already staged entries
            case Tuple.INSERT:
                rightTuples.removeInsert(subnetworkTuple);
                break;
            case Tuple.UPDATE:
                rightTuples.removeUpdate(subnetworkTuple);
                break;
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addDelete(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                subnetworkTuple = (SubnetworkTuple) subnetworkTuple.getPeer();
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                bms[i].getStagedRightTuples().addDelete(subnetworkTuple);
                subnetworkTuple.setStagedOnRight();
            }
        }
        subnetworkTuple = next;
    }
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getUpdateFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (rightTuples.isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addUpdate(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                subnetworkTuple = (SubnetworkTuple) subnetworkTuple.getPeer();
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                bms[i].getStagedRightTuples().addUpdate(subnetworkTuple);
                subnetworkTuple.setStagedOnRight();
            }
        }
        subnetworkTuple = next;
    }
    srcTuples.resetAll();
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ConditionalBranchMemory(org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) TimerNodeMemory(org.drools.core.reteoo.TimerNode.TimerNodeMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) EvalMemory(org.drools.core.reteoo.EvalConditionNode.EvalMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) RiaPathMemory(org.drools.core.reteoo.RiaPathMemory) ObjectSink(org.drools.core.reteoo.ObjectSink) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) SubnetworkTuple(org.drools.core.reteoo.SubnetworkTuple)

Example 3 with RightTuple

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

the class RuleNetworkEvaluator method doUpdatesReorderRightMemory.

public static void doUpdatesReorderRightMemory(BetaMemory bm, TupleSets<RightTuple> srcRightTuples) {
    TupleMemory rtm = bm.getRightTupleMemory();
    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
        if (rightTuple.getMemory() != null) {
            rightTuple.setTempRightTupleMemory(rightTuple.getMemory());
            rtm.remove(rightTuple);
        }
    }
    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
        if (rightTuple.getTempRightTupleMemory() != null) {
            rtm.add(rightTuple);
            for (LeftTuple childLeftTuple = rightTuple.getFirstChild(); childLeftTuple != null; ) {
                LeftTuple childNext = childLeftTuple.getRightParentNext();
                childLeftTuple.reAddLeft();
                childLeftTuple = childNext;
            }
        }
    }
}
Also used : RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory)

Example 4 with RightTuple

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

the class RuleNetworkEvaluator method doUpdatesExistentialReorderRightMemory.

public static void doUpdatesExistentialReorderRightMemory(BetaMemory bm, BetaNode betaNode, TupleSets<RightTuple> srcRightTuples) {
    TupleMemory rtm = bm.getRightTupleMemory();
    boolean resumeFromCurrent = !(betaNode.isIndexedUnificationJoin() || rtm.getIndexType().isComparison());
    if (rtm.getIndexType() != TupleMemory.IndexType.NONE) {
        for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
            rtm.remove(rightTuple);
        }
    }
    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
        if (rightTuple.getMemory() != null) {
            rightTuple.setTempRightTupleMemory(rightTuple.getMemory());
            if (resumeFromCurrent) {
                if (rightTuple.getBlocked() != null) {
                    // look for a non-staged right tuple first forward ...
                    RightTuple tempRightTuple = (RightTuple) rightTuple.getNext();
                    while (tempRightTuple != null && tempRightTuple.getStagedType() != LeftTuple.NONE) {
                        // next cannot be an updated or deleted rightTuple
                        tempRightTuple = (RightTuple) tempRightTuple.getNext();
                    }
                    // ... and if cannot find one try backward
                    if (tempRightTuple == null) {
                        tempRightTuple = (RightTuple) rightTuple.getPrevious();
                        while (tempRightTuple != null && tempRightTuple.getStagedType() != LeftTuple.NONE) {
                            // next cannot be an updated or deleted rightTuple
                            tempRightTuple = (RightTuple) tempRightTuple.getPrevious();
                        }
                    }
                    rightTuple.setTempNextRightTuple(tempRightTuple);
                }
            }
            rightTuple.setTempBlocked(rightTuple.getBlocked());
            rightTuple.setBlocked(null);
            rtm.remove(rightTuple);
        }
    }
    for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
        rtm.add(rightTuple);
        if (resumeFromCurrent) {
            RightTuple tempRightTuple = rightTuple.getTempNextRightTuple();
            if (rightTuple.getBlocked() != null && tempRightTuple == null && rightTuple.getMemory() == rightTuple.getTempRightTupleMemory()) {
                // the next RightTuple was null, but current RightTuple was added back into the same bucket, so reset as root blocker to re-match can be attempted
                rightTuple.setTempNextRightTuple(rightTuple);
            }
        }
        for (LeftTuple childLeftTuple = rightTuple.getFirstChild(); childLeftTuple != null; ) {
            LeftTuple childNext = childLeftTuple.getRightParentNext();
            childLeftTuple.reAddLeft();
            childLeftTuple = childNext;
        }
    }
    if (rtm.getIndexType() != TupleMemory.IndexType.NONE) {
        for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
            rtm.add(rightTuple);
        }
    }
}
Also used : RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory)

Example 5 with RightTuple

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

the class RuleNetworkEvaluator method findLeftTupleBlocker.

public static void findLeftTupleBlocker(BetaNode betaNode, TupleMemory rtm, ContextEntry[] contextEntry, BetaConstraints constraints, LeftTuple leftTuple, boolean useLeftMemory) {
    // This method will also remove rightTuples that are from subnetwork where no leftmemory use used
    FastIterator it = betaNode.getRightIterator(rtm);
    for (RightTuple rightTuple = betaNode.getFirstRightTuple(leftTuple, rtm, null, it); rightTuple != null; ) {
        RightTuple nextRight = (RightTuple) it.next(rightTuple);
        if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandleForEvaluation())) {
            leftTuple.setBlocker(rightTuple);
            if (useLeftMemory) {
                rightTuple.addBlocked(leftTuple);
                break;
            } else if (betaNode.isRightInputIsRiaNode()) {
                // If we aren't using leftMemory and the right input is a RIAN, then we must iterate and find all subetwork right tuples and remove them
                // so we don't break
                rtm.remove(rightTuple);
            } else {
                break;
            }
        }
        rightTuple = nextRight;
    }
}
Also used : FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple)

Aggregations

RightTuple (org.drools.core.reteoo.RightTuple)60 LeftTuple (org.drools.core.reteoo.LeftTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)30 FastIterator (org.drools.core.util.FastIterator)26 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 BetaMemory (org.drools.core.reteoo.BetaMemory)20 BetaConstraints (org.drools.core.common.BetaConstraints)19 ContextEntry (org.drools.core.rule.ContextEntry)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)13 Test (org.junit.Test)13 Tuple (org.drools.core.spi.Tuple)11 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)9 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)8 BetaNode (org.drools.core.reteoo.BetaNode)7 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)7 ArrayList (java.util.ArrayList)6 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)6 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 Accumulate (org.drools.core.rule.Accumulate)6