Search in sources :

Example 1 with Operator

use of org.drools.core.base.evaluators.Operator in project drools by kiegroup.

the class MVELDumper method processRestriction.

public String processRestriction(MVELDumperContext context, String left, OperatorDescr operator, String right) {
    StringBuilder sbuilder = new StringBuilder();
    Operator op = Operator.determineOperator(operator.getOperator(), operator.isNegated());
    if (op == Operator.determineOperator("memberOf", operator.isNegated())) {
        int lastAndPos = left.lastIndexOf("&&");
        if (lastAndPos > 0) {
            sbuilder.append(left.substring(0, lastAndPos).trim()).append(" && ");
            left = left.substring(lastAndPos + 2).trim();
        }
        sbuilder.append(evaluatorPrefix(operator.isNegated())).append(right).append(" contains ").append(left).append(evaluatorSufix(operator.isNegated()));
    } else if (op == Operator.determineOperator("contains", operator.isNegated())) {
        sbuilder.append(evaluatorPrefix(operator.isNegated())).append(left).append(" contains ").append(right).append(evaluatorSufix(operator.isNegated()));
    } else if (op == Operator.determineOperator("excludes", operator.isNegated())) {
        sbuilder.append(evaluatorPrefix(!operator.isNegated())).append(left).append(" contains ").append(right).append(evaluatorSufix(!operator.isNegated()));
    } else if (op == Operator.determineOperator("matches", operator.isNegated())) {
        sbuilder.append(evaluatorPrefix(operator.isNegated())).append(left).append(" ~= ").append(right).append(evaluatorSufix(operator.isNegated()));
    } else if (lookupBasicOperator(operator.getOperator())) {
        if (operator.getOperator().equals("instanceof")) {
            context.addInferredCast(left, right);
        }
        rewriteBasicOperator(sbuilder, left, operator, right);
    } else {
        // rewrite operator as a function call
        rewriteOperator(context, sbuilder, left, operator, right);
    }
    return sbuilder.toString();
}
Also used : Operator(org.drools.core.base.evaluators.Operator)

Example 2 with Operator

use of org.drools.core.base.evaluators.Operator in project drools by kiegroup.

the class RuleModelDRLPersistenceImpl method findOperator.

private static String findOperator(String expr) {
    // only need to try to find the first operator before the ConnectiveConstraint separator.
    if (expr.contains("&&")) {
        expr = expr.substring(0, expr.indexOf("&&")).trim();
    }
    if (expr.contains("||")) {
        expr = expr.substring(0, expr.indexOf("||")).trim();
    }
    final Set<String> potentialOperators = new HashSet<String>();
    for (Operator op : Operator.getAllOperators()) {
        String opString = op.getOperatorString();
        if (op.isNegated()) {
            if (expr.contains(" not " + opString)) {
                return "not " + opString;
            }
        }
        int opPos = expr.indexOf(opString);
        if (opPos >= 0 && !isInQuote(expr, opPos) && !(Character.isLetter(opString.charAt(0)) && (expr.length() == opPos + opString.length() || Character.isLetter(expr.charAt(opPos + opString.length())) || (opPos > 0 && Character.isLetter(expr.charAt(opPos - 1)))))) {
            potentialOperators.add(opString);
        }
    }
    String operator = "";
    if (!potentialOperators.isEmpty()) {
        for (String potentialOperator : potentialOperators) {
            if (potentialOperator.length() > operator.length()) {
                operator = potentialOperator;
            }
        }
    }
    if (!operator.isEmpty()) {
        return operator;
    }
    if (expr.contains(" not in ")) {
        return " not in ";
    }
    if (expr.contains(" in ")) {
        return " in ";
    }
    return null;
}
Also used : Operator(org.drools.core.base.evaluators.Operator) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) HashSet(java.util.HashSet)

Example 3 with Operator

use of org.drools.core.base.evaluators.Operator in project drools by kiegroup.

the class ExprConstraintDescrVisitor method visit.

private void visit(RelationalExprDescr descr) {
    int currentOrderNumber = orderNumber.next();
    String fieldName = visit(descr.getLeft());
    Operator operator = Operator.determineOperator(descr.getOperator(), descr.isNegated());
    String value = visit(descr.getRight());
    setField(descr, fieldName);
    if (isAVariableRestriction(value)) {
        createVariableRestriction(currentOrderNumber, value, operator);
    } else {
        createRestriction(currentOrderNumber, value, operator);
    }
}
Also used : Operator(org.drools.core.base.evaluators.Operator)

Aggregations

Operator (org.drools.core.base.evaluators.Operator)3 HashSet (java.util.HashSet)1 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)1 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)1 ConnectiveConstraint (org.drools.workbench.models.datamodel.rule.ConnectiveConstraint)1 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)1 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)1