use of org.drools.core.common.ReteEvaluator 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.common.ReteEvaluator in project drools by kiegroup.
the class KnowledgeBaseEventSupportTest method setUp.
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
@Before
public void setUp() throws Exception {
kbase = KnowledgeBaseFactory.newKnowledgeBase();
listener1 = new TestRuleBaseListener("(listener-1) ");
listener2 = new TestRuleBaseListener("(listener-2) ");
kbase.addEventListener(listener1);
kbase.addEventListener(listener2);
final RuleImpl rule1 = new RuleImpl("test1");
final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjectType);
ClassFieldAccessorStore store = new ClassFieldAccessorStore();
store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
store.setEagerWire(true);
AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
pattern.addConstraint(constraint);
rule1.addPattern(pattern);
rule1.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
final RuleImpl rule2 = new RuleImpl("test2");
final ClassObjectType cheeseObjectType2 = new ClassObjectType(Cheese.class);
final Pattern pattern2 = new Pattern(0, cheeseObjectType2);
AlphaNodeFieldConstraint constraint2 = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "stilton", useLambdaConstraint);
pattern2.addConstraint(constraint2);
rule2.addPattern(pattern2);
rule2.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools.test1");
pkg.addRule(rule1);
pkg.addRule(rule2);
}
Aggregations