use of org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr in project drools by kiegroup.
the class JavaAccumulateBuilder method buildExternalFunctionCall.
private Accumulate buildExternalFunctionCall(RuleBuildContext context, AccumulateDescr accumDescr, RuleConditionElement source, Map<String, Declaration> declsInScope, Map<String, Class<?>> declCls, boolean readLocalsFromTuple) {
// list of functions to build
final List<AccumulateFunctionCallDescr> funcCalls = accumDescr.getFunctions();
// list of available source declarations
final Declaration[] sourceDeclArr = source.getOuterDeclarations().values().toArray(new Declaration[source.getOuterDeclarations().size()]);
Arrays.sort(sourceDeclArr, RuleTerminalNode.SortDeclarations.instance);
// set of required previous declarations
Set<Declaration> requiredDecl = new HashSet<Declaration>();
Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
if (accumDescr.isMultiFunction()) {
// the accumulator array
Accumulator[] accumulators = new Accumulator[funcCalls.size()];
// creating the custom array reader
InternalReadAccessor reader = new SelfReferenceClassFieldReader(Object[].class);
int index = 0;
for (AccumulateFunctionCallDescr fc : funcCalls) {
AccumulateFunction function = getAccumulateFunction(context, accumDescr, fc, source, declCls);
if (function == null) {
return null;
}
bindReaderToDeclaration(context, accumDescr, pattern, fc, new ArrayElementReader(reader, index, function.getResultType()), function.getResultType(), index);
accumulators[index++] = buildAccumulator(context, accumDescr, declsInScope, declCls, readLocalsFromTuple, sourceDeclArr, requiredDecl, fc, function);
}
return new MultiAccumulate(source, requiredDecl.toArray(new Declaration[requiredDecl.size()]), accumulators);
} else {
AccumulateFunctionCallDescr fc = accumDescr.getFunctions().get(0);
AccumulateFunction function = getAccumulateFunction(context, accumDescr, fc, source, declCls);
if (function == null) {
return null;
}
Class<?> returnType = function.getResultType();
if (!pattern.isCompatibleWithAccumulateReturnType(returnType)) {
context.addError(new DescrBuildError(accumDescr, context.getRuleDescr(), null, "Pattern of type: '" + pattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by accumulate function."));
return null;
}
bindReaderToDeclaration(context, accumDescr, pattern, fc, new SelfReferenceClassFieldReader(function.getResultType()), function.getResultType(), -1);
Accumulator accumulator = buildAccumulator(context, accumDescr, declsInScope, declCls, readLocalsFromTuple, sourceDeclArr, requiredDecl, fc, function);
return new SingleAccumulate(source, requiredDecl.toArray(new Declaration[requiredDecl.size()]), accumulator);
}
}
use of org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateMultipleFunctions.
@Test
public void testAccumulateMultipleFunctions() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateMultipleFunctions.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(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 testImportAccumulate.
@Test
public void testImportAccumulate() throws Exception {
String drl = "package org.drools.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<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());
}
Aggregations