Search in sources :

Example 1 with AnnotationDescr

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

the class TypeDeclarationDescrVisitor method visit.

public void visit(List<TypeDeclarationDescr> typeDeclarationDescrs) {
    for (TypeDeclarationDescr typeDeclaration : typeDeclarationDescrs) {
        Import objectImport = data.getImportByName(typeDeclaration.getTypeName());
        String objectTypeName;
        if (objectImport == null) {
            objectTypeName = typeDeclaration.getTypeName();
        } else {
            objectTypeName = objectImport.getName();
        }
        ObjectType objectType = this.data.getObjectTypeByFullName(objectTypeName);
        if (objectType == null) {
            objectType = new ObjectType(typeDeclaration);
            objectType.setName(typeDeclaration.getTypeName());
            objectType.setFullName(typeDeclaration.getTypeName());
            data.add(objectType);
        }
        for (String fieldName : typeDeclaration.getFields().keySet()) {
            Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
            if (field == null) {
                field = ObjectTypeFactory.createField(typeDeclaration.getFields().get(fieldName), fieldName, objectType);
                field.setFieldType(typeDeclaration.getFields().get(fieldName).getPattern().getObjectType());
                data.add(field);
            }
        }
        for (AnnotationDescr annDescr : typeDeclaration.getAnnotations()) {
            Map<String, Object> values = typeDeclaration.getAnnotation(annDescr.getName()).getValueMap();
            for (String value : values.keySet()) {
                objectType.getMetadata().put(annDescr.getName(), value);
            }
        }
    }
}
Also used : ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) Import(org.drools.verifier.components.Import) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 2 with AnnotationDescr

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

the class DescrTypeDefinition method processClassAnnotations.

private void processClassAnnotations() {
    for (AnnotationDescr ann : typeDeclarationDescr.getAnnotations()) {
        if (ann.getName().equals(SERIAL_VERSION_UID)) {
            DescrFieldDefinition serialVersionField = new DescrFieldDefinition(SERIAL_VERSION_UID, "long", ann.getValue("value").toString()).setFinal(true).setStatic(true);
            fieldDefinition.add(serialVersionField);
        }
        try {
            annotations.add(DescrAnnotationDefinition.fromDescr(typeResolver, ann));
        } catch (UnkownAnnotationClassException | UnknownKeysInAnnotation e) {
        // Do not do anything
        }
    }
}
Also used : AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 3 with AnnotationDescr

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

the class DescrTypeDefinition method typeFieldsSortedByPosition.

private List<TypeFieldDescr> typeFieldsSortedByPosition(List<FieldDefinition> inheritedFields) {
    Collection<TypeFieldDescr> typeFields = typeDeclarationDescr.getFields().values().stream().filter(f -> inheritedFields.stream().map(FieldDefinition::getFieldName).noneMatch(name -> name.equals(f.getFieldName()))).collect(Collectors.toList());
    TypeFieldDescr[] sortedTypes = new TypeFieldDescr[typeFields.size()];
    List<TypeFieldDescr> nonPositionalFields = new ArrayList<>();
    for (TypeFieldDescr descr : typeFields) {
        AnnotationDescr ann = descr.getAnnotation("Position");
        if (ann == null) {
            nonPositionalFields.add(descr);
        } else {
            int pos = Integer.parseInt(ann.getValue().toString());
            if (pos >= sortedTypes.length) {
                errors.add(new TypeDeclarationError(typeDeclarationDescr, "Out of range position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
            } else if (sortedTypes[pos] != null) {
                errors.add(new TypeDeclarationError(typeDeclarationDescr, "Duplicated position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
            } else {
                sortedTypes[pos] = descr;
            }
        }
    }
    if (!errors.isEmpty()) {
        return Collections.emptyList();
    }
    int counter = 0;
    for (TypeFieldDescr descr : nonPositionalFields) {
        for (; sortedTypes[counter] != null; counter++) ;
        sortedTypes[counter++] = descr;
    }
    return Arrays.asList(sortedTypes);
}
Also used : QualifiedName(org.drools.drl.ast.descr.QualifiedName) Optional.empty(java.util.Optional.empty) Arrays(java.util.Arrays) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) Optional.of(java.util.Optional.of) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ArrayList(java.util.ArrayList) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition) StreamUtils.optionalToStream(org.drools.modelcompiler.util.StreamUtils.optionalToStream) MethodDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition) AnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition) Collectors(java.util.stream.Collectors) AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) DroolsError(org.drools.drl.parser.DroolsError) Optional(java.util.Optional) Collections(java.util.Collections) POJOGenerator.quote(org.drools.modelcompiler.builder.generator.declaredtype.POJOGenerator.quote) TypeResolver(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeResolver) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ArrayList(java.util.ArrayList) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 4 with AnnotationDescr

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

