use of org.drools.core.util.AbstractHashTable.FieldIndex 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());
}
use of org.drools.core.util.AbstractHashTable.FieldIndex in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testTwoDifferentEntriesSameHashCode.
@Test
public void testTwoDifferentEntriesSameHashCode() throws Exception {
final InternalReadAccessor extractor = store.getReader(TestClass.class, "object");
final Pattern pattern = new Pattern(0, new ClassObjectType(TestClass.class));
final Declaration declaration = new Declaration("theObject", extractor, pattern);
final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
final TestClass c1 = new TestClass(0, new TestClass(20, "stilton"));
final InternalFactHandle ch1 = new DefaultFactHandle(1, c1);
map.add(new RightTupleImpl(ch1, null));
final TestClass c2 = new TestClass(0, new TestClass(20, "cheddar"));
final InternalFactHandle ch2 = new DefaultFactHandle(2, c2);
map.add(new RightTupleImpl(ch2, null));
// same hashcode, but different values, so it should result in a size of 2
assertEquals(2, map.size());
// however both are in the same table bucket
assertEquals(1, tablePopulationSize(map));
// this table bucket will have two FieldIndexEntries, as they are actually two different values
Entry[] entries = getEntries(map);
assertEquals(1, entries.length);
TupleList list = (TupleList) entries[0];
assertSame(ch2, list.getFirst().getFactHandle());
assertNull(list.getFirst().getNext());
assertSame(ch1, list.getNext().getFirst().getFactHandle());
assertNull(list.getNext().getFirst().getNext());
assertNull(list.getNext().getNext());
}
use of org.drools.core.util.AbstractHashTable.FieldIndex in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testTwoEqualEntries.
@Test
public void testTwoEqualEntries() 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);
map.add(new RightTupleImpl(stiltonHandle1, null));
final Cheese cheddar1 = new Cheese("cheddar", 35);
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
map.add(new RightTupleImpl(cheddarHandle1, null));
final Cheese stilton2 = new Cheese("stilton", 81);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
map.add(new RightTupleImpl(stiltonHandle2, null));
assertEquals(3, map.size());
assertEquals(2, tablePopulationSize(map));
// Check they are correctly chained to the same FieldIndexEntry
final Cheese stilton3 = new Cheese("stilton", 89);
final InternalFactHandle stiltonHandle3 = new DefaultFactHandle(4, stilton2);
final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle3, null, true));
assertSame(stiltonHandle1, tuple.getFactHandle());
assertSame(stiltonHandle2, ((RightTuple) tuple.getNext()).getFactHandle());
}
use of org.drools.core.util.AbstractHashTable.FieldIndex in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testTwoDifferentEntries.
@Test
public void testTwoDifferentEntries() 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);
map.add(new RightTupleImpl(stiltonHandle1, null));
final Cheese cheddar1 = new Cheese("cheddar", 35);
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
map.add(new RightTupleImpl(cheddarHandle1, null));
assertEquals(2, map.size());
assertEquals(2, tablePopulationSize(map));
final Cheese stilton2 = new Cheese("stilton", 77);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
assertSame(stiltonHandle1, tuple.getFactHandle());
assertNull(tuple.getNext());
final Cheese cheddar2 = new Cheese("cheddar", 5);
final InternalFactHandle cheddarHandle2 = new DefaultFactHandle(2, cheddar2);
tuple = map.getFirst(new LeftTupleImpl(cheddarHandle2, null, true));
assertSame(cheddarHandle1, tuple.getFactHandle());
assertNull(tuple.getNext());
}
use of org.drools.core.util.AbstractHashTable.FieldIndex in project drools by kiegroup.
the class FieldIndexEntryTest method testTwoEntries.
@Test
public void testTwoEntries() {
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
final Cheese stilton1 = new Cheese("stilton", 35);
final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
final Cheese stilton2 = new Cheese("stilton", 59);
final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
RightTuple h1RightTuple = new RightTupleImpl(h1, null);
RightTuple h2RightTuple = new RightTupleImpl(h2, null);
// test add
index.add(h1RightTuple);
index.add(h2RightTuple);
assertEquals(h1, index.getFirst().getFactHandle());
assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
// test get
assertEquals(h1, index.get(h1).getFactHandle());
assertEquals(h2, index.get(h2).getFactHandle());
// test removal for combinations
// remove first
index.remove(h2RightTuple);
assertEquals(h1RightTuple.getFactHandle(), index.getFirst().getFactHandle());
// remove second
index.add(h2RightTuple);
index.remove(h1RightTuple);
assertEquals(h2RightTuple.getFactHandle(), index.getFirst().getFactHandle());
// check index type does not change, as this fact is removed
stilton1.setType("cheddar");
}
Aggregations