Search in sources :

Example 1 with OperatorDescr

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;
}
Also used : Target(org.drools.compiler.rule.builder.EvaluatorDefinition.Target) HashMap(java.util.HashMap) OperatorDescr(org.drools.drl.ast.descr.OperatorDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) Evaluator(org.drools.core.spi.Evaluator) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with OperatorDescr

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;
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) OperatorDescr(org.drools.drl.ast.descr.OperatorDescr)

Example 3 with OperatorDescr

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());
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) OperatorDescr(org.drools.drl.ast.descr.OperatorDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

OperatorDescr (org.drools.drl.ast.descr.OperatorDescr)3 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Target (org.drools.compiler.rule.builder.EvaluatorDefinition.Target)1 Declaration (org.drools.core.rule.Declaration)1 PredicateConstraint (org.drools.core.rule.PredicateConstraint)1 NegConstraint (org.drools.core.rule.constraint.NegConstraint)1 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)1 Constraint (org.drools.core.spi.Constraint)1 Evaluator (org.drools.core.spi.Evaluator)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 BindingDescr (org.drools.drl.ast.descr.BindingDescr)1