Search in sources :

Example 21 with LeftTuple

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

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

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

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

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

the class AddRemoveRule method visitChild.

private static void visitChild(InternalWorkingMemory wm, boolean insert, Rule rule, FastIterator it, Tuple lt) {
    for (; lt != null; lt = (LeftTuple) it.next(lt)) {
        LeftTuple childLt = lt.getFirstChild();
        while (childLt != null) {
            LeftTuple nextLt = childLt.getHandleNext();
            visitChild(childLt, insert, wm, rule);
            childLt = nextLt;
        }
    }
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple)

Aggregations

LeftTuple (org.drools.core.reteoo.LeftTuple)125 RightTuple (org.drools.core.reteoo.RightTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)37 InternalFactHandle (org.drools.core.common.InternalFactHandle)34 FastIterator (org.drools.core.util.FastIterator)22 BetaConstraints (org.drools.core.common.BetaConstraints)21 BetaMemory (org.drools.core.reteoo.BetaMemory)21 ContextEntry (org.drools.core.rule.ContextEntry)20 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)16 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)13 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 Test (org.junit.Test)13 KieSession (org.kie.api.runtime.KieSession)13 Tuple (org.drools.core.spi.Tuple)12 WorkingMemory (org.drools.core.WorkingMemory)11 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)11 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)10 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 ArrayList (java.util.ArrayList)8