Search in sources :

Example 1 with AnnotationDescr

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

the class ClassHierarchyManager method buildInheritedFieldDescrFromDefinition.

protected TypeFieldDescr buildInheritedFieldDescrFromDefinition(org.kie.api.definition.type.FactField fld, TypeDeclarationDescr typeDescr) {
    TypeFieldDescr inheritedFldDescr = new TypeFieldDescr();
    inheritedFldDescr.setFieldName(fld.getName());
    inheritedFldDescr.setResource(typeDescr.getResource());
    PatternDescr fldType = new PatternDescr();
    fldType.setObjectType(((FieldDefinition) fld).getTypeName());
    // also sets resource for PatternDescr fldType
    inheritedFldDescr.setPattern(fldType);
    if (fld.isKey()) {
        AnnotationDescr keyAnnotation = new AnnotationDescr(Key.class.getCanonicalName());
        keyAnnotation.setFullyQualifiedName(Key.class.getCanonicalName());
        keyAnnotation.setResource(typeDescr.getResource());
        inheritedFldDescr.addAnnotation(keyAnnotation);
    }
    inheritedFldDescr.setIndex(((FieldDefinition) fld).getDeclIndex());
    inheritedFldDescr.setInherited(true);
    String initExprOverride = ((FieldDefinition) fld).getInitExpr();
    int overrideCount = 0;
    // only @aliasing local fields may override defaults.
    for (TypeFieldDescr localField : typeDescr.getFields().values()) {
        Alias alias = localField.getTypedAnnotation(Alias.class);
        if (alias != null && fld.getName().equals(alias.value().replaceAll("\"", "")) && localField.getInitExpr() != null) {
            overrideCount++;
            initExprOverride = localField.getInitExpr();
        }
    }
    if (overrideCount > 1) {
        // however, only one is allowed
        initExprOverride = null;
    }
    inheritedFldDescr.setInitExpr(initExprOverride);
    return inheritedFldDescr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) Alias(org.drools.core.factmodel.traits.Alias) FieldDefinition(org.drools.core.factmodel.FieldDefinition) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Key(org.kie.api.definition.type.Key)

Example 2 with AnnotationDescr

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

the class KnowledgeBuilderImpl method normalizeAnnotations.

protected void normalizeAnnotations(AnnotatedBaseDescr annotationsContainer, TypeResolver typeResolver, boolean isStrict) {
    for (AnnotationDescr annotationDescr : annotationsContainer.getAnnotations()) {
        annotationDescr.setResource(annotationsContainer.getResource());
        annotationDescr.setStrict(isStrict);
        if (annotationDescr.isDuplicated()) {
            addBuilderResult(new AnnotationDeclarationError(annotationDescr, "Duplicated annotation: " + annotationDescr.getName()));
        }
        if (isStrict) {
            normalizeStrictAnnotation(typeResolver, annotationDescr);
        } else {
            normalizeAnnotation(typeResolver, annotationDescr);
        }
    }
    annotationsContainer.indexByFQN(isStrict);
}
Also used : AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 3 with AnnotationDescr

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

the class DRL6Expressions method fullAnnotation.

