use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testRemove.
@Test
public void testRemove() 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);
assertEquals(0, map.size());
final Cheese stilton1 = new Cheese("stilton", 35);
final InternalFactHandle stiltonHandle1 = new DefaultFactHandle(1, stilton1);
RightTuple stiltonRightTuple1 = new RightTupleImpl(stiltonHandle1, null);
map.add(stiltonRightTuple1);
final Cheese cheddar1 = new Cheese("cheddar", 35);
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
RightTuple cheddarRightTuple1 = new RightTupleImpl(cheddarHandle1, null);
map.add(cheddarRightTuple1);
final Cheese stilton2 = new Cheese("stilton", 81);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
RightTuple stiltonRightTuple2 = new RightTupleImpl(stiltonHandle2, null);
map.add(stiltonRightTuple2);
assertEquals(3, map.size());
assertEquals(2, tablePopulationSize(map));
// cheddar is in its own bucket, which should be removed once empty. We cannot have
// empty FieldIndexEntries in the Map, as they get their value from the first FactEntry.
map.remove(cheddarRightTuple1);
assertEquals(2, map.size());
assertEquals(1, tablePopulationSize(map));
// We remove t he stiltonHandle2, but there is still one more stilton, so size should be the same
map.remove(stiltonRightTuple2);
assertEquals(1, map.size());
assertEquals(1, tablePopulationSize(map));
// No more stiltons, so the table should be empty
map.remove(stiltonRightTuple1);
assertEquals(0, map.size());
assertEquals(0, tablePopulationSize(map));
}
use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testEmptyIterator.
@Test
public void testEmptyIterator() {
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 stilton = new Cheese("stilton", 55);
final InternalFactHandle stiltonHandle = new DefaultFactHandle(2, stilton);
assertNull(map.getFirst(new LeftTupleImpl(stiltonHandle, null, true)));
}
use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class IndexedHashtableIteratorTest method testCanReachAllEntriesInLastTableRowRightTupleIndexHashTable.
@Test
public void testCanReachAllEntriesInLastTableRowRightTupleIndexHashTable() {
// Construct a table with one row, containing one list, containing three entries.
int numEntries = 3;
TupleList[] table = new TupleList[3];
TupleList rtList = new TupleList();
table[0] = rtList;
for (int i = 0; i < numEntries; i++) {
RightTuple rightTuple = new RightTupleImpl();
rightTuple.setMemory(rtList);
rtList.add(rightTuple);
}
rtList = new TupleList();
table[2] = rtList;
for (int i = 0; i < numEntries; i++) {
RightTuple rightTuple = new RightTupleImpl();
rightTuple.setMemory(rtList);
rtList.add(rightTuple);
}
rtList = new TupleList();
table[2].setNext(rtList);
for (int i = 0; i < numEntries; i++) {
RightTuple rightTuple = new RightTupleImpl();
rightTuple.setMemory(rtList);
rtList.add(rightTuple);
}
// test fast
TupleIndexHashTable.FullFastIterator iter = new TupleIndexHashTable.FullFastIterator(table);
List<RightTuple> list = new ArrayList<RightTuple>();
for (RightTuple rightTuple = (RightTuple) iter.next(null); rightTuple != null; rightTuple = (RightTuple) iter.next(rightTuple)) {
// ensure no duplicate
assertFalse(contains(list, rightTuple));
list.add(rightTuple);
}
// test normal
TupleIndexHashTable rthTable = new TupleIndexHashTable();
rthTable.init(table, 3, numEntries * 3);
TupleIndexHashTable.FieldIndexHashTableFullIterator iter2 = new TupleIndexHashTable.FieldIndexHashTableFullIterator(rthTable);
list = new ArrayList<RightTuple>();
for (RightTuple rightTuple = (RightTuple) iter2.next(); rightTuple != null; rightTuple = (RightTuple) iter2.next()) {
// ensure no duplicate
assertFalse(contains(list, rightTuple));
list.add(rightTuple);
}
assertEquals(numEntries * 3, list.size());
}
use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class IndexedHashtableIteratorTest method testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable.
@Test
public void testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable() {
// Construct a table with one row, containing one list, containing three entries.
int numEntries = 3;
TupleList[] table = new TupleList[3];
TupleList rtList = new TupleList();
table[0] = rtList;
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
rtList = new TupleList();
table[2] = rtList;
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
rtList = new TupleList();
table[2].setNext(rtList);
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
// test fast
TupleIndexHashTable.FullFastIterator iter = new TupleIndexHashTable.FullFastIterator(table);
List<LeftTupleImpl> list = new ArrayList<LeftTupleImpl>();
for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter.next(null); leftTuple != null; leftTuple = (LeftTupleImpl) iter.next(leftTuple)) {
// ensure no duplicate
assertFalse(contains(list, leftTuple));
list.add(leftTuple);
}
assertEquals(numEntries * 3, list.size());
// test normal
TupleIndexHashTable lthTable = new TupleIndexHashTable();
lthTable.init(table, 3, numEntries * 3);
TupleIndexHashTable.FieldIndexHashTableFullIterator iter2 = new TupleIndexHashTable.FieldIndexHashTableFullIterator(lthTable);
list = new ArrayList<LeftTupleImpl>();
for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter2.next(); leftTuple != null; leftTuple = (LeftTupleImpl) iter2.next()) {
// ensure no duplicate
assertFalse(contains(list, leftTuple));
list.add(leftTuple);
}
assertEquals(numEntries * 3, list.size());
}
use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.
the class LeftLeftTupleIndexHashTableIteratorTest 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);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) 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.getLeftTupleMemory().add(new LeftTupleImpl(fh1, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh2, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh3, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh4, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh5, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh6, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh7, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh8, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh9, null, true));
TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
// can't create a 0 hashCode, so forcing
TupleList leftTupleList = new TupleList();
leftTupleList.add(new LeftTupleImpl(fh10, null, true));
hashTable.getTable()[0] = leftTupleList;
leftTupleList = new TupleList();
leftTupleList.add(new LeftTupleImpl(fh11, null, true));
leftTupleList.add(new LeftTupleImpl(fh12, null, true));
leftTupleList.add(new LeftTupleImpl(fh13, null, true));
((TupleList) hashTable.getTable()[0]).setNext(leftTupleList);
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());
// This tests the hashcode index allocation. If the rehash function (or any other way hashcodes are computed) changes, these numbers will change.
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.getLeftTupleMemory().iterator();
for (LeftTupleImpl leftTuple = (LeftTupleImpl) it.next(); leftTuple != null; leftTuple = (LeftTupleImpl) it.next()) {
list.add(leftTuple);
}
assertEquals(13, list.size());
}
Aggregations