use of org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr in project drools by kiegroup.
the class XmlDumper method visitAccumulateDescr.
public void visitAccumulateDescr(final AccumulateDescr descr) {
String tmpstr = new String();
tmpstr += this.template + " <from> <accumulate> ";
if (descr.isSinglePattern()) {
visitPatternDescr(descr.getInputPattern());
} else {
this.patternContext = false;
visit(descr.getInput());
}
tmpstr += this.template;
if (descr.isExternalFunction()) {
AccumulateFunctionCallDescr func = descr.getFunctions().get(0);
tmpstr += "<external-function evaluator=\"" + func.getFunction() + "\" expression=\"" + func.getParams()[0] + "\"/>";
} else
tmpstr += "<init>" + descr.getInitCode() + "</init><action>" + descr.getActionCode() + "</action><result>" + descr.getResultCode() + "</result>";
this.template = tmpstr + " </accumulate> </from> ";
}
use of org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr 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<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.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateMnemonic2.
@Test
public void testAccumulateMnemonic2() throws Exception {
String drl = "package org.drools.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<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());
}
use of org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateMnemonic.
@Test
public void testAccumulateMnemonic() throws Exception {
String drl = "package org.drools.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<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());
}
use of org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr in project drools by kiegroup.
the class MVELAccumulateBuilder method buildExternalFunctions.
private Accumulator[] buildExternalFunctions(final RuleBuildContext context, final AccumulateDescr accumDescr, MVELDialect dialect, Map<String, Declaration> decls, Map<String, Declaration> sourceOuterDeclr, BoundIdentifiers boundIds, boolean readLocalsFromTuple) {
Accumulator[] accumulators;
List<AccumulateFunctionCallDescr> functions = accumDescr.getFunctions();
accumulators = new Accumulator[functions.size()];
// creating the custom array reader
InternalReadAccessor arrayReader = new SelfReferenceClassFieldReader(Object[].class);
int index = 0;
Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
for (AccumulateFunctionCallDescr func : functions) {
// build an external function executor
AccumulateFunction function = context.getConfiguration().getAccumulateFunction(func.getFunction());
if (function == null) {
// might have been imported in the package
function = context.getPkg().getAccumulateFunctions().get(func.getFunction());
}
if (function == null) {
context.addError(new DescrBuildError(accumDescr, context.getRuleDescr(), null, "Unknown accumulate function: '" + func.getFunction() + "' on rule '" + context.getRuleDescr().getName() + "'. All accumulate functions must be registered before building a resource."));
return null;
}
final AnalysisResult analysis = dialect.analyzeExpression(context, accumDescr, func.getParams().length > 0 ? func.getParams()[0] : "\"\"", boundIds);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(func.getParams().length > 0 ? func.getParams()[0] : "\"\"", analysis, getUsedDeclarations(decls, analysis), getUsedDeclarations(sourceOuterDeclr, analysis), null, context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
accumulators[index] = new MVELAccumulatorFunctionExecutor(unit, function);
// if there is a binding, create the binding
if (func.getBind() != null) {
if (context.getDeclarationResolver().isDuplicated(context.getRule(), func.getBind(), function.getResultType().getName())) {
if (!func.isUnification()) {
context.addError(new DescrBuildError(context.getParentDescr(), accumDescr, null, "Duplicate declaration for variable '" + func.getBind() + "' in the rule '" + context.getRule().getName() + "'"));
} else {
Declaration inner = context.getDeclarationResolver().getDeclaration(func.getBind());
Constraint c = new MvelConstraint(Collections.singletonList(context.getPkg().getName()), accumDescr.isMultiFunction() ? "this[ " + index + " ] == " + func.getBind() : "this == " + func.getBind(), new Declaration[] { inner }, null, null, IndexUtil.ConstraintType.EQUAL, context.getDeclarationResolver().getDeclaration(func.getBind()), accumDescr.isMultiFunction() ? new ArrayElementReader(arrayReader, index, function.getResultType()) : new SelfReferenceClassFieldReader(function.getResultType()), true);
((MutableTypeConstraint) c).setType(Constraint.ConstraintType.BETA);
pattern.addConstraint(c);
index++;
}
} else {
Declaration declr = pattern.addDeclaration(func.getBind());
if (accumDescr.isMultiFunction()) {
declr.setReadAccessor(new ArrayElementReader(arrayReader, index, function.getResultType()));
} else {
declr.setReadAccessor(new SelfReferenceClassFieldReader(function.getResultType()));
}
}
}
index++;
}
return accumulators;
}
Aggregations