Search in sources :

Example 6 with AccumulateFunctionCallDescr

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);
    }
}
Also used : Accumulator(org.drools.core.spi.Accumulator) Pattern(org.drools.core.rule.Pattern) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) AccumulateFunctionCallDescr(org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) HashSet(java.util.HashSet)

Example 7 with AccumulateFunctionCallDescr

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());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AccumulateFunctionCallDescr(org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Test(org.junit.Test)

Example 8 with AccumulateFunctionCallDescr

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());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AccumulateFunctionCallDescr(org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Test(org.junit.Test)

Aggregations

AccumulateFunctionCallDescr (org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr)8 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)5 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)5 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)5 Test (org.junit.Test)5 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)2 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)2 Declaration (org.drools.core.rule.Declaration)2 MutableTypeConstraint (org.drools.core.rule.MutableTypeConstraint)2 Pattern (org.drools.core.rule.Pattern)2 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)2 Accumulator (org.drools.core.spi.Accumulator)2 Constraint (org.drools.core.spi.Constraint)2 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)2 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)2 HashSet (java.util.HashSet)1 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 AccumulateImportDescr (org.drools.compiler.lang.descr.AccumulateImportDescr)1