Search in sources :

Example 6 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationBuilder method createBean.

protected void createBean(AbstractClassTypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, ClassHierarchyManager hierarchyManager, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
    // descriptor needs fields inherited from superclass
    if (typeDescr instanceof TypeDeclarationDescr) {
        hierarchyManager.inheritFields(pkgRegistry, typeDescr, unprocesseableDescrs);
    }
    TypeDeclaration type = typeDeclarationFactory.processTypeDeclaration(pkgRegistry, typeDescr);
    boolean success = !kbuilder.hasErrors();
    try {
        // the type declaration is generated in any case (to be used by subclasses, if any)
        // the actual class will be generated only if needed
        ClassDefinition def = null;
        if (success) {
            def = classDefinitionFactory.generateDeclaredBean(typeDescr, type, pkgRegistry, unresolvedTypes, unprocesseableDescrs);
            // this has to be done after the classDef has been generated
            if (!type.isNovel()) {
                typeDeclarationFactory.checkRedeclaration(typeDescr, type, pkgRegistry);
            }
        }
        success = (def != null) && (!kbuilder.hasErrors());
        if (success) {
            this.postGenerateDeclaredBean(typeDescr, type, def, pkgRegistry);
        }
        success = !kbuilder.hasErrors();
        if (success) {
            ClassBuilder classBuilder = RuntimeComponentFactory.get().getClassBuilderFactory().getClassBuilder(type);
            declaredClassBuilder.generateBeanFromDefinition(typeDescr, type, pkgRegistry, def, classBuilder);
        }
        success = !kbuilder.hasErrors();
        if (success) {
            Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(typeDescr.getType().getFullName());
            type.setTypeClass(clazz);
            type.setValid(true);
        } else {
            unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
            type.setValid(false);
        }
        typeDeclarationConfigurator.finalizeConfigurator(type, typeDescr, pkgRegistry, kbuilder.getPackageRegistry(), hierarchyManager);
    } catch (final ClassNotFoundException e) {
        unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Class '" + type.getTypeClassName() + "' not found for type declaration of '" + type.getTypeName() + "'"));
    }
    if (!success) {
        unresolvedTypes.add(new TypeDefinition(type, typeDescr));
    } else {
        registerGeneratedType(typeDescr);
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) ClassDefinition(org.drools.core.factmodel.ClassDefinition) ClassBuilder(org.drools.core.factmodel.ClassBuilder) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 7 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationBuilder method processUnresolvedTypes.

private void processUnresolvedTypes(PackageDescr packageDescr, PackageRegistry pkgRegistry, Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
    // merge "duplicate" definitions and declarations
    unsortedDescrs = compactDefinitionsAndDeclarations(unsortedDescrs, unprocesseableDescrs);
    // now sort declarations by mutual dependencies
    ClassHierarchyManager classHierarchyManager = new ClassHierarchyManager(unsortedDescrs, kbuilder);
    for (AbstractClassTypeDeclarationDescr typeDescr : classHierarchyManager.getSortedDescriptors()) {
        PackageRegistry pkgReg = getPackageRegistry(pkgRegistry, packageDescr, typeDescr);
        createBean(typeDescr, pkgReg, classHierarchyManager, unresolvedTypes, unprocesseableDescrs);
    }
    for (AbstractClassTypeDeclarationDescr typeDescr : classHierarchyManager.getSortedDescriptors()) {
        if (!unprocesseableDescrs.containsKey(typeDescr.getType().getFullName())) {
            PackageRegistry pkgReg = getPackageRegistry(pkgRegistry, packageDescr, typeDescr);
            InternalKnowledgePackage pkg = pkgReg.getPackage();
            TypeDeclaration type = pkg.getTypeDeclaration(typeDescr.getType().getName());
            typeDeclarationConfigurator.wireFieldAccessors(pkgReg, typeDescr, type);
            if (kbuilder.getKnowledgeBase() != null) {
                // in case of incremental compilatoin (re)register the new type declaration on the existing kbase
                kbuilder.getKnowledgeBase().registerTypeDeclaration(type, pkg);
            }
        }
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 8 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationBuilder method normalizeForeignPackages.

protected void normalizeForeignPackages(PackageDescr packageDescr) {
    Map<String, PackageDescr> foreignPackages = null;
    for (AbstractClassTypeDeclarationDescr typeDescr : packageDescr.getClassAndEnumDeclarationDescrs()) {
        if (kbuilder.filterAccepts(ResourceChange.Type.DECLARATION, typeDescr.getNamespace(), typeDescr.getTypeName())) {
            if (!typeDescr.getNamespace().equals(packageDescr.getNamespace())) {
                // If the type declaration is for a different namespace, process that separately.
                PackageDescr altDescr;
                if (foreignPackages == null) {
                    foreignPackages = new HashMap<>();
                }
                if (foreignPackages.containsKey(typeDescr.getNamespace())) {
                    altDescr = foreignPackages.get(typeDescr.getNamespace());
                } else {
                    altDescr = new PackageDescr(typeDescr.getNamespace());
                    altDescr.setResource(packageDescr.getResource());
                    foreignPackages.put(typeDescr.getNamespace(), altDescr);
                }
                if (typeDescr instanceof TypeDeclarationDescr) {
                    altDescr.addTypeDeclaration((TypeDeclarationDescr) typeDescr);
                } else if (typeDescr instanceof EnumDeclarationDescr) {
                    altDescr.addEnumDeclaration((EnumDeclarationDescr) typeDescr);
                }
                for (ImportDescr imp : packageDescr.getImports()) {
                    altDescr.addImport(imp);
                }
                kbuilder.getOrCreatePackageRegistry(altDescr);
            }
        }
    }
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 9 with AbstractClassTypeDeclarationDescr

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

the class KnowledgeBuilderImpl method mergePackage.

void mergePackage(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (final ImportDescr importDescr : packageDescr.getImports()) {
        pkgRegistry.addImport(importDescr);
    }
    normalizeTypeDeclarationAnnotations(packageDescr, pkgRegistry.getTypeResolver());
    processAccumulateFunctions(pkgRegistry, packageDescr);
    processEntryPointDeclarations(pkgRegistry, packageDescr);
    Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs = new HashMap<>();
    List<TypeDefinition> unresolvedTypes = new ArrayList<>();
    List<AbstractClassTypeDeclarationDescr> unsortedDescrs = new ArrayList<>();
    unsortedDescrs.addAll(packageDescr.getTypeDeclarations());
    unsortedDescrs.addAll(packageDescr.getEnumDeclarations());
    typeBuilder.processTypeDeclarations(packageDescr, pkgRegistry, unsortedDescrs, unresolvedTypes, unprocesseableDescrs);
    for (AbstractClassTypeDeclarationDescr descr : unprocesseableDescrs.values()) {
        this.addBuilderResult(new TypeDeclarationError(descr, "Unable to process type " + descr.getTypeName()));
    }
    processOtherDeclarations(pkgRegistry, packageDescr);
    normalizeRuleAnnotations(packageDescr, pkgRegistry.getTypeResolver());
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) 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 10 with AbstractClassTypeDeclarationDescr

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

the class ClassHierarchyManager method inheritFields.

public void inheritFields(PackageRegistry pkgRegistry, AbstractClassTypeDeclarationDescr typeDescr, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs) {
    TypeDeclarationDescr tDescr = (TypeDeclarationDescr) typeDescr;
    boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, pkgRegistry);
    boolean inferFields = !isNovel && typeDescr.getFields().isEmpty();
    mergeInheritedFields(tDescr, 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, tDescr.getFields());
    }
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr)

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