Search in sources :

Example 1 with BaseDescr

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

the class TypeDeclarationConfigurator method wireTimestampAccessor.

private static void wireTimestampAccessor(KnowledgeBuilderImpl kbuilder, Annotated annotated, TypeDeclaration type, PackageRegistry pkgRegistry) {
    Timestamp timestamp = annotated.getTypedAnnotation(Timestamp.class);
    if (timestamp != null) {
        BaseDescr typeDescr = annotated instanceof BaseDescr ? ((BaseDescr) annotated) : new BaseDescr();
        String timestampField;
        try {
            timestampField = timestamp.value();
        } catch (Exception e) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, e.getMessage()));
            return;
        }
        type.setTimestampAttribute(timestampField);
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        MVELAnalysisResult results = getMvelAnalysisResult(kbuilder, typeDescr, type, pkgRegistry, timestampField, pkg);
        if (results != null) {
            type.setTimestampExtractor(getFieldExtractor(type, timestampField, pkg, results));
        } else {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Error creating field accessors for timestamp field '" + timestamp + "' for type '" + type.getTypeName() + "'"));
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Timestamp(org.kie.api.definition.type.Timestamp) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with BaseDescr

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

the class DrlExprParser method parse.

/**
 * Parse an expression from text
 */
public ConstraintConnectiveDescr parse(final String text) {
    ConstraintConnectiveDescr constraint = null;
    try {
        DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
        CommonTokenStream input = new CommonTokenStream(lexer);
        RecognizerSharedState state = new RecognizerSharedState();
        helper = new ParserHelper(input, state, languageLevel);
        DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
        parser.setBuildDescr(true);
        // setting initial value just in case
        parser.setLeftMostExpr(null);
        BaseDescr expr = parser.conditionalOrExpression();
        if (expr != null && !parser.hasErrors()) {
            constraint = ConstraintConnectiveDescr.newAnd();
            constraint.addOrMerge(expr);
        }
    } catch (RecognitionException e) {
        helper.reportError(e);
    }
    return constraint;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) DRLExpressions(org.drools.compiler.lang.DRLExpressions) DRLLexer(org.drools.compiler.lang.DRLLexer) ParserHelper(org.drools.compiler.lang.ParserHelper) RecognizerSharedState(org.antlr.runtime.RecognizerSharedState) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Example 3 with BaseDescr

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

the class DRL6Expressions method inExpression.

// $ANTLR end "instanceOfExpression"
// $ANTLR start "inExpression"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:325: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/DRL6Expressions.g:328: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/DRL6Expressions.g:328: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_inExpression1615);
            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/DRL6Expressions.g:337: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))) && synpred8_DRL6Expressions()))) {
                        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/DRL6Expressions.g:337:6: ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN
                    {
                        pushFollow(FOLLOW_not_key_in_inExpression1635);
                        not_key();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        pushFollow(FOLLOW_in_key_in_inExpression1639);
                        in_key();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        match(input, LEFT_PAREN, FOLLOW_LEFT_PAREN_in_inExpression1641);
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
                        }
                        pushFollow(FOLLOW_expression_in_inExpression1663);
                        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 ? ((DRL6Expressions.expression_return) e1).result : null));
                            descr.addOrMerge(rel);
                            result = descr;
                        }
                        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:345: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/DRL6Expressions.g:345:8: COMMA e2= expression
                                    {
                                        match(input, COMMA, FOLLOW_COMMA_in_inExpression1682);
                                        if (state.failed)
                                            return result;
                                        pushFollow(FOLLOW_expression_in_inExpression1686);
                                        e2 = expression();
                                        state._fsp--;
                                        if (state.failed)
                                            return result;
                                        if (state.backtracking == 0) {
                                            RelationalExprDescr rel = new RelationalExprDescr("!=", false, null, leftDescr, (e2 != null ? ((DRL6Expressions.expression_return) e2).result : null));
                                            descr.addOrMerge(rel);
                                        }
                                    }
                                    break;
                                default:
                                    break loop34;
                            }
                        }
                        match(input, RIGHT_PAREN, FOLLOW_RIGHT_PAREN_in_inExpression1707);
                        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/DRL6Expressions.g:351:7: in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN
                    {
                        pushFollow(FOLLOW_in_key_in_inExpression1723);
                        in_key();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        match(input, LEFT_PAREN, FOLLOW_LEFT_PAREN_in_inExpression1725);
                        if (state.failed)
                            return result;
                        if (state.backtracking == 0) {
                            helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
                        }
                        pushFollow(FOLLOW_expression_in_inExpression1747);
                        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 ? ((DRL6Expressions.expression_return) e1).result : null));
                            descr.addOrMerge(rel);
                            result = descr;
                        }
                        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:359: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/DRL6Expressions.g:359:8: COMMA e2= expression
                                    {
                                        match(input, COMMA, FOLLOW_COMMA_in_inExpression1766);
                                        if (state.failed)
                                            return result;
                                        pushFollow(FOLLOW_expression_in_inExpression1770);
                                        e2 = expression();
                                        state._fsp--;
                                        if (state.failed)
                                            return result;
                                        if (state.backtracking == 0) {
                                            RelationalExprDescr rel = new RelationalExprDescr("==", false, null, leftDescr, (e2 != null ? ((DRL6Expressions.expression_return) e2).result : null));
                                            descr.addOrMerge(rel);
                                        }
                                    }
                                    break;
                                default:
                                    break loop35;
                            }
                        }
                        match(input, RIGHT_PAREN, FOLLOW_RIGHT_PAREN_in_inExpression1791);
                        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 4 with BaseDescr

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

