Search in sources :

Example 16 with KieContainerImpl

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

the class GeneratedClassNamesTest method testGeneratedClassNames.

@Test
public void testGeneratedClassNames() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person(name == \"Mario\")\n" + "then\n" + "  System.out.println(\"hello\");\n" + "end";
    KieServices ks = KieServices.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "kjar-test-" + UUID.randomUUID(), "1.0");
    createKieBuilder(ks, getDefaultKieModuleModel(ks), releaseId, toKieFiles(new String[] { str }));
    KieContainer kcontainer = ks.newKieContainer(releaseId);
    KieModule kieModule = ((KieContainerImpl) kcontainer).getKieModuleForKBase("kbase");
    assertTrue(kieModule instanceof CanonicalKieModule);
    KieSession ksession = kcontainer.newKieSession();
    Set<String> generatedClassNames = ((CanonicalKieModule) kieModule).getGeneratedClassNames();
    assertGeneratedClassNames(generatedClassNames);
    Person me = new Person("Mario", 40);
    ksession.insert(me);
    int fired = ksession.fireAllRules();
    assertEquals(1, fired);
}
Also used : KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) Person(org.drools.modelcompiler.domain.Person) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 17 with KieContainerImpl

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

the class GeneratedClassNamesTest method testModuleWithDep.

private void testModuleWithDep(ProjectClassLoader projectClassLoader) throws Exception {
    KieServices ks = KieServices.get();
    ReleaseId releaseIdDep = ks.newReleaseId("org.example", "depRule", "1.0.0");
    ReleaseId releaseIdMain = ks.newReleaseId("org.example", "mainRule", "1.0.0");
    String depStr = "package org.example.dep\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule R_dep when\n" + "  $p : Person(name == \"Mario\")\n" + "then\n" + "  System.out.println(\"hello dep rule\");\n" + "end";
    String mainStr = "package org.example.main\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule R_main when\n" + "  $p : Person(name == \"Luca\")\n" + "then\n" + "  System.out.println(\"hello main rule\");\n" + "end";
    String mainPom = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" + "  <modelVersion>4.0.0</modelVersion>\n" + "\n" + "  <groupId>" + releaseIdMain.getGroupId() + "</groupId>\n" + "  <artifactId>" + releaseIdMain.getArtifactId() + "</artifactId>\n" + "  <version>" + releaseIdMain.getVersion() + "</version>\n" + "  <dependencies>\n" + "    <dependency>\n" + "      <groupId>" + releaseIdDep.getGroupId() + "</groupId>\n" + "      <artifactId>" + releaseIdDep.getArtifactId() + "</artifactId>\n" + "      <version>" + releaseIdDep.getVersion() + "</version>\n" + "    </dependency>\n" + "  </dependencies>" + "</project>";
    // create dep
    KieFileSystem depKfs = ks.newKieFileSystem();
    KieModuleModel depModuleModel = ks.newKieModuleModel();
    KieBaseModel depKieBaseModel = depModuleModel.newKieBaseModel("dep").setDefault(false);
    depKieBaseModel.addPackage("*");
    depKfs.writeKModuleXML(depModuleModel.toXML());
    depKfs.generateAndWritePomXML(releaseIdDep);
    Resource depResource = ResourceFactory.newByteArrayResource(depStr.getBytes());
    depResource.setSourcePath("src/main/resources/org/example/dep/r1.drl");
    depKfs.write(depResource);
    KieBuilder depKieBuilder = ks.newKieBuilder(depKfs);
    depKieBuilder.buildAll(ExecutableModelProject.class);
    CanonicalKieModule depKieModule = (CanonicalKieModule) depKieBuilder.getKieModule();
    MemoryKieModule depMemoryKieModule = (MemoryKieModule) depKieModule.getInternalKieModule();
    MemoryFileSystem depMfs = depMemoryKieModule.getMemoryFileSystem();
    byte[] depBinary = depMfs.writeAsBytes();
    Resource depJarResource = ks.getResources().newByteArrayResource(depBinary);
    // create main
    KieFileSystem mainKfs = ks.newKieFileSystem();
    KieModuleModel mainModuleModel = ks.newKieModuleModel();
    KieBaseModel mainKieBaseModel = mainModuleModel.newKieBaseModel("main").setDefault(true);
    mainKieBaseModel.addPackage("*");
    mainKieBaseModel.addInclude("dep");
    Resource mainResource = ResourceFactory.newByteArrayResource(mainStr.getBytes());
    mainResource.setSourcePath("src/main/resources/org/example/main/r2.drl");
    mainKfs.write(mainResource);
    mainKfs.writeKModuleXML(mainModuleModel.toXML());
    mainKfs.write("pom.xml", mainPom);
    KieBuilder mainKieBuilder = ks.newKieBuilder(mainKfs);
    mainKieBuilder.setDependencies(depJarResource);
    mainKieBuilder.buildAll(ExecutableModelProject.class);
    CanonicalKieModule mainKieModule = (CanonicalKieModule) mainKieBuilder.getKieModule();
    MemoryKieModule mainMemoryKieModule = (MemoryKieModule) mainKieModule.getInternalKieModule();
    MemoryFileSystem mainMfs = mainMemoryKieModule.getMemoryFileSystem();
    byte[] mainBinary = mainMfs.writeAsBytes();
    Resource mainJarResource = ks.getResources().newByteArrayResource(mainBinary);
    // cleanup
    ks.getRepository().removeKieModule(releaseIdMain);
    ks.getRepository().removeKieModule(releaseIdDep);
    ks.getRepository().addKieModule(mainJarResource, depJarResource);
    KieContainerImpl kieContainer;
    if (projectClassLoader == null) {
        kieContainer = (KieContainerImpl) ks.newKieContainer(releaseIdMain);
    } else {
        kieContainer = (KieContainerImpl) ks.newKieContainer(releaseIdMain, projectClassLoader);
    }
    CanonicalKieModule mainLoadedModule = (CanonicalKieModule) kieContainer.getKieModuleForKBase("main");
    KieBase kbase = kieContainer.getKieBase();
    Set<String> mainGeneratedClassNames = mainLoadedModule.getGeneratedClassNames();
    assertGeneratedClassNamesWithDep(mainGeneratedClassNames);
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Person("Mario"));
    ksession.insert(new Person("Luca"));
    int fired = ksession.fireAllRules();
    assertEquals(2, fired);
    ksession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBaseModel(org.kie.api.builder.model.KieBaseModel) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Person(org.drools.modelcompiler.domain.Person)

