use of org.drools.drl.ast.descr.PatternDescr 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.PatternDescr 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.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testCheckOrDescr.
@Test
public void testCheckOrDescr() throws Exception {
final String text = "rule X when Person( eval( age == 25 ) || ( eval( name.equals( \"bob\" ) ) && eval( age == 30 ) ) ) then end";
PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
assertEquals(1, pattern.getDescrs().size());
assertEquals(pattern.getConstraint().getClass(), AndDescr.class);
assertEquals(ExprConstraintDescr.class, pattern.getConstraint().getDescrs().get(0).getClass());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testOrCE.
@Test
public void testOrCE() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_ce.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(2, rule.getLhs().getDescrs().size());
final PatternDescr person = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Person", person.getObjectType());
assertEquals("$p", person.getIdentifier());
final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
assertEquals(2, or.getDescrs().size());
final PatternDescr cheese1 = (PatternDescr) or.getDescrs().get(0);
assertEquals("Cheese", cheese1.getObjectType());
assertEquals("$c", cheese1.getIdentifier());
final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(1);
assertEquals("Cheese", cheese2.getObjectType());
assertNull(cheese2.getIdentifier());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testQualifiedClassname.
@Test
public void testQualifiedClassname() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "qualified_classname.drl");
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
final PatternDescr p = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("com.cheeseco.Cheese", p.getObjectType());
}
Aggregations