Search in sources :

Example 1 with AccumulateFunctionCallDescr

use of org.drools.drl.ast.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> ";
}
Also used : AccumulateFunctionCallDescr(org.drools.drl.ast.descr.AccumulateDescr.AccumulateFunctionCallDescr)

Example 2 with AccumulateFunctionCallDescr

use of org.drools.drl.ast.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, accumulators.length);
    } 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.mvel.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.drl.ast.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 3 with AccumulateFunctionCallDescr

use of org.drools.drl.ast.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, RuleConditionElement source, Map<String, Class<?>> declarationClasses) {
    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
        Supplier<Class<?>> classSupplier = () -> MVELExprAnalyzer.getExpressionType(context, declarationClasses, source, func.getParams()[0]);
        String functionName = AccumulateUtil.getFunctionName(classSupplier, func.getFunction());
        AccumulateFunction function = context.getConfiguration().getAccumulateFunction(functionName);
        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;
}
Also used : Accumulator(org.drools.core.spi.Accumulator) MVELAccumulator(org.drools.mvel.expr.MVELAccumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) Pattern(org.drools.core.rule.Pattern) MVELAccumulatorFunctionExecutor(org.drools.mvel.expr.MVELAccumulatorFunctionExecutor) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) Constraint(org.drools.core.spi.Constraint) MVELCompilationUnit(org.drools.mvel.expr.MVELCompilationUnit) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) Constraint(org.drools.core.spi.Constraint) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELConstraint(org.drools.mvel.MVELConstraint) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) AccumulateFunctionCallDescr(org.drools.drl.ast.descr.AccumulateDescr.AccumulateFunctionCallDescr) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Aggregations

AccumulateFunctionCallDescr (org.drools.drl.ast.descr.AccumulateDescr.AccumulateFunctionCallDescr)3 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 Accumulator (org.drools.core.spi.Accumulator)2 Constraint (org.drools.core.spi.Constraint)2 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)2 MVELConstraint (org.drools.mvel.MVELConstraint)2 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)2 HashSet (java.util.HashSet)1 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 MultiAccumulate (org.drools.core.rule.MultiAccumulate)1 SingleAccumulate (org.drools.core.rule.SingleAccumulate)1 MvelAccumulator (org.drools.core.spi.MvelAccumulator)1 MVELAccumulator (org.drools.mvel.expr.MVELAccumulator)1 MVELAccumulatorFunctionExecutor (org.drools.mvel.expr.MVELAccumulatorFunctionExecutor)1 MVELCompilationUnit (org.drools.mvel.expr.MVELCompilationUnit)1