Search in sources :

Example 6 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError 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 7 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError in project drools by kiegroup.

the class TypeDeclarationNameResolver method ensureQualifiedFieldType.

public void ensureQualifiedFieldType(AbstractClassTypeDeclarationDescr typeDescr, PackageDescr packageDescr, TypeResolver typeResolver, List<TypeDefinition> unresolvedTypes) {
    for (TypeFieldDescr field : typeDescr.getFields().values()) {
        String declaredType = field.getPattern().getObjectType();
        String resolved = resolveName(declaredType, typeDescr, packageDescr, typeResolver, unresolvedTypes, true);
        if (resolved != null) {
            field.getPattern().setObjectType(resolved);
        } else {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Cannot resolve type '" + declaredType + " for field " + field.getFieldName() + " in declared type " + typeDescr.getTypeName()));
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr)

Example 8 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError 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 9 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError 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 10 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError in project drools by kiegroup.

the class ClassHierarchyManager method sortByHierarchy.

/**
 * Utility method to sort declared beans. Linearizes the hierarchy,
 * i.e.generates a sequence of declaration such that, if Sub is subclass of
 * Sup, then the index of Sub will be > than the index of Sup in the
 * resulting collection. This ensures that superclasses are processed before
 * their subclasses
 */
protected List<AbstractClassTypeDeclarationDescr> sortByHierarchy(Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, KnowledgeBuilderImpl kbuilder) {
    taxonomy = new HashMap<QualifiedName, Collection<QualifiedName>>();
    Map<QualifiedName, AbstractClassTypeDeclarationDescr> cache = new HashMap<QualifiedName, AbstractClassTypeDeclarationDescr>();
    for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
        cache.put(tdescr.getType(), tdescr);
    }
    for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
        QualifiedName name = tdescr.getType();
        Collection<QualifiedName> supers = taxonomy.get(name);
        if (supers == null) {
            supers = new ArrayList<QualifiedName>();
            taxonomy.put(name, supers);
        } else {
            kbuilder.addBuilderResult(new TypeDeclarationError(tdescr, "Found duplicate declaration for type " + tdescr.getType()));
        }
        boolean circular = false;
        for (QualifiedName sup : tdescr.getSuperTypes()) {
            if (!Object.class.getName().equals(name.getFullName())) {
                if (!hasCircularDependency(tdescr.getType(), sup, taxonomy)) {
                    if (cache.containsKey(sup)) {
                        supers.add(sup);
                    }
                } else {
                    circular = true;
                    kbuilder.addBuilderResult(new TypeDeclarationError(tdescr, "Found circular dependency for type " + tdescr.getTypeName()));
                    break;
                }
            }
        }
        if (circular) {
            tdescr.getSuperTypes().clear();
        }
    }
    for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
        for (TypeFieldDescr field : tdescr.getFields().values()) {
            QualifiedName name = tdescr.getType();
            QualifiedName typeName = new QualifiedName(field.getPattern().getObjectType());
            if (!hasCircularDependency(name, typeName, taxonomy)) {
                if (cache.containsKey(typeName)) {
                    taxonomy.get(name).add(typeName);
                }
            } else {
                field.setRecursive(true);
            }
        }
    }
    List<QualifiedName> sorted = new HierarchySorter<QualifiedName>().sort(taxonomy);
    ArrayList list = new ArrayList(sorted.size());
    for (QualifiedName name : sorted) {
        list.add(cache.get(name));
    }
    return list;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) ArrayList(java.util.ArrayList) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) Collection(java.util.Collection)

Aggregations

TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)16 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)6 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ClassDefinition (org.drools.core.factmodel.ClassDefinition)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 QualifiedName (org.drools.compiler.lang.descr.QualifiedName)3 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 FieldDefinition (org.drools.core.factmodel.FieldDefinition)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)2 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)2 MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)2 AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)2 Traitable (org.drools.core.factmodel.traits.Traitable)2