use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class PhreakAccumulateNode method evaluateResultConstraints.
private void evaluateResultConstraints(final AccumulateNode accNode, final LeftTupleSink sink, final Accumulate accumulate, final LeftTuple leftTuple, final PropagationContext context, final InternalWorkingMemory workingMemory, final AccumulateMemory memory, final AccumulateContext accctx, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
// get the actual result
Object result = accumulate.getResult(memory.workingMemoryContext, accctx.context, leftTuple, workingMemory);
if (result == null) {
return;
}
if (accctx.getResultFactHandle() == null) {
final InternalFactHandle handle = accNode.createResultFactHandle(context, workingMemory, leftTuple, result);
accctx.setResultFactHandle(handle);
accctx.setResultLeftTuple(sink.createLeftTuple(handle, leftTuple, sink));
} else {
accctx.getResultFactHandle().setObject(result);
}
// First alpha node filters
AlphaNodeFieldConstraint[] resultConstraints = accNode.getResultConstraints();
BetaConstraints resultBinder = accNode.getResultBinder();
boolean isAllowed = true;
for (AlphaNodeFieldConstraint resultConstraint : resultConstraints) {
if (!resultConstraint.isAllowed(accctx.resultFactHandle, workingMemory)) {
isAllowed = false;
break;
}
}
if (isAllowed) {
resultBinder.updateFromTuple(memory.resultsContext, workingMemory, leftTuple);
if (!resultBinder.isAllowedCachedLeft(memory.resultsContext, accctx.getResultFactHandle())) {
isAllowed = false;
}
resultBinder.resetTuple(memory.resultsContext);
}
LeftTuple childLeftTuple = accctx.getResultLeftTuple();
if (accctx.getPropagationContext() != null) {
childLeftTuple.setPropagationContext(accctx.getPropagationContext());
accctx.setPropagationContext(null);
} else {
childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
}
if (accctx.propagated) {
normalizeStagedTuples(stagedLeftTuples, childLeftTuple);
if (isAllowed) {
// modify
trgLeftTuples.addUpdate(childLeftTuple);
} else {
// retract
trgLeftTuples.addDelete(childLeftTuple);
accctx.propagated = false;
}
} else if (isAllowed) {
// assert
trgLeftTuples.addInsert(childLeftTuple);
accctx.propagated = true;
}
}
use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class PhreakFromNode method doLeftInserts.
public void doLeftInserts(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
BetaMemory bm = fm.getBetaMemory();
ContextEntry[] context = bm.getContext();
BetaConstraints betaConstraints = fromNode.getBetaConstraints();
AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
DataProvider dataProvider = fromNode.getDataProvider();
Class<?> resultClass = fromNode.getResultClass();
for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
PropagationContext propagationContext = leftTuple.getPropagationContext();
Map<Object, RightTuple> matches = null;
boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(fromNode, leftTuple);
if (useLeftMemory) {
fm.getBetaMemory().getLeftTupleMemory().add(leftTuple);
matches = new LinkedHashMap<Object, RightTuple>();
leftTuple.setContextObject(matches);
}
betaConstraints.updateFromTuple(context, wm, leftTuple);
for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple, wm, propagationContext, fm.providerContext); it.hasNext(); ) {
final Object object = it.next();
if ((object == null) || !resultClass.isAssignableFrom(object.getClass())) {
// skip anything if it not assignable
continue;
}
RightTuple rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, useLeftMemory, trgLeftTuples, null);
if (useLeftMemory) {
fromNode.addToCreatedHandlesMap(matches, rightTuple);
}
}
leftTuple.clearStaged();
leftTuple = next;
}
betaConstraints.resetTuple(context);
}
use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class PhreakJoinNode method doLeftInserts.
public void doLeftInserts(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
TupleMemory rtm = bm.getRightTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = joinNode.getRawConstraints();
for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(joinNode, leftTuple);
if (useLeftMemory) {
ltm.add(leftTuple);
}
FastIterator it = joinNode.getRightIterator(rtm);
constraints.updateFromTuple(contextEntry, wm, leftTuple);
for (RightTuple rightTuple = joinNode.getFirstRightTuple(leftTuple, rtm, null, it); rightTuple != null; rightTuple = (RightTuple) it.next(rightTuple)) {
if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandle())) {
insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, null, sink, useLeftMemory);
}
}
leftTuple.clearStaged();
leftTuple = next;
}
constraints.resetTuple(contextEntry);
}
use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class PhreakJoinNode method doRightInserts.
public void doRightInserts(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
TupleMemory rtm = bm.getRightTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = joinNode.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) {
FastIterator it = joinNode.getLeftIterator(ltm);
constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
for (LeftTuple leftTuple = joinNode.getFirstLeftTuple(rightTuple, ltm, it); leftTuple != null; leftTuple = (LeftTuple) it.next(leftTuple)) {
if (leftTuple.getStagedType() == LeftTuple.UPDATE) {
// ignore, as it will get processed via left iteration. Children cannot be processed twice
continue;
}
if (constraints.isAllowedCachedRight(contextEntry, leftTuple)) {
insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, null, sink, true);
}
}
}
rightTuple.clearStaged();
rightTuple = next;
}
constraints.resetFactHandle(contextEntry);
}
use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class PhreakJoinNode method doRightUpdates.
public void doRightUpdates(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = joinNode.getRawConstraints();
for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
if (ltm != null && ltm.size() > 0) {
FastIterator it = joinNode.getLeftIterator(ltm);
LeftTuple leftTuple = joinNode.getFirstLeftTuple(rightTuple, ltm, it);
constraints.updateFromFactHandle(contextEntry, wm, rightTuple.getFactHandleForEvaluation());
// first check our index (for indexed nodes only) hasn't changed and we are returning the same bucket
// We assume a bucket change if leftTuple == null
LeftTuple childLeftTuple = rightTuple.getFirstChild();
if (childLeftTuple != null && ltm.isIndexed() && !it.isFullIterator() && (leftTuple == null || (leftTuple.getMemory() != childLeftTuple.getLeftParent().getMemory()))) {
// our index has changed, so delete all the previous propagations
while (childLeftTuple != null) {
childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
LeftTuple nextChild = childLeftTuple.getRightParentNext();
RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
childLeftTuple = nextChild;
}
// childLeftTuple is now null, so the next check will attempt matches for new bucket
}
// we can't do anything if LeftTupleMemory is empty
if (leftTuple != null) {
doRightUpdatesProcessChildren(childLeftTuple, leftTuple, rightTuple, stagedLeftTuples, contextEntry, constraints, sink, it, trgLeftTuples);
}
}
rightTuple.clearStaged();
rightTuple = next;
}
constraints.resetFactHandle(contextEntry);
}
Aggregations