Search in sources :

Example 1 with AbstractClassTypeDeclarationDescr

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

the class TraitsTypeDeclarationBuilderImpl method postGenerateDeclaredBean.

@Override
protected void postGenerateDeclaredBean(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, PackageRegistry pkgRegistry) {
    traitRegistry = ((TraitKnowledgePackageImpl) pkgRegistry.getPackage()).getTraitRegistry();
    if (typeDescr.hasAnnotation(Traitable.class) || (!type.getKind().equals(TypeDeclaration.Kind.TRAIT) && kbuilder.getPackageRegistry().containsKey(def.getSuperClass()) && traitRegistry.getTraitables().containsKey(def.getSuperClass()))) {
        // traitable
        if (type.isNovel()) {
            try {
                PackageRegistry reg = kbuilder.getPackageRegistry(typeDescr.getNamespace());
                String availableName = typeDescr.getType().getFullName();
                Class<?> resolvedType = reg.getTypeResolver().resolveType(availableName);
                updateTraitDefinition(type, resolvedType, false);
            } catch (ClassNotFoundException cnfe) {
            // we already know the class exists
            }
        }
        traitRegistry.addTraitable(def);
    } else if (type.getKind().equals(TypeDeclaration.Kind.TRAIT) || typeDescr.hasAnnotation(Trait.class)) {
        // trait
        if (!type.isNovel()) {
            try {
                PackageRegistry reg = kbuilder.getPackageRegistry(typeDescr.getNamespace());
                String availableName = typeDescr.getType().getFullName();
                Class<?> resolvedType = reg.getTypeResolver().resolveType(availableName);
                if (!Thing.class.isAssignableFrom(resolvedType)) {
                    if (!resolvedType.isInterface()) {
                        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to redeclare concrete class " + resolvedType.getName() + " as a trait."));
                        return;
                    }
                    updateTraitDefinition(type, resolvedType, false);
                    String target = typeDescr.getTypeName() + TraitFactoryImpl.SUFFIX;
                    TypeDeclarationDescr tempDescr = new TypeDeclarationDescr();
                    tempDescr.setNamespace(typeDescr.getNamespace());
                    tempDescr.setFields(typeDescr.getFields());
                    tempDescr.setType(target, typeDescr.getNamespace());
                    tempDescr.setTrait(true);
                    tempDescr.addSuperType(typeDescr.getType());
                    tempDescr.setResource(type.getResource());
                    TypeDeclaration tempDeclr = new TypeDeclaration(target);
                    tempDeclr.setKind(TypeDeclaration.Kind.TRAIT);
                    tempDeclr.setTypesafe(type.isTypesafe());
                    tempDeclr.setNovel(true);
                    tempDeclr.setTypeClassName(tempDescr.getType().getFullName());
                    tempDeclr.setResource(type.getResource());
                    ClassDefinition tempDef = new ClassDefinition(target);
                    tempDef.setClassName(tempDescr.getType().getFullName());
                    tempDef.setTraitable(false);
                    for (FieldDefinition fld : def.getFieldsDefinitions()) {
                        tempDef.addField(fld);
                    }
                    tempDef.setInterfaces(def.getInterfaces());
                    tempDef.setSuperClass(def.getClassName());
                    tempDef.setDefinedClass(resolvedType);
                    tempDef.setAbstrakt(true);
                    tempDeclr.setTypeClassDef(tempDef);
                    declaredClassBuilder.generateBeanFromDefinition(tempDescr, tempDeclr, pkgRegistry, tempDef, new TraitClassBuilderImpl());
                    try {
                        Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(tempDescr.getType().getFullName());
                        tempDeclr.setTypeClass(clazz);
                        traitRegistry.addTrait(tempDef.getClassName().replace(TraitFactoryImpl.SUFFIX, ""), tempDef);
                    } catch (ClassNotFoundException cnfe) {
                        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Internal Trait extension Class '" + target + "' could not be generated correctly'"));
                    } finally {
                        pkgRegistry.getPackage().addTypeDeclaration(tempDeclr);
                    }
                } else {
                    updateTraitDefinition(type, resolvedType, true);
                    traitRegistry.addTrait(def);
                }
            } catch (ClassNotFoundException cnfe) {
            // we already know the class exists
            }
        } else {
            if (def.getClassName().endsWith(TraitFactoryImpl.SUFFIX)) {
                traitRegistry.addTrait(def.getClassName().replace(TraitFactoryImpl.SUFFIX, ""), def);
            } else {
                traitRegistry.addTrait(def);
            }
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) FieldDefinition(org.drools.core.factmodel.FieldDefinition) TraitClassBuilderImpl(org.drools.traits.core.factmodel.TraitClassBuilderImpl) Traitable(org.drools.core.factmodel.traits.Traitable) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 2 with AbstractClassTypeDeclarationDescr

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

the class KnowledgeBuilderImpl method buildTypeDeclarations.

protected void buildTypeDeclarations(Collection<CompositePackageDescr> packages) {
    Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs = new HashMap<>();
    List<TypeDefinition> unresolvedTypes = new ArrayList<>();
    List<AbstractClassTypeDeclarationDescr> unsortedDescrs = new ArrayList<>();
    for (CompositePackageDescr packageDescr : packages) {
        unsortedDescrs.addAll(packageDescr.getTypeDeclarations());
        unsortedDescrs.addAll(packageDescr.getEnumDeclarations());
    }
    getTypeBuilder().processTypeDeclarations(packages, unsortedDescrs, unresolvedTypes, unprocesseableDescrs);
    for (CompositePackageDescr packageDescr : packages) {
        for (ImportDescr importDescr : packageDescr.getImports()) {
            getPackageRegistry(packageDescr.getNamespace()).addImport(importDescr);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) ArrayList(java.util.ArrayList) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr)

Example 3 with AbstractClassTypeDeclarationDescr

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

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

the class DeclaredClassBuilder method ensureJavaTypeConsistency.

private boolean ensureJavaTypeConsistency(AbstractClassTypeDeclarationDescr typeDescr, ClassDefinition def, TypeResolver typeResolver) {
    try {
        if (typeDescr instanceof TypeDeclarationDescr && !((TypeDeclarationDescr) typeDescr).isTrait() && typeResolver.resolveType(def.getSuperClass()).isInterface()) {
            def.addInterface(def.getSuperClass());
            def.setSuperClass(null);
        }
        for (String sup : def.getInterfaces()) {
            if (!typeResolver.resolveType(sup).isInterface()) {
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Non-interface type used as super interface : " + sup));
                return false;
            }
        }
    } catch (ClassNotFoundException cnfe) {
        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to resolve parent type :" + cnfe.getMessage()));
        return false;
    }
    return true;
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)

Example 5 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationBuilder method compactDefinitionsAndDeclarations.

private Collection<AbstractClassTypeDeclarationDescr> compactDefinitionsAndDeclarations(Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
    Map<String, AbstractClassTypeDeclarationDescr> compactedUnsorted = new HashMap<>(unsortedDescrs.size());
    for (AbstractClassTypeDeclarationDescr descr : unsortedDescrs) {
        if (compactedUnsorted.containsKey(descr.getType().getFullName())) {
            AbstractClassTypeDeclarationDescr prev = compactedUnsorted.get(descr.getType().getFullName());
            boolean res = mergeTypeDescriptors(prev, descr);
            if (!res) {
                unprocesseableDescrs.put(prev.getType().getFullName(), prev);
                kbuilder.addBuilderResult(new TypeDeclarationError(prev, "Found duplicate declaration for type " + prev.getType().getFullName() + ", unable to reconcile "));
            }
        } else {
            compactedUnsorted.put(descr.getType().getFullName(), descr);
        }
    }
    return compactedUnsorted.values();
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) HashMap(java.util.HashMap) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)

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