use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testAndOrRules.
/**
* Test that explicit "&&", "||" works as expected
*/
@Test
public void testAndOrRules() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "and_or_rule.drl");
assertNotNull(pkg);
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("simple_rule", rule.getName());
// we will have 3 children under the main And node
final AndDescr and = rule.getLhs();
assertEquals(3, and.getDescrs().size());
PatternDescr left = (PatternDescr) and.getDescrs().get(0);
PatternDescr right = (PatternDescr) and.getDescrs().get(1);
assertEquals("Person", left.getObjectType());
assertEquals("Cheese", right.getObjectType());
assertEquals(1, left.getConstraint().getDescrs().size());
ExprConstraintDescr fld = (ExprConstraintDescr) left.getConstraint().getDescrs().get(0);
assertEquals("name == \"mark\"", fld.getExpression());
assertEquals(1, right.getConstraint().getDescrs().size());
fld = (ExprConstraintDescr) right.getConstraint().getDescrs().get(0);
assertEquals("type == \"stilton\"", fld.getExpression());
// now the "||" part
final OrDescr or = (OrDescr) and.getDescrs().get(2);
assertEquals(2, or.getDescrs().size());
left = (PatternDescr) or.getDescrs().get(0);
right = (PatternDescr) or.getDescrs().get(1);
assertEquals("Person", left.getObjectType());
assertEquals("Cheese", right.getObjectType());
assertEquals(1, left.getConstraint().getDescrs().size());
fld = (ExprConstraintDescr) left.getConstraint().getDescrs().get(0);
assertEquals("name == \"mark\"", fld.getExpression());
assertEquals(1, right.getConstraint().getDescrs().size());
fld = (ExprConstraintDescr) right.getConstraint().getDescrs().get(0);
assertEquals("type == \"stilton\"", fld.getExpression());
assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" );", (String) rule.getConsequence());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testRuleOldSyntax1.
@Test
public void testRuleOldSyntax1() throws Exception {
final String source = "rule \"Test\" when ( not $r :LiteralRestriction( operator == Operator.EQUAL ) ) then end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("Test", rule.getName());
assertEquals(1, rule.getLhs().getDescrs().size());
assertEquals(1, ((NotDescr) rule.getLhs().getDescrs().get(0)).getDescrs().size());
NotDescr notDescr = (NotDescr) rule.getLhs().getDescrs().get(0);
PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().get(0);
assertEquals("$r", patternDescr.getIdentifier());
assertEquals(1, patternDescr.getDescrs().size());
ExprConstraintDescr fieldConstraintDescr = (ExprConstraintDescr) patternDescr.getDescrs().get(0);
assertEquals("operator == Operator.EQUAL", fieldConstraintDescr.getExpression());
}
use of org.drools.drl.ast.descr.PackageDescr 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.drl.ast.descr.PackageDescr 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.PackageDescr 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