use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testForall.
@Test
public void testForall() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "forall.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final ForallDescr forall = (ForallDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, forall.getDescrs().size());
final PatternDescr pattern = forall.getBasePattern();
assertEquals("Person", pattern.getObjectType());
final List<BaseDescr> remaining = forall.getRemainingPatterns();
assertEquals(1, remaining.size());
final PatternDescr cheese = (PatternDescr) remaining.get(0);
assertEquals("Cheese", cheese.getObjectType());
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateReverse.
@Test
public void testAccumulateReverse() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateReverse.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
final AccumulateDescr accum = (AccumulateDescr) out.getSource();
assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
assertEqualsIgnoreWhitespace("x--;", accum.getReverseCode());
assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
assertFalse(accum.isExternalFunction());
final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
assertEquals("Person", pattern.getObjectType());
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testNotWithConstraint.
@Test
public void testNotWithConstraint() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_with_constraint.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(2, rule.getLhs().getDescrs().size());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
final ExprConstraintDescr fieldBinding = (ExprConstraintDescr) pattern.getDescrs().get(0);
assertEquals("$likes:like", fieldBinding.getExpression());
final NotDescr not = (NotDescr) rule.getLhs().getDescrs().get(1);
pattern = (PatternDescr) not.getDescrs().get(0);
final ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("type == $likes", fld.getExpression());
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testOrWithSpecialBind.
@Test
public void testOrWithSpecialBind() throws Exception {
String source = "rule \"A and (B or C or D)\" \n" + " when \n" + " pdo1 : ParametricDataObject( paramID == 101, stringValue == \"1000\" ) and \n" + " pdo2 :(ParametricDataObject( paramID == 101, stringValue == \"1001\" ) or \n" + " ParametricDataObject( paramID == 101, stringValue == \"1002\" ) or \n" + " ParametricDataObject( paramID == 101, stringValue == \"1003\" )) \n" + " then \n" + " System.out.println( \"Rule: A and (B or C or D) Fired. pdo1: \" + pdo1 + \" pdo2: \"+ pdo2); \n" + "end\n";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
AndDescr lhs = rule.getLhs();
assertEquals(2, lhs.getDescrs().size());
PatternDescr pdo1 = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("pdo1", pdo1.getIdentifier());
OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
assertEquals(3, or.getDescrs().size());
for (BaseDescr pdo2 : or.getDescrs()) {
assertEquals("pdo2", ((PatternDescr) pdo2).getIdentifier());
}
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testNestedCEs.
@Test
public void testNestedCEs() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "nested_conditional_elements.drl");
assertNotNull(rule);
final AndDescr root = rule.getLhs();
final NotDescr not1 = (NotDescr) root.getDescrs().get(0);
final AndDescr and1 = (AndDescr) not1.getDescrs().get(0);
final PatternDescr state = (PatternDescr) and1.getDescrs().get(0);
final NotDescr not2 = (NotDescr) and1.getDescrs().get(1);
final AndDescr and2 = (AndDescr) not2.getDescrs().get(0);
final PatternDescr person = (PatternDescr) and2.getDescrs().get(0);
final PatternDescr cheese = (PatternDescr) and2.getDescrs().get(1);
final PatternDescr person2 = (PatternDescr) root.getDescrs().get(1);
final OrDescr or = (OrDescr) root.getDescrs().get(2);
final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(0);
final PatternDescr cheese3 = (PatternDescr) or.getDescrs().get(1);
assertEquals(state.getObjectType(), "State");
assertEquals(person.getObjectType(), "Person");
assertEquals(cheese.getObjectType(), "Cheese");
assertEquals(person2.getObjectType(), "Person");
assertEquals(cheese2.getObjectType(), "Cheese");
assertEquals(cheese3.getObjectType(), "Cheese");
}
Aggregations