Search in sources :

Example 86 with ReleaseId

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

the class MBeansMonitoringTest method testAggregatedAndDispose.

@Test
public void testAggregatedAndDispose() throws Exception {
    String drl = "package org.drools.compiler.integrationtests\n" + "rule ND\n" + "when\n" + "    String() \n" + "    not ( Double() ) \n" + "then\n" + "    // do nothing. \n" + "end\n" + "rule ND2\n" + "salience 1\n" + "when\n" + "    $s : String() \n" + "    not ( Double() ) \n" + "then\n" + "    try { Thread.sleep(10); } catch (Exception e) { }\n" + "    insert( new Double(0) );\n" + "    retract( $s );\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(KBASE1).setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel(KSESSION1).setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieBaseModel kieBaseModel2 = kproj.newKieBaseModel(KBASE2).setDefault(false);
    KieSessionModel ksessionModel2 = kieBaseModel2.newKieSessionModel(KSESSION2).setDefault(true).setType(KieSessionModel.KieSessionType.STATELESS);
    ReleaseId releaseId1 = ks.newReleaseId("org.kie.test", "mbeans", "1.0.0");
    createKJar(ks, kproj, releaseId1, null, drl);
    String containerId = "myContainerId";
    KieContainer kc = ks.newKieContainer(containerId, releaseId1);
    KieBase kiebase = kc.getKieBase(KBASE1);
    KieSessionConfiguration sessionConf = ks.newKieSessionConfiguration();
    sessionConf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = kc.newKieSession(KSESSION1, sessionConf);
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    KieSessionMonitoringMXBean aggrMonitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(containerId, KBASE1, KieSessionType.STATEFUL, KSESSION1), KieSessionMonitoringMXBean.class);
    long tft = 0;
    print(aggrMonitor);
    checkAgendaTotals(aggrMonitor, 0, 0, 0);
    checkTotalFactCount(aggrMonitor, 0);
    tft = checkTotalFiringTimeGEQ(aggrMonitor, tft);
    checkTotalSessions(aggrMonitor, 1);
    assertNull(aggrMonitor.getStatsForRule("ND"));
    assertNull(aggrMonitor.getStatsForRule("ND2"));
    assertFalse(aggrMonitor.getStatsByRule().containsKey("ND"));
    assertFalse(aggrMonitor.getStatsByRule().containsKey("ND2"));
    ksession.insert("Ciao");
    ksession.fireAllRules();
    print(aggrMonitor);
    checkAgendaTotals(aggrMonitor, 2, 1, 1);
    checkTotalFactCount(aggrMonitor, 1);
    tft = checkTotalFiringTimeGEQ(aggrMonitor, tft + 1);
    checkTotalSessions(aggrMonitor, 1);
    checkStatsForRule(aggrMonitor, "ND", 1, 1, 0);
    checkStatsForRule(aggrMonitor, "ND2", 1, 0, 1);
    checkStatsByRule(aggrMonitor, "ND", 1, 1, 0);
    checkStatsByRule(aggrMonitor, "ND2", 1, 0, 1);
    ksession.fireAllRules();
    print(aggrMonitor);
    checkAgendaTotals(aggrMonitor, 2, 1, 1);
    checkTotalFactCount(aggrMonitor, 1);
    tft = checkTotalFiringTimeGEQ(aggrMonitor, tft);
    checkTotalSessions(aggrMonitor, 1);
    checkStatsForRule(aggrMonitor, "ND", 1, 1, 0);
    checkStatsForRule(aggrMonitor, "ND2", 1, 0, 1);
    checkStatsByRule(aggrMonitor, "ND", 1, 1, 0);
    checkStatsByRule(aggrMonitor, "ND2", 1, 0, 1);
    LOG.debug("---");
    KieSession ksession2 = kc.newKieSession(KSESSION1, sessionConf);
    ksession2.insert("Ciao");
    ksession2.fireAllRules();
    print(aggrMonitor);
    checkAgendaTotals(aggrMonitor, 4, 2, 2);
    checkTotalFactCount(aggrMonitor, 2);
    tft = checkTotalFiringTimeGEQ(aggrMonitor, tft + 1);
    checkTotalSessions(aggrMonitor, 2);
    checkStatsForRule(aggrMonitor, "ND", 2, 2, 0);
    checkStatsForRule(aggrMonitor, "ND2", 2, 0, 2);
    checkStatsByRule(aggrMonitor, "ND", 2, 2, 0);
    checkStatsByRule(aggrMonitor, "ND2", 2, 0, 2);
    ksession.dispose();
    checkTotalSessions(aggrMonitor, 1);
    checkTotalFactCount(aggrMonitor, 1);
    ksession2.dispose();
    checkTotalSessions(aggrMonitor, 0);
    checkTotalFactCount(aggrMonitor, 0);
    LOG.debug("--- NOW for the STATELESS ---");
    tft = 0;
    StatelessKieSession stateless = kc.newStatelessKieSession(KSESSION2, sessionConf);
    StatelessKieSessionMonitoringMXBean aggrMonitor2 = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(containerId, KBASE2, KieSessionType.STATELESS, KSESSION2), StatelessKieSessionMonitoringMXBean.class);
    print(aggrMonitor2);
    checkAgendaTotals(aggrMonitor2, 0, 0, 0);
    checkRuleRuntimeTotals(aggrMonitor2, 0, 0);
    tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft);
    checkTotalSessions(aggrMonitor2, 0);
    assertNull(aggrMonitor2.getStatsForRule("ND"));
    assertNull(aggrMonitor2.getStatsForRule("ND2"));
    assertFalse(aggrMonitor2.getStatsByRule().containsKey("ND"));
    assertFalse(aggrMonitor2.getStatsByRule().containsKey("ND2"));
    stateless.execute("Ciao");
    print(aggrMonitor2);
    checkAgendaTotals(aggrMonitor2, 2, 1, 1);
    checkRuleRuntimeTotals(aggrMonitor2, 2, 1);
    tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
    checkTotalSessions(aggrMonitor2, 1);
    checkStatsForRule(aggrMonitor2, "ND", 1, 1, 0);
    checkStatsForRule(aggrMonitor2, "ND2", 1, 0, 1);
    checkStatsByRule(aggrMonitor2, "ND", 1, 1, 0);
    checkStatsByRule(aggrMonitor2, "ND2", 1, 0, 1);
    stateless.execute("Ciao");
    print(aggrMonitor2);
    checkAgendaTotals(aggrMonitor2, 4, 2, 2);
    checkRuleRuntimeTotals(aggrMonitor2, 4, 2);
    tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
    checkTotalSessions(aggrMonitor2, 2);
    checkStatsForRule(aggrMonitor2, "ND", 2, 2, 0);
    checkStatsForRule(aggrMonitor2, "ND2", 2, 0, 2);
    checkStatsByRule(aggrMonitor2, "ND", 2, 2, 0);
    checkStatsByRule(aggrMonitor2, "ND2", 2, 0, 2);
    StatelessKieSession stateless2 = kc.newStatelessKieSession(KSESSION2, sessionConf);
    LOG.debug(stateless + " " + stateless2);
    checkTotalSessions(aggrMonitor2, 2);
    stateless2.execute("Ciao");
    print(aggrMonitor2);
    checkAgendaTotals(aggrMonitor2, 6, 3, 3);
    checkRuleRuntimeTotals(aggrMonitor2, 6, 3);
    tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
    checkTotalSessions(aggrMonitor2, 3);
    checkStatsForRule(aggrMonitor2, "ND", 3, 3, 0);
    checkStatsForRule(aggrMonitor2, "ND2", 3, 0, 3);
    checkStatsByRule(aggrMonitor2, "ND", 3, 3, 0);
    checkStatsByRule(aggrMonitor2, "ND2", 3, 0, 3);
    KieContainer kc2 = ks.newKieContainer("kc2", releaseId1);
    assertEquals(5, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(containerId) + ",*"), null).size());
    kc.dispose();
    assertEquals(0, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(containerId) + ",*"), null).size());
    assertEquals(1, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote("kc2") + ",*"), null).size());
}
Also used : GenericKieSessionMonitoringMXBean(org.kie.api.management.GenericKieSessionMonitoringMXBean) KieSessionMonitoringMXBean(org.kie.api.management.KieSessionMonitoringMXBean) StatelessKieSessionMonitoringMXBean(org.kie.api.management.StatelessKieSessionMonitoringMXBean) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) ObjectName(javax.management.ObjectName) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) StatelessKieSessionMonitoringMXBean(org.kie.api.management.StatelessKieSessionMonitoringMXBean) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 87 with ReleaseId

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

