use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testWithEval.
@Test
public void testWithEval() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_eval.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(3, rule.getLhs().getDescrs().size());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Foo", pattern.getObjectType());
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Bar", pattern.getObjectType());
final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs().get(2);
assertEqualsIgnoreWhitespace("abc(\"foo\")", (String) eval.getContent());
assertEqualsIgnoreWhitespace("Kapow", (String) rule.getConsequence());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testFunctionImport.
@Test
public void testFunctionImport() throws Exception {
final String source = "package foo\n" + "import function java.lang.Math.max\n" + "import function java.lang.Math.min;\n" + "import foo.bar.*\n" + "import baz.Baz";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals("foo", pkg.getName());
assertEquals(2, pkg.getImports().size());
ImportDescr impdescr = pkg.getImports().get(0);
assertEquals("foo.bar.*", impdescr.getTarget());
assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
impdescr = pkg.getImports().get(1);
assertEquals("baz.Baz", 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.getFunctionImports().size());
impdescr = pkg.getFunctionImports().get(0);
assertEquals("java.lang.Math.max", impdescr.getTarget());
assertEquals(source.indexOf("import function " + impdescr.getTarget()), impdescr.getStartCharacter());
assertEquals(source.indexOf("import function " + impdescr.getTarget()) + ("import function " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
impdescr = pkg.getFunctionImports().get(1);
assertEquals("java.lang.Math.min", impdescr.getTarget());
assertEquals(source.indexOf("import function " + impdescr.getTarget()), impdescr.getStartCharacter());
assertEquals(source.indexOf("import function " + impdescr.getTarget()) + ("import function " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testFromWithInlineList.
@Test
public void testFromWithInlineList() throws Exception {
String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " not( Number( ) from [1, 2, 3] ) \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());
PatternDescr number = (PatternDescr) ((NotDescr) rule.getLhs().getDescrs().get(1)).getDescrs().get(0);
assertEquals("[1, 2, 3]", ((FromDescr) number.getSource()).getDataSource().toString());
}
use of org.drools.drl.ast.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());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testAlmostEmptyRule.
@Test
public void testAlmostEmptyRule() throws Exception {
PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "almost_empty_rule.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(pkg);
RuleDescr rule = pkg.getRules().get(0);
assertEquals("almost_empty", rule.getName());
assertNotNull(rule.getLhs());
assertEquals("", ((String) rule.getConsequence()).trim());
}
Aggregations