Search in sources :

Example 1 with InternalKnowledgeBuilder

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

the class KieBaseUpdaterImpl method run.

@Override
public void run() {
    InternalKnowledgeBuilder kbuilder = ctx.kbuilder;
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    boolean shouldRebuild = applyResourceChanges(kbuilder, ckbuilder);
    removeResources(kbuilder);
    // remove all ObjectTypeNodes for the modified classes
    if (ctx.modifyingUsedClass) {
        for (Class<?> cls : ctx.modifiedClasses) {
            clearInstancesOfModifiedClass(cls);
        }
        for (InternalKnowledgePackage pkg : ctx.kBase.getPackagesMap().values()) {
            DialectRuntimeData mvel = pkg.getDialectRuntimeRegistry().getDialectData("mvel");
            if (mvel != null) {
                mvel.resetParserConfiguration();
            }
        }
    }
    if (shouldRebuild) {
        // readd unchanged dsl files to the kbuilder
        for (String dslFile : ctx.unchangedResources) {
            if (isFileInKBase(ctx.newKM, ctx.newKieBaseModel, dslFile)) {
                ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, dslFile);
            }
        }
        KieBaseUpdate kieBaseUpdate = createKieBaseUpdate();
        ctx.kBase.beforeIncrementalUpdate(kieBaseUpdate);
        rebuildAll(kbuilder, ckbuilder);
        ctx.kBase.afterIncrementalUpdate(kieBaseUpdate);
    }
    ctx.kBase.setResolvedReleaseId(ctx.newReleaseId);
    for (InternalWorkingMemory wm : ctx.kBase.getWorkingMemories()) {
        wm.notifyWaitOnRest();
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) DialectRuntimeData(org.drools.core.rule.DialectRuntimeData) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) KieBaseUpdate(org.drools.core.impl.KieBaseUpdate) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with InternalKnowledgeBuilder

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

the class KieContainerImpl method update.