the class DRL5Expressions method orRestriction.

// $ANTLR end "relationalExpression"
// $ANTLR start "orRestriction"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:391:1: orRestriction returns [BaseDescr result] : left= andRestriction ( ( DOUBLE_PIPE ( fullAnnotation[null] )? andRestriction )=>lop= DOUBLE_PIPE (args= fullAnnotation[null] )? right= andRestriction )* ( EOF )? ;
public final BaseDescr orRestriction() throws RecognitionException {
    BaseDescr result = null;
    Token lop = null;
    BaseDescr left = null;
    AnnotationDescr args = null;
    BaseDescr right = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:392:3: (left= andRestriction ( ( DOUBLE_PIPE ( fullAnnotation[null] )? andRestriction )=>lop= DOUBLE_PIPE (args= fullAnnotation[null] )? right= andRestriction )* ( EOF )? )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:392:5: left= andRestriction ( ( DOUBLE_PIPE ( fullAnnotation[null] )? andRestriction )=>lop= DOUBLE_PIPE (args= fullAnnotation[null] )? right= andRestriction )* ( EOF )?
        {
            pushFollow(FOLLOW_andRestriction_in_orRestriction1810);
            left = andRestriction();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:393:5: ( ( DOUBLE_PIPE ( fullAnnotation[null] )? andRestriction )=>lop= DOUBLE_PIPE (args= fullAnnotation[null] )? right= andRestriction )*
            loop39: while (true) {
                int alt39 = 2;
                int LA39_0 = input.LA(1);
                if ((LA39_0 == DOUBLE_PIPE)) {
                    int LA39_9 = input.LA(2);
                    if ((synpred9_DRL5Expressions())) {
                        alt39 = 1;
                    }
                }
                switch(alt39) {
                    case 1:
                        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:393:7: ( DOUBLE_PIPE ( fullAnnotation[null] )? andRestriction )=>lop= DOUBLE_PIPE (args= fullAnnotation[null] )? right= andRestriction
                        {
                            lop = (Token) match(input, DOUBLE_PIPE, FOLLOW_DOUBLE_PIPE_in_orRestriction1832);
                            if (state.failed)
                                return result;
                            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:393:79: (args= fullAnnotation[null] )?
                            int alt38 = 2;
                            int LA38_0 = input.LA(1);
                            if ((LA38_0 == AT)) {
                                alt38 = 1;
                            }
                            switch(alt38) {
                                case 1:
                                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:393:79: args= fullAnnotation[null]
                                    {
                                        pushFollow(FOLLOW_fullAnnotation_in_orRestriction1836);
                                        args = fullAnnotation(null);
                                        state._fsp--;
                                        if (state.failed)
                                            return result;
                                    }
                                    break;
                            }
                            pushFollow(FOLLOW_andRestriction_in_orRestriction1842);
                            right = andRestriction();
                            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 loop39;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:402:7: ( EOF )?
            int alt40 = 2;
            int LA40_0 = input.LA(1);
            if ((LA40_0 == EOF)) {
                int LA40_1 = input.LA(2);
                if ((LA40_1 == EOF)) {
                    int LA40_3 = input.LA(3);
                    if ((LA40_3 == EOF)) {
                        alt40 = 1;
                    }
                } else if (((LA40_1 >= AMPER && LA40_1 <= AND_ASSIGN) || LA40_1 == AT || (LA40_1 >= COLON && LA40_1 <= COMMA) || LA40_1 == DIV_ASSIGN || (LA40_1 >= DOUBLE_AMPER && LA40_1 <= DOUBLE_PIPE) || (LA40_1 >= EQUALS && LA40_1 <= EQUALS_ASSIGN) || (LA40_1 >= GREATER && LA40_1 <= GREATER_EQUALS) || LA40_1 == ID || LA40_1 == LEFT_PAREN || (LA40_1 >= LESS && LA40_1 <= LESS_EQUALS) || LA40_1 == MINUS_ASSIGN || LA40_1 == MOD_ASSIGN || LA40_1 == MULT_ASSIGN || LA40_1 == NOT_EQUALS || LA40_1 == OR_ASSIGN || LA40_1 == PIPE || (LA40_1 >= PLUS_ASSIGN && LA40_1 <= SEMICOLON) || LA40_1 == TILDE || (LA40_1 >= XOR && LA40_1 <= XOR_ASSIGN))) {
                    alt40 = 1;
                }
            }
            switch(alt40) {
                case 1:
                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:402:7: EOF
                    {
                        match(input, EOF, FOLLOW_EOF_in_orRestriction1861);
                        if (state.failed)
                            return result;
                    }
                    break;
            }
        }
    } 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 5 with AnnotationDescr

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

the class ModelGenerator method ruleMetaAttributes.

/**
 * Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#metadata(String, Object)}
 * starting from a drools-compiler {@link RuleDescr}.<br/>
 * Based on {@link org.drools.modelcompiler.KiePackagesBuilder#setRuleMetaAttributes(Rule, RuleImpl)} the reserved annotation keywords are:
 * Propagation, All, Direct.
 */
private static List<MethodCallExpr> ruleMetaAttributes(RuleContext context, RuleDescr ruleDescr) {
    List<MethodCallExpr> ruleMetaAttributes = new ArrayList<>();
    for (String metaAttr : ruleDescr.getAnnotationNames()) {
        MethodCallExpr metaAttributeCall = new MethodCallExpr(METADATA_CALL);
        metaAttributeCall.addArgument(toStringLiteral(metaAttr));
        AnnotationDescr ad = ruleDescr.getAnnotation(metaAttr);
        String adFqn = ad.getFullyQualifiedName();
        if ("Propagation".equals(metaAttr)) {
            // legacy case, as explained in the javadoc annotation above, ref. DROOLS-5685
            metaAttributeCall.addArgument(parseExpression(org.kie.api.definition.rule.Propagation.Type.class.getCanonicalName() + "." + ad.getSingleValueAsString()));
        } else if (adFqn != null) {
            AnnotationDefinition annotationDefinition;
            try {
                annotationDefinition = AnnotationDefinition.build(context.getTypeResolver().resolveType(adFqn), ad.getValueMap(), context.getTypeResolver());
            } catch (NoSuchMethodException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            if (annotationDefinition.getValues().size() == 1 && annotationDefinition.getValues().containsKey(AnnotationDescr.VALUE)) {
                Object annValue = annotationDefinition.getPropertyValue(AnnotationDescr.VALUE);
                metaAttributeCall.addArgument(toStringLiteral(annValue.toString()));
            } else {
                Map<String, Object> map = new HashMap<>(annotationDefinition.getValues().size());
                for (String key : annotationDefinition.getValues().keySet()) {
                    map.put(key, annotationDefinition.getPropertyValue(key));
                }
                metaAttributeCall.addArgument(objectAsJPExpression(map));
            }
        } else {
            if (ad.hasValue()) {
                if (ad.getValueMap().size() == 1) {
                    metaAttributeCall.addArgument(annotationSingleValueExpression(ad));
                } else {
                    metaAttributeCall.addArgument(objectAsJPExpression(ad.getValueMap()));
                }
            } else {
                metaAttributeCall.addArgument(new NullLiteralExpr());
            }
        }
        ruleMetaAttributes.add(metaAttributeCall);
    }
    return ruleMetaAttributes;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) ArrayList(java.util.ArrayList) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Map(java.util.Map) HashMap(java.util.HashMap) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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