use of org.drools.core.spi.Evaluator 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.core.spi.Evaluator in project drools by kiegroup.
the class MVELConstraintBuilder method buildLiteralConstraint.
public Constraint buildLiteralConstraint(RuleBuildContext context, Pattern pattern, ValueType vtype, FieldValue field, String expression, String leftValue, String operator, String rightValue, InternalReadAccessor extractor, LiteralRestrictionDescr restrictionDescr, Map<String, OperatorDescr> aliases) {
if (!isMvelOperator(operator)) {
Evaluator evaluator = buildLiteralEvaluator(context, extractor, restrictionDescr, vtype);
if (evaluator != null && evaluator.isTemporal()) {
try {
field = context.getCompilerFactory().getFieldFactory().getFieldValue(field.getValue(), ValueType.DATE_TYPE);
} catch (Exception e) {
context.addError(new DescrBuildError(context.getParentDescr(), restrictionDescr, null, e.getMessage()));
}
}
return new EvaluatorConstraint(field, evaluator, extractor);
}
String mvelExpr = normalizeMVELLiteralExpression(vtype, field, expression, leftValue, operator, rightValue, restrictionDescr);
IndexUtil.ConstraintType constraintType = IndexUtil.ConstraintType.decode(operator);
MVELCompilationUnit compilationUnit = buildCompilationUnit(context, pattern, mvelExpr, aliases);
EvaluatorWrapper[] operators = getOperators(buildOperators(context, pattern, restrictionDescr, aliases));
return new MvelConstraint(context.getPkg().getName(), mvelExpr, compilationUnit, constraintType, field, extractor, operators);
}
use of org.drools.core.spi.Evaluator in project drools by kiegroup.
the class MVELConstraintBuilder method buildVariableConstraint.
public Constraint buildVariableConstraint(RuleBuildContext context, Pattern pattern, String expression, Declaration[] declarations, String leftValue, OperatorDescr operatorDescr, String rightValue, InternalReadAccessor extractor, Declaration requiredDeclaration, RelationalExprDescr relDescr, Map<String, OperatorDescr> aliases) {
if (!isMvelOperator(operatorDescr.getOperator())) {
if (requiredDeclaration == null) {
return null;
}
EvaluatorDefinition.Target right = getRightTarget(extractor);
EvaluatorDefinition.Target left = (requiredDeclaration.isPatternDeclaration() && !(Date.class.isAssignableFrom(requiredDeclaration.getDeclarationClass()) || Number.class.isAssignableFrom(requiredDeclaration.getDeclarationClass()))) ? EvaluatorDefinition.Target.HANDLE : EvaluatorDefinition.Target.FACT;
final Evaluator evaluator = getEvaluator(context, relDescr, extractor.getValueType(), operatorDescr.getOperator(), relDescr.isNegated(), relDescr.getParametersText(), left, right);
return new EvaluatorConstraint(new Declaration[] { requiredDeclaration }, evaluator, extractor);
}
boolean isUnification = requiredDeclaration != null && requiredDeclaration.getPattern().getObjectType().equals(new ClassObjectType(DroolsQuery.class)) && Operator.EQUAL.getOperatorString().equals(operatorDescr.getOperator());
if (isUnification && leftValue.equals(rightValue)) {
expression = resolveUnificationAmbiguity(expression, declarations, leftValue, rightValue);
}
expression = normalizeMVELVariableExpression(expression, leftValue, rightValue, relDescr);
IndexUtil.ConstraintType constraintType = IndexUtil.ConstraintType.decode(operatorDescr.getOperator());
MVELCompilationUnit compilationUnit = isUnification ? null : buildCompilationUnit(context, pattern, expression, aliases);
EvaluatorWrapper[] operators = getOperators(buildOperators(context, pattern, relDescr, aliases));
return new MvelConstraint(Collections.singletonList(context.getPkg().getName()), expression, declarations, operators, compilationUnit, constraintType, requiredDeclaration, extractor, isUnification);
}
use of org.drools.core.spi.Evaluator 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 Evaluator evaluator = evalDef.getEvaluator(valueType, evaluatorStr, isNegated, parameters);
// System.out.println(evaluator);
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.core.spi.Evaluator in project drools by kiegroup.
the class PatternBuilder method buildOperators.
protected static Map<String, EvaluatorWrapper> buildOperators(RuleBuildContext context, Pattern pattern, BaseDescr predicateDescr, Map<String, OperatorDescr> aliases) {
if (aliases.isEmpty()) {
return Collections.emptyMap();
}
Map<String, EvaluatorWrapper> operators = new HashMap<String, EvaluatorWrapper>();
for (Map.Entry<String, OperatorDescr> entry : aliases.entrySet()) {
OperatorDescr op = entry.getValue();
Declaration leftDecl = createDeclarationForOperator(context, pattern, op.getLeftString());
Declaration rightDecl = createDeclarationForOperator(context, pattern, op.getRightString());
Target left = leftDecl != null && leftDecl.isPatternDeclaration() ? Target.HANDLE : Target.FACT;
Target right = rightDecl != null && rightDecl.isPatternDeclaration() ? Target.HANDLE : Target.FACT;
op.setLeftIsHandle(left == Target.HANDLE);
op.setRightIsHandle(right == Target.HANDLE);
Evaluator evaluator = getConstraintBuilder(context).getEvaluator(context, predicateDescr, ValueType.OBJECT_TYPE, op.getOperator(), // the rewrite takes care of negation
false, op.getParametersText(), left, right);
EvaluatorWrapper wrapper = getConstraintBuilder(context).wrapEvaluator(evaluator, leftDecl, rightDecl);
operators.put(entry.getKey(), wrapper);
}
return operators;
}
Aggregations