private Results update(InternalKieModule currentKM, InternalKieModule newKM) {
    final KieJarChangeSet cs = currentKM.getChanges(newKM);
    List<String> modifiedClassNames = getModifiedClasses(cs);
    final boolean modifyingUsedClass = isModifyingUsedClass(modifiedClassNames, getClassLoader()) || isModifyingUsedFunction(cs);
    final Collection<Class<?>> modifiedClasses = reinitModifiedClasses(newKM, modifiedClassNames, getClassLoader(), modifyingUsedClass);
    final Collection<String> unchangedResources = getUnchangedResources(newKM, cs);
    Map<String, KieBaseModel> currentKieBaseModels = ((KieModuleKieProject) kProject).updateToModule(newKM);
    final ResultsImpl results = new ResultsImpl();
    currentKM.updateKieModule(newKM);
    List<String> kbasesToRemove = new ArrayList<>();
    for (Entry<String, KieBase> kBaseEntry : kBases.entrySet()) {
        String kbaseName = kBaseEntry.getKey();
        KieBaseModelImpl newKieBaseModel = (KieBaseModelImpl) kProject.getKieBaseModel(kbaseName);
        KieBaseModelImpl currentKieBaseModel = (KieBaseModelImpl) currentKieBaseModels.get(kbaseName);
        // if a kbase no longer exists, just remove it from the cache
        if (newKieBaseModel == null) {
            // have to save for later removal to avoid iteration errors
            kbasesToRemove.add(kbaseName);
        } else {
            final InternalKnowledgeBase kBase = (InternalKnowledgeBase) kBaseEntry.getValue();
            // share Knowledge Builder among updater as it's computationally expensive to create this
            KnowledgeBuilderConfigurationImpl builderConfiguration = (KnowledgeBuilderConfigurationImpl) newKM.createBuilderConfiguration(newKieBaseModel, kBase.getRootClassLoader());
            InternalKnowledgeBuilder kbuilder = (InternalKnowledgeBuilder) KnowledgeBuilderFactory.newKnowledgeBuilder(kBase, builderConfiguration);
            KieBaseUpdaterImplContext context = new KieBaseUpdaterImplContext(kProject, kBase, currentKM, newKM, cs, modifiedClasses, modifyingUsedClass, unchangedResources, results, newKieBaseModel, currentKieBaseModel, kbuilder);
            // Multiple updaters are required to be merged together in a single Runnable
            // to avoid a deadlock while using .fireUntilHalt()
            // see IncrementalCompilationTest.testMultipleIncrementalCompilationsWithFireUntilHalt
            // with multiple updaters (such as Alpha NetworkCompilerUpdater)
            CompositeRunnable compositeUpdater = new CompositeRunnable();
            KieBaseUpdater kieBaseUpdater = currentKM.createKieBaseUpdater(context);
            compositeUpdater.add(kieBaseUpdater);
            KieBaseUpdaterOptions kieBaseUpdaterOptions = new KieBaseUpdaterOptions(new KieBaseUpdaterOptions.OptionEntry(AlphaNetworkCompilerOption.class, builderConfiguration.getAlphaNetworkCompilerOption()));
            KieBaseUpdaters updaters = KieService.load(KieBaseUpdaters.class);
            updaters.getChildren().stream().map(kbu -> kbu.create(new KieBaseUpdatersContext(kieBaseUpdaterOptions, context.kBase.getRete(), context.kBase.getRootClassLoader()))).forEach(compositeUpdater::add);
            kBase.enqueueModification(compositeUpdater);
        }
    }
    for (String kbaseToRemove : kbasesToRemove) {
        kBases.remove(kbaseToRemove);
    }
    // remove sessions that no longer exist
    this.kSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
    this.statelessKSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
    return results;
}
Also used : ResourceChange(org.kie.internal.builder.ResourceChange) ChangeType(org.kie.internal.builder.ChangeType) LoggerFactory(org.slf4j.LoggerFactory) SessionConfigurationImpl(org.drools.core.SessionConfigurationImpl) ReleaseId(org.kie.api.builder.ReleaseId) InjectionHelper.wireSessionComponents(org.drools.compiler.kie.util.InjectionHelper.wireSessionComponents) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Map(java.util.Map) StatefulSessionPool(org.drools.kiesession.session.StatefulSessionPool) KieSession(org.kie.api.runtime.KieSession) KieLoggers(org.kie.api.logger.KieLoggers) KieRuntimeEventManager(org.kie.api.event.KieRuntimeEventManager) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) Collection(java.util.Collection) MBeansOption(org.kie.api.conf.MBeansOption) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Results(org.kie.api.builder.Results) UUID(java.util.UUID) ObjectName(javax.management.ObjectName) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) DroolsManagementAgent(org.drools.core.management.DroolsManagementAgent) Environment(org.kie.api.runtime.Environment) List(java.util.List) KieModule(org.kie.api.builder.KieModule) KieSessionModel(org.kie.api.builder.model.KieSessionModel) MaterializedLambda(org.drools.compiler.kie.builder.MaterializedLambda) Entry(java.util.Map.Entry) FileLoggerModel(org.kie.api.builder.model.FileLoggerModel) KieContainerMonitor(org.drools.compiler.management.KieContainerMonitor) RuntimeComponentFactory(org.drools.core.reteoo.RuntimeComponentFactory) ClassUtils.convertResourceToClassName(org.drools.core.util.ClassUtils.convertResourceToClassName) StatelessKnowledgeSessionImpl(org.drools.kiesession.session.StatelessKnowledgeSessionImpl) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) Drools.isJndiAvailable(org.drools.core.util.Drools.isJndiAvailable) ResourceType(org.kie.api.io.ResourceType) AlphaNetworkCompilerOption(org.kie.internal.builder.conf.AlphaNetworkCompilerOption) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieRepository(org.kie.api.builder.KieRepository) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) HashSet(java.util.HashSet) Level(org.kie.api.builder.Message.Level) SessionConfiguration(org.drools.core.SessionConfiguration) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KieServices(org.kie.api.KieServices) Message(org.kie.api.builder.Message) KieBase(org.kie.api.KieBase) KieSessionModelImpl(org.drools.compiler.kproject.models.KieSessionModelImpl) RuleBase(org.drools.core.impl.RuleBase) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKieContainer(org.drools.core.impl.InternalKieContainer) Logger(org.slf4j.Logger) ClassUtils(org.drools.core.util.ClassUtils) Calendar(org.kie.api.time.Calendar) KieService(org.kie.api.internal.utils.KieService) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) CBSKey(org.drools.core.management.DroolsManagementAgent.CBSKey) KieContainerSessionsPool(org.kie.api.runtime.KieContainerSessionsPool) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) Collectors.toList(java.util.stream.Collectors.toList) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) Collections(java.util.Collections) InputStream(java.io.InputStream) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ArrayList(java.util.ArrayList) AlphaNetworkCompilerOption(org.kie.internal.builder.conf.AlphaNetworkCompilerOption) KieBase(org.kie.api.KieBase) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieBaseModel(org.kie.api.builder.model.KieBaseModel)

