Search in sources :

Example 6 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl 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 7 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class KieContainerImpl method getKieBase.

public KieBase getKieBase(String kBaseName) {
    KieBase kBase = kBases.get(kBaseName);
    if (kBase == null) {
        KieBaseModelImpl kBaseModel = getKieBaseModelImpl(kBaseName);
        synchronized (kBaseModel) {
            kBase = kBases.get(kBaseName);
            if (kBase == null) {
                BuildContext buildContext = new BuildContext();
                kBase = createKieBase(kBaseModel, kProject, buildContext, null);
                if (kBase == null) {
                    // build error, throw runtime exception
                    throw new RuntimeException("Error while creating KieBase" + buildContext.getMessages().filterMessages(Level.ERROR));
                }
                kBases.put(kBaseName, kBase);
            }
        }
    }
    return kBase;
}
Also used : KieBase(org.kie.api.KieBase) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl)

Example 8 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class AbstractKieProject method verify.

private void verify(String[] kBaseNames, BuildContext buildContext) {
    for (String modelName : kBaseNames) {
        KieBaseModelImpl kieBaseModel = (KieBaseModelImpl) kBaseModels.get(modelName);
        if (kieBaseModel == null) {
            throw new RuntimeException("Unknown KieBase. Cannot find a KieBase named: " + modelName);
        }
        buildKnowledgePackages(kieBaseModel, buildContext);
    }
}
Also used : KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl)

Example 9 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class KieBuilderTest method testKieModuleDependencies.

@Test
public void testKieModuleDependencies() throws ClassNotFoundException, InterruptedException, IOException {
    KieServices ks = KieServices.Factory.get();
    String namespace1 = "org.kie.test1";
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId(namespace1, "memory", "1.0-SNAPSHOT");
    KieModuleModel kProj1 = createKieProject(namespace1);
    KieFileSystem kfs1 = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs1, namespace1, releaseId1, kProj1);
    KieBuilder kb1 = createKieBuilder(kfs1);
    kb1.buildAll();
    if (kb1.getResults().hasMessages(Level.ERROR)) {
        fail("Unable to build KieJar\n" + kb1.getResults().toString());
    }
    KieRepository kr = ks.getRepository();
    KieModule kModule1 = kr.getKieModule(releaseId1);
    assertNotNull(kModule1);
    String namespace2 = "org.kie.test2";
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId(namespace2, "memory", "1.0-SNAPSHOT");
    KieModuleModel kProj2 = createKieProject(namespace2);
    KieBaseModelImpl kieBase2 = (KieBaseModelImpl) kProj2.getKieBaseModels().get(namespace2);
    kieBase2.addInclude(namespace1);
    KieFileSystem kfs2 = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs2, namespace2, releaseId2, kProj2);
    KieBuilder kb2 = createKieBuilder(kfs2);
    kb2.setDependencies(kModule1);
    kb2.buildAll();
    if (kb2.getResults().hasMessages(Level.ERROR)) {
        fail("Unable to build KieJar\n" + kb2.getResults().toString());
    }
    KieModule kModule2 = kr.getKieModule(releaseId2);
    assertNotNull(kModule2);
    KieContainer kContainer = ks.newKieContainer(releaseId2);
    KieBase kBase = kContainer.getKieBase(namespace2);
    KieSession kSession = kBase.newKieSession();
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    assertEquals(2, list.size());
    if ("org.kie.test1.Message".equals(list.get(0).getClass().getName())) {
        assertEquals("org.kie.test2.Message", list.get(1).getClass().getName());
    } else {
        assertEquals("org.kie.test2.Message", list.get(0).getClass().getName());
        assertEquals("org.kie.test1.Message", list.get(1).getClass().getName());
    }
}
Also used : KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 10 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class AbstractKieProject method indexKieModule.

private void indexKieModule(Map<String, InternalKieModule> kJarFromKBaseName, InternalKieModule kJar, boolean isMainModule) {
    boolean defaultKieBaseFromMain = false;
    boolean defaultKieSessionFromMain = false;
    boolean defaultStatelessKieSessionFromMain = false;
    KieModuleModel kieProject = kJar.getKieModuleModel();
    for (KieBaseModel kieBaseModel : kieProject.getKieBaseModels().values()) {
        if (kieBaseModel.isDefault()) {
            if (defaultKieBase == null || (isMainModule && !defaultKieBaseFromMain)) {
                defaultKieBase = kieBaseModel;
                defaultKieBaseFromMain = isMainModule;
            } else {
                defaultKieBase = null;
                log.warn("Found more than one default KieBase: disabling all. KieBases will be accessible only by name");
            }
        }
        kBaseModels.put(kieBaseModel.getName(), kieBaseModel);
        // should already be set, but just in case
        ((KieBaseModelImpl) kieBaseModel).setKModule(kieProject);
        kJarFromKBaseName.put(kieBaseModel.getName(), kJar);
        for (KieSessionModel kieSessionModel : kieBaseModel.getKieSessionModels().values()) {
            if (kieSessionModel.isDefault()) {
                if (kieSessionModel.getType() == KieSessionModel.KieSessionType.STATEFUL) {
                    if (defaultKieSession == null || (isMainModule && !defaultKieSessionFromMain)) {
                        defaultKieSession = kieSessionModel;
                        defaultKieSessionFromMain = isMainModule;
                    } else {
                        defaultKieSession = null;
                        log.warn("Found more than one default KieSession: disabling all. KieSessions will be accessible only by name");
                    }
                } else {
                    if (defaultStatelessKieSession == null || (isMainModule && !defaultStatelessKieSessionFromMain)) {
                        defaultStatelessKieSession = kieSessionModel;
                        defaultStatelessKieSessionFromMain = isMainModule;
                    } else {
                        defaultStatelessKieSession = null;
                        log.warn("Found more than one default StatelessKieSession: disabling all. StatelessKieSessions will be accessible only by name");
                    }
                }
            }
            // should already be set, but just in case
            ((KieSessionModelImpl) kieSessionModel).setKBase(kieBaseModel);
            kSessionModels.put(kieSessionModel.getName(), kieSessionModel);
        }
    }
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieSessionModelImpl(org.drools.compiler.kproject.models.KieSessionModelImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Aggregations

KieBaseModelImpl (org.drools.compiler.kproject.models.KieBaseModelImpl)13 ArrayList (java.util.ArrayList)6 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 KieBaseModel (org.kie.api.builder.model.KieBaseModel)5 KieServices (org.kie.api.KieServices)4 ReleaseId (org.kie.api.builder.ReleaseId)4 KieModuleModel (org.kie.api.builder.model.KieModuleModel)4 Collections (java.util.Collections)3 Map (java.util.Map)3 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)3 KieJarChangeSet (org.drools.compiler.kie.util.KieJarChangeSet)3 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)3 KieSessionModel (org.kie.api.builder.model.KieSessionModel)3 ResourceType (org.kie.api.io.ResourceType)3 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)2 InputStream (java.io.InputStream)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2