Search in sources :

Example 11 with KieContainerImpl

use of org.drools.compiler.kie.builder.impl.KieContainerImpl in project drools by kiegroup.

the class KieProjectRuntimeModulesTest method createMultipleJarAndFileResources.

@Test
public void createMultipleJarAndFileResources() throws IOException, ClassNotFoundException, InterruptedException {
    KieModuleModel kProjModel1 = createKieModule("jar1", true);
    KieModuleModel kProjModel2 = createKieModule("jar2", true);
    KieModuleModel kProjModel3 = createKieModule("jar3", true);
    KieModuleModel kProjModel4 = createKieModule("fol4", false);
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId("jar1", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId("jar2", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId3 = KieServices.Factory.get().newReleaseId("jar3", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId4 = KieServices.Factory.get().newReleaseId("fol4", "art1", "1.0-SNAPSHOT");
    java.io.File file1 = fileManager.newFile("jar1-1.0-SNAPSHOT.jar");
    java.io.File file2 = fileManager.newFile("jar2-1.0-SNAPSHOT.jar");
    java.io.File file3 = fileManager.newFile("jar3-1.0-SNAPSHOT.jar");
    java.io.File fol4 = fileManager.newFile("fol4-1.0-SNAPSHOT");
    ZipKieModule mod1 = new ZipKieModule(releaseId1, kProjModel1, file1);
    ZipKieModule mod2 = new ZipKieModule(releaseId2, kProjModel2, file2);
    ZipKieModule mod3 = new ZipKieModule(releaseId3, kProjModel3, file3);
    FileKieModule mod4 = new FileKieModule(releaseId4, kProjModel4, fol4);
    mod1.addKieDependency(mod2);
    mod1.addKieDependency(mod3);
    mod1.addKieDependency(mod4);
    KieModuleKieProject kProject = new KieModuleKieProject(mod1);
    KieContainer kContainer = new KieContainerImpl(kProject, null);
    KieBase kBase = kContainer.getKieBase("jar1.KBase1");
    ClassLoader cl = ((RuleBase) kBase).getRootClassLoader();
    Class cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar1");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar2");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar3");
    assertNotNull(cls);
    testEntry(new KProjectTestClassImpl("jar1", kContainer), "jar1");
    testEntry(new KProjectTestClassImpl("jar2", kContainer), "jar2");
    testEntry(new KProjectTestClassImpl("jar3", kContainer), "jar3");
    testEntry(new KProjectTestClassImpl("fol4", kContainer), "fol4");
}
Also used : ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) FileKieModule(org.drools.compiler.kie.builder.impl.FileKieModule) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) ReleaseId(org.kie.api.builder.ReleaseId) KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) RuleBase(org.drools.core.impl.RuleBase) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 12 with KieContainerImpl

use of org.drools.compiler.kie.builder.impl.KieContainerImpl in project drools by kiegroup.

the class KieCDIExtension method addKBaseBean.

