Search in sources :

Example 11 with AbstractClassTypeDeclarationDescr

use of org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr 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<>();
    Map<QualifiedName, AbstractClassTypeDeclarationDescr> cache = new HashMap<>();
    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<>();
            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().getGenericType().getRawType());
            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.drl.ast.descr.QualifiedName) ArrayList(java.util.ArrayList) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)

Example 12 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationFactory method processAnnotations.

public static void processAnnotations(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
    Role role = getTypedAnnotation(typeDescr, Role.class);
    if (role != null) {
        type.setRole(role.value());
    }
    TypeSafe typeSafe = getTypedAnnotation(typeDescr, TypeSafe.class);
    if (typeSafe != null) {
        type.setTypesafe(typeSafe.value());
    }
    if (typeDescr instanceof EnumDeclarationDescr) {
        type.setKind(TypeDeclaration.Kind.ENUM);
    } else if (typeDescr instanceof TypeDeclarationDescr && ((TypeDeclarationDescr) typeDescr).isTrait()) {
        type.setKind(TypeDeclaration.Kind.TRAIT);
    }
    type.setDynamic(typeDescr.hasAnnotation(PropertyChangeSupport.class));
}
Also used : Role(org.kie.api.definition.type.Role) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) PropertyChangeSupport(org.kie.api.definition.type.PropertyChangeSupport) TypeSafe(org.kie.api.definition.type.TypeSafe)

Example 13 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationUtils method resolveType.

/**
 * Tries to determine the namespace (package) of a simple type chosen to be
 * the superclass of a declared bean. Looks among imports, local
 * declarations and previous declarations. Means that a class can't extend
 * another class declared in package that has not been loaded yet.
 *
 * @param klass        the simple name of the class
 * @param packageDescr the descriptor of the package the base class is declared in
 * @param pkgRegistry  the current package registry
 * @return the fully qualified name of the superclass
 */
public static String resolveType(String klass, PackageDescr packageDescr, PackageRegistry pkgRegistry) {
    String arraySuffix = "";
    int arrayIndex = klass.indexOf("[");
    if (arrayIndex >= 0) {
        arraySuffix = klass.substring(arrayIndex);
        klass = klass.substring(0, arrayIndex);
    }
    // look among imports
    String temp = klass;
    while (temp.length() > 0) {
        for (ImportDescr id : packageDescr.getImports()) {
            String fqKlass = id.getTarget();
            if (fqKlass.endsWith("." + temp)) {
                // logger.info("Replace supertype " + sup + " with full name " + id.getTarget());
                fqKlass = fqKlass.substring(0, fqKlass.lastIndexOf(temp)) + klass;
                return arrayIndex < 0 ? fqKlass : fqKlass + arraySuffix;
            }
        }
        temp = temp.substring(0, Math.max(0, temp.lastIndexOf('.')));
    }
    // look among local declarations
    if (pkgRegistry != null) {
        for (String declaredName : pkgRegistry.getPackage().getTypeDeclarations().keySet()) {
            if (declaredName.equals(klass)) {
                TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(declaredName);
                if (typeDeclaration.getTypeClass() != null) {
                    klass = typeDeclaration.getTypeClass().getName();
                }
            }
        }
    }
    if (!klass.contains(".") && packageDescr.getNamespace() != null && !packageDescr.getNamespace().isEmpty()) {
        for (AbstractClassTypeDeclarationDescr td : packageDescr.getClassAndEnumDeclarationDescrs()) {
            if (klass.equals(td.getTypeName())) {
                if (td.getType().getFullName().contains(".")) {
                    klass = td.getType().getFullName();
                }
            }
        }
    }
    return arrayIndex < 0 ? klass : klass + arraySuffix;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 14 with AbstractClassTypeDeclarationDescr

use of org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr 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.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) QualifiedName(org.drools.drl.ast.descr.QualifiedName) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Aggregations

AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)14 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)8 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)7 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 ImportDescr (org.drools.drl.ast.descr.ImportDescr)4 LinkedHashMap (java.util.LinkedHashMap)3 ClassDefinition (org.drools.core.factmodel.ClassDefinition)3 QualifiedName (org.drools.drl.ast.descr.QualifiedName)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2 Traitable (org.drools.core.factmodel.traits.Traitable)2 AccumulateImportDescr (org.drools.drl.ast.descr.AccumulateImportDescr)2 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)2 FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)2 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 ClassBuilder (org.drools.core.factmodel.ClassBuilder)1 EnumClassDefinition (org.drools.core.factmodel.EnumClassDefinition)1