use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class RuleNetworkEvaluator method doRiaNode2.
private void doRiaNode2(InternalWorkingMemory wm, TupleSets<LeftTuple> srcTuples, RightInputAdapterNode riaNode) {
ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
BetaNode betaNode = (BetaNode) sinks[0];
BetaMemory bm;
Memory nodeMem = wm.getNodeMemory(betaNode);
if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
bm = ((AccumulateMemory) nodeMem).getBetaMemory();
} else {
bm = (BetaMemory) nodeMem;
}
TupleSets<RightTuple> rightTuples = bm.getStagedRightTuples();
// Build up iteration array for other sinks
BetaNode[] bns = null;
BetaMemory[] bms = null;
int length = sinks.length;
if (length > 1) {
bns = new BetaNode[sinks.length - 1];
bms = new BetaMemory[sinks.length - 1];
for (int i = 1; i < length; i++) {
bns[i - 1] = (BetaNode) sinks[i];
Memory nodeMem2 = wm.getNodeMemory(bns[i - 1]);
if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
bms[i - 1] = ((AccumulateMemory) nodeMem2).getBetaMemory();
} else {
bms[i - 1] = (BetaMemory) nodeMem2;
}
}
}
// subtract one, as first is not in the array;
length--;
for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getInsertFirst(); subnetworkTuple != null; ) {
SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
if (bm.getStagedRightTuples().isEmpty()) {
bm.setNodeDirtyWithoutNotify();
}
subnetworkTuple.prepareStagingOnRight();
rightTuples.addInsert(subnetworkTuple);
if (bns != null) {
for (int i = 0; i < length; i++) {
if (bms[i].getStagedRightTuples().isEmpty()) {
bms[i].setNodeDirtyWithoutNotify();
}
subnetworkTuple = riaNode.createPeer(subnetworkTuple);
bms[i].getStagedRightTuples().addInsert(subnetworkTuple);
}
}
subnetworkTuple = next;
}
for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getDeleteFirst(); subnetworkTuple != null; ) {
SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
if (rightTuples.isEmpty()) {
bm.setNodeDirtyWithoutNotify();
}
switch(subnetworkTuple.getStagedTypeOnRight()) {
// handle clash with already staged entries
case Tuple.INSERT:
rightTuples.removeInsert(subnetworkTuple);
break;
case Tuple.UPDATE:
rightTuples.removeUpdate(subnetworkTuple);
break;
}
subnetworkTuple.prepareStagingOnRight();
rightTuples.addDelete(subnetworkTuple);
if (bns != null) {
for (int i = 0; i < length; i++) {
subnetworkTuple = (SubnetworkTuple) subnetworkTuple.getPeer();
if (bms[i].getStagedRightTuples().isEmpty()) {
bms[i].setNodeDirtyWithoutNotify();
}
bms[i].getStagedRightTuples().addDelete(subnetworkTuple);
subnetworkTuple.setStagedOnRight();
}
}
subnetworkTuple = next;
}
for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getUpdateFirst(); subnetworkTuple != null; ) {
SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
if (rightTuples.isEmpty()) {
bm.setNodeDirtyWithoutNotify();
}
subnetworkTuple.prepareStagingOnRight();
rightTuples.addUpdate(subnetworkTuple);
if (bns != null) {
for (int i = 0; i < length; i++) {
subnetworkTuple = (SubnetworkTuple) subnetworkTuple.getPeer();
if (bms[i].getStagedRightTuples().isEmpty()) {
bms[i].setNodeDirtyWithoutNotify();
}
bms[i].getStagedRightTuples().addUpdate(subnetworkTuple);
subnetworkTuple.setStagedOnRight();
}
}
subnetworkTuple = next;
}
srcTuples.resetAll();
}
use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class RuleNetworkEvaluator method evalBetaNode.
private boolean evalBetaNode(PathMemory pmem, NetworkNode node, Memory nodeMem, SegmentMemory[] smems, int smemIndex, TupleSets<LeftTuple> trgTuples, InternalAgenda agenda, LinkedList<StackEntry> stack, boolean processRian, RuleExecutor executor, TupleSets<LeftTuple> srcTuples, TupleSets<LeftTuple> stagedLeftTuples, LeftTupleSinkNode sink) {
BetaNode betaNode = (BetaNode) node;
BetaMemory bm;
AccumulateMemory am = null;
if (NodeTypeEnums.AccumulateNode == node.getType()) {
am = (AccumulateMemory) nodeMem;
bm = am.getBetaMemory();
} else {
bm = (BetaMemory) nodeMem;
}
if (processRian && betaNode.isRightInputIsRiaNode()) {
// if the subnetwork is nested in this segment, it will create srcTuples containing
// peer LeftTuples, suitable for the node in the main path.
doRiaNode(agenda, pmem, srcTuples, betaNode, sink, smems, smemIndex, nodeMem, bm, stack, executor);
// return here, doRiaNode queues the evaluation on the stack, which is necessary to handled nested query nodes
return true;
}
switchOnDoBetaNode(node, trgTuples, agenda.getWorkingMemory(), srcTuples, stagedLeftTuples, sink, bm, am);
return false;
}
use of org.drools.core.reteoo.BetaMemory 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.BetaMemory 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.BetaMemory 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;
}
}
Aggregations