use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulate.
@Test
public void testAccumulate() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate.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("int x = 0 ;", accum.getInitCode());
assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
assertNull(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 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.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateMultiPattern.
@Test
public void testAccumulateMultiPattern() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_multi_pattern.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 AndDescr and = (AndDescr) accum.getInput();
assertEquals(2, and.getDescrs().size());
final PatternDescr person = (PatternDescr) and.getDescrs().get(0);
final PatternDescr cheese = (PatternDescr) and.getDescrs().get(1);
assertEquals("Person", person.getObjectType());
assertEquals("Cheese", cheese.getObjectType());
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testAttributes2.
@Test
public void testAttributes2() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "rule_attributes2.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
List<RuleDescr> rules = pkg.getRules();
assertEquals(3, rules.size());
RuleDescr rule = rules.get(0);
assertEquals("rule1", rule.getName());
Map<String, AttributeDescr> attrs = rule.getAttributes();
assertEquals(2, attrs.size());
AttributeDescr at = (AttributeDescr) attrs.get("salience");
assertEquals("salience", at.getName());
assertEquals("(42)", at.getValue());
at = (AttributeDescr) attrs.get("agenda-group");
assertEquals("agenda-group", at.getName());
assertEquals("my_group", at.getValue());
rule = rules.get(1);
assertEquals("rule2", rule.getName());
attrs = rule.getAttributes();
assertEquals(2, attrs.size());
at = (AttributeDescr) attrs.get("salience");
assertEquals("salience", at.getName());
assertEquals("(Integer.MIN_VALUE)", at.getValue());
at = (AttributeDescr) attrs.get("no-loop");
assertEquals("no-loop", at.getName());
rule = rules.get(2);
assertEquals("rule3", rule.getName());
attrs = rule.getAttributes();
assertEquals(2, attrs.size());
at = (AttributeDescr) attrs.get("enabled");
assertEquals("enabled", at.getName());
assertEquals("(Boolean.TRUE)", at.getValue());
at = (AttributeDescr) attrs.get("activation-group");
assertEquals("activation-group", at.getName());
assertEquals("my_activation_group", at.getValue());
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleParserTest method testCollect.
@Test
public void testCollect() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "collect.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 CollectDescr collect = (CollectDescr) outPattern.getSource();
final PatternDescr pattern = (PatternDescr) collect.getInputPattern();
assertEquals("Person", pattern.getObjectType());
}
Aggregations