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");
}
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());
}
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());
}
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());
}
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());
}
Aggregations