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;
}
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;
}
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);
}
}
}
}
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);
}
}
}
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;
}
}
}
Aggregations