Search in sources :

Example 1 with AccumulateFunction

use of org.kie.api.runtime.rule.AccumulateFunction 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;
}
Also used : MVELAccumulator(org.drools.core.base.mvel.MVELAccumulator) Accumulator(org.drools.core.spi.Accumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) Pattern(org.drools.core.rule.Pattern) MVELAccumulatorFunctionExecutor(org.drools.core.base.accumulators.MVELAccumulatorFunctionExecutor) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) 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)

Example 2 with AccumulateFunction

use of org.kie.api.runtime.rule.AccumulateFunction in project drools by kiegroup.

the class JavaAccumulateBuilder method getAccumulateFunction.

private AccumulateFunction getAccumulateFunction(RuleBuildContext context, AccumulateDescr accumDescr, AccumulateFunctionCallDescr fc, RuleConditionElement source, Map<String, Class<?>> declCls) {
    String functionName = AccumulateUtil.getFunctionName(() -> MVELExprAnalyzer.getExpressionType(context, declCls, source, fc.getParams()[0]), fc.getFunction());
    // find the corresponding function
    AccumulateFunction function = context.getConfiguration().getAccumulateFunction(functionName);
    if (function == null) {
        // might have been imported in the package
        function = context.getPkg().getAccumulateFunctions().get(functionName);
    }
    if (function == null) {
        context.addError(new DescrBuildError(accumDescr, context.getRuleDescr(), null, "Unknown accumulate function: '" + functionName + "' on rule '" + context.getRuleDescr().getName() + "'. All accumulate functions must be registered before building a resource."));
    }
    return function;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Example 3 with AccumulateFunction

use of org.kie.api.runtime.rule.AccumulateFunction in project drools by kiegroup.

the class KnowledgeBuilderConfigurationImpl method getProperty.

public String getProperty(String name) {
    name = name.trim();
    if (StringUtils.isEmpty(name)) {
        return null;
    }
    if (name.equals(DefaultDialectOption.PROPERTY_NAME)) {
        return getDefaultDialect();
    } else if (name.equals(DefaultPackageNameOption.PROPERTY_NAME)) {
        return getDefaultPackageName();
    } else if (name.startsWith(AccumulateFunctionOption.PROPERTY_NAME)) {
        int index = AccumulateFunctionOption.PROPERTY_NAME.length();
        AccumulateFunction function = this.accumulateFunctions.get(name.substring(index));
        return function != null ? function.getClass().getName() : null;
    } else if (name.startsWith(EvaluatorOption.PROPERTY_NAME)) {
        String key = name.substring(name.lastIndexOf('.') + 1);
        EvaluatorDefinition evalDef = this.evaluatorRegistry.getEvaluatorDefinition(key);
        return evalDef != null ? evalDef.getClass().getName() : null;
    } else if (name.equals(DumpDirOption.PROPERTY_NAME)) {
        return this.dumpDirectory != null ? this.dumpDirectory.toString() : null;
    } else if (name.equals(ProcessStringEscapesOption.PROPERTY_NAME)) {
        return String.valueOf(isProcessStringEscapes());
    } else if (name.equals(ClassLoaderCacheOption.PROPERTY_NAME)) {
        return String.valueOf(isClassLoaderCacheEnabled());
    } else if (name.startsWith(KBuilderSeverityOption.PROPERTY_NAME)) {
        String key = name.substring(name.lastIndexOf('.') + 1);
        ResultSeverity severity = this.severityMap.get(key);
        return severity.toString();
    } else if (name.equals(LanguageLevelOption.PROPERTY_NAME)) {
        return "" + getLanguageLevel();
    }
    return null;
}
Also used : EvaluatorDefinition(org.drools.core.base.evaluators.EvaluatorDefinition) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) ResultSeverity(org.kie.internal.builder.ResultSeverity)

Example 4 with AccumulateFunction

use of org.kie.api.runtime.rule.AccumulateFunction in project drools by kiegroup.

the class KnowledgeBuilderImpl method processAccumulateFunctions.

private void processAccumulateFunctions(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (final AccumulateImportDescr aid : packageDescr.getAccumulateImports()) {
        AccumulateFunction af = loadAccumulateFunction(pkgRegistry, aid.getFunctionName(), aid.getTarget());
        pkgRegistry.getPackage().addAccumulateFunction(aid.getFunctionName(), af);
    }
}
Also used : AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Example 5 with AccumulateFunction

use of org.kie.api.runtime.rule.AccumulateFunction in project drools by kiegroup.

