Search in sources :

Example 16 with AnnotationDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.TypeDeclarationDescr) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 17 with AnnotationDescr

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

the class FactModelPersistence method toModel.

private static List<FactMetaModel> toModel(String drl) throws DroolsParserException {
    Preconditions.checkNotNull(drl, "The string representing DRL can't be null!");
    if (drl.startsWith("#advanced") || drl.startsWith("//advanced")) {
        throw new DroolsParserException("Using advanced editor");
    }
    final DrlParser parser = new DrlParser();
    final StringReader reader = new StringReader(drl);
    final PackageDescr pkg = parser.parse(reader);
    if (parser.hasErrors()) {
        throw new DroolsParserException("The model drl " + drl + " is not valid");
    }
    if (pkg == null) {
        return emptyList();
    }
    final List<TypeDeclarationDescr> types = pkg.getTypeDeclarations();
    final List<FactMetaModel> list = new ArrayList<FactMetaModel>(types.size());
    for (final TypeDeclarationDescr td : types) {
        final FactMetaModel mm = new FactMetaModel();
        mm.setName(td.getTypeName());
        mm.setSuperType(td.getSuperTypeName());
        final Map<String, TypeFieldDescr> fields = td.getFields();
        for (Map.Entry<String, TypeFieldDescr> en : fields.entrySet()) {
            final String fieldName = en.getKey();
            final TypeFieldDescr descr = en.getValue();
            final FieldMetaModel fm = new FieldMetaModel(fieldName, descr.getPattern().getObjectType());
            mm.getFields().add(fm);
        }
        for (final AnnotationDescr descr : td.getAnnotations()) {
            final String annotationName = descr.getName();
            final Map<String, String> values = extractStringValues(descr);
            final AnnotationMetaModel am = new AnnotationMetaModel(annotationName, values);
            mm.getAnnotations().add(am);
        }
        list.add(mm);
    }
    return list;
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) ArrayList(java.util.ArrayList) FactMetaModel(org.drools.workbench.screens.factmodel.model.FactMetaModel) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) FieldMetaModel(org.drools.workbench.screens.factmodel.model.FieldMetaModel) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) AnnotationMetaModel(org.drools.workbench.screens.factmodel.model.AnnotationMetaModel) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with AnnotationDescr

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

the class ClassDefinitionFactory method wireAnnotationDefs.

