use of org.drools.drl.ast.descr.OrDescr 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.OrDescr 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.OrDescr in project drools by kiegroup.
the class RuleParserTest method testOrWithBinding.
/**
* test basic foo : Fact() || Fact() stuff
*/
@Test
public void testOrWithBinding() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(2, rule.getLhs().getDescrs().size());
final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, or.getDescrs().size());
final PatternDescr leftPattern = (PatternDescr) or.getDescrs().get(0);
assertEquals("Person", leftPattern.getObjectType());
assertEquals("foo", leftPattern.getIdentifier());
final PatternDescr rightPattern = (PatternDescr) or.getDescrs().get(1);
assertEquals("Person", rightPattern.getObjectType());
assertEquals("foo", rightPattern.getIdentifier());
final PatternDescr cheeseDescr = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Cheese", cheeseDescr.getObjectType());
assertEquals(null, cheeseDescr.getIdentifier());
assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
use of org.drools.drl.ast.descr.OrDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testOr.
@Test
public void testOr() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final RuleImpl rule = createRule(new OrDescr(), builder, "update(stilton);");
assertLength(0, builder.getErrors().getErrors());
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement or = (GroupElement) lhs.getChildren().get(0);
assertLength(1, or.getChildren());
final Pattern pattern = (Pattern) or.getChildren().get(0);
}
use of org.drools.drl.ast.descr.OrDescr 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());
}
Aggregations