use of org.drools.mvel.model.Cheese in project drools by kiegroup.
the class FieldConstraintTest method testPrimitiveLiteralConstraint.
/**
* <pre>
*
* Cheese( price == 5 )
*
* </pre>
*/
@Test
public void testPrimitiveLiteralConstraint() {
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
;
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final ClassFieldReader extractor = store.getReader(Cheese.class, "price");
AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheesePriceEqualsConstraint(extractor, 5, useLambdaConstraint);
final Cheese cheddar = new Cheese("cheddar", 5);
final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert(cheddar);
// check constraint
assertTrue(constraint.isAllowed(cheddarHandle, ksession));
final Cheese stilton = new Cheese("stilton", 10);
final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
// check constraint
assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
use of org.drools.mvel.model.Cheese in project drools by kiegroup.
the class BaseBetaConstraintsTest method getCheeseTypeConstraint.
protected BetaNodeFieldConstraint getCheeseTypeConstraint(final String identifier, Operator operator) {
if (useLambdaConstraint) {
Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
Predicate1<Cheese> predicate;
if (operator == Operator.BuiltInOperator.EQUAL.getOperator()) {
predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.areNullSafeEquals(_this.getType(), identifier));
} else if (operator == Operator.BuiltInOperator.NOT_EQUAL.getOperator()) {
predicate = new Predicate1.Impl<Cheese>(_this -> !EvaluationUtil.areNullSafeEquals(_this.getType(), identifier));
} else if (operator == Operator.BuiltInOperator.GREATER.getOperator()) {
predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.greaterThan(_this.getType(), identifier));
} else if (operator == Operator.BuiltInOperator.GREATER_OR_EQUAL.getOperator()) {
predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.greaterOrEqual(_this.getType(), identifier));
} else if (operator == Operator.BuiltInOperator.LESS.getOperator()) {
predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.lessThan(_this.getType(), identifier));
} else if (operator == Operator.BuiltInOperator.GREATER_OR_EQUAL.getOperator()) {
predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.lessOrEqual(_this.getType(), identifier));
} else {
throw new RuntimeException(operator + " is not supported");
}
return LambdaConstraintTestUtil.createLambdaConstraint1(Cheese.class, pattern, predicate, null);
} else {
ClassFieldAccessorStore store = new ClassFieldAccessorStore();
store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
store.setEagerWire(true);
InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(Cheese.class)));
String expression = "type " + operator.getOperatorString() + " " + identifier;
return new MVELConstraintTestUtil(expression, operator.getOperatorString(), declaration, extractor);
}
}
use of org.drools.mvel.model.Cheese in project drools by kiegroup.
the class CompositeObjectSinkAdapterTest method keyForCheeseCharObjectType.
private HashKey keyForCheeseCharObjectType(char c) {
Cheese cheese = new Cheese();
cheese.setCharObjectType(c);
HashKey hashKey = new CompositeObjectSinkAdapter.HashKey();
hashKey.setValue(extractor.getIndex(), cheese, extractor);
return hashKey;
}
use of org.drools.mvel.model.Cheese in project drools by kiegroup.
the class CompositeObjectSinkAdapterTest method testAddThreeAlphasVerifyRangeQuery.
@Test
public void testAddThreeAlphasVerifyRangeQuery() {
extractor = store.getReader(Cheese.class, "price");
final AlphaNode al1 = createAlphaNode(cheesePriceGreaterThan(10));
ad.addObjectSink(al1);
final AlphaNode al2 = createAlphaNode(cheesePriceGreaterThan(20));
ad.addObjectSink(al2);
final AlphaNode al3 = createAlphaNode(cheesePriceGreaterThan(30));
ad.addObjectSink(al3);
// test propagation
CompositeObjectSinkAdapter.FieldIndex fieldIndex = ad.getRangeIndexedFieldIndexes().get(0);
Cheese cheese = new Cheese();
cheese.setPrice(25);
Collection<AlphaNode> matchingAlphaNodes = ad.getRangeIndexMap().get(fieldIndex).getMatchingAlphaNodes(cheese);
assertEquals(2, matchingAlphaNodes.size());
assertTrue(matchingAlphaNodes.contains(al1));
assertTrue(matchingAlphaNodes.contains(al2));
// should not find this one
cheese.setPrice(5);
matchingAlphaNodes = ad.getRangeIndexMap().get(fieldIndex).getMatchingAlphaNodes(cheese);
assertTrue(matchingAlphaNodes.isEmpty());
}
use of org.drools.mvel.model.Cheese in project drools by kiegroup.
the class LambdaConstraintTestUtil method createCheeseTypeEqualsConstraint.
public static LambdaConstraint createCheeseTypeEqualsConstraint(final String rightValue, int indexId) {
// Typical LambdaConstraint used in drools-test-coverage. (type == "xxx")
Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
Predicate1<Cheese> predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.areNullSafeEquals(_this.getType(), rightValue));
AlphaIndexImpl<Cheese, String> index = new AlphaIndexImpl<Cheese, String>(String.class, org.drools.model.Index.ConstraintType.EQUAL, indexId, _this -> _this.getType(), rightValue);
return createLambdaConstraint1(Cheese.class, pattern, predicate, index);
}
Aggregations