// $ANTLR end "ternaryExpression"
// $ANTLR start "fullAnnotation"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:180:1: fullAnnotation[AnnotatedDescrBuilder inDescrBuilder] returns [AnnotationDescr result] : AT name= ID ( DOT x= ID )* annotationArgs[result, annoBuilder] ;
public final AnnotationDescr fullAnnotation(AnnotatedDescrBuilder inDescrBuilder) throws RecognitionException {
    AnnotationDescr result = null;
    Token name = null;
    Token x = null;
    String n = "";
    AnnotationDescrBuilder annoBuilder = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:182:3: ( AT name= ID ( DOT x= ID )* annotationArgs[result, annoBuilder] )
        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:182:5: AT name= ID ( DOT x= ID )* annotationArgs[result, annoBuilder]
        {
            match(input, AT, FOLLOW_AT_in_fullAnnotation894);
            if (state.failed)
                return result;
            name = (Token) match(input, ID, FOLLOW_ID_in_fullAnnotation898);
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                n = (name != null ? name.getText() : null);
            }
            // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:182:36: ( DOT x= ID )*
            loop18: while (true) {
                int alt18 = 2;
                int LA18_0 = input.LA(1);
                if ((LA18_0 == DOT)) {
                    alt18 = 1;
                }
                switch(alt18) {
                    case 1:
                        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:182:38: DOT x= ID
                        {
                            match(input, DOT, FOLLOW_DOT_in_fullAnnotation904);
                            if (state.failed)
                                return result;
                            x = (Token) match(input, ID, FOLLOW_ID_in_fullAnnotation908);
                            if (state.failed)
                                return result;
                            if (state.backtracking == 0) {
                                n += "." + (x != null ? x.getText() : null);
                            }
                        }
                        break;
                    default:
                        break loop18;
                }
            }
            if (state.backtracking == 0) {
                if (buildDescr) {
                    if (inDescrBuilder == null) {
                        result = new AnnotationDescr(n);
                    } else {
                        annoBuilder = inDescrBuilder instanceof AnnotationDescrBuilder ? ((AnnotationDescrBuilder) inDescrBuilder).newAnnotation(n) : inDescrBuilder.newAnnotation(n);
                        result = (AnnotationDescr) annoBuilder.getDescr();
                    }
                }
            }
            pushFollow(FOLLOW_annotationArgs_in_fullAnnotation929);
            annotationArgs(result, annoBuilder);
            state._fsp--;
            if (state.failed)
                return result;
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : AnnotationDescrBuilder(org.drools.compiler.lang.api.AnnotationDescrBuilder) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 4 with AnnotationDescr

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

the class DRL6Expressions method annotationValue.

// $ANTLR end "annotationElementValuePair"
// $ANTLR start "annotationValue"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:213:1: annotationValue[AnnotatedDescrBuilder inDescrBuilder] returns [Object result] : (exp= expression |annos= annotationArray[inDescrBuilder] |anno= fullAnnotation[inDescrBuilder] );
public final Object annotationValue(AnnotatedDescrBuilder inDescrBuilder) throws RecognitionException {
    Object result = null;
    ParserRuleReturnScope exp = null;
    java.util.List annos = null;
    AnnotationDescr anno = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:214:3: (exp= expression |annos= annotationArray[inDescrBuilder] |anno= fullAnnotation[inDescrBuilder] )
        int alt21 = 3;
        switch(input.LA(1)) {
            case BOOL:
            case DECIMAL:
            case DECR:
            case DIV:
            case DOT:
            case FLOAT:
            case HEX:
            case ID:
            case INCR:
            case LEFT_PAREN:
            case LEFT_SQUARE:
            case LESS:
            case MINUS:
            case NEGATION:
            case NULL:
            case PLUS:
            case QUESTION_DIV:
            case STAR:
            case STRING:
            case TILDE:
            case TIME_INTERVAL:
                {
                    alt21 = 1;
                }
                break;
            case LEFT_CURLY:
                {
                    alt21 = 2;
                }
                break;
            case AT:
                {
                    alt21 = 3;
                }
                break;
            default:
                if (state.backtracking > 0) {
                    state.failed = true;
                    return result;
                }
                NoViableAltException nvae = new NoViableAltException("", 21, 0, input);
                throw nvae;
        }
        switch(alt21) {
            case 1:
                // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:214:5: exp= expression
                {
                    pushFollow(FOLLOW_expression_in_annotationValue1070);
                    exp = expression();
                    state._fsp--;
                    if (state.failed)
                        return result;
                    if (state.backtracking == 0) {
                        if (buildDescr)
                            result = (exp != null ? input.toString(exp.start, exp.stop) : null);
                    }
                }
                break;
            case 2:
                // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:215:7: annos= annotationArray[inDescrBuilder]
                {
                    pushFollow(FOLLOW_annotationArray_in_annotationValue1082);
                    annos = annotationArray(inDescrBuilder);
                    state._fsp--;
                    if (state.failed)
                        return result;
                    if (state.backtracking == 0) {
                        if (buildDescr)
                            result = annos.toArray();
                    }
                }
                break;
            case 3:
                // src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:216:7: anno= fullAnnotation[inDescrBuilder]
                {
                    pushFollow(FOLLOW_fullAnnotation_in_annotationValue1095);
                    anno = fullAnnotation(inDescrBuilder);
                    state._fsp--;
                    if (state.failed)
                        return result;
                    if (state.backtracking == 0) {
                        if (buildDescr)
                            result = anno;
                    }
                }
                break;
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : List(java.util.List) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 5 with AnnotationDescr

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

the class DRL5Expressions method conditionalAndExpression.

// $ANTLR end "conditionalOrExpression"
// $ANTLR start "conditionalAndExpression"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:230:1: conditionalAndExpression returns [BaseDescr result] : left= inclusiveOrExpression ( DOUBLE_AMPER (args= fullAnnotation[null] )? right= inclusiveOrExpression )* ;
public final BaseDescr conditionalAndExpression() throws RecognitionException {
    BaseDescr result = null;
    BaseDescr left = null;
    AnnotationDescr args = null;
    BaseDescr right = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:231:3: (left= inclusiveOrExpression ( DOUBLE_AMPER (args= fullAnnotation[null] )? right= inclusiveOrExpression )* )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:231:5: left= inclusiveOrExpression ( DOUBLE_AMPER (args= fullAnnotation[null] )? right= inclusiveOrExpression )*
        {
            pushFollow(FOLLOW_inclusiveOrExpression_in_conditionalAndExpression1182);
            left = inclusiveOrExpression();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:232:3: ( DOUBLE_AMPER (args= fullAnnotation[null] )? right= inclusiveOrExpression )*
            loop27: while (true) {
                int alt27 = 2;
                int LA27_0 = input.LA(1);
                if ((LA27_0 == DOUBLE_AMPER)) {
                    alt27 = 1;
                }
                switch(alt27) {
                    case 1:
                        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:232:5: DOUBLE_AMPER (args= fullAnnotation[null] )? right= inclusiveOrExpression
                        {
                            match(input, DOUBLE_AMPER, FOLLOW_DOUBLE_AMPER_in_conditionalAndExpression1190);
                            if (state.failed)
                                return result;
                            if (state.backtracking == 0) {
                                if (isNotEOF())
                                    helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_OPERATOR);
                            }
                            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:234:13: (args= fullAnnotation[null] )?
                            int alt26 = 2;
                            int LA26_0 = input.LA(1);
                            if ((LA26_0 == AT)) {
                                alt26 = 1;
                            }
                            switch(alt26) {
                                case 1:
                                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:234:13: args= fullAnnotation[null]
                                    {
                                        pushFollow(FOLLOW_fullAnnotation_in_conditionalAndExpression1213);
                                        args = fullAnnotation(null);
                                        state._fsp--;
                                        if (state.failed)
                                            return result;
                                    }
                                    break;
                            }
                            pushFollow(FOLLOW_inclusiveOrExpression_in_conditionalAndExpression1219);
                            right = inclusiveOrExpression();
                            state._fsp--;
                            if (state.failed)
                                return result;
                            if (state.backtracking == 0) {
                                if (buildDescr) {
                                    ConstraintConnectiveDescr descr = ConstraintConnectiveDescr.newAnd();
                                    descr.addOrMerge(result);
                                    descr.addOrMerge(right);
                                    if (args != null) {
                                        descr.addAnnotation(args);
                                    }
                                    result = descr;
                                }
                            }
                        }
                        break;
                    default:
                        break loop27;
                }
            }
        }
    } 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) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Aggregations

AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)27 AnnotatedBaseDescr (org.drools.compiler.lang.descr.AnnotatedBaseDescr)8 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)8 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)8 HashMap (java.util.HashMap)5 AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)5 ArrayList (java.util.ArrayList)4 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)4 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)4 Map (java.util.Map)3 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)3 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)3 List (java.util.List)2 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 FieldDefinition (org.drools.core.factmodel.FieldDefinition)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 StringReader (java.io.StringReader)1