use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testRuleParseLhs3.
@Test
public void testRuleParseLhs3() throws Exception {
final String text = "rule X when (or\nnot Person()\n(and Cheese()\nMeat()\nWine())) then end";
AndDescr pattern = ((RuleDescr) parse("rule", text)).getLhs();
assertEquals(1, pattern.getDescrs().size());
OrDescr or = (OrDescr) pattern.getDescrs().get(0);
assertEquals(2, or.getDescrs().size());
NotDescr not = (NotDescr) or.getDescrs().get(0);
AndDescr and = (AndDescr) or.getDescrs().get(1);
assertEquals(1, not.getDescrs().size());
PatternDescr person = (PatternDescr) not.getDescrs().get(0);
assertEquals("Person", person.getObjectType());
assertEquals(3, and.getDescrs().size());
PatternDescr cheese = (PatternDescr) and.getDescrs().get(0);
assertEquals("Cheese", cheese.getObjectType());
PatternDescr meat = (PatternDescr) and.getDescrs().get(1);
assertEquals("Meat", meat.getObjectType());
PatternDescr wine = (PatternDescr) and.getDescrs().get(2);
assertEquals("Wine", wine.getObjectType());
}
use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testRuleWithLHSNesting.
@Test
public void testRuleWithLHSNesting() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_nested_LHS.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
assertEquals("test", rule.getName());
AndDescr lhs = rule.getLhs();
assertNotNull(lhs);
assertEquals(2, lhs.getDescrs().size());
PatternDescr a = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("A", a.getObjectType());
OrDescr or = (OrDescr) lhs.getDescrs().get(1);
assertEquals(3, or.getDescrs().size());
AndDescr and1 = (AndDescr) or.getDescrs().get(0);
assertEquals(2, and1.getDescrs().size());
PatternDescr b = (PatternDescr) and1.getDescrs().get(0);
PatternDescr c = (PatternDescr) and1.getDescrs().get(1);
assertEquals("B", b.getObjectType());
assertEquals("C", c.getObjectType());
AndDescr and2 = (AndDescr) or.getDescrs().get(1);
assertEquals(2, and2.getDescrs().size());
PatternDescr d = (PatternDescr) and2.getDescrs().get(0);
PatternDescr e = (PatternDescr) and2.getDescrs().get(1);
assertEquals("D", d.getObjectType());
assertEquals("E", e.getObjectType());
AndDescr and3 = (AndDescr) or.getDescrs().get(2);
assertEquals(2, and3.getDescrs().size());
PatternDescr f = (PatternDescr) and3.getDescrs().get(0);
PatternDescr g = (PatternDescr) and3.getDescrs().get(1);
assertEquals("F", f.getObjectType());
assertEquals("G", g.getObjectType());
}
use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testRuleExtends.
@Test
public void testRuleExtends() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_Extends.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
assertTrue(rule.getParentName() != null);
assertEquals("rule1", rule.getParentName());
AndDescr lhs = rule.getLhs();
assertNotNull(lhs);
assertEquals(1, lhs.getDescrs().size());
PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("foo", pattern.getObjectType());
assertEquals("$foo", pattern.getIdentifier());
}
use of org.drools.compiler.lang.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.compiler.lang.descr.AndDescr 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());
}
Aggregations