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;
}
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;
}
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;
}
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);
}
}
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() );
// }
}
Aggregations