Search in sources :

Example 11 with TypeDeclarationDescr

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

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

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

the class DescrTypeDefinition method typeFieldsSortedByPosition.

private List<TypeFieldDescr> typeFieldsSortedByPosition(List<FieldDefinition> inheritedFields) {
    Collection<TypeFieldDescr> typeFields = typeDeclarationDescr.getFields().values().stream().filter(f -> inheritedFields.stream().map(FieldDefinition::getFieldName).noneMatch(name -> name.equals(f.getFieldName()))).collect(Collectors.toList());
    TypeFieldDescr[] sortedTypes = new TypeFieldDescr[typeFields.size()];
    List<TypeFieldDescr> nonPositionalFields = new ArrayList<>();
    for (TypeFieldDescr descr : typeFields) {
        AnnotationDescr ann = descr.getAnnotation("Position");
        if (ann == null) {
            nonPositionalFields.add(descr);
        } else {
            int pos = Integer.parseInt(ann.getValue().toString());
            if (pos >= sortedTypes.length) {
                errors.add(new TypeDeclarationError(typeDeclarationDescr, "Out of range position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
            } else if (sortedTypes[pos] != null) {
                errors.add(new TypeDeclarationError(typeDeclarationDescr, "Duplicated position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
            } else {
                sortedTypes[pos] = descr;
            }
        }
    }
    if (!errors.isEmpty()) {
        return Collections.emptyList();
    }
    int counter = 0;
    for (TypeFieldDescr descr : nonPositionalFields) {
        for (; sortedTypes[counter] != null; counter++) ;
        sortedTypes[counter++] = descr;
    }
    return Arrays.asList(sortedTypes);
}
Also used : QualifiedName(org.drools.drl.ast.descr.QualifiedName) Optional.empty(java.util.Optional.empty) Arrays(java.util.Arrays) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) Optional.of(java.util.Optional.of) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ArrayList(java.util.ArrayList) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition) StreamUtils.optionalToStream(org.drools.modelcompiler.util.StreamUtils.optionalToStream) MethodDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition) AnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition) Collectors(java.util.stream.Collectors) AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) DroolsError(org.drools.drl.parser.DroolsError) Optional(java.util.Optional) Collections(java.util.Collections) POJOGenerator.quote(org.drools.modelcompiler.builder.generator.declaredtype.POJOGenerator.quote) TypeResolver(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeResolver) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ArrayList(java.util.ArrayList) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 14 with TypeDeclarationDescr

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

the class POJOGenerator method findPOJOorGenerate.

public void findPOJOorGenerate() {
    TypeResolver typeResolver = pkg.getTypeResolver();
    Set<String> generatedPojos = new HashSet<>();
    for (TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations()) {
        if (!generatedPojos.add(typeDescr.getFullTypeName())) {
            builder.addBuilderResult(new DuplicatedDeclarationError(typeDescr.getFullTypeName()));
            continue;
        }
        try {
            Class<?> type = typeResolver.resolveType(typeDescr.getFullTypeName());
            checkRedeclarationCompatibility(type, typeDescr);
            processTypeMetadata(type, typeDescr.getAnnotations());
        } catch (ClassNotFoundException e) {
            createPOJO(typeDescr);
        }
    }
    for (EnumDeclarationDescr enumDescr : packageDescr.getEnumDeclarations()) {
        try {
            Class<?> type = typeResolver.resolveType(enumDescr.getFullTypeName());
            processTypeMetadata(type, enumDescr.getAnnotations());
        } catch (ClassNotFoundException e) {
            TypeDeclaration generatedEnum = new EnumGenerator().generate(enumDescr);
            packageModel.addGeneratedPOJO(generatedEnum);
            addTypeMetadata(enumDescr.getTypeName());
        }
    }
}
Also used : DuplicatedDeclarationError(org.drools.modelcompiler.builder.errors.DuplicatedDeclarationError) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) TypeResolver(org.drools.core.addon.TypeResolver) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) HashSet(java.util.HashSet)

Example 15 with TypeDeclarationDescr

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

TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)22 PackageDescr (org.drools.drl.ast.descr.PackageDescr)10 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)9 Test (org.junit.Test)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 TypeFieldDescr (org.drools.drl.ast.descr.TypeFieldDescr)6 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)5 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)5 ArrayList (java.util.ArrayList)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 HashSet (java.util.HashSet)3 TypeResolver (org.drools.core.addon.TypeResolver)3 ClassDefinition (org.drools.core.factmodel.ClassDefinition)3 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)3 RuleDescr (org.drools.drl.ast.descr.RuleDescr)3 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2