use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class IndexingTest method testFullFastIteratorResume.
@Test(timeout = 10000)
public void testFullFastIteratorResume() throws Exception {
String str = "";
str += "package org.drools.compiler.test \n";
str += "import org.drools.compiler.Person \n";
str += "query peeps( String $name, int $age ) \n";
str += " not $p2 : Person( $name := name, age > $age ) \n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
node = n;
break;
}
}
StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
// assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
System.out.println(bm.getLeftTupleMemory().getClass());
System.out.println(bm.getRightTupleMemory().getClass());
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
final Map<String, Integer> map = new HashMap<String, Integer>();
map.put("inserted", new Integer(0));
map.put("deleted", new Integer(0));
map.put("updated", new Integer(0));
wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {
@Override
public void rowInserted(Row row) {
}
@Override
public void rowDeleted(Row row) {
}
@Override
public void rowUpdated(Row row) {
}
});
Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
Person p = new Person("x0", 100);
InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
peeps.put(p.getName(), fh);
for (int i = 1; i < 100; i++) {
p = new Person("x" + i, 101);
fh = (InternalFactHandle) wm.insert(p);
wm.fireAllRules();
peeps.put(p.getName(), fh);
}
List<RightTuple> list = new ArrayList<RightTuple>(100);
FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, 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++) {
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++;
}
}
}
use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class IndexingTest method testIndexingOnQueryUnification.
@Test(timeout = 10000)
public void testIndexingOnQueryUnification() throws Exception {
String str = "";
str += "package org.drools.compiler.test \n";
str += "import org.drools.compiler.Person \n";
str += "query peeps( String $name, String $likes, String $street) \n";
str += " $p : Person( $name := name, $likes := likes, $street := address.street ) \n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
node = n;
break;
}
}
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
// $p2
JoinNode j = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
TripleNonIndexSkipBetaConstraints c = (TripleNonIndexSkipBetaConstraints) j.getRawConstraints();
// assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(j);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
}
use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class IndexingTest method testBuildsIndexedMemory.
@Test(timeout = 10000)
public void testBuildsIndexedMemory() {
// tests indexes are correctly built
String drl = "";
drl += "package org.drools.compiler.test\n";
drl += "import org.drools.compiler.Person\n";
drl += "global java.util.List list\n";
drl += "rule test1\n";
drl += "when\n";
drl += " $p1 : Person($name : name )\n";
// indexed
drl += " $p2 : Person(name == $name)\n";
// indexed
drl += " $p3 : Person(name == $p1.name)\n";
// not indexed
drl += " $p4 : Person(address.street == $p1.address.street)\n";
// indexed
drl += " $p5 : Person(address.street == $p1.name)\n";
// drl += " $p6 : Person( $name == name)\n"; // not indexed and won't compile
// indexed
drl += " $p7 : Person(addresses[\"key\"].street == $p1.name)\n";
// indexed
drl += " $p8 : Person(addresses[0].street == $p1.name)\n";
// not indexed
drl += " $p9 : Person(name == $p1.address.street)\n";
// indexed
drl += " $p10 : Person(addresses[0].street + 'xx' == $p1.name)\n";
// not indexed
drl += " $p11 : Person(addresses[$p1].street == $p1.name)\n";
drl += "then\n";
drl += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(drl);
ObjectTypeNode node = getObjectTypeNode(kbase, Person.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
// $p2
JoinNode j2 = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
// $p3
JoinNode j3 = (JoinNode) j2.getSinkPropagator().getSinks()[0];
// $p4
JoinNode j4 = (JoinNode) j3.getSinkPropagator().getSinks()[0];
// $p5
JoinNode j5 = (JoinNode) j4.getSinkPropagator().getSinks()[0];
// JoinNode j6 = ( JoinNode ) j5.getSinkPropagator().getSinks()[0]; // $p6 // won't compile
// $p7
JoinNode j7 = (JoinNode) j5.getSinkPropagator().getSinks()[0];
// $p8
JoinNode j8 = (JoinNode) j7.getSinkPropagator().getSinks()[0];
// $p9
JoinNode j9 = (JoinNode) j8.getSinkPropagator().getSinks()[0];
// $p10
JoinNode j10 = (JoinNode) j9.getSinkPropagator().getSinks()[0];
// $p11
JoinNode j11 = (JoinNode) j10.getSinkPropagator().getSinks()[0];
SingleBetaConstraints c = (SingleBetaConstraints) j2.getRawConstraints();
assertEquals("$name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(j2);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j3.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j3);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j4.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j4);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
c = (SingleBetaConstraints) j5.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j5);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
// won't compile
// c = ( SingleBetaConstraints ) j6.getRawConstraints();
// assertEquals( "name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
// assertFalse( c.isIndexed() );
// bm = ( BetaMemory ) wm.getNodeMemory( j6 );
// assertTrue( bm.getLeftTupleMemory() instanceof LeftTupleList );
// assertTrue( bm.getRightTupleMemory() instanceof RightTupleList );
c = (SingleBetaConstraints) j7.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j7);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j8.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j8);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j9.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j9);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
c = (SingleBetaConstraints) j10.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j10);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j11.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j11);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
}
use of org.drools.core.util.index.TupleIndexHashTable 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());
}
use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testSingleEntry.
@Test
public void testSingleEntry() throws Exception {
final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(0, cheddar);
assertEquals(0, map.size());
assertNull(map.getFirst(new LeftTupleImpl(cheddarHandle1, null, true)));
final Cheese stilton1 = new Cheese("stilton", 35);
RightTuple stiltonRighTuple = new RightTupleImpl(new DefaultFactHandle(1, stilton1), null);
map.add(stiltonRighTuple);
assertEquals(1, map.size());
assertEquals(1, tablePopulationSize(map));
final Cheese stilton2 = new Cheese("stilton", 80);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
assertSame(stiltonRighTuple.getFactHandle(), tuple.getFactHandle());
assertNull(tuple.getNext());
}
Aggregations