use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateWithBindings.
@Test
public void testAccumulateWithBindings() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_with_bindings.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
final AccumulateDescr accum = (AccumulateDescr) outPattern.getSource();
assertEqualsIgnoreWhitespace("$counter", outPattern.getIdentifier());
assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
assertEquals("Person", pattern.getObjectType());
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testEndPosition.
@Test
public void testEndPosition() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "test_EndPosition.drl");
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals(21, col.getLine());
assertEquals(23, col.getEndLine());
}
use of org.drools.drl.ast.descr.RuleDescr 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.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testWithEval.
@Test
public void testWithEval() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_eval.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(3, rule.getLhs().getDescrs().size());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Foo", pattern.getObjectType());
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Bar", pattern.getObjectType());
final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs().get(2);
assertEqualsIgnoreWhitespace("abc(\"foo\")", (String) eval.getContent());
assertEqualsIgnoreWhitespace("Kapow", (String) rule.getConsequence());
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testFromWithInlineList.
@Test
public void testFromWithInlineList() throws Exception {
String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " not( Number( ) from [1, 2, 3] ) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n" + " o.addError(\"Invalid customer id\"); \n" + "end \n";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("XYZ", rule.getName());
PatternDescr number = (PatternDescr) ((NotDescr) rule.getLhs().getDescrs().get(1)).getDescrs().get(0);
assertEquals("[1, 2, 3]", ((FromDescr) number.getSource()).getDataSource().toString());
}
Aggregations