Search in sources :

Example 11 with TupleMemory

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

the class PhreakJoinNode method doLeftUpdates.

public void doLeftUpdates(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = joinNode.getRawConstraints();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        FastIterator it = joinNode.getRightIterator(rtm);
        RightTuple rightTuple = joinNode.getFirstRightTuple(leftTuple, rtm, null, it);
        // if rightTuple is null, we assume there was a bucket change and that bucket is empty
        if (rtm.isIndexed() && !it.isFullIterator()) {
            // our index has changed, so delete all the previous propagations
            for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; ) {
                LeftTuple nextChild = childLeftTuple.getHandleNext();
                if (rightTuple == null || rightTuple.getMemory() != childLeftTuple.getRightParent().getMemory()) {
                    RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                }
                childLeftTuple = nextChild;
            }
        }
        // we can't do anything if RightTupleMemory is empty
        if (rightTuple != null) {
            doLeftUpdatesProcessChildren(leftTuple.getFirstChild(), leftTuple, rightTuple, stagedLeftTuples, contextEntry, constraints, sink, it, trgLeftTuples);
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 12 with TupleMemory

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

the class PhreakNotNode method doLeftUpdates.

public void doLeftUpdates(NotNode notNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();
    boolean leftUpdateOptimizationAllowed = notNode.isLeftUpdateOptimizationAllowed();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        FastIterator rightIt = notNode.getRightIterator(rtm);
        RightTuple firstRightTuple = notNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
        // If in memory, remove it, because we'll need to add it anyway if it's not blocked, to ensure iteration order
        RightTuple blocker = leftTuple.getBlocker();
        if (blocker == null) {
            if (leftTuple.getMemory() != null) {
                // memory can be null, if blocker was deleted in same do loop
                ltm.remove(leftTuple);
            }
        } else {
            // check if we changed bucket
            if (rtm.isIndexed() && !rightIt.isFullIterator()) {
                // if newRightTuple is null, we assume there was a bucket change and that bucket is empty
                if (firstRightTuple == null || firstRightTuple.getMemory() != blocker.getMemory()) {
                    blocker.removeBlocked(leftTuple);
                    blocker = null;
                }
            }
        }
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        if (!leftUpdateOptimizationAllowed && blocker != null) {
            blocker.removeBlocked(leftTuple);
            blocker = null;
        }
        // if we where not blocked before (or changed buckets), or the previous blocker no longer blocks, then find the next blocker
        if (blocker == null || !constraints.isAllowedCachedLeft(contextEntry, blocker.getFactHandleForEvaluation())) {
            if (blocker != null) {
                // remove previous blocker if it exists, as we know it doesn't block any more
                blocker.removeBlocked(leftTuple);
            }
            // find first blocker, because it's a modify, we need to start from the beginning again
            for (RightTuple newBlocker = firstRightTuple; newBlocker != null; newBlocker = (RightTuple) rightIt.next(newBlocker)) {
                if (constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandleForEvaluation())) {
                    leftTuple.setBlocker(newBlocker);
                    newBlocker.addBlocked(leftTuple);
                    break;
                }
            }
            LeftTuple childLeftTuple = leftTuple.getFirstChild();
            if (leftTuple.getBlocker() != null) {
                // blocked
                if (childLeftTuple != null) {
                    // blocked, with previous children, so must have not been previously blocked, so retract
                    // no need to remove, as we removed at the start
                    // to be matched against, as it's now blocked
                    // we have the righttuple, so use it for the pctx
                    childLeftTuple.setPropagationContext(leftTuple.getBlocker().getPropagationContext());
                    RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                }
            // else: it's blocked now and no children so blocked before, thus do nothing
            } else if (childLeftTuple == null) {
                // not blocked, with no children, must have been previously blocked so assert
                insertChildLeftTuple(sink, trgLeftTuples, ltm, leftTuple, leftTuple.getPropagationContext(), true);
            } else {
                updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);
                // not blocked, with children, so wasn't previous blocked and still isn't so modify
                // add to memory so other fact handles can attempt to match
                ltm.add(leftTuple);
                childLeftTuple.reAddLeft();
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 13 with TupleMemory

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

the class PhreakNotNode method doLeftInserts.

public void doLeftInserts(NotNode notNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = notNode.getRawConstraints();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(notNode, leftTuple);
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        // This method will also remove rightTuples that are from subnetwork where no leftmemory use used
        RuleNetworkEvaluator.findLeftTupleBlocker(notNode, rtm, contextEntry, constraints, leftTuple, useLeftMemory);
        if (leftTuple.getBlocker() == null) {
            insertChildLeftTuple(sink, trgLeftTuples, ltm, leftTuple, leftTuple.getPropagationContext(), useLeftMemory);
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) 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 14 with TupleMemory

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

the class PhreakActivationIterator method processLeftTuples.

public static void processLeftTuples(LeftTupleSource node, List<AgendaItem> agendaItems, Set<RuleTerminalNode> nodeSet, InternalWorkingMemory wm) {
    LeftTupleSource node1 = node;
    while (NodeTypeEnums.LeftInputAdapterNode != node1.getType()) {
        node1 = node1.getLeftTupleSource();
    }
    int maxShareCount = node1.getAssociationsSize();
    while (NodeTypeEnums.LeftInputAdapterNode != node.getType()) {
        Memory memory = wm.getNodeMemory((MemoryFactory) node);
        if (memory.getSegmentMemory() == null) {
            // segment has never been initialized, which means the rule has never been linked.
            return;
        }
        if (node.getAssociationsSize() == maxShareCount) {
            // the recurse must start from the first split node, otherwise we get partial overlaps in propagations
            if (NodeTypeEnums.isBetaNode(node)) {
                BetaMemory bm;
                if (NodeTypeEnums.AccumulateNode == node.getType()) {
                    AccumulateMemory am = (AccumulateMemory) memory;
                    bm = am.getBetaMemory();
                    FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
                    Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
                    for (; lt != null; lt = (LeftTuple) it.next(lt)) {
                        AccumulateContext accctx = (AccumulateContext) lt.getContextObject();
                        collectFromPeers(accctx.getResultLeftTuple(), agendaItems, nodeSet, wm);
                    }
                } else if (NodeTypeEnums.ExistsNode == node.getType()) {
                    bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
                    // done off the RightTupleMemory, as exists only have unblocked tuples on the left side
                    FastIterator it = bm.getRightTupleMemory().fullFastIterator();
                    RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it);
                    for (; rt != null; rt = (RightTuple) it.next(rt)) {
                        for (LeftTuple lt = rt.getBlocked(); lt != null; lt = lt.getBlockedNext()) {
                            if (lt.getFirstChild() != null) {
                                collectFromPeers(lt.getFirstChild(), agendaItems, nodeSet, wm);
                            }
                        }
                    }
                } else {
                    bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
                    FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
                    Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
                    for (; lt != null; lt = (LeftTuple) it.next(lt)) {
                        if (lt.getFirstChild() != null) {
                            collectFromLeftInput(lt.getFirstChild(), agendaItems, nodeSet, wm);
                        }
                    }
                }
                return;
            } else if (NodeTypeEnums.FromNode == node.getType()) {
                FromMemory fm = (FromMemory) wm.getNodeMemory((MemoryFactory) node);
                TupleMemory ltm = fm.getBetaMemory().getLeftTupleMemory();
                FastIterator it = ltm.fullFastIterator();
                for (LeftTuple lt = (LeftTuple) ltm.getFirst(null); lt != null; lt = (LeftTuple) it.next(lt)) {
                    if (lt.getFirstChild() != null) {
                        collectFromLeftInput(lt.getFirstChild(), agendaItems, nodeSet, wm);
                    }
                }
                return;
            }
        }
        node = node.getLeftTupleSource();
    }
    // No beta or from nodes, so must retrieve LeftTuples from the LiaNode.
    // This is done by scanning all the LeftTuples referenced from the FactHandles in the ObjectTypeNode
    LeftInputAdapterNode lian = (LeftInputAdapterNode) node;
    Memory memory = wm.getNodeMemory((MemoryFactory) node);
    if (memory.getSegmentMemory() == null) {
        // segment has never been initialized, which means the rule has never been linked.
        return;
    }
    ObjectSource os = lian.getObjectSource();
    while (os.getType() != NodeTypeEnums.ObjectTypeNode) {
        os = os.getParentObjectSource();
    }
    ObjectTypeNode otn = (ObjectTypeNode) os;
    final ObjectTypeNodeMemory omem = wm.getNodeMemory(otn);
    LeftTupleSink firstLiaSink = lian.getSinkPropagator().getFirstLeftTupleSink();
    java.util.Iterator<InternalFactHandle> it = omem.iterator();
    while (it.hasNext()) {
        InternalFactHandle fh = it.next();
        fh.forEachLeftTuple(lt -> {
            if (lt.getTupleSink() == firstLiaSink) {
                collectFromLeftInput(lt, agendaItems, nodeSet, wm);
            }
        });
    }
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) TupleMemory(org.drools.core.reteoo.TupleMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ObjectSource(org.drools.core.reteoo.ObjectSource) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 15 with TupleMemory

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

the class Scenario method equalsLeftMemory.

public void equalsLeftMemory(List<LeftTuple> leftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    int length = 0;
    for (LeftTuple expectedLeftTuple : leftTuples) {
        FastIterator it = betaNode.getLeftIterator(ltm);
        Tuple actualLeftTuple = null;
        for (actualLeftTuple = BetaNode.getFirstTuple(ltm, it); actualLeftTuple != null; actualLeftTuple = (LeftTuple) it.next(actualLeftTuple)) {
            if (expectedLeftTuple.equals(actualLeftTuple)) {
                length++;
                break;
            }
        }
        if (actualLeftTuple == null) {
            fail("Could not find LeftTuple: " + expectedLeftTuple);
        }
    }
    if (leftTuples.size() != ltm.size()) {
        fail("LeftTuple memory size did not match: " + length);
    }
}
Also used : FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple)

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