Search in sources :

Example 1 with AccumulateContext

use of org.drools.core.reteoo.AccumulateNode.AccumulateContext in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeAccumulateNodeMemory.

private static ProtobufMessages.NodeMemory writeAccumulateNodeMemory(final int nodeId, final Memory memory) {
    // for accumulate nodes, we need to store the ID of created (result) handles
    AccumulateMemory accmem = (AccumulateMemory) memory;
    if (accmem.getBetaMemory().getLeftTupleMemory().size() > 0) {
        ProtobufMessages.NodeMemory.AccumulateNodeMemory.Builder _accumulate = ProtobufMessages.NodeMemory.AccumulateNodeMemory.newBuilder();
        final org.drools.core.util.Iterator<LeftTuple> tupleIter = accmem.getBetaMemory().getLeftTupleMemory().iterator();
        for (LeftTuple leftTuple = tupleIter.next(); leftTuple != null; leftTuple = tupleIter.next()) {
            AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
            if (accctx.getResultFactHandle() != null) {
                FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(accctx.getResultFactHandle().getId()).setRecency(accctx.getResultFactHandle().getRecency()).build();
                _accumulate.addContext(ProtobufMessages.NodeMemory.AccumulateNodeMemory.AccumulateContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setResultHandle(_handle).build());
            }
        }
        return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.ACCUMULATE).setAccumulate(_accumulate.build()).build();
    }
    return null;
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext)

Example 2 with AccumulateContext

use of org.drools.core.reteoo.AccumulateNode.AccumulateContext 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 3 with AccumulateContext

use of org.drools.core.reteoo.AccumulateNode.AccumulateContext 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 4 with AccumulateContext

use of org.drools.core.reteoo.AccumulateNode.AccumulateContext in project drools by kiegroup.

the class PhreakAccumulateNode method doLeftDeletes.

public void doLeftDeletes(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = am.getBetaMemory();
    TupleMemory ltm = bm.getLeftTupleMemory();
    Accumulate accumulate = accNode.getAccumulate();
    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        if (leftTuple.getMemory() != null) {
            // it may have been staged and never actually added
            ltm.remove(leftTuple);
            final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
            leftTuple.setContextObject(null);
            removePreviousMatchesForLeftTuple(accumulate, leftTuple, wm, am, accctx, false);
            if (accctx.propagated) {
                normalizeStagedTuples(stagedLeftTuples, accctx.resultLeftTuple);
                trgLeftTuples.addDelete(accctx.resultLeftTuple);
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : BetaMemory(org.drools.core.reteoo.BetaMemory) 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)

Example 5 with AccumulateContext

use of org.drools.core.reteoo.AccumulateNode.AccumulateContext 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

AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)13 LeftTuple (org.drools.core.reteoo.LeftTuple)12 BetaMemory (org.drools.core.reteoo.BetaMemory)7 RightTuple (org.drools.core.reteoo.RightTuple)7 TupleMemory (org.drools.core.reteoo.TupleMemory)7 FastIterator (org.drools.core.util.FastIterator)7 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 Accumulate (org.drools.core.rule.Accumulate)5 Tuple (org.drools.core.spi.Tuple)4 BetaConstraints (org.drools.core.common.BetaConstraints)3 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)3 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)3 ObjectSource (org.drools.core.reteoo.ObjectSource)3 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)3 ContextEntry (org.drools.core.rule.ContextEntry)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 AccumulateNode (org.drools.core.reteoo.AccumulateNode)2 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)2 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)2 ObjectTypeNodeMemory (org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)2