use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class AddRemoveRule method flushLeftTupleIfNecessary.
public static boolean flushLeftTupleIfNecessary(InternalWorkingMemory wm, SegmentMemory sm, LeftTuple leftTuple, boolean streamMode, short stagedType) {
PathMemory pmem = streamMode ? sm.getPathMemories().get(0) : sm.getFirstDataDrivenPathMemory();
if (pmem == null) {
return false;
}
TupleSets<LeftTuple> leftTupleSets = new TupleSetsImpl<LeftTuple>();
if (leftTuple != null) {
switch(stagedType) {
case Tuple.INSERT:
leftTupleSets.addInsert(leftTuple);
break;
case Tuple.DELETE:
leftTupleSets.addDelete(leftTuple);
break;
case Tuple.UPDATE:
leftTupleSets.addUpdate(leftTuple);
break;
}
}
forceFlushLeftTuple(pmem, sm, wm, leftTupleSets);
if (pmem.isDataDriven() && pmem.getNodeType() == NodeTypeEnums.RightInputAdaterNode) {
for (PathEndNode pnode : pmem.getPathEndNode().getPathEndNodes()) {
if (pnode instanceof TerminalNode) {
PathMemory outPmem = wm.getNodeMemory((TerminalNode) pnode);
if (outPmem.isDataDriven()) {
SegmentMemory outSmem = outPmem.getSegmentMemories()[0];
if (outSmem != null) {
forceFlushLeftTuple(outPmem, outSmem, wm, new TupleSetsImpl<LeftTuple>());
}
}
}
}
}
return true;
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class AddRemoveRule method insertPeerLeftTuple.
/**
* Create all missing peers
*/
private static LeftTuple insertPeerLeftTuple(LeftTuple lt, LeftTupleSinkNode node, InternalWorkingMemory wm) {
LeftInputAdapterNode.LiaNodeMemory liaMem = null;
if (node.getLeftTupleSource().getType() == NodeTypeEnums.LeftInputAdapterNode) {
liaMem = wm.getNodeMemory(((LeftInputAdapterNode) node.getLeftTupleSource()));
}
LeftTuple peer = node.createPeer(lt);
Memory memory = wm.getNodeMemories().peekNodeMemory(node);
if (memory == null || memory.getSegmentMemory() == null) {
throw new IllegalStateException("Defensive Programming: this should not be possilbe, as the addRule code should init child segments if they are needed ");
}
if (liaMem == null) {
memory.getSegmentMemory().getStagedLeftTuples().addInsert(peer);
} else {
// If parent is Lian, then this must be called, so that any linking or unlinking can be done.
LeftInputAdapterNode.doInsertSegmentMemory(wm, true, liaMem, memory.getSegmentMemory(), peer, node.getLeftTupleSource().isStreamMode());
}
return peer;
}
use of org.drools.core.reteoo.LeftTuple 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.
}
}
}
use of org.drools.core.reteoo.LeftTuple 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);
}
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class PhreakAccumulateNode method removeMatch.
/**
* Removes a match between left and right tuple
*/
private static void removeMatch(final AccumulateNode accNode, final Accumulate accumulate, final RightTuple rightTuple, final LeftTuple match, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx, final boolean reaccumulate) {
// save the matching tuple
LeftTuple leftTuple = match.getLeftParent();
// removing link between left and right
match.unlinkFromLeftParent();
match.unlinkFromRightParent();
// if there is a subnetwork, we need to unwrap the object from inside the tuple
InternalFactHandle handle = rightTuple.getFactHandle();
LeftTuple tuple = leftTuple;
if (accNode.isUnwrapRightObject()) {
tuple = (LeftTuple) rightTuple;
handle = rightTuple.getFactHandleForEvaluation();
}
if (accumulate.supportsReverse()) {
// just reverse this single match
accumulate.reverse(am.workingMemoryContext, accctx.context, tuple, handle, wm);
} else {
// otherwise need to recalculate all matches for the given leftTuple
if (reaccumulate) {
reaccumulateForLeftTuple(accNode, accumulate, leftTuple, wm, am, accctx);
}
}
}
Aggregations