use of org.drools.compiler.rule.builder.EvaluatorDefinition in project drools by kiegroup.
the class CustomOperatorSpec method addOperatorArgument.
@Override
protected Operator addOperatorArgument(RuleContext context, MethodCallExpr methodCallExpr, String opName) {
EvaluatorDefinition evalDef = context.getEvaluatorDefinition(opName);
if (evalDef == null) {
throw new RuntimeException("Unknown custom operator: " + opName);
}
String arg = "new " + CustomOperatorWrapper.class.getCanonicalName() + "( new " + evalDef.getClass().getCanonicalName() + "().getEvaluator(" + ValueType.class.getCanonicalName() + ".OBJECT_TYPE, \"" + opName + "\", false, null), \"" + opName + "\")";
methodCallExpr.addArgument(arg);
return null;
}
use of org.drools.compiler.rule.builder.EvaluatorDefinition in project drools by kiegroup.
the class MVELConstraintBuilder method getEvaluator.
public Evaluator getEvaluator(final RuleBuildContext context, final BaseDescr descr, final ValueType valueType, final String evaluatorString, final boolean isNegated, final String parameters, final EvaluatorDefinition.Target left, final EvaluatorDefinition.Target right) {
final EvaluatorDefinition def = context.getConfiguration().getEvaluatorRegistry().getEvaluatorDefinition(evaluatorString);
if (def == null) {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to determine the Evaluator for ID '" + evaluatorString + "'"));
return null;
}
final Evaluator evaluator = def.getEvaluator(valueType, evaluatorString, isNegated, parameters, left, right);
if (evaluator == null) {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Evaluator '" + (isNegated ? "not " : "") + evaluatorString + "' does not support type '" + valueType));
}
return evaluator;
}
use of org.drools.compiler.rule.builder.EvaluatorDefinition in project drools by kiegroup.
the class TemporalEvaluatorFactoryTest method runEvaluatorTest.
private void runEvaluatorTest(final Object[][] data, final ValueType valueType) {
final InternalReadAccessor extractor = new MockExtractor();
for (int i = 0; i < data.length; i++) {
final Object[] row = data[i];
boolean isNegated = ((String) row[1]).startsWith("not ");
// System.out.println((String) row[1]);
String evaluatorStr = isNegated ? ((String) row[1]).substring(4) : (String) row[1];
boolean isConstrained = evaluatorStr.endsWith("]");
String parameters = null;
if (isConstrained) {
parameters = evaluatorStr.split("\\[")[1];
evaluatorStr = evaluatorStr.split("\\[")[0];
parameters = parameters.split("\\]")[0];
}
EvaluatorDefinition evalDef = registry.getEvaluatorDefinition(evaluatorStr);
assertNotNull(evalDef);
final MvelEvaluator evaluator = (MvelEvaluator) evalDef.getEvaluator(valueType, evaluatorStr, isNegated, parameters);
checkEvaluatorMethodWith2Extractors(valueType, extractor, row, evaluator);
checkEvaluatorMethodCachedRight(valueType, extractor, row, evaluator);
checkEvaluatorMethodCachedLeft(valueType, extractor, row, evaluator);
checkEvaluatorMethodWithFieldValue(valueType, extractor, row, evaluator);
assertEquals(valueType, evaluator.getValueType());
}
}
use of org.drools.compiler.rule.builder.EvaluatorDefinition in project drools by kiegroup.
the class KnowledgeBuilderConfigurationTest method testEvaluatorConfiguration.
@Test
public void testEvaluatorConfiguration() {
// in this use case, the application already has the instance of the evaluator definition
EvaluatorDefinition afterDef = new AfterEvaluatorDefinition();
assertNotNull(afterDef);
// creating the option and storing in a local var just to make test easier
EvaluatorOption option = EvaluatorOption.get("after", afterDef);
// wiring the evaluator definition using the type safe method
config.setOption(option);
// checking the type safe getOption() method
assertEquals(option, config.getOption(EvaluatorOption.class, "after"));
// checking string conversion
assertEquals("after", config.getOption(EvaluatorOption.class, "after").getName());
assertEquals(afterDef, config.getOption(EvaluatorOption.class, "after").getEvaluatorDefinition());
// checking the string based getProperty() method
assertEquals(AfterEvaluatorDefinition.class.getName(), config.getProperty(EvaluatorOption.PROPERTY_NAME + "after"));
// wiring the evaluator definition using the string based setProperty() method
config.setProperty(EvaluatorOption.PROPERTY_NAME + "before", BeforeEvaluatorDefinition.class.getName());
BeforeEvaluatorDefinition beforeDef = new BeforeEvaluatorDefinition();
// checking the type safe getOption() method
assertEquals(EvaluatorOption.get("before", beforeDef), config.getOption(EvaluatorOption.class, "before"));
// checking string conversion
assertEquals("before", config.getOption(EvaluatorOption.class, "before").getName());
assertEquals(beforeDef.getClass().getName(), config.getOption(EvaluatorOption.class, "before").getEvaluatorDefinition().getClass().getName());
// checking the string based getProperty() method
assertEquals(beforeDef.getClass().getName(), config.getProperty(EvaluatorOption.PROPERTY_NAME + "before"));
}
use of org.drools.compiler.rule.builder.EvaluatorDefinition 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.equals(TrimCellsInDTableOption.PROPERTY_NAME)) {
return String.valueOf(isTrimCellsInDTable());
} else if (name.equals(GroupDRLsInKieBasesByFolderOption.PROPERTY_NAME)) {
return String.valueOf(isGroupDRLsInKieBasesByFolder());
} 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();
} else if (name.equals(ParallelRulesBuildThresholdOption.PROPERTY_NAME)) {
return String.valueOf(getParallelRulesBuildThreshold());
} else if (name.equals(ExternaliseCanonicalModelLambdaOption.PROPERTY_NAME)) {
return String.valueOf(isExternaliseCanonicalModelLambda());
} else if (name.equals(ParallelLambdaExternalizationOption.PROPERTY_NAME)) {
return String.valueOf(isParallelLambdaExternalization());
}
return null;
}
Aggregations