use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testEnumeration.
@Test
public void testEnumeration() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "enumeration.drl");
assertEquals("simple_rule", rule.getName());
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Foo", col.getObjectType());
assertEquals(1, col.getConstraint().getDescrs().size());
final ExprConstraintDescr fld = (ExprConstraintDescr) col.getConstraint().getDescrs().get(0);
assertEquals("bar == Foo.BAR", fld.getExpression());
}
use of org.drools.compiler.lang.descr.PatternDescr 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.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testFromFollowedByQuery.
@Test
public void testFromFollowedByQuery() throws Exception {
// the 'from' expression requires a ";" to disambiguate the "?"
// prefix for queries from the ternary operator "? :"
final String text = "rule X when Cheese() from $cheesery ?person( \"Mark\", 42; ) then end";
RuleDescr rule = (RuleDescr) parse("rule", text);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Cheese", pattern.getObjectType());
assertEquals("from $cheesery", pattern.getSource().getText());
assertFalse(pattern.isQuery());
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("person", pattern.getObjectType());
assertTrue(pattern.isQuery());
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testSimpleConstraint.
@Test
public void testSimpleConstraint() throws Exception {
String source = "package com.sample rule test when Cheese( type == 'stilton', price > 10 ) then end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertEquals("com.sample", pkg.getName());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("test", rule.getName());
assertEquals(1, rule.getLhs().getDescrs().size());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
AndDescr constraint = (AndDescr) pattern.getConstraint();
assertEquals(2, constraint.getDescrs().size());
assertEquals("type == \"stilton\"", constraint.getDescrs().get(0).toString());
assertEquals("price > 10", constraint.getDescrs().get(1).toString());
}
use of org.drools.compiler.lang.descr.PatternDescr 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());
}
Aggregations