protected boolean wireAnnotationDefs(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, TypeResolver resolver) {
    for (AnnotationDescr annotationDescr : typeDescr.getAnnotations()) {
        Class annotation = null;
        try {
            annotation = annotationDescr.getFullyQualifiedName() != null ? resolver.resolveType(annotationDescr.getFullyQualifiedName()) : null;
        } catch (ClassNotFoundException e) {
            continue;
        }
        if (annotation != null && annotation.isAnnotation()) {
            try {
                AnnotationDefinition annotationDefinition = AnnotationDefinition.build(annotation, annotationDescr.getValueMap(), resolver);
                def.addAnnotation(annotationDefinition);
            } catch (NoSuchMethodException nsme) {
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Annotated type " + typeDescr.getType().getFullName() + "  - undefined property in @annotation " + annotationDescr.getName() + ": " + nsme.getMessage() + ";"));
            }
        }
        if (annotation == null || annotation.getCanonicalName().startsWith("org.kie.api.definition.type")) {
            def.addMetaData(annotationDescr.getName(), annotationDescr.getSingleValue());
        }
    }
    return true;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 19 with AnnotationDescr

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

the class ClassDefinitionFactory method sortFields.

private static List<FieldDefinition> sortFields(Map<String, TypeFieldDescr> fields, TypeResolver typeResolver, KnowledgeBuilderImpl kbuilder) {
    List<FieldDefinition> fieldDefs = new ArrayList<FieldDefinition>(fields.size());
    int maxDeclaredPos = 0;
    BitSet occupiedPositions = new BitSet(fields.size());
    for (TypeFieldDescr field : fields.values()) {
        String typeName = field.getPattern().getObjectType();
        String typeNameKey = typeName;
        String fullFieldType = kbuilder != null ? TypeDeclarationUtils.toBuildableType(typeNameKey, kbuilder.getRootClassLoader()) : typeNameKey;
        FieldDefinition fieldDef = new FieldDefinition(field.getFieldName(), fullFieldType);
        fieldDefs.add(fieldDef);
        if (field.hasOverride()) {
            fieldDef.setOverriding(field.getOverriding().getPattern().getObjectType());
        }
        fieldDef.setInherited(field.isInherited());
        fieldDef.setRecursive(field.isRecursive());
        fieldDef.setInitExpr(TypeDeclarationUtils.rewriteInitExprWithImports(field.getInitExpr(), typeResolver));
        if (field.getIndex() >= 0) {
            int pos = field.getIndex();
            occupiedPositions.set(pos);
            maxDeclaredPos = Math.max(maxDeclaredPos, pos);
            fieldDef.addMetaData("position", pos);
        } else {
            Position position = field.getTypedAnnotation(Position.class);
            if (position != null) {
                int pos = position.value();
                field.setIndex(pos);
                occupiedPositions.set(pos);
                maxDeclaredPos = Math.max(maxDeclaredPos, pos);
                fieldDef.addMetaData("position", pos);
            }
        }
        if (field.hasAnnotation(Key.class)) {
            fieldDef.setKey(true);
            fieldDef.addMetaData("key", null);
        }
        for (AnnotationDescr annotationDescr : field.getAnnotations()) {
            if (annotationDescr.getFullyQualifiedName() == null) {
                if (annotationDescr.isStrict()) {
                    kbuilder.addBuilderResult(new TypeDeclarationError(field, "Unknown annotation @" + annotationDescr.getName() + " on field " + field.getFieldName()));
                } else {
                    // Annotation is custom metadata
                    fieldDef.addMetaData(annotationDescr.getName(), annotationDescr.getSingleValue());
                    continue;
                }
            }
            Annotation annotation = AnnotationFactory.buildAnnotation(typeResolver, annotationDescr);
            if (annotation != null) {
                try {
                    AnnotationDefinition annotationDefinition = AnnotationDefinition.build(annotation.annotationType(), field.getAnnotation(annotationDescr.getFullyQualifiedName()).getValueMap(), typeResolver);
                    fieldDef.addAnnotation(annotationDefinition);
                } catch (Exception e) {
                    kbuilder.addBuilderResult(new TypeDeclarationError(field, "Annotated field " + field.getFieldName() + "  - undefined property in @annotation " + annotationDescr.getName() + ": " + e.getMessage() + ";"));
                }
            } else {
                if (annotationDescr.isStrict()) {
                    kbuilder.addBuilderResult(new TypeDeclarationError(field, "Unknown annotation @" + annotationDescr.getName() + " on field " + field.getFieldName()));
                }
            }
        }
        fieldDef.setDeclIndex(field.getIndex());
    }
    int curr = 0;
    for (FieldDefinition fieldDef : fieldDefs) {
        if (fieldDef.getDeclIndex() < 0) {
            int freePos = occupiedPositions.nextClearBit(0);
            if (freePos < maxDeclaredPos) {
                occupiedPositions.set(freePos);
            } else {
                freePos = maxDeclaredPos + 1;
            }
            fieldDef.setPriority(freePos * 256 + curr++);
        } else {
            fieldDef.setPriority(fieldDef.getDeclIndex() * 256 + curr++);
        }
    }
    Collections.sort(fieldDefs);
    return fieldDefs;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Position(org.kie.api.definition.type.Position) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Annotation(java.lang.annotation.Annotation) IOException(java.io.IOException) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr)

Example 20 with AnnotationDescr

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

the class TypeDeclarationBuilder method mergeTypeDescriptors.

private boolean mergeTypeDescriptors(AbstractClassTypeDeclarationDescr prev, AbstractClassTypeDeclarationDescr descr) {
    boolean isDef1 = isDefinition(prev);
    boolean isDef2 = isDefinition(descr);
    if (isDef1 && isDef2) {
        return false;
    }
    if (!prev.getSuperTypes().isEmpty() && !descr.getSuperTypes().isEmpty() && prev.getSuperTypes().size() != descr.getSuperTypes().size()) {
        return false;
    }
    if (prev.getSuperTypes().isEmpty()) {
        for (QualifiedName qn : descr.getSuperTypes()) {
            ((TypeDeclarationDescr) prev).addSuperType(qn);
        }
    }
    if (prev.getFields().isEmpty()) {
        for (String fieldName : descr.getFields().keySet()) {
            prev.addField(descr.getFields().get(fieldName));
        }
    }
    for (AnnotationDescr ad : descr.getAnnotations()) {
        prev.addQualifiedAnnotation(ad);
    }
    for (AnnotationDescr ad : prev.getAnnotations()) {
        if (!descr.getAnnotations().contains(ad)) {
            descr.addQualifiedAnnotation(ad);
        }
    }
    return true;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) 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