Search in sources :

Example 6 with RelationalExprDescr

use of org.drools.compiler.lang.descr.RelationalExprDescr in project drools by kiegroup.

the class DRL5Expressions method instanceOfExpression.

// $ANTLR end "equalityExpression"
// $ANTLR start "instanceOfExpression"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:298:1: instanceOfExpression returns [BaseDescr result] : left= inExpression (op= instanceof_key right= type )? ;
public final BaseDescr instanceOfExpression() throws RecognitionException {
    BaseDescr result = null;
    BaseDescr left = null;
    ParserRuleReturnScope op = null;
    ParserRuleReturnScope right = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:299:3: (left= inExpression (op= instanceof_key right= type )? )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:299:5: left= inExpression (op= instanceof_key right= type )?
        {
            pushFollow(FOLLOW_inExpression_in_instanceOfExpression1464);
            left = inExpression();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:300:3: (op= instanceof_key right= type )?
            int alt33 = 2;
            int LA33_0 = input.LA(1);
            if ((LA33_0 == ID)) {
                int LA33_1 = input.LA(2);
                if ((LA33_1 == ID) && (((helper.validateIdentifierKey(DroolsSoftKeywords.INSTANCEOF))))) {
                    alt33 = 1;
                }
            }
            switch(alt33) {
                case 1:
                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:300:5: op= instanceof_key right= type
                    {
                        pushFollow(FOLLOW_instanceof_key_in_instanceOfExpression1474);
                        op = instanceof_key();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            helper.setHasOperator(true);
                            if (input.LA(1) != DRL5Lexer.EOF)
                                helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
                        }
                        pushFollow(FOLLOW_type_in_instanceOfExpression1488);
                        right = type();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            if (buildDescr) {
                                result = new RelationalExprDescr((op != null ? input.toString(op.start, op.stop) : null), false, null, left, new AtomicExprDescr((right != null ? input.toString(right.start, right.stop) : null)));
                            }
                        }
                    }
                    break;
            }
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr) AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr)

Example 7 with RelationalExprDescr

use of org.drools.compiler.lang.descr.RelationalExprDescr in project drools by kiegroup.

the class DRL5Expressions method equalityExpression.