the class MBeansMonitoringTest method testEventOffset.

@Test
public void testEventOffset() throws Exception {
    String drl = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "    @expires(10s)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(KBASE1).setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel(KSESSION1).setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie.test", "mbeans", "1.0.0");
    createKJar(ks, kproj, releaseId1, null, drl);
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieBase kiebase = kc.getKieBase(KBASE1);
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    ObjectName kbOn = DroolsManagementAgent.createObjectNameFor((InternalKnowledgeBase) kiebase);
    mbserver.invoke(kbOn, "startInternalMBeans", new Object[0], new String[0]);
    Object expOffset = mbserver.getAttribute(new ObjectName(kbOn + ",group=EntryPoints,EntryPoint=DEFAULT,ObjectType=org.drools.compiler.StockTick"), "ExpirationOffset");
    assertEquals(10001, ((Number) expOffset).longValue());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 88 with ReleaseId

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

the class DynamicRuleLoadTest method testKJarUpgrade.

@Test
public void testKJarUpgrade() throws Exception {
    // DROOLS-919
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2_1);
    // Create a session and fire rules
    kieContainer = ks.newKieContainer(km.getReleaseId());
    ksession = kieContainer.newKieSession();
    ksession.setGlobal("test", this);
    ksession.insert(new Message("Hi Universe"));
    ksession.fireAllRules();
    assertTrue(done);
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 89 with ReleaseId

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

