use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testRestrictionsMultiple.
@Test
public void testRestrictionsMultiple() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "restrictions_test.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
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());
AndDescr and = (AndDescr) pattern.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) and.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());
and = (AndDescr) pattern.getConstraint();
fld = (ExprConstraintDescr) and.getDescrs().get(0);
assertEquals("type == \"sedan\" || == \"wagon\"", fld.getExpression());
// now the second field
fld = (ExprConstraintDescr) and.getDescrs().get(1);
assertEquals("age < 3", fld.getExpression());
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testLhsSemicolonDelim.
@Test
public void testLhsSemicolonDelim() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "lhs_semicolon_delim.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
final AndDescr lhs = rule.getLhs();
assertNotNull(lhs);
assertEquals(3, lhs.getDescrs().size());
// System.err.println( lhs.getDescrs() );
// 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());
// LiteralDescr constraint = (LiteralDescr) first.getDescrs().get( 0 );
AndDescr and = (AndDescr) first.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
assertNotNull(fld);
assertEquals("a==3", fld.getExpression());
// Check second pattern
final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
assertEquals("foo4", second.getIdentifier());
assertEquals("Bar", second.getObjectType());
assertEquals(1, second.getDescrs().size());
final ExprConstraintDescr fieldBindingDescr = (ExprConstraintDescr) second.getDescrs().get(0);
assertEquals("a4:a==4", fieldBindingDescr.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.AndDescr in project drools by kiegroup.
the class RuleParserTest method testLineNumberInAST.
@Test
public void testLineNumberInAST() throws Exception {
// also see testSimpleExpander to see how this works with an expander
// (should be the same).
final RuleDescr rule = (RuleDescr) parseResource("rule", "simple_rule.drl");
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());
// Check second pattern
final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
assertEquals("foo4", second.getIdentifier());
assertEquals("Bar", second.getObjectType());
final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
assertEquals("Baz", third.getObjectType());
assertEquals(19, first.getLine());
assertEquals(20, second.getLine());
assertEquals(21, third.getLine());
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testNotExistWithBrackets.
@Test
public void testNotExistWithBrackets() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_exist_with_brackets.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
final AndDescr lhs = rule.getLhs();
assertEquals(2, lhs.getDescrs().size());
final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
assertEquals(1, not.getDescrs().size());
final PatternDescr pattern = (PatternDescr) not.getDescrs().get(0);
assertEquals("Cheese", pattern.getObjectType());
final ExistsDescr ex = (ExistsDescr) lhs.getDescrs().get(1);
assertEquals(1, ex.getDescrs().size());
final PatternDescr exPattern = (PatternDescr) ex.getDescrs().get(0);
assertEquals("Foo", exPattern.getObjectType());
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testOrNesting.
@Test
public void testOrNesting() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_nesting.drl");
assertNotNull(pkg);
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("simple_rule", rule.getName());
assertEquals(1, rule.getLhs().getDescrs().size());
final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, or.getDescrs().size());
final PatternDescr first = (PatternDescr) or.getDescrs().get(0);
assertEquals("Person", first.getObjectType());
final AndDescr and = (AndDescr) or.getDescrs().get(1);
assertEquals(2, and.getDescrs().size());
final PatternDescr left = (PatternDescr) and.getDescrs().get(0);
assertEquals("Person", left.getObjectType());
final PatternDescr right = (PatternDescr) and.getDescrs().get(1);
assertEquals("Cheese", right.getObjectType());
}
Aggregations