use of org.drools.compiler.lang.descr.PackageDescr 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.PackageDescr 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.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testTypeDeclarationWithFields.
@Test
public void testTypeDeclarationWithFields() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "declare_type_with_fields.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
List<TypeDeclarationDescr> td = pkg.getTypeDeclarations();
assertEquals(3, td.size());
TypeDeclarationDescr d = td.get(0);
assertEquals("SomeFact", d.getTypeName());
assertEquals(2, d.getFields().size());
assertTrue(d.getFields().containsKey("name"));
assertTrue(d.getFields().containsKey("age"));
TypeFieldDescr f = d.getFields().get("name");
assertEquals("String", f.getPattern().getObjectType());
f = d.getFields().get("age");
assertEquals("Integer", f.getPattern().getObjectType());
d = td.get(1);
assertEquals("AnotherFact", d.getTypeName());
TypeDeclarationDescr type = td.get(2);
assertEquals("Person", type.getTypeName());
assertEquals("fact", type.getAnnotation("role").getValue());
assertEquals("\"Models a person\"", type.getAnnotation("doc").getValue("descr"));
assertEquals("\"Bob\"", type.getAnnotation("doc").getValue("author"));
assertEquals("Calendar.getInstance().getDate()", type.getAnnotation("doc").getValue("date"));
assertEquals(2, type.getFields().size());
TypeFieldDescr field = type.getFields().get("name");
assertEquals("name", field.getFieldName());
assertEquals("String", field.getPattern().getObjectType());
assertEquals("\"John Doe\"", field.getInitExpr());
assertEquals("50", field.getAnnotation("length").getValue("max"));
assertNotNull(field.getAnnotation("key"));
field = type.getFields().get("age");
assertEquals("age", field.getFieldName());
assertEquals("int", field.getPattern().getObjectType());
assertEquals("-1", field.getInitExpr());
assertEquals("0", field.getAnnotation("ranged").getValue("min"));
assertEquals("150", field.getAnnotation("ranged").getValue("max"));
assertEquals("-1", field.getAnnotation("ranged").getValue("unknown"));
}
use of org.drools.compiler.lang.descr.PackageDescr 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.PackageDescr 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