Search in sources :

Example 81 with PackageDescr

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());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 82 with PackageDescr

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());
}
Also used : ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 83 with PackageDescr

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());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 84 with PackageDescr

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());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 85 with PackageDescr

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());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.drl.ast.descr.PackageDescr)262 Test (org.junit.Test)210 RuleDescr (org.drools.drl.ast.descr.RuleDescr)104 PatternDescr (org.drools.drl.ast.descr.PatternDescr)85 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)52 AndDescr (org.drools.drl.ast.descr.AndDescr)39 DrlParser (org.drools.drl.parser.DrlParser)39 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 InputStreamReader (java.io.InputStreamReader)23 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)23 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)23 KieSession (org.kie.api.runtime.KieSession)19 List (java.util.List)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)17 KiePackage (org.kie.api.definition.KiePackage)17 DrlDumper (org.drools.mvel.DrlDumper)16 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15