use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testInOperator.
@Test
public void testInOperator() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "in_operator_test.drl");
assertNotNull(rule);
assertEqualsIgnoreWhitespace("consequence();", (String) rule.getConsequence());
assertEquals("simple_rule", rule.getName());
assertEquals(2, rule.getLhs().getDescrs().size());
// The first pattern, with 2 restrictions on a single field (plus a
// connective)
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertEquals(1, pattern.getConstraint().getDescrs().size());
ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("age > 30 && < 40", fld.getExpression());
// the second col, with 2 fields, the first with 2 restrictions, the
// second field with one
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Vehicle", pattern.getObjectType());
assertEquals(2, pattern.getConstraint().getDescrs().size());
fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("type in ( \"sedan\", \"wagon\" )", fld.getExpression());
// now the second field
fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1);
assertEquals("age < 3", fld.getExpression());
}
use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testSimpleRule.
@Test
public void testSimpleRule() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "simple_rule.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
assertEquals(22, rule.getConsequenceLine());
assertEquals(2, rule.getConsequencePattern());
final AndDescr lhs = rule.getLhs();
assertNotNull(lhs);
assertEquals(3, lhs.getDescrs().size());
// Check first pattern
final PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("foo3", first.getIdentifier());
assertEquals("Bar", first.getObjectType());
assertEquals(1, first.getConstraint().getDescrs().size());
AndDescr fieldAnd = (AndDescr) first.getConstraint();
ExprConstraintDescr constraint = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertNotNull(constraint);
assertEquals("a==3", constraint.getExpression());
// Check second pattern
final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
assertEquals("foo4", second.getIdentifier());
assertEquals("Bar", second.getObjectType());
// no constraints, only a binding
fieldAnd = (AndDescr) second.getConstraint();
assertEquals(1, fieldAnd.getDescrs().size());
final ExprConstraintDescr binding = (ExprConstraintDescr) second.getConstraint().getDescrs().get(0);
assertEquals("a4:a==4", binding.getExpression());
// Check third pattern
final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
assertNull(third.getIdentifier());
assertEquals("Baz", third.getObjectType());
assertEqualsIgnoreWhitespace("if ( a == b ) { " + " assert( foo3 );" + "} else {" + " retract( foo4 );" + "}" + " System.out.println( a4 );", (String) rule.getConsequence());
}
use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testWithPredicate.
@Test
public void testWithPredicate() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_predicate.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
AndDescr and = (AndDescr) col.getConstraint();
assertEquals(2, and.getDescrs().size());
final ExprConstraintDescr field = (ExprConstraintDescr) col.getDescrs().get(0);
final ExprConstraintDescr pred = (ExprConstraintDescr) and.getDescrs().get(1);
assertEquals("$age2:age", field.getExpression());
assertEqualsIgnoreWhitespace("$age2 == $age1+2", pred.getExpression());
}
use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method createRule.
private RuleImpl createRule(final ConditionalElementDescr ceDescr, final KnowledgeBuilderImpl builder, final String consequence) throws Exception {
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
lhs.addDescr((BaseDescr) ceDescr);
final PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
patternDescr.addConstraint(new ExprConstraintDescr("type == \"stilton\" "));
ceDescr.addDescr(patternDescr);
ruleDescr.setConsequence(consequence);
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
assertEquals("rule-1", rule.getName());
return rule;
}
use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testNumbers.
@Test
public void testNumbers() throws Exception {
// test boolean
createLiteralRule(new ExprConstraintDescr("booleanPrimitive == true "));
// test boolean
createLiteralRule(new ExprConstraintDescr("booleanPrimitive == false "));
// test char
createLiteralRule(new ExprConstraintDescr("charPrimitive == 'a' "));
createLiteralRule(new ExprConstraintDescr("charPrimitive == \"a\" "));
// test byte
createLiteralRule(new ExprConstraintDescr("bytePrimitive == 1 "));
createLiteralRule(new ExprConstraintDescr("bytePrimitive == 0 "));
createLiteralRule(new ExprConstraintDescr("bytePrimitive == -1 "));
// test short
createLiteralRule(new ExprConstraintDescr("shortPrimitive == 1 "));
createLiteralRule(new ExprConstraintDescr("shortPrimitive == 0 "));
createLiteralRule(new ExprConstraintDescr("shortPrimitive == -1 "));
// test int
createLiteralRule(new ExprConstraintDescr("intPrimitive == 1"));
createLiteralRule(new ExprConstraintDescr("intPrimitive == 0"));
createLiteralRule(new ExprConstraintDescr("intPrimitive == -1"));
// test long
createLiteralRule(new ExprConstraintDescr("longPrimitive == 1"));
createLiteralRule(new ExprConstraintDescr("longPrimitive == 0"));
createLiteralRule(new ExprConstraintDescr("longPrimitive == -1"));
// test float
createLiteralRule(new ExprConstraintDescr("floatPrimitive == 1.1"));
createLiteralRule(new ExprConstraintDescr("floatPrimitive == 0"));
createLiteralRule(new ExprConstraintDescr("floatPrimitive == -1.1"));
// test double
createLiteralRule(new ExprConstraintDescr("doublePrimitive == 1.1"));
createLiteralRule(new ExprConstraintDescr("doublePrimitive == 0"));
createLiteralRule(new ExprConstraintDescr("doublePrimitive == -1.1"));
}
Aggregations