Example 18 with KieContainerImpl

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

the class KieCDIExtension method addKSessionBean.

public void addKSessionBean(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("Unable to create KSession({}), could not retrieve KieContainer for ReleaseId {}", entry.getValue(), releaseId.toString());
            return;
        }
    }
    KieProject kProject = kieContainer.getKieProject();
    String kSessionName = entry.getValue();
    KieSessionModel kSessionModel = null;
    if (StringUtils.isEmpty(kSessionName)) {
        kSessionModel = (entry.getType() == KieSession.class) ? kProject.getDefaultKieSession() : kProject.getDefaultStatelessKieSession();
    } else {
        kSessionModel = kProject.getKieSessionModel(kSessionName);
    }
    if (kSessionModel == null) {
        log.error("Annotation @KSession({}) found, but no KieSessionModel exists.\nEither the required kmodule.xml does not exist, is corrupted, or is missing the KieBase entry", kSessionName);
        return;
    }
    if (kSessionModel.getScope() != null && !kSessionModel.getScope().trim().equals(entry.getScope().getClass().getName())) {
        try {
            if (kSessionModel.getScope().indexOf('.') >= 0) {
                entry.setScope((Class<? extends Annotation>) Class.forName(kSessionModel.getScope()));
            } else {
                entry.setScope((Class<? extends Annotation>) Class.forName("javax.enterprise.context." + kSessionModel.getScope()));
            }
        } catch (ClassNotFoundException e) {
            log.error("KieBaseModule {} overrides default annotation, but it was not able to find it {}\n{}", new String[] { kSessionName, kSessionModel.getScope(), e.getMessage() });
        }
    }
    if (KieSessionType.STATELESS.equals(kSessionModel.getType())) {
        if (log.isDebugEnabled()) {
            InternalKieModule kModule = kProject.getKieModuleForKBase(((KieSessionModelImpl) kSessionModel).getKieBaseModel().getName());
            log.debug("Added Bean for Stateless @KSession({}) from: {}", kSessionName, kModule);
        }
        abd.addBean(new StatelessKSessionBean(kSessionModel, kieContainer, entry.getKReleaseId(), entry.getScope(), entry.getName(), entry.getInjectionPoints()));
    } else {
        InternalKieModule kModule = kProject.getKieModuleForKBase(((KieSessionModelImpl) kSessionModel).getKieBaseModel().getName());
        log.debug("Added Bean for Stateful @KSession({})  from: {}", kSessionName, kModule);
        abd.addBean(new StatefulKSessionBean(kSessionName, kSessionModel, kieContainer, entry.getKReleaseId(), entry.getScope(), entry.getName(), entry.getInjectionPoints()));
    }
}
Also used : KieSessionModelImpl(org.drools.compiler.kproject.models.KieSessionModelImpl) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) KReleaseId(org.kie.api.cdi.KReleaseId) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 19 with KieContainerImpl

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

