Search in sources :

Example 1 with Operator

use of org.drools.drl.parser.impl.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.drl.parser.impl.Operator)

Example 2 with Operator

use of org.drools.drl.parser.impl.Operator in project drools by kiegroup.

the class DescrDumper method processRestriction.

public String processRestriction(DumperContext 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.drl.parser.impl.Operator)

Example 3 with Operator

use of org.drools.drl.parser.impl.Operator in project drools by kiegroup.

the class BaseBetaConstraintsTest method getCheeseTypeConstraint.

protected BetaNodeFieldConstraint getCheeseTypeConstraint(final String identifier, Operator operator) {
    if (useLambdaConstraint) {
        Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
        Predicate1<Cheese> predicate;
        if (operator == Operator.BuiltInOperator.EQUAL.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.areNullSafeEquals(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.NOT_EQUAL.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> !EvaluationUtil.areNullSafeEquals(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.GREATER.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.greaterThan(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.GREATER_OR_EQUAL.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.greaterOrEqual(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.LESS.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.lessThan(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.GREATER_OR_EQUAL.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.lessOrEqual(_this.getType(), identifier));
        } else {
            throw new RuntimeException(operator + " is not supported");
        }
        return LambdaConstraintTestUtil.createLambdaConstraint1(Cheese.class, pattern, predicate, null);
    } else {
        ClassFieldAccessorStore store = new ClassFieldAccessorStore();
        store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
        store.setEagerWire(true);
        InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
        Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(Cheese.class)));
        String expression = "type " + operator.getOperatorString() + " " + identifier;
        return new MVELConstraintTestUtil(expression, operator.getOperatorString(), declaration, extractor);
    }
}
Also used : LinkedList(org.drools.core.util.LinkedList) ClassObjectType(org.drools.core.base.ClassObjectType) BetaConstraints(org.drools.core.common.BetaConstraints) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) EvaluatorRegistry(org.drools.compiler.builder.impl.EvaluatorRegistry) Pattern(org.drools.core.rule.Pattern) RunWith(org.junit.runner.RunWith) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) BetaMemory(org.drools.core.reteoo.BetaMemory) ArrayList(java.util.ArrayList) ConstraintType(org.drools.core.util.index.IndexUtil.ConstraintType) Assert.assertSame(org.junit.Assert.assertSame) Declaration(org.drools.core.rule.Declaration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) Index(org.drools.core.util.AbstractHashTable.Index) LinkedListEntry(org.drools.core.util.LinkedListEntry) TupleList(org.drools.core.util.index.TupleList) Parameterized(org.junit.runners.Parameterized) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) Collection(java.util.Collection) Operator(org.drools.drl.parser.impl.Operator) Assert.assertTrue(org.junit.Assert.assertTrue) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) Predicate1(org.drools.model.functions.Predicate1) EvaluationUtil(org.drools.modelcompiler.util.EvaluationUtil) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) Cheese(org.drools.mvel.model.Cheese) Assert.assertEquals(org.junit.Assert.assertEquals) NodeTypeEnums(org.drools.core.reteoo.NodeTypeEnums) IndexableConstraint(org.drools.core.rule.IndexableConstraint) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.mvel.model.Cheese) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Predicate1(org.drools.model.functions.Predicate1) Declaration(org.drools.core.rule.Declaration)

Aggregations

Operator (org.drools.drl.parser.impl.Operator)3 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 EvaluatorRegistry (org.drools.compiler.builder.impl.EvaluatorRegistry)1 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)1 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 BetaConstraints (org.drools.core.common.BetaConstraints)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1 NodeTypeEnums (org.drools.core.reteoo.NodeTypeEnums)1 Declaration (org.drools.core.rule.Declaration)1 IndexableConstraint (org.drools.core.rule.IndexableConstraint)1 Pattern (org.drools.core.rule.Pattern)1 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)1 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)1 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)1 Index (org.drools.core.util.AbstractHashTable.Index)1 LinkedList (org.drools.core.util.LinkedList)1 LinkedListEntry (org.drools.core.util.LinkedListEntry)1