Search in sources :

Example 1 with QualifiedName

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

the class ClassDefinitionFactory method createClassDefinition.

protected ClassDefinition createClassDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
    // extracts type, supertype and interfaces
    String fullName = typeDescr.getType().getFullName();
    if (type.getKind().equals(TypeDeclaration.Kind.CLASS)) {
        TypeDeclarationDescr tdescr = (TypeDeclarationDescr) typeDescr;
        if (tdescr.getSuperTypes().size() > 1) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Declared class " + fullName + "  - has more than one supertype;"));
            return null;
        } else if (tdescr.getSuperTypes().isEmpty()) {
            tdescr.addSuperType("java.lang.Object");
        }
    }
    Traitable traitableAnn = typeDescr.getTypedAnnotation(Traitable.class);
    boolean traitable = traitableAnn != null;
    String[] fullSuperTypes = new String[typeDescr.getSuperTypes().size() + 1];
    int j = 0;
    for (QualifiedName qname : typeDescr.getSuperTypes()) {
        fullSuperTypes[j++] = qname.getFullName();
    }
    fullSuperTypes[j] = Thing.class.getName();
    List<String> interfaceList = new ArrayList<String>();
    interfaceList.add(traitable ? Externalizable.class.getName() : Serializable.class.getName());
    if (traitable) {
        interfaceList.add(TraitableBean.class.getName());
    }
    String[] interfaces = interfaceList.toArray(new String[interfaceList.size()]);
    // prepares a class definition
    ClassDefinition def;
    switch(type.getKind()) {
        case TRAIT:
            def = new ClassDefinition(fullName, Object.class.getName(), fullSuperTypes);
            break;
        case ENUM:
            def = new EnumClassDefinition(fullName, fullSuperTypes[0], null);
            break;
        case CLASS:
        default:
            def = new ClassDefinition(fullName, fullSuperTypes[0], interfaces);
            def.setTraitable(traitable, traitableAnn != null && traitableAnn.logical());
    }
    return def;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) Traitable(org.drools.core.factmodel.traits.Traitable) Thing(org.drools.core.factmodel.traits.Thing)

Example 2 with QualifiedName

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

the class ClassHierarchyManager method addDeclarationToPackagePreservingOrder.

public void addDeclarationToPackagePreservingOrder(TypeDeclaration type, AbstractClassTypeDeclarationDescr typeDescr, InternalKnowledgePackage tgtPackage, Map<String, PackageRegistry> pkgRegistryMap) {
    Collection<QualifiedName> parents = taxonomy.get(new QualifiedName(type.getFullName()));
    int index = getSortedDescriptors().indexOf(typeDescr);
    if (parents != null && !parents.isEmpty()) {
        for (QualifiedName parentName : parents) {
            String nameSpace = parentName.getNamespace();
            String name = parentName.getName();
            PackageRegistry parentPkgRegistry = pkgRegistryMap.get(nameSpace);
            if (parentPkgRegistry != null) {
                TypeDeclaration parentDeclaration = parentPkgRegistry.getPackage().getTypeDeclaration(name);
                if (parentDeclaration != null && parentDeclaration.getNature() == TypeDeclaration.Nature.DEFINITION) {
                    index = Math.max(index, parentDeclaration.getOrder());
                }
            }
        }
    }
    type.setOrder(index + 1);
    tgtPackage.addTypeDeclaration(type);
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 3 with QualifiedName

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

the class ClassHierarchyManager method inheritFields.

public void inheritFields(PackageRegistry pkgRegistry, AbstractClassTypeDeclarationDescr typeDescr, Collection<AbstractClassTypeDeclarationDescr> sortedTypeDescriptors, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs) {
    TypeDeclarationDescr tDescr = (TypeDeclarationDescr) typeDescr;
    boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, pkgRegistry);
    boolean inferFields = !isNovel && typeDescr.getFields().isEmpty();
    for (QualifiedName qname : tDescr.getSuperTypes()) {
        // descriptor needs fields inherited from superclass
        mergeInheritedFields(tDescr, unresolvedTypes, unprocessableDescrs, pkgRegistry.getTypeResolver());
    }
    if (inferFields) {
        // not novel, but only an empty declaration was provided.
        // after inheriting the fields from supertypes, now we fill in the locally declared fields
        Class existingClass = TypeDeclarationUtils.getExistingDeclarationClass(typeDescr, pkgRegistry);
        buildDescrsFromFields(existingClass, tDescr, pkgRegistry, tDescr.getFields());
    }
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) QualifiedName(org.drools.compiler.lang.descr.QualifiedName)

Example 4 with QualifiedName

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

Example 5 with QualifiedName

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

the class ClassHierarchyManager method mergeInheritedFields.

/**
 * In order to build a declared class, the fields inherited from its
 * superclass(es) are added to its declaration. Inherited descriptors are
 * marked as such to distinguish them from native ones. Various scenarioes
 * are possible. (i) The superclass has been declared in the DRL as well :
 * the fields are cloned as inherited (ii) The superclass is imported
 * (external), but some of its fields have been tagged with metadata (iii)
 * The superclass is imported.
 * <p>
 * The search for field descriptors is carried out in the order. (i) and
 * (ii+iii) are mutually exclusive. The search is as such: (i) The
 * superclass' declared fields are used to build the base class additional
 * fields (iii) The superclass is inspected to discover its (public) fields,
 * from which descriptors are generated (ii) Both (i) and (iii) are applied,
 * but the declared fields override the inspected ones
 *
 * @param typeDescr The base class descriptor, to be completed with the inherited
 *                  fields descriptors
 * @return true if all went well
 */
protected void mergeInheritedFields(TypeDeclarationDescr typeDescr, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs, TypeResolver typeResolver) {
    if (typeDescr.getSuperTypes().isEmpty()) {
        return;
    }
    for (int j = typeDescr.getSuperTypes().size() - 1; j >= 0; j--) {
        QualifiedName qname = typeDescr.getSuperTypes().get(j);
        String simpleSuperTypeName = qname.getName();
        String superTypePackageName = qname.getNamespace();
        String fullSuper = qname.getFullName();
        mergeFields(simpleSuperTypeName, superTypePackageName, fullSuper, typeDescr, unresolvedTypes, unprocessableDescrs, typeResolver);
    }
}
Also used : QualifiedName(org.drools.compiler.lang.descr.QualifiedName)

Aggregations

QualifiedName (org.drools.compiler.lang.descr.QualifiedName)7 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)4 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)1 ClassDefinition (org.drools.core.factmodel.ClassDefinition)1 EnumClassDefinition (org.drools.core.factmodel.EnumClassDefinition)1 Thing (org.drools.core.factmodel.traits.Thing)1 Traitable (org.drools.core.factmodel.traits.Traitable)1 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1