the class ClassLoaderLeakTest method buildKjarsInALoop.

private void buildKjarsInALoop(boolean withExecModel) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieContainer kieContainer = null;
    int oldSize = Integer.MAX_VALUE;
    for (int i = 0; i < 3; i++) {
        KieModule kModule = buildKieModule("1.0." + i, RULES, withExecModel);
        if (kieContainer == null) {
            kieContainer = ks.newKieContainer(kModule.getReleaseId());
        } else {
            kieContainer.updateToVersion(kModule.getReleaseId());
        }
        KieProject kp = ((KieContainerImpl) kieContainer).getKieProject();
        ProjectClassLoader cl = (ProjectClassLoader) kp.getClassLoader();
        System.out.println(String.format("ProjectClassLoader.store size: %d, " + "retained bytes: %d.", cl.getStore().size(), cl.getStore().values().stream().map(b -> b.length).reduce(0, Integer::sum)));
        assertTrue(cl.getStore().size() <= oldSize);
        oldSize = cl.getStore().size();
        kr.removeKieModule(kModule.getReleaseId());
    }
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) KieFileSystem(org.kie.api.builder.KieFileSystem) KieBuilder(org.kie.api.builder.KieBuilder) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KieRepository(org.kie.api.builder.KieRepository) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) Message(org.kie.api.builder.Message) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) KieRepository(org.kie.api.builder.KieRepository) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KieServices(org.kie.api.KieServices) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer)

Example 20 with KieContainerImpl

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

the class DMNUpdateTest method test_as_kie_wb_common_services_backend_Builder.

@Test
public void test_as_kie_wb_common_services_backend_Builder() throws Exception {
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId v100 = ks.newReleaseId("org.kie", "dmn-test", "1.0.0");
    final KieModule kieModule = KieHelper.createAndDeployJar(ks, v100, ks.getResources().newClassPathResource("0001-input-data-string.dmn", this.getClass()));
    final KieContainer kieContainer = ks.newKieContainer(v100);
    final KieSession kieSession = kieContainer.newKieSession();
    final DMNRuntime runtime = kieSession.getKieRuntime(DMNRuntime.class);
    Assert.assertNotNull(runtime);
    assertThat(runtime.getModels(), hasSize(1));
    check0001_input_data_string(runtime);
    // the below is performed by the WB at: https://github.com/kiegroup/kie-wb-common/blob/9e6b6da145e61ac8f5a9f7c0259d44aa9d090a2b/kie-wb-common-services/kie-wb-common-services-backend/src/main/java/org/kie/workbench/common/services/backend/builder/core/Builder.java#L592-L620
    final KieProject kieProject = new KieModuleKieProject((InternalKieModule) kieModule, null);
    final KieContainer kieContainer2 = new KieContainerImpl(kieProject, ks.getRepository(), v100);
    // exhibit the issue.
    final KieSession kieSession2 = kieContainer2.newKieSession();
    final DMNRuntime runtime2 = kieSession2.getKieRuntime(DMNRuntime.class);
    Assert.assertNotNull(runtime2);
    assertThat(runtime2.getModels(), hasSize(1));
    check0001_input_data_string(runtime2);
}
Also used : KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) 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