Search in sources :

Example 76 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieLoggersTest method testDeclarativeKieFileLogger.

@Test
public void testDeclarativeKieFileLogger() throws Exception {
    String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + "    when\n" + "        m : Message( myMessage : message )\n" + "    then\n" + "end";
    String fileName = "target/testKieFileLogger";
    File file = new File(fileName + ".log");
    if (file.exists()) {
        file.delete();
    }
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setFileLogger(fileName);
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML());
    kfs.write("src/main/resources/KBase1/rule.drl", drl);
    KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
    KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());
    KieSession ksession = kieContainer.newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    int fired = ksession.fireAllRules();
    assertEquals(1, fired);
    // disposing the ksession also flushes and closes the logger
    ksession.dispose();
    file = new File(fileName + ".log");
    assertTrue(file.exists());
    file.delete();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 77 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieLoggersTest method testDeclarativeKieConsoleLogger.

@Test
public void testDeclarativeKieConsoleLogger() throws Exception {
    String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + "    when\n" + "        m : Message( myMessage : message )\n" + "    then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setConsoleLogger("logger");
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML());
    kfs.write("src/main/resources/KBase1/rule.drl", drl);
    KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
    KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());
    KieSession ksession = kieContainer.newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    int fired = ksession.fireAllRules();
    assertEquals(1, fired);
    KieRuntimeLogger logger = ksession.getLogger();
    assertNotNull(logger);
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 78 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieLoggersTest method testDeclarativeKieConsoleLoggerStateless.

@Test
public void testDeclarativeKieConsoleLoggerStateless() throws Exception {
    String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + "    when\n" + "        m : Message( myMessage : message )\n" + "    then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setType(KieSessionModel.KieSessionType.STATELESS).setConsoleLogger("logger");
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML());
    kfs.write("src/main/resources/KBase1/rule.drl", drl);
    KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
    KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());
    StatelessKieSession ksession = kieContainer.newStatelessKieSession("KSession1");
    ksession.execute(new Message("Hello World"));
    KieRuntimeLogger logger = ksession.getLogger();
    assertNotNull(logger);
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 79 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class MBeansMonitoringTest method testLoadKjarFromClasspath.

/**
 * Copied from KieRepositoryTest to test JMX monitoring
 */
@Test
public void testLoadKjarFromClasspath() {
    // DROOLS-1335
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { this.getClass().getResource("/kie-project-simple-1.0.0.jar") });
    Thread.currentThread().setContextClassLoader(urlClassLoader);
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    try {
        KieServices ks = KieServices.Factory.get();
        KieRepository kieRepository = ks.getRepository();
        ReleaseId releaseId = ks.newReleaseId("org.test", "kie-project-simple", "1.0.0");
        KieModule kieModule = kieRepository.getKieModule(releaseId);
        assertNotNull(kieModule);
        assertEquals(releaseId, kieModule.getReleaseId());
        ks.newKieContainer("myID", releaseId);
        KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID"), KieContainerMonitorMXBean.class);
        assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(releaseId));
        assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(releaseId));
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}
Also used : KieRepository(org.kie.api.builder.KieRepository) URLClassLoader(java.net.URLClassLoader) KieContainerMonitorMXBean(org.kie.api.management.KieContainerMonitorMXBean) URLClassLoader(java.net.URLClassLoader) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 80 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class DynamicRuleLoadTest method updateToVersion.

public void updateToVersion() {
    KieServices ks = KieServices.Factory.get();
    // Create a new jar for version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    KieModule km = createAndDeployJar(ks, releaseId2, drl1, drl2_2);
    // try to update the container to version 1.1.0
    kieContainer.updateToVersion(releaseId2);
    // create and use a new session
    ksession.insert(new Message("Hello World"));
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule)

Aggregations

KieModule (org.kie.api.builder.KieModule)124 KieServices (org.kie.api.KieServices)104 Test (org.junit.Test)94 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)84 ReleaseId (org.kie.api.builder.ReleaseId)84 KieContainer (org.kie.api.runtime.KieContainer)75 KieSession (org.kie.api.runtime.KieSession)64 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)53 Message (org.drools.compiler.Message)25 KieFileSystem (org.kie.api.builder.KieFileSystem)23 Resource (org.kie.api.io.Resource)20 ArrayList (java.util.ArrayList)15 KieModuleModel (org.kie.api.builder.model.KieModuleModel)15 KieBuilder (org.kie.api.builder.KieBuilder)13 KieRepository (org.kie.api.builder.KieRepository)10 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)7 KieBaseModel (org.kie.api.builder.model.KieBaseModel)7 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 Results (org.kie.api.builder.Results)5