public void addKBaseBean(AfterBeanDiscovery abd, KieCDIEntry entry) {
    ReleaseId releaseId = entry.getReleaseId();
    // default to classpath, but allow it to be overridden
    KieContainerImpl kieContainer = classpathKContainer;
    if (releaseId != null) {
        kieContainer = (KieContainerImpl) gavs.get(releaseId);
        if (kieContainer == null) {
            log.error("Unable to create @KBase({}), could not retrieve KieContainer for ReleaseId {}", entry.getValue(), releaseId.toString());
            return;
        }
    }
    KieProject kProject = kieContainer.getKieProject();
    KieBaseModel kBaseModel = null;
    String kBaseQName = entry.getValue();
    if (StringUtils.isEmpty(kBaseQName)) {
        kBaseModel = kProject.getDefaultKieBaseModel();
    } else {
        kBaseModel = kProject.getKieBaseModel(kBaseQName);
    }
    if (kBaseModel == null) {
        log.error("Annotation @KBase({}) found, but no KieBaseModel exists.\nEither the required kmodule.xml does not exist, is corrupted, or is missing the KieBase entry", kBaseQName);
        return;
    }
    if (!kBaseModel.getScope().trim().equals(entry.getScope().getClass().getName())) {
        try {
            if (kBaseModel.getScope().indexOf('.') >= 0) {
                entry.setScope((Class<? extends Annotation>) Class.forName(kBaseModel.getScope()));
            } else {
                entry.setScope((Class<? extends Annotation>) Class.forName("javax.enterprise.context." + kBaseModel.getScope()));
            }
        } catch (ClassNotFoundException e) {
            log.error("KieBaseModule {} overrides default annotation, but it was not able to find it {}\n{}", new String[] { kBaseQName, kBaseModel.getScope(), e.getMessage() });
        }
    }
    KBaseBean bean = new KBaseBean(kBaseQName, kBaseModel, kieContainer, entry.getKReleaseId(), entry.getScope(), entry.getName(), entry.getInjectionPoints());
    if (log.isDebugEnabled()) {
        InternalKieModule kModule = kProject.getKieModuleForKBase(kBaseQName);
        log.debug("Added Bean for @KBase({})", kBaseQName, kModule);
    }
    abd.addBean(bean);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KReleaseId(org.kie.api.cdi.KReleaseId) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 13 with KieContainerImpl

use of org.drools.compiler.kie.builder.impl.KieContainerImpl in project drools by kiegroup.

the class KieCDIExtension method addKContainerBean.

public void addKContainerBean(AfterBeanDiscovery abd, KieCDIEntry entry) {
    ReleaseId releaseId = entry.getReleaseId();
    // default to classpath, but allow it to be overriden
    KieContainerImpl kieContainer = classpathKContainer;
    if (releaseId != null) {
        kieContainer = (KieContainerImpl) gavs.get(releaseId);
        if (kieContainer == null) {
            log.error("Could not retrieve KieContainer for ReleaseId {}", entry.getValue(), releaseId.toString());
            return;
        }
    }
    KContainerBean bean = new KContainerBean(kieContainer, entry.getKReleaseId(), entry.getName(), entry.getInjectionPoints());
    if (log.isDebugEnabled()) {
        log.debug("Added Bean for @KContainer({})", releaseId);
    }
    abd.addBean(bean);
}
Also used : KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KReleaseId(org.kie.api.cdi.KReleaseId) ReleaseId(org.kie.api.builder.ReleaseId)

Example 14 with KieContainerImpl

use of org.drools.compiler.kie.builder.impl.KieContainerImpl in project drools by kiegroup.

the class KieProjectRuntimeModulesTest method createMultpleJarAndFileResources.

@Test
public void createMultpleJarAndFileResources() throws IOException, ClassNotFoundException, InterruptedException {
    KieModuleModel kProjModel1 = createKieModule("jar1", true);
    KieModuleModel kProjModel2 = createKieModule("jar2", true);
    KieModuleModel kProjModel3 = createKieModule("jar3", true);
    KieModuleModel kProjModel4 = createKieModule("fol4", false);
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId("jar1", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId("jar2", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId3 = KieServices.Factory.get().newReleaseId("jar3", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId4 = KieServices.Factory.get().newReleaseId("fol4", "art1", "1.0-SNAPSHOT");
    java.io.File file1 = fileManager.newFile("jar1-1.0-SNAPSHOT.jar");
    java.io.File file2 = fileManager.newFile("jar2-1.0-SNAPSHOT.jar");
    java.io.File file3 = fileManager.newFile("jar3-1.0-SNAPSHOT.jar");
    java.io.File fol4 = fileManager.newFile("fol4-1.0-SNAPSHOT");
    ZipKieModule mod1 = new ZipKieModule(releaseId1, kProjModel1, file1);
    ZipKieModule mod2 = new ZipKieModule(releaseId2, kProjModel2, file2);
    ZipKieModule mod3 = new ZipKieModule(releaseId3, kProjModel3, file3);
    FileKieModule mod4 = new FileKieModule(releaseId4, kProjModel4, fol4);
    mod1.addKieDependency(mod2);
    mod1.addKieDependency(mod3);
    mod1.addKieDependency(mod4);
    KieModuleKieProject kProject = new KieModuleKieProject(mod1);
    KieContainer kContainer = new KieContainerImpl(kProject, null);
    KieBase kBase = kContainer.getKieBase("jar1.KBase1");
    ClassLoader cl = ((KnowledgeBaseImpl) kBase).getRootClassLoader();
    Class cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar1");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar2");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar3");
    assertNotNull(cls);
    testEntry(new KProjectTestClassImpl("jar1", kContainer), "jar1");
    testEntry(new KProjectTestClassImpl("jar2", kContainer), "jar2");
    testEntry(new KProjectTestClassImpl("jar3", kContainer), "jar3");
    testEntry(new KProjectTestClassImpl("fol4", kContainer), "fol4");
}
Also used : ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) FileKieModule(org.drools.compiler.kie.builder.impl.FileKieModule) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ReleaseId(org.kie.api.builder.ReleaseId) KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 15 with KieContainerImpl

use of org.drools.compiler.kie.builder.impl.KieContainerImpl in project drools by kiegroup.

the class KieHelloWorldTest method testClassLoaderStore.

@Test
public void testClassLoaderStore() throws Exception {
    // DROOLS-1766
    String drl = "package org; declare Person name : String end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieProject kieProject = ((KieContainerImpl) kcontainer).getKieProject();
    ResultsImpl messages = kieProject.verify();
    assertSame(kieProject.getClassLoader(), kcontainer.getClassLoader());
    ProjectClassLoader pcl = ((ProjectClassLoader) kieProject.getClassLoader());
    assertNotNull(pcl.getStore().get("org/Person.class"));
}
Also used : ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KieFileSystem(org.kie.api.builder.KieFileSystem) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KieServices(org.kie.api.KieServices) ResultsImpl(org.drools.compiler.kie.builder.impl.ResultsImpl) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieContainerImpl (org.drools.compiler.kie.builder.impl.KieContainerImpl)25 ReleaseId (org.kie.api.builder.ReleaseId)17 KieContainer (org.kie.api.runtime.KieContainer)15 Test (org.junit.Test)14 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)11 KieServices (org.kie.api.KieServices)10 KieProject (org.drools.compiler.kie.builder.impl.KieProject)9 KieModule (org.kie.api.builder.KieModule)9 KieModuleKieProject (org.drools.compiler.kie.builder.impl.KieModuleKieProject)8 KieSession (org.kie.api.runtime.KieSession)7 KieBase (org.kie.api.KieBase)4 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)4 HashMap (java.util.HashMap)3 MemoryKieModule (org.drools.compiler.kie.builder.impl.MemoryKieModule)3 Person (org.drools.modelcompiler.domain.Person)3 KieFileSystem (org.kie.api.builder.KieFileSystem)3 KieBaseModel (org.kie.api.builder.model.KieBaseModel)3 KieModuleModel (org.kie.api.builder.model.KieModuleModel)3 KReleaseId (org.kie.api.cdi.KReleaseId)3 FileKieModule (org.drools.compiler.kie.builder.impl.FileKieModule)2