Search in sources :

Example 76 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testImportAccumulate.

@Test
public void testImportAccumulate() throws Exception {
    String drl = "package org.drools.mvel.compiler\n" + "import acc foo.Bar baz\n" + "import accumulate foo.Bar2 baz2\n" + "rule \"Accumulate 1\"\n" + "when\n" + "     acc( Cheese( $price : price ),\n" + "          $v1 : baz( $price ), \n" + "          $v2 : baz2( $price ) )\n" + "then\n" + "end\n";
    final PackageDescr pkg = (PackageDescr) parse("compilationUnit", drl);
    assertEquals(2, pkg.getAccumulateImports().size());
    AccumulateImportDescr imp = (AccumulateImportDescr) pkg.getAccumulateImports().get(0);
    assertEquals("foo.Bar", imp.getTarget());
    assertEquals("baz", imp.getFunctionName());
    imp = (AccumulateImportDescr) pkg.getAccumulateImports().get(1);
    assertEquals("foo.Bar2", imp.getTarget());
    assertEquals("baz2", imp.getFunctionName());
    assertEquals(1, pkg.getRules().size());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Object", out.getObjectType());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(2, functions.size());
    assertEquals("baz", functions.get(0).getFunction());
    assertEquals("$v1", functions.get(0).getBind());
    assertEquals("$price", functions.get(0).getParams()[0]);
    assertEquals("baz2", functions.get(1).getFunction());
    assertEquals("$v2", functions.get(1).getBind());
    assertEquals("$price", functions.get(1).getParams()[0]);
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Cheese", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) Test(org.junit.Test)

Example 77 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testExpanderLineSpread.

@Test
public void testExpanderLineSpread() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final PackageDescr pkg = parser.parse(this.getReader("expander_spread_lines.dslr"), this.getReader("complex.dsl"));
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    assertNotNull((String) rule.getConsequence());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 78 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testAccumulateWithBindings.

@Test
public void testAccumulateWithBindings() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_with_bindings.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 PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Person", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) Test(org.junit.Test)

Example 79 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testEndPosition.

@Test
public void testEndPosition() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "test_EndPosition.drl");
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(21, col.getLine());
    assertEquals(23, col.getEndLine());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 80 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testRuleWithLHSNesting.

@Test
public void testRuleWithLHSNesting() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_nested_LHS.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    assertEquals("test", rule.getName());
    AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(2, lhs.getDescrs().size());
    PatternDescr a = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("A", a.getObjectType());
    OrDescr or = (OrDescr) lhs.getDescrs().get(1);
    assertEquals(3, or.getDescrs().size());
    AndDescr and1 = (AndDescr) or.getDescrs().get(0);
    assertEquals(2, and1.getDescrs().size());
    PatternDescr b = (PatternDescr) and1.getDescrs().get(0);
    PatternDescr c = (PatternDescr) and1.getDescrs().get(1);
    assertEquals("B", b.getObjectType());
    assertEquals("C", c.getObjectType());
    AndDescr and2 = (AndDescr) or.getDescrs().get(1);
    assertEquals(2, and2.getDescrs().size());
    PatternDescr d = (PatternDescr) and2.getDescrs().get(0);
    PatternDescr e = (PatternDescr) and2.getDescrs().get(1);
    assertEquals("D", d.getObjectType());
    assertEquals("E", e.getObjectType());
    AndDescr and3 = (AndDescr) or.getDescrs().get(2);
    assertEquals(2, and3.getDescrs().size());
    PatternDescr f = (PatternDescr) and3.getDescrs().get(0);
    PatternDescr g = (PatternDescr) and3.getDescrs().get(1);
    assertEquals("F", f.getObjectType());
    assertEquals("G", g.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) 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