use of org.drools.core.reteoo.LeftTupleImpl 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 = 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, ReteEvaluator reteEvaluator, Object context) {
int price1 = previousDeclarations[0].getIntValue(reteEvaluator, tuple.getObject(previousDeclarations[0]));
int price2 = localDeclarations[0].getIntValue(reteEvaluator, 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));
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class LeftTupleIndexHashTableIteratorTest 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);
LeftTupleImpl[] tuples = new LeftTupleImpl[] { mock(LeftTupleImpl.class), mock(LeftTupleImpl.class), mock(LeftTupleImpl.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));
}
Aggregations