Search in sources :

Example 16 with TypeDeclaration

use of org.drools.core.rule.TypeDeclaration 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 17 with TypeDeclaration

use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.

the class TypeDeclarationCache method getAndRegisterTypeDeclaration.

public TypeDeclaration getAndRegisterTypeDeclaration(Class<?> cls, String packageName) {
    if (cls.isPrimitive() || cls.isArray()) {
        return null;
    }
    TypeDeclaration typeDeclaration = getCachedTypeDeclaration(cls);
    if (typeDeclaration != null) {
        registerTypeDeclaration(packageName, typeDeclaration);
        return typeDeclaration;
    }
    typeDeclaration = getExistingTypeDeclaration(cls);
    if (typeDeclaration != null) {
        initTypeDeclaration(cls, typeDeclaration);
        return typeDeclaration;
    }
    typeDeclaration = createTypeDeclarationForBean(cls);
    initTypeDeclaration(cls, typeDeclaration);
    registerTypeDeclaration(packageName, typeDeclaration);
    return typeDeclaration;
}
Also used : TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 18 with TypeDeclaration

use of org.drools.core.rule.TypeDeclaration 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 19 with TypeDeclaration

use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.

the class TypeDeclarationCache method initBuiltinTypeDeclaration.

private TypeDeclaration initBuiltinTypeDeclaration(Class<?> cls) {
    TypeDeclaration type = new TypeDeclaration(cls.getSimpleName());
    type.setTypesafe(false);
    type.setTypeClass(cls);
    setClassDefinitionOnTypeDeclaration(cls, type);
    cacheTypes.put(cls.getCanonicalName(), type);
    return type;
}
Also used : TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 20 with TypeDeclaration

use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.

the class TypeDeclarationCache method buildTypeDeclarationInterfaces.

private boolean buildTypeDeclarationInterfaces(Class cls, Set<TypeDeclaration> tdecls) {
    PackageRegistry pkgReg;
    TypeDeclaration tdecl = this.cacheTypes.get((cls.getName()));
    if (tdecl == null) {
        pkgReg = kbuilder.getPackageRegistry(ClassUtils.getPackage(cls));
        if (pkgReg != null) {
            tdecl = pkgReg.getPackage().getTypeDeclaration(cls.getSimpleName());
        }
    }
    if (tdecl != null) {
        if (!tdecls.add(tdecl)) {
            // the interface already exists, return to stop recursion
            return false;
        }
    }
    Class<?>[] intfs = cls.getInterfaces();
    for (Class<?> intf : intfs) {
        pkgReg = kbuilder.getPackageRegistry(ClassUtils.getPackage(intf));
        if (pkgReg != null) {
            tdecl = pkgReg.getPackage().getTypeDeclaration(intf.getSimpleName());
        }
        if (tdecl != null) {
            tdecls.add(tdecl);
        }
    }
    for (Class<?> intf : intfs) {
        if (!buildTypeDeclarationInterfaces(intf, tdecls)) {
            return false;
        }
    }
    return true;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

TypeDeclaration (org.drools.core.rule.TypeDeclaration)72 Test (org.junit.Test)15 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)10 ClassDefinition (org.drools.core.factmodel.ClassDefinition)9 ClassObjectType (org.drools.core.base.ClassObjectType)7 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)4 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)4 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 ObjectType (org.drools.core.spi.ObjectType)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)3 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)3 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)3