use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testGlobal1.
@Test
public void testGlobal1() throws Exception {
final String source = "package foo.bar.baz\n" + "import com.foo.Bar\n" + "global java.util.List<java.util.Map<String,Integer>> aList;\n" + "global Integer aNumber";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals("foo.bar.baz", pkg.getName());
assertEquals(1, pkg.getImports().size());
ImportDescr impdescr = pkg.getImports().get(0);
assertEquals("com.foo.Bar", impdescr.getTarget());
assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
assertEquals(2, pkg.getGlobals().size());
GlobalDescr global = pkg.getGlobals().get(0);
assertEquals("java.util.List<java.util.Map<String,Integer>>", global.getType());
assertEquals("aList", global.getIdentifier());
assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
global = pkg.getGlobals().get(1);
assertEquals("Integer", global.getType());
assertEquals("aNumber", global.getIdentifier());
assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testNotWithConstraint.
@Test
public void testNotWithConstraint() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_with_constraint.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(2, rule.getLhs().getDescrs().size());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
final ExprConstraintDescr fieldBinding = (ExprConstraintDescr) pattern.getDescrs().get(0);
assertEquals("$likes:like", fieldBinding.getExpression());
final NotDescr not = (NotDescr) rule.getLhs().getDescrs().get(1);
pattern = (PatternDescr) not.getDescrs().get(0);
final ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("type == $likes", fld.getExpression());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testOrWithSpecialBind.
@Test
public void testOrWithSpecialBind() throws Exception {
String source = "rule \"A and (B or C or D)\" \n" + " when \n" + " pdo1 : ParametricDataObject( paramID == 101, stringValue == \"1000\" ) and \n" + " pdo2 :(ParametricDataObject( paramID == 101, stringValue == \"1001\" ) or \n" + " ParametricDataObject( paramID == 101, stringValue == \"1002\" ) or \n" + " ParametricDataObject( paramID == 101, stringValue == \"1003\" )) \n" + " then \n" + " System.out.println( \"Rule: A and (B or C or D) Fired. pdo1: \" + pdo1 + \" pdo2: \"+ pdo2); \n" + "end\n";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
AndDescr lhs = rule.getLhs();
assertEquals(2, lhs.getDescrs().size());
PatternDescr pdo1 = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("pdo1", pdo1.getIdentifier());
OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
assertEquals(3, or.getDescrs().size());
for (BaseDescr pdo2 : or.getDescrs()) {
assertEquals("pdo2", ((PatternDescr) pdo2).getIdentifier());
}
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testDialect2.
@Test
public void testDialect2() throws Exception {
final String source = "dialect \"mvel\"";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
AttributeDescr attr = pkg.getAttributes().get(0);
assertEquals("dialect", attr.getName());
assertEquals("mvel", attr.getValue());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testGlobal.
@Test
public void testGlobal() throws Exception {
PackageDescr pack = (PackageDescr) parseResource("compilationUnit", "globals.drl");
assertEquals(1, pack.getRules().size());
final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
assertEquals(1, pack.getImports().size());
assertEquals(2, pack.getGlobals().size());
final GlobalDescr foo = (GlobalDescr) pack.getGlobals().get(0);
assertEquals("java.lang.String", foo.getType());
assertEquals("foo", foo.getIdentifier());
final GlobalDescr bar = (GlobalDescr) pack.getGlobals().get(1);
assertEquals("java.lang.Integer", bar.getType());
assertEquals("bar", bar.getIdentifier());
}
Aggregations