Search in sources :

Example 6 with RightTuple

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

the class ProtobufOutputMarshaller method writeFromNodeMemory.

@SuppressWarnings("unchecked")
private static ProtobufMessages.NodeMemory writeFromNodeMemory(final int nodeId, final Memory memory) {
    FromMemory fromMemory = (FromMemory) memory;
    if (fromMemory.getBetaMemory().getLeftTupleMemory().size() > 0) {
        ProtobufMessages.NodeMemory.FromNodeMemory.Builder _from = ProtobufMessages.NodeMemory.FromNodeMemory.newBuilder();
        final org.drools.core.util.Iterator<LeftTuple> tupleIter = fromMemory.getBetaMemory().getLeftTupleMemory().iterator();
        for (LeftTuple leftTuple = tupleIter.next(); leftTuple != null; leftTuple = tupleIter.next()) {
            Map<Object, RightTuple> matches = (Map<Object, RightTuple>) leftTuple.getContextObject();
            ProtobufMessages.NodeMemory.FromNodeMemory.FromContext.Builder _context = ProtobufMessages.NodeMemory.FromNodeMemory.FromContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple));
            for (RightTuple rightTuple : matches.values()) {
                FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(rightTuple.getFactHandle().getId()).setRecency(rightTuple.getFactHandle().getRecency()).build();
                _context.addHandle(_handle);
            }
            _from.addContext(_context.build());
        }
        return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.FROM).setFrom(_from.build()).build();
    }
    return null;
}
Also used : 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) RightTuple(org.drools.core.reteoo.RightTuple) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) Map(java.util.Map) ObjectHashMap(org.drools.core.util.ObjectHashMap)

Example 7 with RightTuple

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

the class AddRemoveRule method deleteLeftTuple.

private static void deleteLeftTuple(LeftTuple removingLt, LeftTuple removingLt2, LeftTuple prevLt) {
    // only the first LT in a peer chain is hooked into left and right parents or the FH.
    // If the first LT is being remove, those hooks need to be shifted to the next peer,
    // or nulled if there is no next peer.
    // When there is a subnetwork, it needs to shift to the peer of the next lt.
    // if it is not the first LT in the peer chain, leftParent and rightParent are null.
    // And the previous peer will need to point to the peer after removingLt, or removingLt2 if it exists.
    // is this the first LT in a peer chain chain
    boolean isFirstLt = prevLt == null;
    // if there is a subnetwork, skip to the peer after that
    LeftTuple nextPeerLt = (removingLt2 == null) ? removingLt.getPeer() : removingLt2.getPeer();
    if (!isFirstLt) {
        // This LT is not the first tuple in a peer chain. So just correct the peer chain linked list
        prevLt.setPeer(nextPeerLt);
    } else {
        if (nextPeerLt == null) {
            removingLt.unlinkFromLeftParent();
            removingLt.unlinkFromRightParent();
            return;
        }
        InternalFactHandle fh = removingLt.getFactHandle();
        // This is the first LT in a peer chain. Only this LT is hooked into the left and right parent LT and RT and
        // if it's the root (form the lian) it will be hooked itno the FH.
        LeftTuple leftPrevious = removingLt.getHandlePrevious();
        LeftTuple leftNext = removingLt.getHandleNext();
        LeftTuple rightPrevious = removingLt.getRightParentPrevious();
        LeftTuple rightNext = removingLt.getRightParentNext();
        LeftTuple leftParent = removingLt.getLeftParent();
        RightTuple rightParent = removingLt.getRightParent();
        // This tuple is the first peer and thus is linked into the left parent LT.
        nextPeerLt.setFactHandle(removingLt.getFactHandle());
        // correct the linked list
        if (leftPrevious != null) {
            nextPeerLt.setHandlePrevious(leftPrevious);
            leftPrevious.setHandleNext(nextPeerLt);
        }
        if (leftNext != null) {
            nextPeerLt.setHandleNext(leftNext);
            leftNext.setHandlePrevious(nextPeerLt);
        }
        // correct the linked list
        if (rightPrevious != null) {
            nextPeerLt.setRightParentPrevious(rightPrevious);
            rightPrevious.setRightParentNext(nextPeerLt);
        }
        if (rightNext != null) {
            nextPeerLt.setRightParentNext(rightNext);
            rightNext.setRightParentPrevious(nextPeerLt);
        }
        // correct the parent's first/last references
        if (leftParent != null) {
            nextPeerLt.setLeftParent(leftParent);
            if (leftParent.getFirstChild() == removingLt) {
                leftParent.setFirstChild(nextPeerLt);
            }
            if (leftParent.getLastChild() == removingLt) {
                leftParent.setLastChild(nextPeerLt);
            }
        } else {
            // is the LT for the LIAN, if so we need to process the FH too
            fh.removeLeftTuple(removingLt);
        }
        if (rightParent != null) {
            // This tuple is the first peer and thus is linked into the right parent RT.
            nextPeerLt.setRightParent(rightParent);
            // if nextLT is null, it's ok for parent's reference to be null
            if (rightParent.getFirstChild() == removingLt) {
                // if next peer exists, set it to this
                rightParent.setFirstChild(nextPeerLt);
            }
            if (rightParent.getLastChild() == removingLt) {
                rightParent.setLastChild(nextPeerLt);
            }
        }
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple)

Example 8 with RightTuple

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

the class AddRemoveRule method insertPeerRightTuple.

private static void insertPeerRightTuple(LeftTuple lt, InternalWorkingMemory wm, Rule rule, boolean insert) {
    // There's a shared RightInputAdaterNode, so check if one of its sinks is associated only to the new rule
    LeftTuple prevLt = null;
    RightInputAdapterNode rian = lt.getTupleSink();
    for (ObjectSink sink : rian.getObjectSinkPropagator().getSinks()) {
        if (lt != null) {
            if (prevLt != null && !insert && sink.isAssociatedWith(rule) && sink.getAssociationsSize() == 1) {
                prevLt.setPeer(null);
            }
            prevLt = lt;
            lt = lt.getPeer();
        } else if (insert) {
            BetaMemory bm = (BetaMemory) wm.getNodeMemory((BetaNode) sink);
            prevLt = rian.createPeer(prevLt);
            bm.linkNode((BetaNode) sink, wm);
            bm.getStagedRightTuples().addInsert((RightTuple) prevLt);
        }
    }
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) ObjectSink(org.drools.core.reteoo.ObjectSink) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 9 with RightTuple

use of org.drools.core.reteoo.RightTuple 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 10 with RightTuple

use of org.drools.core.reteoo.RightTuple 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)

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