use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class PhreakJoinNode method doLeftUpdates.
public void doLeftUpdates(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TupleMemory rtm = bm.getRightTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = joinNode.getRawConstraints();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
constraints.updateFromTuple(contextEntry, wm, leftTuple);
FastIterator it = joinNode.getRightIterator(rtm);
RightTuple rightTuple = joinNode.getFirstRightTuple(leftTuple, rtm, null, it);
// if rightTuple is null, we assume there was a bucket change and that bucket is empty
if (rtm.isIndexed() && !it.isFullIterator()) {
// our index has changed, so delete all the previous propagations
for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; ) {
LeftTuple nextChild = childLeftTuple.getHandleNext();
if (rightTuple == null || rightTuple.getMemory() != childLeftTuple.getRightParent().getMemory()) {
RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
}
childLeftTuple = nextChild;
}
}
// we can't do anything if RightTupleMemory is empty
if (rightTuple != null) {
doLeftUpdatesProcessChildren(leftTuple.getFirstChild(), leftTuple, rightTuple, stagedLeftTuples, contextEntry, constraints, sink, it, trgLeftTuples);
}
leftTuple.clearStaged();
leftTuple = next;
}
constraints.resetTuple(contextEntry);
}
use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class PhreakNotNode method doLeftUpdates.
public void doLeftUpdates(NotNode notNode, LeftTupleSink sink, BetaMemory bm, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
TupleMemory rtm = bm.getRightTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = notNode.getRawConstraints();
boolean leftUpdateOptimizationAllowed = notNode.isLeftUpdateOptimizationAllowed();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
FastIterator rightIt = notNode.getRightIterator(rtm);
RightTuple firstRightTuple = notNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
// If in memory, remove it, because we'll need to add it anyway if it's not blocked, to ensure iteration order
RightTuple blocker = leftTuple.getBlocker();
if (blocker == null) {
if (leftTuple.getMemory() != null) {
// memory can be null, if blocker was deleted in same do loop
ltm.remove(leftTuple);
}
} else {
// check if we changed bucket
if (rtm.isIndexed() && !rightIt.isFullIterator()) {
// if newRightTuple is null, we assume there was a bucket change and that bucket is empty
if (firstRightTuple == null || firstRightTuple.getMemory() != blocker.getMemory()) {
blocker.removeBlocked(leftTuple);
blocker = null;
}
}
}
constraints.updateFromTuple(contextEntry, wm, leftTuple);
if (!leftUpdateOptimizationAllowed && blocker != null) {
blocker.removeBlocked(leftTuple);
blocker = null;
}
// if we where not blocked before (or changed buckets), or the previous blocker no longer blocks, then find the next blocker
if (blocker == null || !constraints.isAllowedCachedLeft(contextEntry, blocker.getFactHandleForEvaluation())) {
if (blocker != null) {
// remove previous blocker if it exists, as we know it doesn't block any more
blocker.removeBlocked(leftTuple);
}
// find first blocker, because it's a modify, we need to start from the beginning again
for (RightTuple newBlocker = firstRightTuple; newBlocker != null; newBlocker = (RightTuple) rightIt.next(newBlocker)) {
if (constraints.isAllowedCachedLeft(contextEntry, newBlocker.getFactHandleForEvaluation())) {
leftTuple.setBlocker(newBlocker);
newBlocker.addBlocked(leftTuple);
break;
}
}
LeftTuple childLeftTuple = leftTuple.getFirstChild();
if (leftTuple.getBlocker() != null) {
// blocked
if (childLeftTuple != null) {
// blocked, with previous children, so must have not been previously blocked, so retract
// no need to remove, as we removed at the start
// to be matched against, as it's now blocked
// we have the righttuple, so use it for the pctx
childLeftTuple.setPropagationContext(leftTuple.getBlocker().getPropagationContext());
RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
}
// else: it's blocked now and no children so blocked before, thus do nothing
} else if (childLeftTuple == null) {
// not blocked, with no children, must have been previously blocked so assert
insertChildLeftTuple(sink, trgLeftTuples, ltm, leftTuple, leftTuple.getPropagationContext(), true);
} else {
updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);
// not blocked, with children, so wasn't previous blocked and still isn't so modify
// add to memory so other fact handles can attempt to match
ltm.add(leftTuple);
childLeftTuple.reAddLeft();
}
}
leftTuple.clearStaged();
leftTuple = next;
}
constraints.resetTuple(contextEntry);
}
use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class PhreakNotNode method doLeftInserts.
public void doLeftInserts(NotNode notNode, 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 = notNode.getRawConstraints();
for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(notNode, leftTuple);
constraints.updateFromTuple(contextEntry, wm, leftTuple);
// This method will also remove rightTuples that are from subnetwork where no leftmemory use used
RuleNetworkEvaluator.findLeftTupleBlocker(notNode, rtm, contextEntry, constraints, leftTuple, useLeftMemory);
if (leftTuple.getBlocker() == null) {
insertChildLeftTuple(sink, trgLeftTuples, ltm, leftTuple, leftTuple.getPropagationContext(), useLeftMemory);
}
leftTuple.clearStaged();
leftTuple = next;
}
constraints.resetTuple(contextEntry);
}
use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class BetaNodeBuilder method build.
public BetaNode build() {
NodeFactory nFactory = buildContext.getComponentFactory().getNodeFactoryService();
EntryPointNode epn = buildContext.getKnowledgeBase().getRete().getEntryPointNodes().values().iterator().next();
ObjectTypeNode otn = nFactory.buildObjectTypeNode(buildContext.getNextId(), epn, new ClassObjectType(leftType), buildContext);
LeftInputAdapterNode leftInput = nFactory.buildLeftInputAdapterNode(buildContext.getNextId(), otn, buildContext);
ObjectSource rightInput = nFactory.buildObjectTypeNode(buildContext.getNextId(), epn, new ClassObjectType(rightType), buildContext);
ReteTesterHelper reteTesterHelper = new ReteTesterHelper();
Pattern pattern = new Pattern(0, new ClassObjectType(leftType));
// BetaNodeFieldConstraint betaConstraint = null;
BetaConstraints betaConstraints = null;
if (constraintFieldName != null) {
ClassFieldAccessorStore store = (ClassFieldAccessorStore) reteTesterHelper.getStore();
InternalReadAccessor extractor = store.getReader(leftType, leftFieldName);
Declaration declr = new Declaration(leftVariableName, extractor, pattern);
betaConstraints = new SingleBetaConstraints(reteTesterHelper.getBoundVariableConstraint(rightType, constraintFieldName, declr, constraintOperator), buildContext.getKnowledgeBase().getConfiguration());
} else {
betaConstraints = new EmptyBetaConstraints();
}
switch(nodeType) {
case NodeTypeEnums.JoinNode:
return new JoinNode(0, leftInput, rightInput, betaConstraints, buildContext);
case NodeTypeEnums.NotNode:
return new NotNode(0, leftInput, rightInput, betaConstraints, buildContext);
case NodeTypeEnums.ExistsNode:
return new ExistsNode(0, leftInput, rightInput, betaConstraints, buildContext);
}
throw new IllegalStateException("Unable to build Node");
}
use of org.drools.core.common.BetaConstraints in project drools by kiegroup.
the class RightTupleIndexHashTableIteratorTest method test1.
@Test
public void test1() {
BetaNodeFieldConstraint constraint0 = getConstraint("d", Operator.EQUAL, "this", Foo.class);
BetaNodeFieldConstraint[] constraints = new BetaNodeFieldConstraint[] { constraint0 };
RuleBaseConfiguration config = new RuleBaseConfiguration();
BetaConstraints betaConstraints = null;
betaConstraints = new SingleBetaConstraints(constraints, config);
BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
KieBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ss = kBase.newKieSession();
InternalFactHandle fh1 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
InternalFactHandle fh2 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
InternalFactHandle fh3 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
InternalFactHandle fh4 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
InternalFactHandle fh5 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
InternalFactHandle fh6 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
InternalFactHandle fh7 = (InternalFactHandle) ss.insert(new Foo("cream", 3));
InternalFactHandle fh8 = (InternalFactHandle) ss.insert(new Foo("gorda", 15));
InternalFactHandle fh9 = (InternalFactHandle) ss.insert(new Foo("beer", 16));
InternalFactHandle fh10 = (InternalFactHandle) ss.insert(new Foo("mars", 0));
InternalFactHandle fh11 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
InternalFactHandle fh12 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
InternalFactHandle fh13 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh1, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh2, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh3, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh4, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh5, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh6, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh7, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh8, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh9, null));
TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
// can't create a 0 hashCode, so forcing
TupleList rightTupleList = new TupleList();
rightTupleList.add(new RightTupleImpl(fh10, null));
hashTable.getTable()[0] = rightTupleList;
rightTupleList = new TupleList();
rightTupleList.add(new RightTupleImpl(fh11, null));
rightTupleList.add(new RightTupleImpl(fh12, null));
rightTupleList.add(new RightTupleImpl(fh13, null));
((TupleList) hashTable.getTable()[0]).setNext(rightTupleList);
Entry[] table = hashTable.getTable();
List list = new ArrayList();
for (int i = 0; i < table.length; i++) {
if (table[i] != null) {
List entries = new ArrayList();
entries.add(i);
Entry entry = table[i];
while (entry != null) {
entries.add(entry);
entry = entry.getNext();
}
list.add(entries.toArray());
}
}
assertEquals(5, list.size());
Object[] entries = (Object[]) list.get(0);
assertEquals(0, entries[0]);
assertEquals(3, entries.length);
entries = (Object[]) list.get(1);
assertEquals(102, entries[0]);
assertEquals(2, entries.length);
entries = (Object[]) list.get(2);
assertEquals(103, entries[0]);
assertEquals(2, entries.length);
entries = (Object[]) list.get(3);
assertEquals(115, entries[0]);
assertEquals(3, entries.length);
entries = (Object[]) list.get(4);
assertEquals(117, entries[0]);
assertEquals(3, entries.length);
// System.out.println( entries );
list = new ArrayList<LeftTupleImpl>();
Iterator it = betaMemory.getRightTupleMemory().iterator();
for (RightTuple rightTuple = (RightTuple) it.next(); rightTuple != null; rightTuple = (RightTuple) it.next()) {
list.add(rightTuple);
}
assertEquals(13, list.size());
}
Aggregations