the class DynamicRuleLoadTest method testKJarUpgradeWithJavaClass.

@Test
public void testKJarUpgradeWithJavaClass() throws Exception {
    KieServices ks = KieServices.Factory.get();
    String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade-java", "1.0.0");
    Resource javaResource = ResourceFactory.newByteArrayResource(javaSrc.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/test/PersonObject.java");
    Resource drlResource = ResourceFactory.newByteArrayResource(person_drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/person.drl");
    KieModule km = createAndDeployJar(ks, kmodule, releaseId1, javaResource, drlResource);
    // Create a session and fire rules
    kieContainer = ks.newKieContainer(km.getReleaseId());
    ksession = kieContainer.newKieSession();
    Class<?> clazz = kieContainer.getClassLoader().loadClass("org.drools.compiler.test.PersonObject");
    Object person = clazz.newInstance();
    ksession.insert(person);
    ksession.fireAllRules();
    assertNotNull(person);
    Object personId = valueOf(person, "id");
    assertNotNull(personId);
    assertEquals("Person from version 1", personId);
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade-java", "1.1.0");
    Resource javaResource2 = ResourceFactory.newByteArrayResource(javaSrc_2.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/test/PersonObject.java");
    Resource drlResource2 = ResourceFactory.newByteArrayResource(person_drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/person.drl");
    createAndDeployJar(ks, kmodule, releaseId2, javaResource2, drlResource2);
    // update container
    kieContainer.updateToVersion(releaseId2);
    assertEquals(releaseId2, kieContainer.getReleaseId());
    // now let's run the rules
    ksession = kieContainer.newKieSession();
    person = kieContainer.getClassLoader().loadClass("org.drools.compiler.test.PersonObject").newInstance();
    ksession.insert(person);
    ksession.fireAllRules();
    assertNotNull(person);
    personId = valueOf(person, "id");
    assertNotNull(personId);
    assertEquals("Person from version 2", personId);
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 90 with ReleaseId

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

the class KieBuilderTest method testNoPomXml.

@Test
public void testNoPomXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    KieModuleModel kProj = createKieProject(namespace);
    ReleaseId releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generateKProjectXML(kfs, namespace, kProj);
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    createAndTestKieContainer(releaseId, createKieBuilder(kfs), namespace);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Aggregations

ReleaseId (org.kie.api.builder.ReleaseId)273 Test (org.junit.Test)219 KieServices (org.kie.api.KieServices)214 KieContainer (org.kie.api.runtime.KieContainer)176 KieSession (org.kie.api.runtime.KieSession)137 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)117 KieModule (org.kie.api.builder.KieModule)84 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)73 KieFileSystem (org.kie.api.builder.KieFileSystem)64 KieModuleModel (org.kie.api.builder.model.KieModuleModel)40 KieBuilder (org.kie.api.builder.KieBuilder)34 ArrayList (java.util.ArrayList)32 Message (org.drools.compiler.Message)32 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)27 Resource (org.kie.api.io.Resource)23 Results (org.kie.api.builder.Results)20 KieBaseModel (org.kie.api.builder.model.KieBaseModel)20 InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)18 KieScanner (org.kie.api.builder.KieScanner)18 IncrementalResults (org.kie.internal.builder.IncrementalResults)18