the class KnowledgeBuilderConfigurationTest method testAccumulateFunctionConfiguration.

@Test
public void testAccumulateFunctionConfiguration() {
    Set<String> keySet = new HashSet<String>();
    // in this use case, the application already has the instance of the accumulate function
    AccumulateFunction function = new AverageAccumulateFunction();
    // creating the option and storing in a local var just to make test easier
    AccumulateFunctionOption option = AccumulateFunctionOption.get("avg", function);
    // wiring the accumulate function using the type safe method
    config.setOption(option);
    // checking the type safe getOption() method
    assertEquals(option, config.getOption(AccumulateFunctionOption.class, "avg"));
    // checking string conversion
    assertEquals("avg", config.getOption(AccumulateFunctionOption.class, "avg").getName());
    assertEquals(function, config.getOption(AccumulateFunctionOption.class, "avg").getFunction());
    // checking the string based getProperty() method
    assertEquals(AverageAccumulateFunction.class.getName(), config.getProperty(AccumulateFunctionOption.PROPERTY_NAME + "avg"));
    // check the key set
    keySet.add("avg");
    assertTrue(config.getOptionKeys(AccumulateFunctionOption.class).contains("avg"));
    // wiring the accumulate function using the string based setProperty() method
    config.setProperty(AccumulateFunctionOption.PROPERTY_NAME + "maximum", MaxAccumulateFunction.class.getName());
    MaxAccumulateFunction max = new MaxAccumulateFunction();
    // checking the type safe getOption() method
    assertEquals(AccumulateFunctionOption.get("maximum", max), config.getOption(AccumulateFunctionOption.class, "maximum"));
    // checking string conversion
    assertEquals("maximum", config.getOption(AccumulateFunctionOption.class, "maximum").getName());
    assertEquals(max.getClass().getName(), config.getOption(AccumulateFunctionOption.class, "maximum").getFunction().getClass().getName());
    // checking the string based getProperty() method
    assertEquals(MaxAccumulateFunction.class.getName(), config.getProperty(AccumulateFunctionOption.PROPERTY_NAME + "maximum"));
    keySet.add("avg");
    // wiring the inner class accumulate function using the string based setProperty() method
    config.setProperty(AccumulateFunctionOption.PROPERTY_NAME + "inner", InnerAccumulateFuncion.class.getName());
    InnerAccumulateFuncion inner = new InnerAccumulateFuncion();
    // checking the type safe getOption() method
    assertEquals(AccumulateFunctionOption.get("inner", inner), config.getOption(AccumulateFunctionOption.class, "inner"));
    // checking string conversion
    assertEquals("inner", config.getOption(AccumulateFunctionOption.class, "inner").getName());
    assertEquals(inner.getClass().getName(), config.getOption(AccumulateFunctionOption.class, "inner").getFunction().getClass().getName());
    // checking the string based getProperty() method
    assertEquals(InnerAccumulateFuncion.class.getName(), config.getProperty(AccumulateFunctionOption.PROPERTY_NAME + "inner"));
    keySet.add("avg");
    assertTrue(config.getOptionKeys(AccumulateFunctionOption.class).containsAll(keySet));
// for( String key: config.getOptionKeys(AccumulateFunctionOption.class ) ){
// System.out.println( key + "->" + config.getOption(AccumulateFunctionOption.class, key).getClass().getName() );
// }
}
Also used : MaxAccumulateFunction(org.drools.core.base.accumulators.MaxAccumulateFunction) AverageAccumulateFunction(org.drools.core.base.accumulators.AverageAccumulateFunction) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) MaxAccumulateFunction(org.drools.core.base.accumulators.MaxAccumulateFunction) AverageAccumulateFunction(org.drools.core.base.accumulators.AverageAccumulateFunction) AccumulateFunctionOption(org.kie.internal.builder.conf.AccumulateFunctionOption) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)8 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)3 HashSet (java.util.HashSet)2 AccumulateFunctionCallDescr (org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr)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 ByteArrayInputStream (java.io.ByteArrayInputStream)1 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 AccumulateImportDescr (org.drools.compiler.lang.descr.AccumulateImportDescr)1 AverageAccumulateFunction (org.drools.core.base.accumulators.AverageAccumulateFunction)1 MVELAccumulatorFunctionExecutor (org.drools.core.base.accumulators.MVELAccumulatorFunctionExecutor)1 MaxAccumulateFunction (org.drools.core.base.accumulators.MaxAccumulateFunction)1 EvaluatorDefinition (org.drools.core.base.evaluators.EvaluatorDefinition)1