use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.
the class RightTupleIndexHashTableIteratorTest method testLastBucketInTheTable.
@Test
public void testLastBucketInTheTable() {
// JBRULES-2574
// setup the entry array with an element in the first bucket, one
// in the middle and one in the last bucket
Entry[] entries = new Entry[10];
entries[0] = mock(TupleList.class);
entries[5] = mock(TupleList.class);
entries[9] = mock(TupleList.class);
RightTuple[] tuples = new RightTuple[] { mock(RightTuple.class), mock(RightTuple.class), mock(RightTuple.class) };
// set return values for methods
when(entries[0].getNext()).thenReturn(null);
when(((TupleList) entries[0]).getFirst()).thenReturn(tuples[0]);
when(entries[5].getNext()).thenReturn(null);
when(((TupleList) entries[5]).getFirst()).thenReturn(tuples[1]);
when(entries[9].getNext()).thenReturn(null);
when(((TupleList) entries[9]).getFirst()).thenReturn(tuples[2]);
// create the mock table for the iterator
AbstractHashTable table = mock(AbstractHashTable.class);
when(table.getTable()).thenReturn(entries);
// create the iterator
FieldIndexHashTableFullIterator iterator = new FieldIndexHashTableFullIterator(table);
// test it
assertThat(iterator.next(), sameInstance((Object) tuples[0]));
assertThat(iterator.next(), sameInstance((Object) tuples[1]));
assertThat(iterator.next(), sameInstance((Object) tuples[2]));
assertThat(iterator.next(), is((Object) null));
}
use of org.drools.core.reteoo.RightTuple 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.reteoo.RightTuple 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.reteoo.RightTuple 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");
}
use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.
the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasLastWithMultipleData.
@Test
public void testRemoveChildLeftTupleThatWasLastWithMultipleData() {
final String[] rules = getRules3Pattern();
final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
try {
final List resultsList = new ArrayList();
kieSession.setGlobal("list", resultsList);
TestUtil.insertFacts(kieSession, 3, 4, 5);
kieSession.fireAllRules();
Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
resultsList.clear();
TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE2_NAME);
kieSession.fireAllRules();
final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
final InternalFactHandle fh2 = (InternalFactHandle) kieSession.getFactHandle(4);
final InternalFactHandle fh3 = (InternalFactHandle) kieSession.getFactHandle(5);
final LeftTuple lt1 = fh1.getFirstLeftTuple();
final LeftTuple lt1_1 = lt1.getFirstChild();
final LeftTuple lt1_2 = lt1_1.getHandleNext();
final LeftTuple lt1_3 = lt1_2.getHandleNext();
assertNotNull(lt1_1);
assertNotNull(lt1_2);
assertNotNull(lt1_3);
assertSame(lt1_3, lt1.getLastChild());
assertSame(lt1_2, lt1_3.getHandlePrevious());
assertSame(lt1_1, lt1_2.getHandlePrevious());
assertEquals(1, lt1_1.getTupleSink().getAssociatedRuleSize());
assertTrue(lt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(lt1_1.getPeer());
assertEquals(1, lt1_2.getTupleSink().getAssociatedRuleSize());
assertTrue(lt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(lt1_2.getPeer());
assertEquals(1, lt1_3.getTupleSink().getAssociatedRuleSize());
assertTrue(lt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(lt1_3.getPeer());
final RightTuple rt1 = fh3.getFirstRightTuple();
final LeftTuple rt1_1 = rt1.getLastChild();
assertSame(lt1_1, rt1_1);
final LeftTuple rt1_2 = rt1_1.getRightParentPrevious();
final LeftTuple rt1_3 = rt1_2.getRightParentPrevious();
assertNotNull(rt1_1);
assertNotNull(rt1_2);
assertNotNull(rt1_3);
assertSame(rt1_2, rt1_3.getRightParentNext());
assertSame(rt1_1, rt1_2.getRightParentNext());
assertEquals(1, rt1_1.getTupleSink().getAssociatedRuleSize());
assertTrue(rt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(rt1_1.getPeer());
assertEquals(1, rt1_2.getTupleSink().getAssociatedRuleSize());
assertTrue(rt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(rt1_2.getPeer());
assertEquals(1, rt1_3.getTupleSink().getAssociatedRuleSize());
assertTrue(rt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
assertNull(rt1_3.getPeer());
} finally {
kieSession.dispose();
}
}
Aggregations