Search in sources :

Example 21 with AccumulateDescr

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

the class RuleParserTest method testAccumulateMnemonic.

@Test
public void testAccumulateMnemonic() throws Exception {
    String drl = "package org.drools.mvel.compiler\n" + "rule \"Accumulate 1\"\n" + "when\n" + "     acc( Cheese( $price : price ),\n" + "          $a1 : average( $price ) )\n" + "then\n" + "end\n";
    final PackageDescr pkg = (PackageDescr) parse("compilationUnit", 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());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(1, functions.size());
    assertEquals("average", functions.get(0).getFunction());
    assertEquals("$a1", functions.get(0).getBind());
    assertEquals("$price", functions.get(0).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 22 with AccumulateDescr

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

the class RuleParserTest method testAccumulateMnemonic2.

@Test
public void testAccumulateMnemonic2() throws Exception {
    String drl = "package org.drools.mvel.compiler\n" + "rule \"Accumulate 1\"\n" + "when\n" + "     Number() from acc( Cheese( $price : price ),\n" + "                        average( $price ) )\n" + "then\n" + "end\n";
    final PackageDescr pkg = (PackageDescr) parse("compilationUnit", 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("Number", out.getObjectType());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(1, functions.size());
    assertEquals("average", functions.get(0).getFunction());
    assertEquals("$price", functions.get(0).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 23 with AccumulateDescr

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

the class RuleParserTest method testAccumulate.

@Test
public void testAccumulate() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate.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("int x = 0 ;", accum.getInitCode());
    assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
    assertNull(accum.getReverseCode());
    assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
    assertFalse(accum.isExternalFunction());
    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)

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