Search in sources :

Example 6 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition 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, hierarchyManager.getSortedDescriptors(), unresolvedTypes, 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) {
            updateTraitInformation(typeDescr, type, def, pkgRegistry);
        }
        success = !kbuilder.hasErrors();
        if (success) {
            declaredClassBuilder.generateBeanFromDefinition(typeDescr, type, pkgRegistry, def);
        }
        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.finalize(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.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 7 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class TypeDeclarationBuilder method updateTraitInformation.

protected void updateTraitInformation(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, PackageRegistry pkgRegistry) {
    if (typeDescr.hasAnnotation(Traitable.class) || (!type.getKind().equals(TypeDeclaration.Kind.TRAIT) && kbuilder.getPackageRegistry().containsKey(def.getSuperClass()) && kbuilder.getPackageRegistry(def.getSuperClass()).getTraitRegistry().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
            }
        }
        pkgRegistry.getTraitRegistry().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() + TraitFactory.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);
                    try {
                        Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(tempDescr.getType().getFullName());
                        tempDeclr.setTypeClass(clazz);
                        pkgRegistry.getTraitRegistry().addTrait(tempDef.getClassName().replace(TraitFactory.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);
                    pkgRegistry.getTraitRegistry().addTrait(def);
                }
            } catch (ClassNotFoundException cnfe) {
            // we already know the class exists
            }
        } else {
            if (def.getClassName().endsWith(TraitFactory.SUFFIX)) {
                pkgRegistry.getTraitRegistry().addTrait(def.getClassName().replace(TraitFactory.SUFFIX, ""), def);
            } else {
                pkgRegistry.getTraitRegistry().addTrait(def);
            }
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) FieldDefinition(org.drools.core.factmodel.FieldDefinition) Traitable(org.drools.core.factmodel.traits.Traitable) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 8 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class KieMetaInfoBuilder method generateKieModuleMetaInfo.

public KieModuleMetaInfo generateKieModuleMetaInfo(ResourceStore trgMfs) {
    // TODO: I think this method is wrong because it is only inspecting packages that are included
    // in at least one kbase, but I believe it should inspect all packages, even if not included in
    // any kbase, as they could be included in the future
    Map<String, TypeMetaInfo> typeInfos = new HashMap<String, TypeMetaInfo>();
    Map<String, Set<String>> rulesPerPackage = new HashMap<String, Set<String>>();
    KieModuleModel kieModuleModel = kModule.getKieModuleModel();
    for (String kieBaseName : kieModuleModel.getKieBaseModels().keySet()) {
        KnowledgeBuilderImpl kBuilder = (KnowledgeBuilderImpl) kModule.getKnowledgeBuilderForKieBase(kieBaseName);
        Map<String, PackageRegistry> pkgRegistryMap = kBuilder.getPackageRegistry();
        KieModuleCache.KModuleCache.Builder _kmoduleCacheBuilder = createCacheBuilder();
        KieModuleCache.CompilationData.Builder _compData = createCompilationData();
        for (KiePackage kPkg : kBuilder.getKnowledgePackages()) {
            PackageRegistry pkgRegistry = pkgRegistryMap.get(kPkg.getName());
            JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
            List<String> types = new ArrayList<String>();
            for (FactType factType : kPkg.getFactTypes()) {
                Class<?> typeClass = ((ClassDefinition) factType).getDefinedClass();
                TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(typeClass);
                if (typeDeclaration != null) {
                    typeInfos.put(typeClass.getName(), new TypeMetaInfo(typeDeclaration));
                }
                String className = factType.getName();
                String internalName = className.replace('.', '/') + ".class";
                if (trgMfs != null) {
                    byte[] bytes = runtimeData.getBytecode(internalName);
                    if (bytes != null) {
                        trgMfs.write(internalName, bytes, true);
                    }
                }
                types.add(internalName);
            }
            Set<String> rules = rulesPerPackage.get(kPkg.getName());
            if (rules == null) {
                rules = new HashSet<String>();
            }
            for (Rule rule : kPkg.getRules()) {
                if (!rules.contains(rule.getName())) {
                    rules.add(rule.getName());
                }
            }
            if (!rules.isEmpty()) {
                rulesPerPackage.put(kPkg.getName(), rules);
            }
            addToCompilationData(_compData, runtimeData, types);
        }
        _kmoduleCacheBuilder.addCompilationData(_compData.build());
        if (trgMfs != null) {
            writeCompilationDataToTrg(_kmoduleCacheBuilder.build(), kieBaseName, trgMfs);
        }
    }
    return new KieModuleMetaInfo(typeInfos, rulesPerPackage);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ClassDefinition(org.drools.core.factmodel.ClassDefinition) KiePackage(org.kie.api.definition.KiePackage) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) FactType(org.kie.api.definition.type.FactType) TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieModuleMetaInfo(org.drools.core.rule.KieModuleMetaInfo) Rule(org.kie.api.definition.rule.Rule) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 9 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class TypeDeclarationCache method initTypeDeclaration.

