Search in sources :

Example 6 with InternalKnowledgeBuilder

use of org.drools.compiler.builder.InternalKnowledgeBuilder in project drools by kiegroup.

the class MarshallingKieMetaInfoBuilder method generateKieModuleMetaInfo.

public KieModuleMetaInfo generateKieModuleMetaInfo(ResourceStore trgMfs) {
    Map<String, TypeMetaInfo> typeInfos = new HashMap<>();
    Map<String, Set<String>> rulesPerPackage = new HashMap<>();
    KieModuleModel kieModuleModel = kModule.getKieModuleModel();
    for (String kieBaseName : kieModuleModel.getKieBaseModels().keySet()) {
        KnowledgeBuilder kBuilder = kModule.getKnowledgeBuilderForKieBase(kieBaseName);
        KieModuleCache.KModuleCache.Builder _kmoduleCacheBuilder = createCacheBuilder();
        KieModuleCache.CompilationData.Builder _compData = createCompilationData();
        for (KiePackage kPkg : kBuilder.getKnowledgePackages()) {
            PackageRegistry pkgRegistry = ((InternalKnowledgeBuilder) kBuilder).getPackageRegistry(kPkg.getName());
            JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
            List<String> types = new ArrayList<>();
            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<>();
            }
            for (Rule rule : kPkg.getRules()) {
                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) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ClassDefinition(org.drools.core.factmodel.ClassDefinition) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) 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) KieModuleMetaInfo(org.drools.core.rule.KieModuleMetaInfo) Rule(org.kie.api.definition.rule.Rule) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

InternalKnowledgeBuilder (org.drools.compiler.builder.InternalKnowledgeBuilder)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 KiePackage (org.kie.api.definition.KiePackage)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 TypeDeclaration (org.drools.core.rule.TypeDeclaration)3 Rule (org.kie.api.definition.rule.Rule)3 CompositeKnowledgeBuilder (org.kie.internal.builder.CompositeKnowledgeBuilder)3 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2 KieBaseModelImpl (org.drools.compiler.kproject.models.KieBaseModelImpl)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 KieBaseUpdate (org.drools.core.impl.KieBaseUpdate)2 ChangeType (org.kie.internal.builder.ChangeType)2 ResourceChange (org.kie.internal.builder.ResourceChange)2 ResourceChangeSet (org.kie.internal.builder.ResourceChangeSet)2