use of org.drools.core.rule.Accumulate 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.rule.Accumulate 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.rule.Accumulate 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);
}
use of org.drools.core.rule.Accumulate in project drools by kiegroup.
the class PhreakAccumulateNode method doLeftInserts.
public void doLeftInserts(AccumulateNode accNode, AccumulateMemory am, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, 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();
boolean leftTupleMemoryEnabled = accNode.isLeftTupleMemoryEnabled();
for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
boolean useLeftMemory = leftTupleMemoryEnabled || RuleNetworkEvaluator.useLeftMemory(accNode, leftTuple);
if (useLeftMemory) {
ltm.add(leftTuple);
}
AccumulateContext accresult = new AccumulateContext();
leftTuple.setContextObject(accresult);
accresult.context = accumulate.createContext();
accumulate.init(am.workingMemoryContext, accresult.context, leftTuple, wm);
constraints.updateFromTuple(contextEntry, wm, leftTuple);
FastIterator rightIt = accNode.getRightIterator(rtm);
for (RightTuple rightTuple = accNode.getFirstRightTuple(leftTuple, rtm, null, rightIt); rightTuple != null; ) {
RightTuple nextRightTuple = (RightTuple) rightIt.next(rightTuple);
if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandleForEvaluation())) {
// add a match
addMatch(accNode, accumulate, leftTuple, rightTuple, null, null, wm, am, accresult, useLeftMemory);
if (!useLeftMemory && accNode.isRightInputIsRiaNode()) {
// RIAN with no left memory must have their right tuples removed
rtm.remove(rightTuple);
}
}
rightTuple = nextRightTuple;
}
leftTuple.clearStaged();
trgLeftTuples.addInsert(leftTuple);
constraints.resetTuple(contextEntry);
leftTuple = next;
}
constraints.resetTuple(contextEntry);
}
use of org.drools.core.rule.Accumulate in project drools by kiegroup.
the class JavaAccumulateBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
// build source
BaseDescr input = accumDescr.getInput();
if (input instanceof AndDescr && ((AndDescr) input).getDescrs().size() == 1) {
input = ((AndDescr) input).getDescrs().get(0);
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(input.getClass());
final RuleConditionElement source = builder.build(context, input);
if (source == null) {
return null;
}
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Map<String, Declaration> declsInScope = context.getDeclarationResolver().getDeclarations(context.getRule());
Map<String, Class<?>> declCls = DeclarationScopeResolver.getDeclarationClasses(declsInScope);
Accumulate accumulate;
if (accumDescr.isExternalFunction()) {
// if it uses 1+ external function, build methods for them
accumulate = buildExternalFunctionCall(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
} else {
// if it uses inline code, build the class for it
accumulate = buildInlineAccumulate(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
}
return accumulate;
}
Aggregations