Search in sources :

Example 11 with AtomicExprDescr

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

the class DRL5Expressions method parExpression.

// $ANTLR end "mapEntry"
// $ANTLR start "parExpression"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:577:1: parExpression returns [BaseDescr result] : LEFT_PAREN expr= expression RIGHT_PAREN ;
public final BaseDescr parExpression() throws RecognitionException {
    BaseDescr result = null;
    ParserRuleReturnScope expr = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:578:5: ( LEFT_PAREN expr= expression RIGHT_PAREN )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:578:7: LEFT_PAREN expr= expression RIGHT_PAREN
        {
            match(input, LEFT_PAREN, FOLLOW_LEFT_PAREN_in_parExpression3200);
            if (state.failed)
                return result;
            pushFollow(FOLLOW_expression_in_parExpression3204);
            expr = expression();
            state._fsp--;
            if (state.failed)
                return result;
            match(input, RIGHT_PAREN, FOLLOW_RIGHT_PAREN_in_parExpression3206);
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = (expr != null ? ((DRL5Expressions.expression_return) expr).result : null);
                    if (result instanceof AtomicExprDescr) {
                        ((AtomicExprDescr) result).setExpression("(" + ((AtomicExprDescr) result).getExpression() + ")");
                    }
                }
            }
        }
    } 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)

Example 12 with AtomicExprDescr

use of org.drools.compiler.lang.descr.AtomicExprDescr 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 13 with AtomicExprDescr

use of org.drools.compiler.lang.descr.AtomicExprDescr 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 14 with AtomicExprDescr

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

the class PatternBuilder method processAtomicExpression.

protected Constraint processAtomicExpression(RuleBuildContext context, Pattern pattern, BaseDescr d, String expr, Map<String, OperatorDescr> aliases) {
    if (d instanceof AtomicExprDescr) {
        Matcher m = evalRegexp.matcher(((AtomicExprDescr) d).getExpression());
        if (m.find()) {
            // MVELDumper already stripped the eval
            // this will build the eval using the specified dialect
            PredicateDescr pdescr = new PredicateDescr(context.getRuleDescr().getResource(), expr);
            pdescr.copyLocation(d);
            return buildEval(context, pattern, pdescr, aliases, expr, true);
        }
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) PredicateDescr(org.drools.compiler.lang.descr.PredicateDescr)

Example 15 with AtomicExprDescr

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

the class MVELDumper method processInlineCast.

private String[] processInlineCast(String expr, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr ccd, MVELDumperContext context, int sharpPos, int parentIdx, int childIdx) {
    // convert "field1#Class.field2" in ["field1 instanceof Class && ", "((Class)field1).field2"]
    String field1 = expr.substring(0, sharpPos).trim();
    int sharpPos2 = expr.indexOf('#', sharpPos + 1);
    String part2 = sharpPos2 < 0 ? expr.substring(sharpPos + 1).trim() : expr.substring(sharpPos + 1, sharpPos2).trim();
    String[] classAndField = splitInClassAndField(part2, context);
    BaseDescr desc = parentIdx >= 0 ? ccd.getDescrs().get(parentIdx) : null;
    if (classAndField == null) {
        return new String[] { "", expr };
    } else if (desc instanceof AtomicExprDescr && classAndField.length == 1) {
        return new String[] { "", field1 + " instanceof " + classAndField[0] };
    }
    String className = classAndField[0];
    String castedExpression = classAndField.length == 1 ? "((" + className + ")" + field1 + ")" : "((" + className + ")" + field1 + ")." + classAndField[1] + (sharpPos2 > 0 ? expr.substring(sharpPos2) : "");
    RelationalExprDescr check = new RelationalExprDescr("instanceof", false, null, new AtomicExprDescr(field1), new AtomicExprDescr(className));
    if (ccd.getConnective() == ConnectiveType.AND || ccd.getConnective() == ConnectiveType.INC_AND) {
        ccd.getDescrs().add(childIdx, check);
    } else {
        if (desc instanceof ConstraintConnectiveDescr) {
            ((ConstraintConnectiveDescr) desc).getDescrs().add(childIdx, check);
        } else {
            ConstraintConnectiveDescr localAnd = new ConstraintConnectiveDescr(ConnectiveType.AND);
            BaseDescr original = ccd.getDescrs().remove(parentIdx);
            localAnd.getDescrs().add(check);
            localAnd.getDescrs().add(original);
            ccd.getDescrs().add(parentIdx, localAnd);
        }
    }
    atomicExpr.setRewrittenExpression(castedExpression);
    String innerCheck = check.toString() + " && ";
    return new String[] { innerCheck, castedExpression };
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr)

Aggregations

AtomicExprDescr (org.drools.compiler.lang.descr.AtomicExprDescr)29 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)17 RelationalExprDescr (org.drools.compiler.lang.descr.RelationalExprDescr)16 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)15 AnnotatedBaseDescr (org.drools.compiler.lang.descr.AnnotatedBaseDescr)14 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)10 Test (org.junit.Test)10 Matcher (java.util.regex.Matcher)1 PredicateDescr (org.drools.compiler.lang.descr.PredicateDescr)1 PredicateConstraint (org.drools.core.rule.PredicateConstraint)1 EvaluatorConstraint (org.drools.core.rule.constraint.EvaluatorConstraint)1 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)1 NegConstraint (org.drools.core.rule.constraint.NegConstraint)1 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)1 Constraint (org.drools.core.spi.Constraint)1