use of org.drools.drl.ast.descr.OperatorDescr in project drools by kiegroup.
the class PatternBuilder method buildOperators.
public 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<>();
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().getEvaluator(context, predicateDescr, ValueType.OBJECT_TYPE, op.getOperator(), // the rewrite takes care of negation
false, op.getParametersText(), left, right);
EvaluatorWrapper wrapper = getConstraintBuilder().wrapEvaluator(evaluator, leftDecl, rightDecl);
operators.put(entry.getKey(), wrapper);
}
return operators;
}
use of org.drools.drl.ast.descr.OperatorDescr in project drools by kiegroup.
the class ConstraintUtil method inverseExpression.
/**
* Swap left and right operands in a constraint when a fact property is located on the right side.
*
* e.g. Person(20 < age) should be normalized to Person(age > 20)
*
* @param expression
* @param operator
* @param rightValue
* @param leftValue
* @return Normalized <code>expression</code>
*/
public static String inverseExpression(RelationalExprDescr relDescr, String expression, String leftValue, String rightValue, String operator, Pattern pattern) {
if (!ENABLE_NORMALIZE) {
return expression;
}
String leftProp = getFirstProp(leftValue);
String rightProp = getFirstProp(rightValue);
OperatorDescr operatorDescr = relDescr.getOperatorDescr();
if (canInverse(pattern, operator, operatorDescr, leftProp, rightProp) && isPropertyOnRight(pattern.getObjectType(), leftProp, rightProp)) {
boolean negate = false;
if (isNegatedExpression(expression, leftValue, rightValue, operator)) {
if (relDescr.getOperatorDescr().isNegated()) {
negate = true;
} else {
// do not inverse
return expression;
}
}
BaseDescr left = relDescr.getLeft();
relDescr.setLeft(relDescr.getRight());
relDescr.setRight(left);
String inversedOperator = IndexUtil.ConstraintType.decode(operator).inverse().getOperator();
operatorDescr.setOperator(inversedOperator);
String inversedExpression = rightValue + " " + inversedOperator + " " + leftValue;
if (negate) {
inversedExpression = "!( " + inversedExpression + " )";
}
return inversedExpression;
}
// do not inverse
return expression;
}
use of org.drools.drl.ast.descr.OperatorDescr in project drools by kiegroup.
the class PatternBuilder method buildCcdDescr.
private Constraint buildCcdDescr(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, BaseDescr d, ConstraintConnectiveDescr ccd, DumperContext mvelCtx) {
d.copyLocation(patternDescr);
mvelCtx.clear();
String expr = DescrDumper.getInstance().dump(d, ccd, mvelCtx);
Map<String, OperatorDescr> aliases = mvelCtx.getAliases();
// create bindings
TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
for (BindingDescr bind : mvelCtx.getBindings()) {
buildRuleBindings(context, patternDescr, xpathStartDeclaration, pattern, bind, typeDeclaration);
}
if (expr.length() == 0) {
return null;
}
// check if it is an atomic expression
Constraint constraint = processAtomicExpression(context, pattern, d, expr, aliases);
// otherwise check if it is a simple expression
return constraint != null ? constraint : buildExpression(context, pattern, d, expr, aliases, ccd.isNegated());
}
Aggregations