Search in sources :

Example 21 with AnnotationDescr

use of org.drools.drl.ast.descr.AnnotationDescr in project drools by kiegroup.

the class DRL5Expressions method conditionalOrExpression.

// $ANTLR end "annotationArray"
// $ANTLR start "conditionalOrExpression"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:214:1: conditionalOrExpression returns [BaseDescr result] : left= conditionalAndExpression ( DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression )* ;
public final BaseDescr conditionalOrExpression() throws RecognitionException {
    BaseDescr result = null;
    BaseDescr left = null;
    AnnotationDescr args = null;
    BaseDescr right = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:215:3: (left= conditionalAndExpression ( DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression )* )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:215:5: left= conditionalAndExpression ( DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression )*
        {
            pushFollow(FOLLOW_conditionalAndExpression_in_conditionalOrExpression1110);
            left = conditionalAndExpression();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:216:3: ( DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression )*
            loop25: while (true) {
                int alt25 = 2;
                int LA25_0 = input.LA(1);
                if ((LA25_0 == DOUBLE_PIPE)) {
                    alt25 = 1;
                }
                switch(alt25) {
                    case 1:
                        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:216:5: DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression
                        {
                            match(input, DOUBLE_PIPE, FOLLOW_DOUBLE_PIPE_in_conditionalOrExpression1119);
                            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:218:13: (args= fullAnnotation[null] )?
                            int alt24 = 2;
                            int LA24_0 = input.LA(1);
                            if ((LA24_0 == AT)) {
                                alt24 = 1;
                            }
                            switch(alt24) {
                                case 1:
                                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:218:13: args= fullAnnotation[null]
                                    {
                                        pushFollow(FOLLOW_fullAnnotation_in_conditionalOrExpression1141);
                                        args = fullAnnotation(null);
                                        state._fsp--;
                                        if (state.failed)
                                            return result;
                                    }
                                    break;
                            }
                            pushFollow(FOLLOW_conditionalAndExpression_in_conditionalOrExpression1147);
                            right = conditionalAndExpression();
                            state._fsp--;
                            if (state.failed)
                                return result;
                            if (state.backtracking == 0) {
                                if (buildDescr) {
                                    ConstraintConnectiveDescr descr = ConstraintConnectiveDescr.newOr();
                                    descr.addOrMerge(result);
                                    descr.addOrMerge(right);
                                    if (args != null) {
                                        descr.addAnnotation(args);
                                    }
                                    result = descr;
                                }
                            }
                        }
                        break;
                    default:
                        break loop25;
                }
            }
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 22 with AnnotationDescr

use of org.drools.drl.ast.descr.AnnotationDescr in project drools by kiegroup.

the class DRL5Expressions method fullAnnotation.

// $ANTLR end "ternaryExpression"
// $ANTLR start "fullAnnotation"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:180:1: fullAnnotation[AnnotatedDescrBuilder inDescrBuilder] returns [AnnotationDescr result] : AT name= ID ( DOT x= ID )* annotationArgs[result] ;
public final AnnotationDescr fullAnnotation(AnnotatedDescrBuilder inDescrBuilder) throws RecognitionException {
    AnnotationDescr result = null;
    Token name = null;
    Token x = null;
    String n = "";
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:182:3: ( AT name= ID ( DOT x= ID )* annotationArgs[result] )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:182:5: AT name= ID ( DOT x= ID )* annotationArgs[result]
        {
            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/DRL5Expressions.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/DRL5Expressions.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) {
                    result = inDescrBuilder != null ? (AnnotationDescr) inDescrBuilder.newAnnotation(n).getDescr() : new AnnotationDescr(n);
                }
            }
            pushFollow(FOLLOW_annotationArgs_in_fullAnnotation929);
            annotationArgs(result);
            state._fsp--;
            if (state.failed)
                return result;
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 23 with AnnotationDescr

use of org.drools.drl.ast.descr.AnnotationDescr in project drools by kiegroup.

the class POJOGenerator method processTypeMetadata.

private void processTypeMetadata(Class<?> type, Collection<AnnotationDescr> annotations) {
    MethodCallExpr typeMetaDataCall = registerTypeMetaData(type.getCanonicalName());
    for (AnnotationDescr ann : annotations) {
        typeMetaDataCall = new MethodCallExpr(typeMetaDataCall, ADD_ANNOTATION_CALL);
        typeMetaDataCall.addArgument(toStringLiteral(ann.getName()));
        for (Map.Entry<String, Object> entry : ann.getValueMap().entrySet()) {
            MethodCallExpr annotationValueCall = createDslTopLevelMethod(ANNOTATION_VALUE_CALL);
            annotationValueCall.addArgument(toStringLiteral(entry.getKey()));
            String expr = entry.getValue().toString();
            if (hasMvel() && exprAnnotations.contains(ann.getName()) && ConstraintBuilder.get().analyzeExpression(type, expr) == null) {
                builder.addBuilderResult(new InvalidExpressionErrorResult("Unable to analyze expression '" + expr + "' for " + ann.getName() + " attribute"));
            }
            annotationValueCall.addArgument(quote(expr));
            typeMetaDataCall.addArgument(annotationValueCall);
        }
    }
    packageModel.addTypeMetaDataExpressions(typeMetaDataCall);
}
Also used : InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) Map(java.util.Map) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 24 with AnnotationDescr

use of org.drools.drl.ast.descr.AnnotationDescr in project drools by kiegroup.

the class RuleContext method findUnitDescr.

private void findUnitDescr() {
    if (ruleDescr == null) {
        return;
    }
    boolean useNamingConvention = false;
    String unitName;
    AnnotationDescr unitAnn = ruleDescr.getAnnotation("Unit");
    if (unitAnn != null) {
        unitName = (String) unitAnn.getValue();
        unitName = unitName.substring(0, unitName.length() - ".class".length());
    } else if (ruleDescr.getUnit() != null) {
        unitName = ruleDescr.getUnit().getTarget();
    } else {
        if (ruleDescr.getResource() == null) {
            return;
        }
        String drlFile = ruleDescr.getResource().getSourcePath();
        if (drlFile != null) {
            String drlFileName = drlFile.substring(drlFile.lastIndexOf('/') + 1);
            unitName = packageModel.getName() + '.' + drlFileName.substring(0, drlFileName.length() - ".drl".length());
            useNamingConvention = true;
        } else {
            return;
        }
    }
    RuleUnitDescriptionLoader ruleUnitDescriptionLoader = kbuilder.getPackageRegistry(packageModel.getName()).getPackage().getRuleUnitDescriptionLoader();
    Optional<RuleUnitDescription> ruDescr = ruleUnitDescriptionLoader.getDescription(unitName);
    if (ruDescr.isPresent()) {
        ruleUnitDescr = ruDescr.get();
    } else if (!useNamingConvention) {
        throw new UnknownRuleUnitException(unitName);
    }
}
Also used : RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription) UnknownRuleUnitException(org.drools.modelcompiler.builder.errors.UnknownRuleUnitException) RuleUnitDescriptionLoader(org.drools.core.ruleunit.RuleUnitDescriptionLoader) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 25 with AnnotationDescr