private void initTypeDeclaration(Class<?> cls, TypeDeclaration typeDeclaration) {
    ClassDefinition clsDef = typeDeclaration.getTypeClassDef();
    if (clsDef == null) {
        clsDef = setClassDefinitionOnTypeDeclaration(cls, typeDeclaration);
    } else {
        processFieldsPosition(cls, clsDef, typeDeclaration);
    }
    if (typeDeclaration.isPropertyReactive()) {
        processModifiedProps(cls, clsDef);
    }
    // build up a set of all the super classes and interfaces
    Set<TypeDeclaration> tdecls = new LinkedHashSet<TypeDeclaration>();
    tdecls.add(typeDeclaration);
    buildTypeDeclarations(cls, tdecls);
    // Iterate and for each typedeclr assign it's value if it's not already set
    // We start from the rear as those are the furthest away classes and interfaces
    TypeDeclaration[] tarray = tdecls.toArray(new TypeDeclaration[tdecls.size()]);
    for (int i = tarray.length - 1; i >= 0; i--) {
        TypeDeclaration currentTDecl = tarray[i];
        if (!isSet(typeDeclaration.getSetMask(), TypeDeclaration.ROLE_BIT) && isSet(currentTDecl.getSetMask(), TypeDeclaration.ROLE_BIT)) {
            typeDeclaration.setRole(currentTDecl.getRole());
        }
        if (!isSet(typeDeclaration.getSetMask(), TypeDeclaration.FORMAT_BIT) && isSet(currentTDecl.getSetMask(), TypeDeclaration.FORMAT_BIT)) {
            typeDeclaration.setFormat(currentTDecl.getFormat());
        }
        if (!isSet(typeDeclaration.getSetMask(), TypeDeclaration.TYPESAFE_BIT) && isSet(currentTDecl.getSetMask(), TypeDeclaration.TYPESAFE_BIT)) {
            typeDeclaration.setTypesafe(currentTDecl.isTypesafe());
        }
    }
    this.cacheTypes.put(cls.getName(), typeDeclaration);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 10 with ClassDefinition

use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.

the class TypeDeclarationConfigurator method buildFieldAccessors.

protected void buildFieldAccessors(final TypeDeclaration type, final PackageRegistry pkgRegistry) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
    ClassDefinition cd = type.getTypeClassDef();
    ClassFieldAccessorStore store = pkgRegistry.getPackage().getClassFieldAccessorStore();
    for (FieldDefinition attrDef : cd.getFieldsDefinitions()) {
        ClassFieldAccessor accessor = store.getAccessor(cd.getDefinedClass().getName(), attrDef.getName());
        attrDef.setReadWriteAccessor(accessor);
    }
}
Also used : ClassFieldAccessor(org.drools.core.base.ClassFieldAccessor) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) ClassDefinition(org.drools.core.factmodel.ClassDefinition)

Aggregations

ClassDefinition (org.drools.core.factmodel.ClassDefinition)20 TypeDeclaration (org.drools.core.rule.TypeDeclaration)9 FieldDefinition (org.drools.core.factmodel.FieldDefinition)6 ArrayList (java.util.ArrayList)4 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)4 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)3 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)3 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)3 IOException (java.io.IOException)2 BitSet (java.util.BitSet)2 ClassFieldAccessor (org.drools.core.base.ClassFieldAccessor)2 ClassFieldAccessorStore (org.drools.core.base.ClassFieldAccessorStore)2 EnumClassDefinition (org.drools.core.factmodel.EnumClassDefinition)2 Traitable (org.drools.core.factmodel.traits.Traitable)2 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)2 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)2 FactField (org.kie.api.definition.type.FactField)2 ByteString (com.google.protobuf.ByteString)1 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1