the class DRL6Expressions method xpathChunk.

// $ANTLR end "xpathPrimary"
// $ANTLR start "xpathChunk"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:568:1: xpathChunk returns [BaseDescr result] : ( xpathSeparator ID )=> xpathSeparator ID ( DOT ID )* ( HASH ID )? ( LEFT_SQUARE xpathExpressionList RIGHT_SQUARE )? ;
public final BaseDescr xpathChunk() throws RecognitionException {
    BaseDescr result = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:569:5: ( ( xpathSeparator ID )=> xpathSeparator ID ( DOT ID )* ( HASH ID )? ( LEFT_SQUARE xpathExpressionList RIGHT_SQUARE )? )
        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:569:7: ( xpathSeparator ID )=> xpathSeparator ID ( DOT ID )* ( HASH ID )? ( LEFT_SQUARE xpathExpressionList RIGHT_SQUARE )?
        {
            pushFollow(FOLLOW_xpathSeparator_in_xpathChunk2947);
            xpathSeparator();
            state._fsp--;
            if (state.failed)
                return result;
            match(input, ID, FOLLOW_ID_in_xpathChunk2949);
            if (state.failed)
                return result;
            // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:569:47: ( DOT ID )*
            loop59: while (true) {
                int alt59 = 2;
                int LA59_0 = input.LA(1);
                if ((LA59_0 == DOT)) {
                    int LA59_2 = input.LA(2);
                    if ((LA59_2 == ID)) {
                        alt59 = 1;
                    }
                }
                switch(alt59) {
                    case 1:
                        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:569:48: DOT ID
                        {
                            match(input, DOT, FOLLOW_DOT_in_xpathChunk2952);
                            if (state.failed)
                                return result;
                            match(input, ID, FOLLOW_ID_in_xpathChunk2954);
                            if (state.failed)
                                return result;
                        }
                        break;
                    default:
                        break loop59;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:569:57: ( HASH ID )?
            int alt60 = 2;
            int LA60_0 = input.LA(1);
            if ((LA60_0 == HASH)) {
                alt60 = 1;
            }
            switch(alt60) {
                case 1:
                    // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:569:58: HASH ID
                    {
                        match(input, HASH, FOLLOW_HASH_in_xpathChunk2959);
                        if (state.failed)
                            return result;
                        match(input, ID, FOLLOW_ID_in_xpathChunk2961);
                        if (state.failed)
                            return result;
                    }
                    break;
            }
            // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:569:68: ( LEFT_SQUARE xpathExpressionList RIGHT_SQUARE )?
            int alt61 = 2;
            int LA61_0 = input.LA(1);
            if ((LA61_0 == LEFT_SQUARE)) {
                alt61 = 1;
            }
            switch(alt61) {
                case 1:
                    // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:569:69: LEFT_SQUARE xpathExpressionList RIGHT_SQUARE
                    {
                        match(input, LEFT_SQUARE, FOLLOW_LEFT_SQUARE_in_xpathChunk2966);
                        if (state.failed)
                            return result;
                        pushFollow(FOLLOW_xpathExpressionList_in_xpathChunk2968);
                        xpathExpressionList();
                        state._fsp--;
                        if (state.failed)
                            return result;
                        match(input, RIGHT_SQUARE, FOLLOW_RIGHT_SQUARE_in_xpathChunk2970);
                        if (state.failed)
                            return result;
                    }
                    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)

Example 5 with BaseDescr

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

the class DRL6Expressions method conditionalExpression.

// $ANTLR end "expression"
// $ANTLR start "conditionalExpression"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:168:1: conditionalExpression returns [BaseDescr result] : left= conditionalOrExpression ( ternaryExpression )? ;
public final BaseDescr conditionalExpression() throws RecognitionException {
    BaseDescr result = null;
    BaseDescr left = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:169:5: (left= conditionalOrExpression ( ternaryExpression )? )
        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:169:9: left= conditionalOrExpression ( ternaryExpression )?
        {
            pushFollow(FOLLOW_conditionalOrExpression_in_conditionalExpression820);
            left = conditionalOrExpression();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:170:9: ( ternaryExpression )?
            int alt17 = 2;
            int LA17_0 = input.LA(1);
            if ((LA17_0 == QUESTION)) {
                alt17 = 1;
            }
            switch(alt17) {
                case 1:
                    // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:170:9: ternaryExpression
                    {
                        pushFollow(FOLLOW_ternaryExpression_in_conditionalExpression832);
                        ternaryExpression();
                        state._fsp--;
                        if (state.failed)
                            return result;
                    }
                    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)

Aggregations

BaseDescr (org.drools.compiler.lang.descr.BaseDescr)109 AnnotatedBaseDescr (org.drools.compiler.lang.descr.AnnotatedBaseDescr)60 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)23 AtomicExprDescr (org.drools.compiler.lang.descr.AtomicExprDescr)17 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)16 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)12 AndDescr (org.drools.compiler.lang.descr.AndDescr)11 RelationalExprDescr (org.drools.compiler.lang.descr.RelationalExprDescr)11 DeclareDescrBuilder (org.drools.compiler.lang.api.DeclareDescrBuilder)9 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)7 ArrayList (java.util.ArrayList)6 AccumulateDescrBuilder (org.drools.compiler.lang.api.AccumulateDescrBuilder)6 AnnotatedDescrBuilder (org.drools.compiler.lang.api.AnnotatedDescrBuilder)6 AnnotationDescrBuilder (org.drools.compiler.lang.api.AnnotationDescrBuilder)6 AttributeDescrBuilder (org.drools.compiler.lang.api.AttributeDescrBuilder)6 BehaviorDescrBuilder (org.drools.compiler.lang.api.BehaviorDescrBuilder)6 CEDescrBuilder (org.drools.compiler.lang.api.CEDescrBuilder)6 CollectDescrBuilder (org.drools.compiler.lang.api.CollectDescrBuilder)6 ConditionalBranchDescrBuilder (org.drools.compiler.lang.api.ConditionalBranchDescrBuilder)6