use of org.drools.drl.ast.descr.AnnotationDescr in project drools by kiegroup.

the class RuleBuilder method buildMetaAttributes.

public static void buildMetaAttributes(final RuleBuildContext context) {
    RuleImpl rule = context.getRule();
    for (String metaAttr : context.getRuleDescr().getAnnotationNames()) {
        AnnotationDescr ad = context.getRuleDescr().getAnnotation(metaAttr);
        String adFqn = ad.getFullyQualifiedName();
        if (adFqn != null) {
            AnnotationDefinition annotationDefinition;
            try {
                annotationDefinition = AnnotationDefinition.build(context.getDialect().getTypeResolver().resolveType(adFqn), ad.getValueMap(), context.getDialect().getTypeResolver());
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            if (annotationDefinition.getValues().size() == 1 && annotationDefinition.getValues().containsKey(AnnotationDescr.VALUE)) {
                rule.addMetaAttribute(metaAttr, annotationDefinition.getPropertyValue(AnnotationDescr.VALUE));
            } else {
                Map<String, Object> map = new HashMap<>(annotationDefinition.getValues().size());
                for (String key : annotationDefinition.getValues().keySet()) {
                    map.put(key, annotationDefinition.getPropertyValue(key));
                }
                rule.addMetaAttribute(metaAttr, map);
            }
        } else {
            if (ad.hasValue()) {
                if (ad.getValueMap().size() == 1) {
                    rule.addMetaAttribute(metaAttr, resolveValue(ad.getSingleValueAsString()));
                } else {
                    rule.addMetaAttribute(metaAttr, ad.getValueMap());
                }
            } else {
                rule.addMetaAttribute(metaAttr, null);
            }
        }
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) HashMap(java.util.HashMap) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Aggregations

AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)26 BaseDescr (org.drools.drl.ast.descr.BaseDescr)8 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)8 AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)5 ArrayList (java.util.ArrayList)3 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)3 TypeFieldDescr (org.drools.drl.ast.descr.TypeFieldDescr)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 AnnotationDeclarationError (org.drools.compiler.compiler.AnnotationDeclarationError)2 FieldDefinition (org.drools.core.factmodel.FieldDefinition)2 QualifiedName (org.drools.drl.ast.descr.QualifiedName)2 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Arrays (java.util.Arrays)1 BitSet (java.util.BitSet)1