Example 3 with InternalKnowledgeBuilder

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

the class KieMetaInfoBuilder 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);
        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);
            }
        }
    }
    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) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ClassDefinition(org.drools.core.factmodel.ClassDefinition) FactType(org.kie.api.definition.type.FactType) TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KiePackage(org.kie.api.definition.KiePackage) 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)

Example 4 with InternalKnowledgeBuilder

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

the class CanonicalKieBaseUpdater method run.

@Override
public void run() {
    CanonicalKieModule oldKM = (CanonicalKieModule) ctx.currentKM;
    CanonicalKieModule newKM = (CanonicalKieModule) ctx.newKM;
    Map<String, AtomicInteger> globalsCounter = new HashMap<>();
    // To keep compatible the classes generated from declared types the new kmodule has to be loaded with the classloader of the old one
    newKM.setIncrementalUpdate(true);
    CanonicalKiePackages newPkgs = newKM.getKiePackages(ctx.newKieBaseModel);
    InternalKnowledgeBuilder pkgbuilder = ctx.kbuilder;
    CompositeKnowledgeBuilder ckbuilder = pkgbuilder.batch();
    newKM.setIncrementalUpdate(false);
    removeResources(pkgbuilder);
    List<RuleImpl> rulesToBeRemoved;
    List<RuleImpl> rulesToBeAdded;
    if (ctx.modifyingUsedClass) {
        // remove all ObjectTypeNodes for the modified classes
        for (Class<?> cls : ctx.modifiedClasses) {
            clearInstancesOfModifiedClass(cls);
        }
        for (InternalKnowledgePackage kpkg : ctx.kBase.getPackagesMap().values()) {
            List<TypeDeclaration> types = new ArrayList<>(kpkg.getTypeDeclarations().values());
            for (TypeDeclaration type : types) {
                kpkg.removeTypeDeclaration(type.getTypeName());
                kpkg.addTypeDeclaration(((InternalKnowledgePackage) newPkgs.getKiePackage(kpkg.getName())).getTypeDeclaration(type.getTypeName()));
            }
        }
        rulesToBeRemoved = getAllRulesInKieBase(oldKM, ctx.currentKieBaseModel);
        rulesToBeAdded = getAllRulesInKieBase(newKM, ctx.newKieBaseModel);
    } else {
        ctx.kBase.processAllTypesDeclaration(newPkgs.getKiePackages());
        rulesToBeRemoved = new ArrayList<>();
        rulesToBeAdded = new ArrayList<>();
        for (ResourceChangeSet changeSet : ctx.cs.getChanges().values()) {
            if (!isPackageInKieBase(ctx.newKieBaseModel, changeSet.getResourceName())) {
                continue;
            }
            InternalKnowledgePackage kpkg = (InternalKnowledgePackage) newPkgs.getKiePackage(changeSet.getResourceName());
            InternalKnowledgePackage oldKpkg = ctx.kBase.getPackage(changeSet.getResourceName());
            if (oldKpkg == null) {
                try {
                    oldKpkg = (InternalKnowledgePackage) ctx.kBase.addPackage(CoreComponentFactory.get().createKnowledgePackage(changeSet.getResourceName())).get();
                } catch (InterruptedException | ExecutionException e) {
                    throw new RuntimeException(e);
                }
            }
            if (kpkg != null) {
                for (Rule newRule : kpkg.getRules()) {
                    RuleImpl rule = oldKpkg.getRule(newRule.getName());
                    if (rule != null) {
                        rule.setLoadOrder(((RuleImpl) newRule).getLoadOrder());
                    }
                }
            }
            this.updateResource(pkgbuilder, ckbuilder, changeSet);
            for (ResourceChange change : changeSet.getChanges()) {
                String changedItemName = change.getName();
                if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.REMOVED) {
                    switch(change.getType()) {
                        case GLOBAL:
                            oldKpkg.removeGlobal(changedItemName);
                            AtomicInteger globalCounter = globalsCounter.get(changedItemName);
                            if (globalCounter == null || globalCounter.decrementAndGet() <= 0) {
                                ctx.kBase.removeGlobal(changedItemName);
                            }
                            break;
                        case RULE:
                            RuleImpl removedRule = oldKpkg.getRule(changedItemName);
                            if (removedRule != null) {
                                rulesToBeRemoved.add(removedRule);
                                oldKpkg.removeRule(removedRule);
                            }
                            break;
                        case DECLARATION:
                            oldKpkg.removeTypeDeclaration(changedItemName);
                            break;
                        case FUNCTION:
                            oldKpkg.removeFunction(changedItemName);
                            break;
                        default:
                            throw new IllegalArgumentException("Unsupported change type: " + change.getType() + "!");
                    }
                }
                if (kpkg != null && (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.ADDED)) {
                    switch(change.getType()) {
                        case GLOBAL:
                            globalsCounter.computeIfAbsent(changedItemName, name -> ctx.kBase.getGlobals().get(name) == null ? new AtomicInteger(1) : new AtomicInteger(0)).incrementAndGet();
                            Class<?> globalClass = kpkg.getGlobals().get(changedItemName);
                            oldKpkg.addGlobal(changedItemName, globalClass);
                            ctx.kBase.addGlobal(changedItemName, globalClass);
                            break;
                        case RULE:
                            RuleImpl addedRule = kpkg.getRule(changedItemName);
                            rulesToBeAdded.add(addedRule);
                            oldKpkg.addRule(addedRule);
                            break;
                        case DECLARATION:
                            TypeDeclaration addedType = kpkg.getTypeDeclaration(changedItemName);
                            oldKpkg.addTypeDeclaration(addedType);
                            break;
                        case FUNCTION:
                            Function addedFunction = kpkg.getFunctions().get(changedItemName);
                            oldKpkg.addFunction(addedFunction);
                            break;
                        default:
                            throw new IllegalArgumentException("Unsupported change type: " + change.getType() + "!");
                    }
                }
            }
        }
    }
    if (ctx.modifyingUsedClass) {
        invalidateAccessorForOldClass();
        updateAllResources(pkgbuilder, ckbuilder);
    }
    ((CompositeKnowledgeBuilderImpl) ckbuilder).build(false);
    KieBaseUpdate kieBaseUpdate = new KieBaseUpdate(rulesToBeRemoved, rulesToBeAdded);
    ctx.kBase.beforeIncrementalUpdate(kieBaseUpdate);
    ctx.kBase.removeRules(rulesToBeRemoved);
    ctx.kBase.addRules(rulesToBeAdded);
    ctx.kBase.afterIncrementalUpdate(kieBaseUpdate);
    for (InternalWorkingMemory wm : ctx.kBase.getWorkingMemories()) {
        wm.notifyWaitOnRest();
    }
}
Also used : CanonicalKiePackages(org.drools.modelcompiler.CanonicalKiePackages) ResourceChange(org.kie.internal.builder.ResourceChange) Function(org.drools.core.rule.Function) TypeDeclaration(org.drools.core.rule.TypeDeclaration) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) ChangeType(org.kie.internal.builder.ChangeType) HashMap(java.util.HashMap) CoreComponentFactory(org.drools.core.reteoo.CoreComponentFactory) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) KieBaseUpdate(org.drools.core.impl.KieBaseUpdate) Rule(org.kie.api.definition.rule.Rule) CompositeKnowledgeBuilderImpl(org.drools.compiler.builder.impl.CompositeKnowledgeBuilderImpl) KiePackage(org.kie.api.definition.KiePackage) KieBaseUpdaterImplContext(org.drools.compiler.kie.builder.impl.KieBaseUpdaterImplContext) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) ExecutionException(java.util.concurrent.ExecutionException) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) List(java.util.List) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KieBaseUpdaterImpl(org.drools.compiler.kie.builder.impl.KieBaseUpdaterImpl) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Function(org.drools.core.rule.Function) ExecutionException(java.util.concurrent.ExecutionException) ResourceChange(org.kie.internal.builder.ResourceChange) CanonicalKiePackages(org.drools.modelcompiler.CanonicalKiePackages) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositeKnowledgeBuilderImpl(org.drools.compiler.builder.impl.CompositeKnowledgeBuilderImpl) KieBaseUpdate(org.drools.core.impl.KieBaseUpdate) Rule(org.kie.api.definition.rule.Rule) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 5 with InternalKnowledgeBuilder

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

