use of org.drools.core.common.DoubleNonIndexSkipBetaConstraints in project drools by kiegroup.
the class IndexingTest method testFullFastIteratorResume.
@Test(timeout = 10000)
public void testFullFastIteratorResume() {
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();
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);
wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {
@Override
public void rowInserted(final Row row) {
}
@Override
public void rowDeleted(final Row row) {
}
@Override
public void rowUpdated(final Row row) {
}
});
Person p = new Person("x0", 100);
wm.insert(p);
for (int i = 1; i < 100; i++) {
p = new Person("x" + i, 101);
wm.insert(p);
wm.fireAllRules();
}
final List<RightTuple> list = new ArrayList<>(100);
FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), it); rt != null; rt = (RightTuple) it.next(rt)) {
list.add(rt);
}
assertEquals(100, list.size());
// check we can resume from each entry in the list above.
for (int i = 0; i < 100; i++) {
final RightTuple rightTuple = list.get(i);
// resumes from the current rightTuple
it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
int j = i + 1;
for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
assertSame(list.get(j), rt);
j++;
}
}
} finally {
wm.dispose();
}
}
use of org.drools.core.common.DoubleNonIndexSkipBetaConstraints 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();
}
}
Aggregations