Search in sources :

Example 1 with AccumulateDescr

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

the class XmlPackageReaderTest method testAccumulateMultiPattern.

@Test
public void testAccumulateMultiPattern() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseAccumulate.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(1);
    Object patternobj = obj.getLhs().getDescrs().get(0);
    assertTrue(patternobj instanceof PatternDescr);
    final PatternDescr patterncheese = (PatternDescr) patternobj;
    assertEquals(patterncheese.getIdentifier(), "cheese");
    assertEquals(patterncheese.getObjectType(), "Cheese");
    AccumulateDescr accumulatedescr = (AccumulateDescr) patterncheese.getSource();
    assertEquals("total += $cheese.getPrice();", accumulatedescr.getActionCode());
    assertEquals("int total = 0;", accumulatedescr.getInitCode());
    assertEquals("new Integer( total ) );", accumulatedescr.getResultCode());
    AndDescr anddescr = (AndDescr) accumulatedescr.getInput();
    List descrlist = anddescr.getDescrs();
    PatternDescr[] listpattern = (PatternDescr[]) descrlist.toArray(new PatternDescr[descrlist.size()]);
    assertEquals(listpattern[0].getObjectType(), "Milk");
    assertEquals(listpattern[1].getObjectType(), "Cup");
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 2 with AccumulateDescr

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

the class RuleParserTest method testAccumulateMultipleFunctionsConstraint.

@Test
public void testAccumulateMultipleFunctionsConstraint() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateMultipleFunctionsConstraint.drl");
    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());
    assertEquals(2, out.getConstraint().getDescrs().size());
    assertEquals("$a1 > 10 && $M1 <= 100", out.getConstraint().getDescrs().get(0).toString());
    assertEquals("$m1 == 5", out.getConstraint().getDescrs().get(1).toString());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(3, functions.size());
    assertEquals("average", functions.get(0).getFunction());
    assertEquals("$a1", functions.get(0).getBind());
    assertEquals("$price", functions.get(0).getParams()[0]);
    assertEquals("min", functions.get(1).getFunction());
    assertEquals("$m1", functions.get(1).getBind());
    assertEquals("$price", functions.get(1).getParams()[0]);
    assertEquals("max", functions.get(2).getFunction());
    assertEquals("$M1", functions.get(2).getBind());
    assertEquals("$price", functions.get(2).getParams()[0]);
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Cheese", 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 3 with AccumulateDescr

use of org.drools.drl.ast.descr.AccumulateDescr 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 4 with AccumulateDescr

use of org.drools.drl.ast.descr.AccumulateDescr 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 5 with AccumulateDescr

use of org.drools.drl.ast.descr.AccumulateDescr 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());
}
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) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) Test(org.junit.Test)

Aggregations

AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)23 PatternDescr (org.drools.drl.ast.descr.PatternDescr)17 PackageDescr (org.drools.drl.ast.descr.PackageDescr)15 RuleDescr (org.drools.drl.ast.descr.RuleDescr)15 Test (org.junit.Test)15 Accumulate (org.drools.core.rule.Accumulate)4 AndDescr (org.drools.drl.ast.descr.AndDescr)3 InputStreamReader (java.io.InputStreamReader)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)2 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)2 RuleConditionBuilder (org.drools.compiler.rule.builder.RuleConditionBuilder)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 Declaration (org.drools.core.rule.Declaration)2 MultiAccumulate (org.drools.core.rule.MultiAccumulate)2 RuleConditionElement (org.drools.core.rule.RuleConditionElement)2 SingleAccumulate (org.drools.core.rule.SingleAccumulate)2 BaseDescr (org.drools.drl.ast.descr.BaseDescr)2