use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class PhreakAccumulateNode method removePreviousMatchesForLeftTuple.
private static void removePreviousMatchesForLeftTuple(final Accumulate accumulate, final LeftTuple leftTuple, final InternalWorkingMemory workingMemory, final AccumulateMemory memory, final AccumulateContext accctx, boolean reInit) {
for (LeftTuple match = leftTuple.getFirstChild(); match != null; ) {
LeftTuple next = match.getHandleNext();
match.unlinkFromRightParent();
match.unlinkFromLeftParent();
match = next;
}
if (reInit) {
// since there are no more matches, the following call will just re-initialize the accumulation
accumulate.init(memory.workingMemoryContext, accctx.context, leftTuple, workingMemory);
}
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class PhreakAccumulateNode method doRightUpdates.
public void doRightUpdates(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
BetaMemory bm = am.getBetaMemory();
TupleMemory ltm = bm.getLeftTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = accNode.getRawConstraints();
Accumulate accumulate = accNode.getAccumulate();
for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
if (ltm != null && ltm.size() > 0) {
LeftTuple childLeftTuple = rightTuple.getFirstChild();
FastIterator leftIt = accNode.getLeftIterator(ltm);
LeftTuple leftTuple = accNode.getFirstLeftTuple(rightTuple, ltm, leftIt);
constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
// We assume a bucket change if leftTuple == null
if (childLeftTuple != null && ltm.isIndexed() && !leftIt.isFullIterator() && (leftTuple == null || (leftTuple.getMemory() != childLeftTuple.getLeftParent().getMemory()))) {
// our index has changed, so delete all the previous matches
removePreviousMatchesForRightTuple(accNode, accumulate, rightTuple, wm, am, childLeftTuple, trgLeftTuples);
// null so the next check will attempt matches for new bucket
childLeftTuple = null;
}
// if LeftTupleMemory is empty, there are no matches to modify
if (leftTuple != null) {
if (leftTuple.getStagedType() == LeftTuple.NONE) {
trgLeftTuples.addUpdate(leftTuple);
}
doRightUpdatesProcessChildren(accNode, am, wm, bm, constraints, accumulate, leftIt, rightTuple, childLeftTuple, leftTuple, trgLeftTuples);
}
}
rightTuple.clearStaged();
rightTuple = next;
}
constraints.resetFactHandle(contextEntry);
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class PhreakAccumulateNode method doNode.
public void doNode(AccumulateNode accNode, LeftTupleSink sink, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
BetaMemory bm = am.getBetaMemory();
TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
// order of left and right operations is to minimise wasted of innefficient joins.
// We need to collect which leftTuple where updated, so that we can
// add their result tuple to the real target tuples later
TupleSets<LeftTuple> tempLeftTuples = new TupleSetsImpl<LeftTuple>();
if (srcLeftTuples.getDeleteFirst() != null) {
// use the real target here, as dealing direct with left tuples
doLeftDeletes(accNode, am, wm, srcLeftTuples, trgLeftTuples, stagedLeftTuples);
}
if (srcRightTuples.getDeleteFirst() != null) {
doRightDeletes(accNode, am, wm, srcRightTuples, tempLeftTuples);
}
if (srcRightTuples.getUpdateFirst() != null) {
RuleNetworkEvaluator.doUpdatesReorderRightMemory(bm, srcRightTuples);
doRightUpdates(accNode, am, wm, srcRightTuples, tempLeftTuples);
}
if (srcLeftTuples.getUpdateFirst() != null) {
RuleNetworkEvaluator.doUpdatesReorderLeftMemory(bm, srcLeftTuples);
doLeftUpdates(accNode, am, wm, srcLeftTuples, tempLeftTuples);
}
if (srcRightTuples.getInsertFirst() != null) {
doRightInserts(accNode, am, wm, srcRightTuples, tempLeftTuples);
}
if (srcLeftTuples.getInsertFirst() != null) {
doLeftInserts(accNode, am, wm, srcLeftTuples, tempLeftTuples);
}
Accumulate accumulate = accNode.getAccumulate();
// LeftTuple retracts are already on the trgLeftTuples
for (LeftTuple leftTuple = tempLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
leftTuple.clearStaged();
leftTuple = next;
}
for (LeftTuple leftTuple = tempLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
evaluateResultConstraints(accNode, sink, accumulate, leftTuple, leftTuple.getPropagationContext(), wm, am, (AccumulateContext) leftTuple.getContextObject(), trgLeftTuples, stagedLeftTuples);
leftTuple.clearStaged();
leftTuple = next;
}
srcRightTuples.resetAll();
srcLeftTuples.resetAll();
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class PhreakAccumulateNode method addMatch.
private static void addMatch(final AccumulateNode accNode, final Accumulate accumulate, final LeftTuple leftTuple, final RightTuple rightTuple, final LeftTuple currentLeftChild, final LeftTuple currentRightChild, final InternalWorkingMemory wm, final AccumulateMemory am, final AccumulateContext accctx, final boolean useLeftMemory) {
LeftTuple tuple = leftTuple;
InternalFactHandle handle = rightTuple.getFactHandle();
if (accNode.isUnwrapRightObject()) {
// if there is a subnetwork, handle must be unwrapped
tuple = (LeftTuple) rightTuple;
handle = rightTuple.getFactHandleForEvaluation();
}
if (handle.isExpired()) {
return;
}
accctx.setPropagationContext(rightTuple.getPropagationContext());
accumulate.accumulate(am.workingMemoryContext, accctx.context, tuple, handle, wm);
// in sequential mode, we don't need to keep record of matched tuples
if (useLeftMemory) {
// linking left and right by creating a new left tuple
accNode.createLeftTuple(leftTuple, rightTuple, currentLeftChild, currentRightChild, accNode, true);
}
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class PhreakAccumulateNode method doRightDeletes.
public void doRightDeletes(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
TupleMemory rtm = am.getBetaMemory().getRightTupleMemory();
Accumulate accumulate = accNode.getAccumulate();
for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
if (rightTuple.getMemory() != null) {
// it may have been staged and never actually added
rtm.remove(rightTuple);
if (rightTuple.getFirstChild() != null) {
LeftTuple match = rightTuple.getFirstChild();
while (match != null) {
LeftTuple nextLeft = match.getRightParentNext();
LeftTuple leftTuple = match.getLeftParent();
final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
removeMatch(accNode, accumulate, rightTuple, match, wm, am, accctx, true);
if (leftTuple.getStagedType() == LeftTuple.NONE) {
trgLeftTuples.addUpdate(leftTuple);
}
match = nextLeft;
}
}
}
rightTuple.clearStaged();
rightTuple = next;
}
}
Aggregations