Search in sources :

Example 1 with FastIterator

use of org.drools.core.util.FastIterator 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 FastIterator

use of org.drools.core.util.FastIterator 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)

Example 3 with FastIterator

use of org.drools.core.util.FastIterator in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeBeliefSet.

private static void writeBeliefSet(MarshallerWriteContext context, BeliefSet beliefSet, org.drools.core.marshalling.impl.ProtobufMessages.EqualityKey.Builder _key) throws IOException {
    ProtobufMessages.BeliefSet.Builder _beliefSet = ProtobufMessages.BeliefSet.newBuilder();
    _beliefSet.setHandleId(beliefSet.getFactHandle().getId());
    ObjectMarshallingStrategyStore objectMarshallingStrategyStore = context.objectMarshallingStrategyStore;
    // for ( LinkedListEntry node = (LinkedListEntry) beliefSet.getFirst(); node != null; node = (LinkedListEntry) node.getNext() ) {
    FastIterator it = beliefSet.iterator();
    for (LinkedListEntry node = (LinkedListEntry) beliefSet.getFirst(); node != null; node = (LinkedListEntry) it.next(node)) {
        LogicalDependency belief = (LogicalDependency) node.getObject();
        ProtobufMessages.LogicalDependency.Builder _logicalDependency = ProtobufMessages.LogicalDependency.newBuilder();
        // _belief.setActivation( value )
        LogicalDependency dependency = (LogicalDependency) node.getObject();
        org.drools.core.spi.Activation activation = dependency.getJustifier();
        ProtobufMessages.Activation _activation = ProtobufMessages.Activation.newBuilder().setPackageName(activation.getRule().getPackage()).setRuleName(activation.getRule().getName()).setTuple(PersisterHelper.createTuple(activation.getTuple())).build();
        _logicalDependency.setActivation(_activation);
        if (belief.getObject() != null) {
            ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(belief.getObject());
            Integer index = context.getStrategyIndex(strategy);
            _logicalDependency.setObjectStrategyIndex(index);
            _logicalDependency.setObject(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, belief.getObject())));
        }
        if (belief.getMode() != null) {
            ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(belief.getMode());
            Integer index = context.getStrategyIndex(strategy);
            _logicalDependency.setValueStrategyIndex(index);
            _logicalDependency.setValue(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, belief.getMode())));
        }
        _beliefSet.addLogicalDependency(_logicalDependency.build());
    }
    _key.setBeliefSet(_beliefSet);
}
Also used : ObjectMarshallingStrategyStore(org.kie.api.marshalling.ObjectMarshallingStrategyStore) LinkedListEntry(org.drools.core.util.LinkedListEntry) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) Activation(org.drools.core.spi.Activation) BeliefSet(org.drools.core.beliefsystem.BeliefSet) LogicalDependency(org.drools.core.common.LogicalDependency) FastIterator(org.drools.core.util.FastIterator)

Example 4 with FastIterator

use of org.drools.core.util.FastIterator in project drools by kiegroup.

the class PhreakAccumulateNode method doRightInserts.

public void doRightInserts(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, 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());
    }
    for (RightTuple rightTuple = srcRightTuples.getInsertFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        rtm.add(rightTuple);
        if (ltm != null && ltm.size() > 0) {
            constraints.updateFromFactHandle(contextEntry, wm, 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 AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
                    addMatch(accNode, accumulate, leftTuple, rightTuple, null, null, wm, am, accctx, true);
                    // 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) 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) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) Accumulate(org.drools.core.rule.Accumulate)

Example 5 with FastIterator

use of org.drools.core.util.FastIterator in project drools by kiegroup.

the class PhreakAccumulateNode method doLeftUpdates.

public void doLeftUpdates(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    Accumulate accumulate = accNode.getAccumulate();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = accNode.getRawConstraints();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
        constraints.updateFromTuple(contextEntry, wm, leftTuple);
        FastIterator rightIt = accNode.getRightIterator(rtm);
        RightTuple rightTuple = accNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
        LeftTuple childLeftTuple = leftTuple.getFirstChild();
        // if rightTuple is null, we assume there was a bucket change and that bucket is empty
        if (childLeftTuple != null && rtm.isIndexed() && !rightIt.isFullIterator() && (rightTuple == null || (rightTuple.getMemory() != childLeftTuple.getRightParent().getMemory()))) {
            // our index has changed, so delete all the previous matchings
            removePreviousMatchesForLeftTuple(accumulate, leftTuple, wm, am, accctx, true);
            // null so the next check will attempt matches for new bucket
            childLeftTuple = null;
        }
        // we can't do anything if RightTupleMemory is empty
        if (rightTuple != null) {
            doLeftUpdatesProcessChildren(accNode, am, wm, bm, accumulate, constraints, rightIt, leftTuple, accctx, rightTuple, childLeftTuple);
        }
        leftTuple.clearStaged();
        trgLeftTuples.addUpdate(leftTuple);
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) BetaMemory(org.drools.core.reteoo.BetaMemory) 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) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) Accumulate(org.drools.core.rule.Accumulate)

Aggregations

FastIterator (org.drools.core.util.FastIterator)40 RightTuple (org.drools.core.reteoo.RightTuple)27 LeftTuple (org.drools.core.reteoo.LeftTuple)24 TupleMemory (org.drools.core.reteoo.TupleMemory)21 BetaConstraints (org.drools.core.common.BetaConstraints)18 ContextEntry (org.drools.core.rule.ContextEntry)18 Tuple (org.drools.core.spi.Tuple)12 BetaMemory (org.drools.core.reteoo.BetaMemory)11 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)9 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)8 ArrayList (java.util.ArrayList)5 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)4 ObjectSource (org.drools.core.reteoo.ObjectSource)4 HashMap (java.util.HashMap)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 AccumulateNode (org.drools.core.reteoo.AccumulateNode)3 BetaNode (org.drools.core.reteoo.BetaNode)3