the class KieBuilderSetImpl method buildChanges.

private IncrementalResults buildChanges(Collection<String> filesToBuild) {
    Map<String, Collection<KnowledgeBuilderResult>> currentResults = new HashMap<>();
    InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModuleIgnoringErrors();
    for (KieBaseModel kBaseModel : kieModule.getKieModuleModel().getKieBaseModels().values()) {
        InternalKnowledgeBuilder kBuilder = (InternalKnowledgeBuilder) kieModule.getKnowledgeBuilderForKieBase(kBaseModel.getName());
        if (kBuilder == null) {
            continue;
        }
        CompositeKnowledgeBuilder ckbuilder = kBuilder.batch();
        boolean useFolders = kBuilder.getBuilderConfiguration().isGroupDRLsInKieBasesByFolder();
        KnowledgeBuilderImpl.ResourceRemovalResult removalResult = new KnowledgeBuilderImpl.ResourceRemovalResult();
        Set<String> wrongResources = resourcesWithErrors.get(kBaseModel.getName());
        for (String resourceName : wrongResources) {
            removalResult.add(kBuilder.removeObjectsGeneratedFromResource(new DummyResource(resourceName)));
            removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, resourceName, useFolders));
        }
        for (String file : filesToBuild) {
            if (wrongResources.contains(file)) {
                removalResult.mergeModified(true);
            } else {
                // remove the objects generated by the old Resource
                removalResult.add(kBuilder.removeObjectsGeneratedFromResource(new DummyResource(file)));
                // add the modified Resource
                removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, file, useFolders));
            }
        }
        if (removalResult.isModified()) {
            boolean typeRefreshed = !removalResult.getRemovedTypes().isEmpty();
            if (typeRefreshed) {
                ProjectClassLoader projectClassLoader = (ProjectClassLoader) kBuilder.getRootClassLoader();
                projectClassLoader.reinitTypes();
                for (String removedType : removalResult.getRemovedTypes()) {
                    projectClassLoader.undefineClass(removedType);
                }
            }
            ckbuilder.build();
            if (typeRefreshed) {
                Collection<KiePackage> kiePackages = kBuilder.getKnowledgePackages();
                for (KiePackage kiePackage : kiePackages) {
                    ((InternalKnowledgePackage) kiePackage).wireStore();
                    ((InternalKnowledgePackage) kiePackage).wireTypeDeclarations();
                }
            }
            resourcesWithErrors.put(kBaseModel.getName(), findResourcesWithMessages(kBuilder));
            if (kBuilder.hasResults(getSeverities())) {
                currentResults.put(kBaseModel.getName(), kBuilder.getResults(getSeverities()));
            }
            if (kBuilder.hasErrors()) {
                kBuilder.undo();
            } else {
                KieServices.Factory.get().getRepository().addKieModule(kieModule);
                kieBuilder.updateKieModuleMetaInfo();
            }
        }
    }
    IncrementalResultsImpl results = getIncrementalResults(currentResults);
    previousErrors = currentResults;
    return results;
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) HashMap(java.util.HashMap) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Collection(java.util.Collection) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

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