use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testMultipleRules.
@Test
public void testMultipleRules() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "multiple_rules.drl");
final List<RuleDescr> rules = pkg.getRules();
assertEquals(2, rules.size());
final RuleDescr rule0 = rules.get(0);
assertEquals("Like Stilton", rule0.getName());
final RuleDescr rule1 = rules.get(1);
assertEquals("Like Cheddar", rule1.getName());
// checkout the first rule
AndDescr lhs = rule1.getLhs();
assertNotNull(lhs);
assertEquals(1, lhs.getDescrs().size());
assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule0.getConsequence());
// Check first pattern
PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("Cheese", first.getObjectType());
// checkout the second rule
lhs = rule1.getLhs();
assertNotNull(lhs);
assertEquals(1, lhs.getDescrs().size());
assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule1.getConsequence());
// Check first pattern
first = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("Cheese", first.getObjectType());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testEntryPoint2.
@Test
public void testEntryPoint2() throws Exception {
final String text = "rule X when StockTick( symbol==\"ACME\") from entry-point \"StreamA\" then end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals(1, pattern.getDescrs().size());
ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
assertEquals("symbol==\"ACME\"", fcd.getExpression());
assertNotNull(pattern.getSource());
EntryPointDescr entry = (EntryPointDescr) pattern.getSource();
assertEquals("StreamA", entry.getEntryId());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testFromWithInlineListIndex.
@Test
public void testFromWithInlineListIndex() throws Exception {
String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " Number( ) from [1, 2, 3][1] \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());
assertFalse(parser.hasErrors());
PatternDescr number = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("[1, 2, 3][1]", ((FromDescr) number.getSource()).getDataSource().toString());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testExpanderMultipleConstraints.
@Test
public void testExpanderMultipleConstraints() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final PackageDescr pkg = parser.parse(this.getReader("expander_multiple_constraints.dslr"), this.getReader("multiple_constraints.dsl"));
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(2, rule.getLhs().getDescrs().size());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertEquals(2, pattern.getConstraint().getDescrs().size());
assertEquals("age < 42", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0)).getExpression());
assertEquals("location==atlanta", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1)).getExpression());
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Bar", pattern.getObjectType());
assertNotNull((String) rule.getConsequence());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testForallWithFrom.
@Test
public void testForallWithFrom() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "forallwithfrom.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());
assertEquals("$village", ((FromDescr) pattern.getSource()).getDataSource().toString());
final List<BaseDescr> remaining = forall.getRemainingPatterns();
assertEquals(1, remaining.size());
final PatternDescr cheese = (PatternDescr) remaining.get(0);
assertEquals("Cheese", cheese.getObjectType());
assertEquals("$cheesery", ((FromDescr) cheese.getSource()).getDataSource().toString());
}
Aggregations