use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeQueryElementNodeMemory.
private static ProtobufMessages.NodeMemory writeQueryElementNodeMemory(final int nodeId, final Memory memory, final InternalWorkingMemory wm) {
org.drools.core.util.Iterator<LeftTuple> it = LeftTupleIterator.iterator(wm, ((QueryElementNodeMemory) memory).getNode());
ProtobufMessages.NodeMemory.QueryElementNodeMemory.Builder _query = ProtobufMessages.NodeMemory.QueryElementNodeMemory.newBuilder();
for (LeftTuple leftTuple = it.next(); leftTuple != null; leftTuple = it.next()) {
InternalFactHandle handle = (InternalFactHandle) leftTuple.getContextObject();
FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext.Builder _context = ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setHandle(_handle);
LeftTuple childLeftTuple = leftTuple.getFirstChild();
while (childLeftTuple != null) {
RightTuple rightParent = childLeftTuple.getRightParent();
_context.addResult(ProtobufMessages.FactHandle.newBuilder().setId(rightParent.getFactHandle().getId()).setRecency(rightParent.getFactHandle().getRecency()).build());
while (childLeftTuple != null && childLeftTuple.getRightParent() == rightParent) {
// skip to the next child that has a different right parent
childLeftTuple = childLeftTuple.getHandleNext();
}
}
_query.addContext(_context.build());
}
return _query.getContextCount() > 0 ? ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.QUERY_ELEMENT).setQueryElement(_query.build()).build() : null;
}
use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeRIANodeMemory.
private static ProtobufMessages.NodeMemory writeRIANodeMemory(final int nodeId, final BaseNode node, final NodeMemories memories) {
RightInputAdapterNode riaNode = (RightInputAdapterNode) node;
ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
BetaNode betaNode = (BetaNode) sinks[0];
Memory betaMemory = memories.peekNodeMemory(betaNode);
if (betaMemory == null) {
return null;
}
BetaMemory bm;
if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) betaMemory).getBetaMemory();
} else {
bm = (BetaMemory) betaMemory;
}
// for RIA nodes, we need to store the ID of the created handles
bm.getRightTupleMemory().iterator();
if (bm.getRightTupleMemory().size() > 0) {
ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria = ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder();
final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();
// iterates over all propagated handles and assert them to the new sink
for (RightTuple entry = (RightTuple) it.next(); entry != null; entry = (RightTuple) it.next()) {
LeftTuple leftTuple = entry instanceof LeftTuple ? // with phreak the entry is always both a right and a left tuple
(LeftTuple) entry : // this is necessary only for reteoo
(LeftTuple) entry.getFactHandle().getObject();
InternalFactHandle handle = (InternalFactHandle) leftTuple.getFactHandle();
if (handle == null) {
continue;
}
FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
_ria.addContext(ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setResultHandle(_handle).build());
}
return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.RIA).setRia(_ria.build()).build();
}
return null;
}
use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.
the class AddRemoveRule method unlinkRightTuples.
private static void unlinkRightTuples(RightTuple rightTuple) {
for (RightTuple rt = rightTuple; rt != null; ) {
RightTuple next = rt.getStagedNext();
// this RightTuple could have been already unlinked by the former cycle
if (rt.getFactHandle() != null) {
rt.unlinkFromRightParent();
}
rt = next;
}
}
use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.
the class AddRemoveRule method processLeftTuples.
/**
* Populates the SegmentMemory with staged LeftTuples. If the parent is not a Beta or From node, it iterates up to find the first node with memory. If necessary
* It traverses to the LiaNode's ObjectTypeNode. It then iterates the LeftTuple chains, where an existing LeftTuple is staged
* as delete. Or a new LeftTuple is created and staged as an insert.
*/
private static void processLeftTuples(LeftTupleNode node, InternalWorkingMemory wm, boolean insert, Rule rule) {
// *** if you make a fix here, it most likely needs to be in PhreakActivationIteratorToo ***
// Must iterate up until a node with memory is found, this can be followed to find the LeftTuples
// which provide the potential peer of the tuple being added or removed
Memory memory = wm.getNodeMemories().peekNodeMemory(node);
if (memory == null || memory.getSegmentMemory() == null) {
// segment has never been initialized, which means the rule(s) have never been linked and thus no Tuples to fix
return;
}
SegmentMemory sm = memory.getSegmentMemory();
while (NodeTypeEnums.LeftInputAdapterNode != node.getType()) {
if (NodeTypeEnums.isBetaNode(node)) {
BetaMemory bm;
if (NodeTypeEnums.AccumulateNode == node.getType()) {
AccumulateMemory am = (AccumulateMemory) memory;
bm = am.getBetaMemory();
FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
for (; lt != null; lt = (LeftTuple) it.next(lt)) {
AccumulateContext accctx = (AccumulateContext) lt.getContextObject();
visitChild(accctx.getResultLeftTuple(), insert, wm, rule);
}
} else if (NodeTypeEnums.ExistsNode == node.getType()) {
bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
// done off the RightTupleMemory, as exists only have unblocked tuples on the left side
FastIterator it = bm.getRightTupleMemory().fullFastIterator();
RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it);
for (; rt != null; rt = (RightTuple) it.next(rt)) {
for (LeftTuple lt = rt.getBlocked(); lt != null; lt = lt.getBlockedNext()) {
visitChild(wm, insert, rule, it, lt);
}
}
} else {
bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
visitChild(wm, insert, rule, it, lt);
}
return;
} else if (NodeTypeEnums.FromNode == node.getType()) {
FromMemory fm = (FromMemory) wm.getNodeMemory((MemoryFactory) node);
TupleMemory ltm = fm.getBetaMemory().getLeftTupleMemory();
FastIterator it = ltm.fullFastIterator();
for (LeftTuple lt = (LeftTuple) ltm.getFirst(null); lt != null; lt = (LeftTuple) it.next(lt)) {
visitChild(lt, insert, wm, rule);
}
return;
}
if (sm.getRootNode() == node) {
sm = wm.getNodeMemory((MemoryFactory<Memory>) node.getLeftTupleSource()).getSegmentMemory();
}
node = node.getLeftTupleSource();
}
// No beta or from nodes, so must retrieve LeftTuples from the LiaNode.
// This is done by scanning all the LeftTuples referenced from the FactHandles in the ObjectTypeNode
LeftInputAdapterNode lian = (LeftInputAdapterNode) node;
ObjectSource os = lian.getObjectSource();
while (os.getType() != NodeTypeEnums.ObjectTypeNode) {
os = os.getParentObjectSource();
}
ObjectTypeNode otn = (ObjectTypeNode) os;
final ObjectTypeNodeMemory omem = wm.getNodeMemory(otn);
if (omem == null) {
// no OTN memory yet, i.e. no inserted matching objects, so no Tuples to process
return;
}
Iterator<InternalFactHandle> it = omem.iterator();
while (it.hasNext()) {
InternalFactHandle fh = it.next();
fh.forEachLeftTuple(lt -> {
LeftTuple nextLt = lt.getHandleNext();
// Each lt is for a different lian, skip any lian not associated with the rule. Need to use lt parent (souce) not child to check the lian.
if (lt.getTupleSource().isAssociatedWith(rule)) {
visitChild(lt, insert, wm, rule);
if (lt.getHandlePrevious() != null) {
lt.getHandlePrevious().setHandleNext(nextLt);
}
if (nextLt != null) {
nextLt.setHandlePrevious(lt.getHandlePrevious());
}
}
});
}
}
use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.
the class AddRemoveRule method deleteRightInputData.
private static void deleteRightInputData(LeftTupleSink node, InternalWorkingMemory wm) {
if (wm.getNodeMemories().peekNodeMemory(node) != null) {
BetaNode bn = (BetaNode) node;
BetaMemory bm;
if (bn.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) wm.getNodeMemory(bn)).getBetaMemory();
} else {
bm = (BetaMemory) wm.getNodeMemory(bn);
}
TupleMemory rtm = bm.getRightTupleMemory();
FastIterator it = rtm.fullFastIterator();
for (Tuple rightTuple = BetaNode.getFirstTuple(rtm, it); rightTuple != null; ) {
Tuple next = (Tuple) it.next(rightTuple);
rtm.remove(rightTuple);
rightTuple.unlinkFromRightParent();
rightTuple = next;
}
if (!bm.getStagedRightTuples().isEmpty()) {
bm.setNodeDirtyWithoutNotify();
}
TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
unlinkRightTuples(srcRightTuples.getInsertFirst());
unlinkRightTuples(srcRightTuples.getUpdateFirst());
unlinkRightTuples(srcRightTuples.getDeleteFirst());
deleteFactsFromRightInput(bn, wm);
}
}
Aggregations