use of org.drools.core.reteoo.AccumulateNode.AccumulateContext 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.AccumulateNode.AccumulateContext 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.AccumulateNode.AccumulateContext in project drools by kiegroup.
the class PhreakAccumulateNode method doRightInserts.
public void doRightInserts(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
Accumulate accumulate = accNode.getAccumulate();
BetaMemory bm = am.getBetaMemory();
TupleMemory ltm = bm.getLeftTupleMemory();
TupleMemory rtm = bm.getRightTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = accNode.getRawConstraints();
if (srcRightTuples.getInsertSize() > 32 && rtm instanceof AbstractHashTable) {
((AbstractHashTable) rtm).ensureCapacity(srcRightTuples.getInsertSize());
}
for (RightTuple rightTuple = srcRightTuples.getInsertFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
rtm.add(rightTuple);
if (ltm != null && ltm.size() > 0) {
constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
FastIterator leftIt = accNode.getLeftIterator(ltm);
for (LeftTuple leftTuple = accNode.getFirstLeftTuple(rightTuple, ltm, leftIt); leftTuple != null; leftTuple = (LeftTuple) leftIt.next(leftTuple)) {
if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
addMatch(accNode, accumulate, leftTuple, rightTuple, null, null, wm, am, accctx, true);
// so any existing leftTuples we know are updates, but only add if not already added
if (leftTuple.getStagedType() == LeftTuple.NONE) {
trgLeftTuples.addUpdate(leftTuple);
}
}
}
}
rightTuple.clearStaged();
rightTuple = next;
}
constraints.resetFactHandle(contextEntry);
}
use of org.drools.core.reteoo.AccumulateNode.AccumulateContext in project drools by kiegroup.
the class PhreakAccumulateNode method doLeftDeletes.
public void doLeftDeletes(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
BetaMemory bm = am.getBetaMemory();
TupleMemory ltm = bm.getLeftTupleMemory();
Accumulate accumulate = accNode.getAccumulate();
for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
if (leftTuple.getMemory() != null) {
// it may have been staged and never actually added
ltm.remove(leftTuple);
final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
leftTuple.setContextObject(null);
removePreviousMatchesForLeftTuple(accumulate, leftTuple, wm, am, accctx, false);
if (accctx.propagated) {
normalizeStagedTuples(stagedLeftTuples, accctx.resultLeftTuple);
trgLeftTuples.addDelete(accctx.resultLeftTuple);
}
}
leftTuple.clearStaged();
leftTuple = next;
}
}
use of org.drools.core.reteoo.AccumulateNode.AccumulateContext in project drools by kiegroup.
the class PhreakAccumulateNode method doLeftUpdates.
public void doLeftUpdates(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
BetaMemory bm = am.getBetaMemory();
TupleMemory rtm = bm.getRightTupleMemory();
Accumulate accumulate = accNode.getAccumulate();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = accNode.getRawConstraints();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
final AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
constraints.updateFromTuple(contextEntry, wm, leftTuple);
FastIterator rightIt = accNode.getRightIterator(rtm);
RightTuple rightTuple = accNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
LeftTuple childLeftTuple = leftTuple.getFirstChild();
// if rightTuple is null, we assume there was a bucket change and that bucket is empty
if (childLeftTuple != null && rtm.isIndexed() && !rightIt.isFullIterator() && (rightTuple == null || (rightTuple.getMemory() != childLeftTuple.getRightParent().getMemory()))) {
// our index has changed, so delete all the previous matchings
removePreviousMatchesForLeftTuple(accumulate, leftTuple, wm, am, accctx, true);
// null so the next check will attempt matches for new bucket
childLeftTuple = null;
}
// we can't do anything if RightTupleMemory is empty
if (rightTuple != null) {
doLeftUpdatesProcessChildren(accNode, am, wm, bm, accumulate, constraints, rightIt, leftTuple, accctx, rightTuple, childLeftTuple);
}
leftTuple.clearStaged();
trgLeftTuples.addUpdate(leftTuple);
leftTuple = next;
}
constraints.resetTuple(contextEntry);
}
Aggregations