Search in sources :

Example 1 with QualifiedName

use of org.drools.drl.ast.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 = getTypedAnnotation(typeDescr, 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<>();
    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.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) QualifiedName(org.drools.drl.ast.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.drl.ast.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
 */
protected void mergeInheritedFields(TypeDeclarationDescr typeDescr, 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, unprocessableDescrs, typeResolver);
    }
}
Also used : QualifiedName(org.drools.drl.ast.descr.QualifiedName)

Example 3 with QualifiedName

use of org.drools.drl.ast.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.drl.ast.descr.QualifiedName) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 4 with QualifiedName

use of org.drools.drl.ast.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<>();
    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 5 with QualifiedName

use of org.drools.drl.ast.descr.QualifiedName 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

QualifiedName (org.drools.drl.ast.descr.QualifiedName)7 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)3 ArrayList (java.util.ArrayList)2 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)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 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)1 TypeFieldDescr (org.drools.drl.ast.descr.TypeFieldDescr)1