// $ANTLR end "andExpression"
// $ANTLR start "equalityExpression"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:285:1: equalityExpression returns [BaseDescr result] : left= instanceOfExpression ( (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression )* ;
public final BaseDescr equalityExpression() throws RecognitionException {
    BaseDescr result = null;
    Token op = null;
    BaseDescr left = null;
    BaseDescr right = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:286:3: (left= instanceOfExpression ( (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression )* )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:286:5: left= instanceOfExpression ( (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression )*
        {
            pushFollow(FOLLOW_instanceOfExpression_in_equalityExpression1395);
            left = instanceOfExpression();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:287:3: ( (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression )*
            loop32: while (true) {
                int alt32 = 2;
                int LA32_0 = input.LA(1);
                if ((LA32_0 == EQUALS || LA32_0 == NOT_EQUALS)) {
                    alt32 = 1;
                }
                switch(alt32) {
                    case 1:
                        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:287:5: (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression
                        {
                            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:287:5: (op= EQUALS |op= NOT_EQUALS )
                            int alt31 = 2;
                            int LA31_0 = input.LA(1);
                            if ((LA31_0 == EQUALS)) {
                                alt31 = 1;
                            } else if ((LA31_0 == NOT_EQUALS)) {
                                alt31 = 2;
                            } else {
                                if (state.backtracking > 0) {
                                    state.failed = true;
                                    return result;
                                }
                                NoViableAltException nvae = new NoViableAltException("", 31, 0, input);
                                throw nvae;
                            }
                            switch(alt31) {
                                case 1:
                                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:287:7: op= EQUALS
                                    {
                                        op = (Token) match(input, EQUALS, FOLLOW_EQUALS_in_equalityExpression1407);
                                        if (state.failed)
                                            return result;
                                    }
                                    break;
                                case 2:
                                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:287:19: op= NOT_EQUALS
                                    {
                                        op = (Token) match(input, NOT_EQUALS, FOLLOW_NOT_EQUALS_in_equalityExpression1413);
                                        if (state.failed)
                                            return result;
                                    }
                                    break;
                            }
                            if (state.backtracking == 0) {
                                helper.setHasOperator(true);
                                if (input.LA(1) != DRL5Lexer.EOF)
                                    helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
                            }
                            pushFollow(FOLLOW_instanceOfExpression_in_equalityExpression1429);
                            right = instanceOfExpression();
                            state._fsp--;
                            if (state.failed)
                                return result;
                            if (state.backtracking == 0) {
                                if (buildDescr) {
                                    result = new RelationalExprDescr((op != null ? op.getText() : null), false, null, left, right);
                                }
                            }
                        }
                        break;
                    default:
                        break loop32;
                }
            }
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr)

Example 8 with RelationalExprDescr

use of org.drools.compiler.lang.descr.RelationalExprDescr in project drools by kiegroup.

the class DRL5Expressions method inExpression.

// $ANTLR end "instanceOfExpression"
// $ANTLR start "inExpression"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:311:1: inExpression returns [BaseDescr result] : left= relationalExpression ( ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN |in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN )? ;
public final BaseDescr inExpression() throws RecognitionException {
    BaseDescr result = null;
    BaseDescr left = null;
    ParserRuleReturnScope e1 = null;
    ParserRuleReturnScope e2 = null;
    ConstraintConnectiveDescr descr = null;
    BaseDescr leftDescr = null;
    BindingDescr binding = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:314:3: (left= relationalExpression ( ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN |in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN )? )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:314:5: left= relationalExpression ( ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN |in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN )?
        {
            pushFollow(FOLLOW_relationalExpression_in_inExpression1533);
            left = relationalExpression();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
                if (left instanceof BindingDescr) {
                    binding = (BindingDescr) left;
                    leftDescr = new AtomicExprDescr(binding.getExpression());
                } else {
                    leftDescr = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:323:5: ( ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN |in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN )?
            int alt36 = 3;
            int LA36_0 = input.LA(1);
            if ((LA36_0 == ID)) {
                int LA36_1 = input.LA(2);
                if ((LA36_1 == ID)) {
                    int LA36_3 = input.LA(3);
                    if ((LA36_3 == LEFT_PAREN) && ((((helper.validateIdentifierKey(DroolsSoftKeywords.NOT))) && synpred7_DRL5Expressions()))) {
                        alt36 = 1;
                    }
                } else if ((LA36_1 == LEFT_PAREN) && (((helper.validateIdentifierKey(DroolsSoftKeywords.IN))))) {
                    alt36 = 2;
                }
            }
            switch(alt36) {
                case 1:
                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:323:6: ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN
                    {
                        pushFollow(FOLLOW_not_key_in_inExpression1553);
                        not_key();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        pushFollow(FOLLOW_in_key_in_inExpression1557);
                        in_key();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        match(input, LEFT_PAREN, FOLLOW_LEFT_PAREN_in_inExpression1559);
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
                        }
                        pushFollow(FOLLOW_expression_in_inExpression1581);
                        e1 = expression();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            descr = ConstraintConnectiveDescr.newAnd();
                            RelationalExprDescr rel = new RelationalExprDescr("!=", false, null, leftDescr, (e1 != null ? ((DRL5Expressions.expression_return) e1).result : null));
                            descr.addOrMerge(rel);
                            result = descr;
                        }
                        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:331:7: ( COMMA e2= expression )*
                        loop34: while (true) {
                            int alt34 = 2;
                            int LA34_0 = input.LA(1);
                            if ((LA34_0 == COMMA)) {
                                alt34 = 1;
                            }
                            switch(alt34) {
                                case 1:
                                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:331:8: COMMA e2= expression
                                    {
                                        match(input, COMMA, FOLLOW_COMMA_in_inExpression1600);
                                        if (state.failed)
                                            return result;
                                        pushFollow(FOLLOW_expression_in_inExpression1604);
                                        e2 = expression();
                                        state._fsp--;
                                        if (state.failed)
                                            return result;
                                        if (state.backtracking == 0) {
                                            RelationalExprDescr rel = new RelationalExprDescr("!=", false, null, leftDescr, (e2 != null ? ((DRL5Expressions.expression_return) e2).result : null));
                                            descr.addOrMerge(rel);
                                        }
                                    }
                                    break;
                                default:
                                    break loop34;
                            }
                        }
                        match(input, RIGHT_PAREN, FOLLOW_RIGHT_PAREN_in_inExpression1625);
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
                        }
                    }
                    break;
                case 2:
                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:337:7: in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN
                    {
                        pushFollow(FOLLOW_in_key_in_inExpression1641);
                        in_key();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        match(input, LEFT_PAREN, FOLLOW_LEFT_PAREN_in_inExpression1643);
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
                        }
                        pushFollow(FOLLOW_expression_in_inExpression1665);
                        e1 = expression();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            descr = ConstraintConnectiveDescr.newOr();
                            RelationalExprDescr rel = new RelationalExprDescr("==", false, null, leftDescr, (e1 != null ? ((DRL5Expressions.expression_return) e1).result : null));
                            descr.addOrMerge(rel);
                            result = descr;
                        }
                        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:345:7: ( COMMA e2= expression )*
                        loop35: while (true) {
                            int alt35 = 2;
                            int LA35_0 = input.LA(1);
                            if ((LA35_0 == COMMA)) {
                                alt35 = 1;
                            }
                            switch(alt35) {
                                case 1:
                                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:345:8: COMMA e2= expression
                                    {
                                        match(input, COMMA, FOLLOW_COMMA_in_inExpression1684);
                                        if (state.failed)
                                            return result;
                                        pushFollow(FOLLOW_expression_in_inExpression1688);
                                        e2 = expression();
                                        state._fsp--;
                                        if (state.failed)
                                            return result;
                                        if (state.backtracking == 0) {
                                            RelationalExprDescr rel = new RelationalExprDescr("==", false, null, leftDescr, (e2 != null ? ((DRL5Expressions.expression_return) e2).result : null));
                                            descr.addOrMerge(rel);
                                        }
                                    }
                                    break;
                                default:
                                    break loop35;
                            }
                        }
                        match(input, RIGHT_PAREN, FOLLOW_RIGHT_PAREN_in_inExpression1709);
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
                        }
                    }
                    break;
            }
        }
        if (state.backtracking == 0) {
            if (binding != null && descr != null)
                descr.addOrMerge(binding);
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr) AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr)

