use of org.drools.core.reteoo.RightTupleImpl 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.RightTupleImpl 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.reteoo.RightTupleImpl 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.RightTupleImpl 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.reteoo.RightTupleImpl in project drools by kiegroup.
the class FieldConstraintTest method testPredicateConstraint.
/**
* <pre>
*
* (Cheese (price ?price1 )
* (Cheese (price ?price2&:(= ?price2 (* 2 ?price1) )
*
* </pre>
*/
@Test
public void testPredicateConstraint() {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final InternalReadAccessor priceExtractor = store.getReader(Cheese.class, "price");
Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
// Bind the extractor to a decleration
// Declarations know the pattern they derive their value form
final Declaration price1Declaration = new Declaration("price1", priceExtractor, pattern);
pattern = new Pattern(1, new ClassObjectType(Cheese.class));
// Bind the extractor to a decleration
// Declarations know the pattern they derive their value form
final Declaration price2Declaration = new Declaration("price2", priceExtractor, pattern);
final PredicateExpression evaluator = new PredicateExpression() {
private static final long serialVersionUID = 510l;
public boolean evaluate(InternalFactHandle handle, Tuple tuple, Declaration[] previousDeclarations, Declaration[] localDeclarations, WorkingMemory workingMemory, Object context) {
int price1 = previousDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, tuple.getObject(previousDeclarations[0]));
int price2 = localDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, handle.getObject());
return (price2 == (price1 * 2));
}
public Object createContext() {
return null;
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
};
final PredicateConstraint constraint1 = new PredicateConstraint(evaluator, new Declaration[] { price1Declaration }, new Declaration[] { price2Declaration });
final Cheese cheddar0 = new Cheese("cheddar", 5);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar0);
LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
final Cheese cheddar1 = new Cheese("cheddar", 10);
final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
tuple = new LeftTupleImpl(tuple, new RightTupleImpl(f1, null), null, true);
final PredicateContextEntry context = (PredicateContextEntry) constraint1.createContextEntry();
context.updateFromTuple(ksession, tuple);
assertTrue(constraint1.isAllowedCachedLeft(context, f1));
}
Aggregations