use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class LeftTupleIterator method getNextLeftTuple.
public LeftTuple getNextLeftTuple(LeftTupleSource source, LeftTupleSink sink, LeftTuple leftTuple, InternalWorkingMemory wm) {
if (otnIterator != null) {
LeftTuple leftParent = leftTuple.getLeftParent();
while (leftTuple != null) {
leftTuple = leftTuple.getHandleNext();
for (; leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
// Iterate to find the next left tuple for this sink, skip tuples for other sinks due to sharing split
if (leftTuple.getTupleSink() == sink) {
return leftTuple;
}
}
}
// We have a parent LeftTuple so try there next
if (leftParent != null) {
// we know it has to be evalNode query element node
while (leftParent != null) {
leftParent = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftParent, wm);
if (leftParent != null) {
for (leftTuple = leftParent.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
if (leftTuple.getTupleSink() == sink) {
return leftTuple;
}
}
}
}
return null;
}
// We have exhausted the current FactHandle, now try the next
while (otnIterator.hasNext()) {
InternalFactHandle handle = otnIterator.next();
leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
if (leftTuple != null) {
return leftTuple;
}
}
// We've exhausted this OTN so set the iterator to null
otnIterator = null;
} else if (source instanceof AccumulateNode) {
// when using phreak, accumulate result tuples will not link to leftParent, but to parent instead
BetaMemory memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator(leftTuple.getParent());
LeftTuple childLeftTuple = leftTuple;
leftTuple = childLeftTuple.getParent();
while (leftTuple != null) {
if (childLeftTuple == null) {
childLeftTuple = leftTuple.getFirstChild();
} else {
childLeftTuple = childLeftTuple.getHandleNext();
}
for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
if (childLeftTuple.getTupleSink() == sink) {
return childLeftTuple;
}
}
leftTuple = (LeftTuple) localIt.next(leftTuple);
}
} else if (source instanceof JoinNode || source instanceof NotNode || source instanceof FromNode || source instanceof AccumulateNode) {
BetaMemory memory;
FastIterator localIt;
if (source instanceof FromNode) {
memory = ((FromMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
} else if (source instanceof AccumulateNode) {
memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
} else {
memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
}
localIt = memory.getLeftTupleMemory().fullFastIterator(leftTuple.getLeftParent());
LeftTuple childLeftTuple = leftTuple;
leftTuple = childLeftTuple.getLeftParent();
while (leftTuple != null) {
if (childLeftTuple == null) {
childLeftTuple = leftTuple.getFirstChild();
} else {
childLeftTuple = childLeftTuple.getHandleNext();
}
for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
if (childLeftTuple.getTupleSink() == sink) {
return childLeftTuple;
}
}
leftTuple = (LeftTuple) localIt.next(leftTuple);
}
}
if (source instanceof ExistsNode) {
BetaMemory memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
if (leftTuple != null) {
RightTuple rightTuple = leftTuple.getLeftParent().getBlocker();
FastIterator localIt = memory.getRightTupleMemory().fullFastIterator(rightTuple);
for (LeftTuple childleftTuple = leftTuple.getHandleNext(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
if (childleftTuple.getTupleSink() == sink) {
return childleftTuple;
}
}
leftTuple = leftTuple.getLeftParent();
// now move onto next RightTuple
while (rightTuple != null) {
if (rightTuple.getBlocked() != null) {
if (leftTuple != null) {
leftTuple = leftTuple.getBlockedNext();
} else {
leftTuple = rightTuple.getBlocked();
}
for (; leftTuple != null; leftTuple = leftTuple.getBlockedNext()) {
for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
if (childleftTuple.getTupleSink() == sink) {
return childleftTuple;
}
}
}
}
rightTuple = (RightTuple) localIt.next(rightTuple);
}
}
} else if (source instanceof EvalConditionNode || source instanceof QueryElementNode) {
LeftTuple childLeftTuple = leftTuple;
if (leftTuple != null) {
leftTuple = leftTuple.getLeftParent();
while (leftTuple != null) {
if (childLeftTuple != null) {
childLeftTuple = childLeftTuple.getHandleNext();
} else {
childLeftTuple = leftTuple.getFirstChild();
}
for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
if (childLeftTuple.getTupleSink() == sink) {
return childLeftTuple;
}
}
leftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftTuple, wm);
}
}
}
return null;
}
use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class IndexingTest method testIndexingOnQueryUnificationWithNot.
@Test(timeout = 10000)
public void testIndexingOnQueryUnificationWithNot() {
final String drl = "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "query peeps( String $name, int $age ) \n" + " not $p2 : Person( $name := name, age > $age ) \n" + "end\n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("indexing-test", kieBaseTestConfiguration, drl);
final StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
ReteDumper.dumpRete(wm);
try {
final List<ObjectTypeNode> nodes = ((RuleBase) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (final ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
node = n;
break;
}
}
assertNotNull(node);
final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
final LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
final NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
final DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
assertTrue(c.isIndexed());
final BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
final Map<String, Integer> map = new HashMap<>();
map.put("inserted", 0);
map.put("deleted", 0);
map.put("updated", 0);
wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {
@Override
public void rowInserted(final Row row) {
final Integer integer = map.get("inserted");
map.put("inserted", integer + 1);
}
@Override
public void rowDeleted(final Row row) {
final Integer integer = map.get("deleted");
map.put("deleted", integer + 1);
}
@Override
public void rowUpdated(final Row row) {
final Integer integer = map.get("updated");
map.put("updated", integer + 1);
}
});
final Map<String, InternalFactHandle> peeps = new HashMap<>();
Person p;
InternalFactHandle fh;
final int max = 3;
// 1 matched, prior to any insertions
assertInsertedUpdatedDeleted(map, 1, 0, 0);
// x0 is the blocker
for (int i = 0; i < max; i++) {
p = new Person("x" + i, 100);
fh = (InternalFactHandle) wm.insert(p);
wm.fireAllRules();
peeps.put(p.getName(), fh);
}
// insertions case 1 deletion
assertInsertedUpdatedDeleted(map, 1, 0, 1);
// each x is blocker in turn up to x99
for (int i = 0; i < (max - 1); i++) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// no change
assertInsertedUpdatedDeleted(map, 1, 0, 1);
// x99 is still the blocker, everything else is just added
for (int i = 0; i < (max - 1); i++) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(102);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// no change
assertInsertedUpdatedDeleted(map, 1, 0, 1);
// x99 is still the blocker
for (int i = (max - 2); i >= 0; i--) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// move x99, should no longer be a blocker, now it can increase
fh = peeps.get("x" + (max - 1));
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
assertEquals(2, map.get("inserted").intValue());
} finally {
wm.dispose();
}
}
use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class SegmentCreationTest method tesSubnetworkAfterShare.
@Test
public void tesSubnetworkAfterShare() throws Exception {
KieBase kbase = buildKnowledgeBase(" X() A() \n", " X() A() not ( B() and C() ) \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
ObjectTypeNode dotn = getObjectTypeNode(kbase, LinkingTest.X.class);
LeftInputAdapterNode lian = (LeftInputAdapterNode) dotn.getObjectSinkPropagator().getSinks()[0];
JoinNode joinNode = (JoinNode) aotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) joinNode.getSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) joinNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[0];
NotNode notNode = (NotNode) joinNode.getSinkPropagator().getSinks()[2];
RuleTerminalNode rtn2 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];
wm.insert(new LinkingTest.X());
wm.insert(new LinkingTest.A());
wm.insert(new LinkingTest.B());
wm.insert(new LinkingTest.C());
wm.fireAllRules();
BetaMemory liaMem = (BetaMemory) wm.getNodeMemory(joinNode);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(lian, smem.getRootNode());
assertEquals(joinNode, smem.getTipNode());
SegmentMemory rtnSmem1 = smem.getFirst();
assertEquals(rtn1, rtnSmem1.getRootNode());
assertEquals(rtn1, rtnSmem1.getTipNode());
SegmentMemory bSmem = rtnSmem1.getNext();
assertEquals(bNode, bSmem.getRootNode());
assertEquals(riaNode, bSmem.getTipNode());
SegmentMemory notSmem = bSmem.getNext();
assertEquals(notNode, notSmem.getRootNode());
assertEquals(rtn2, notSmem.getTipNode());
// child segment is not yet initialised, so null
assertNull(bSmem.getFirst());
}
use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class SegmentCreationTest method tesShareInSubnetwork.
@Test
public void tesShareInSubnetwork() throws Exception {
KieBase kbase = buildKnowledgeBase(" X() A() \n", " X() A() B() C() \n", " X() A() not ( B() and C() ) \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
ObjectTypeNode dotn = getObjectTypeNode(kbase, LinkingTest.X.class);
LeftInputAdapterNode lian = (LeftInputAdapterNode) dotn.getObjectSinkPropagator().getSinks()[0];
JoinNode beta = (JoinNode) aotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) beta.getSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) beta.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[1];
NotNode notNode = (NotNode) beta.getSinkPropagator().getSinks()[2];
RuleTerminalNode rtn3 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];
wm.insert(new LinkingTest.X());
wm.insert(new LinkingTest.A());
wm.insert(new LinkingTest.B());
wm.insert(new LinkingTest.C());
wm.flushPropagations();
// LiaNode is in it's own segment
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(lian);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(lian, smem.getRootNode());
assertEquals(beta, smem.getTipNode());
// segment is not initialized yet
assertNull(smem.getFirst());
wm.fireAllRules();
SegmentMemory rtnSmem1 = smem.getFirst();
assertEquals(rtn1, rtnSmem1.getRootNode());
assertEquals(rtn1, rtnSmem1.getTipNode());
SegmentMemory bSmem = rtnSmem1.getNext();
assertEquals(bNode, bSmem.getRootNode());
assertEquals(cNode, bSmem.getTipNode());
SegmentMemory rtn2Smem = bSmem.getFirst();
assertEquals(rtn2, rtn2Smem.getRootNode());
assertEquals(rtn2, rtn2Smem.getTipNode());
SegmentMemory riaSmem = rtn2Smem.getNext();
assertEquals(riaNode, riaSmem.getRootNode());
assertEquals(riaNode, riaSmem.getTipNode());
SegmentMemory notSmem = bSmem.getNext();
assertEquals(notNode, notSmem.getRootNode());
assertEquals(rtn3, notSmem.getTipNode());
}
Aggregations