Example 9 with RelationalExprDescr

use of org.drools.compiler.lang.descr.RelationalExprDescr in project drools by kiegroup.

the class PatternBuilder method rewriteCompositeExpressions.

private String rewriteCompositeExpressions(RuleBuildContext context, Pattern pattern, ConstraintConnectiveDescr d) {
    int i = 0;
    StringBuilder sb = new StringBuilder();
    for (BaseDescr subDescr : d.getDescrs()) {
        if (subDescr instanceof BindingDescr) {
            continue;
        }
        if (i++ > 0) {
            sb.append(" ").append(d.getConnective().getConnective()).append(" ");
        }
        String normalizedExpr;
        if (subDescr instanceof RelationalExprDescr && isSimpleExpr((RelationalExprDescr) subDescr)) {
            RelationalExprDescr relDescr = (RelationalExprDescr) subDescr;
            if (relDescr.getExpression() != null) {
                normalizedExpr = normalizeExpression(context, pattern, relDescr, relDescr.getExpression());
            } else {
                i--;
                normalizedExpr = "";
            }
        } else if (subDescr instanceof ConstraintConnectiveDescr) {
            String rewrittenExpr = rewriteCompositeExpressions(context, pattern, (ConstraintConnectiveDescr) subDescr);
            if (rewrittenExpr == null) {
                return null;
            }
            normalizedExpr = "(" + rewrittenExpr + ")";
        } else if (subDescr instanceof AtomicExprDescr) {
            normalizedExpr = ((AtomicExprDescr) subDescr).getRewrittenExpression();
        } else {
            return null;
        }
        sb.append(normalizedExpr);
    }
    return sb.toString();
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr)

Example 10 with RelationalExprDescr

use of org.drools.compiler.lang.descr.RelationalExprDescr in project drools by kiegroup.

the class PatternBuilder method buildExpression.

protected Constraint buildExpression(final RuleBuildContext context, final Pattern pattern, final BaseDescr d, final String expr, final Map<String, OperatorDescr> aliases) {
    if ("_.neg".equals(expr)) {
        pattern.setHasNegativeConstraint(true);
        return new NegConstraint();
    } else if ("!_.neg".equals(expr)) {
        pattern.setHasNegativeConstraint(true);
        return new NegConstraint(false);
    }
    RelationalExprDescr relDescr = d instanceof RelationalExprDescr ? (RelationalExprDescr) d : null;
    boolean simple = isSimpleExpr(relDescr);
    if (// simple means also relDescr is != null
    simple && !ClassObjectType.Map_ObjectType.isAssignableFrom(pattern.getObjectType()) && !ClassObjectType.Match_ObjectType.isAssignableFrom(pattern.getObjectType())) {
        String normalizedExpr = normalizeExpression(context, pattern, relDescr, expr);
        return buildRelationalExpression(context, pattern, relDescr, normalizedExpr, aliases);
    }
    // Or it's a Map and we have to treat it as a special case
    return createAndBuildPredicate(context, pattern, d, rewriteOrExpressions(context, pattern, d, expr), aliases);
}
Also used : NegConstraint(org.drools.core.rule.constraint.NegConstraint) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr)

Aggregations

RelationalExprDescr (org.drools.compiler.lang.descr.RelationalExprDescr)19 AtomicExprDescr (org.drools.compiler.lang.descr.AtomicExprDescr)16 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)12 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)11 AnnotatedBaseDescr (org.drools.compiler.lang.descr.AnnotatedBaseDescr)8 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)8 Test (org.junit.Test)7 NegConstraint (org.drools.core.rule.constraint.NegConstraint)2 PredicateConstraint (org.drools.core.rule.PredicateConstraint)1 EvaluatorConstraint (org.drools.core.rule.constraint.EvaluatorConstraint)1 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)1 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)1 Constraint (org.drools.core.spi.Constraint)1