Search in sources :

Example 21 with KieModuleModel

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

the class KieHelloWorldTest method testImport.

@Test
public void testImport() throws Exception {
    // DROOLS-859
    String drl1 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R1 when\n" + "    $m : Message( message == \"Hello World\" )\n" + "then\n" + "    list.add(\"ok\");\n" + "end\n";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R2 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "   list.add(\"ko\");\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "test-import", "1.0-SNAPSHOT");
    KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("kbase").addPackage("rules").newKieSessionModel("ksession").setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/rules/r1.drl", drl1).write("src/main/resources/myrules/r2.drl", drl2).writeKModuleXML(kproj.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(releaseId).newKieSession();
    List<String> results = new ArrayList<String>();
    ksession.setGlobal("list", results);
    ksession.insert(new Message("Hello World"));
    ksession.insert(new Message("Hi Universe"));
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals("ok", results.get(0));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 22 with KieModuleModel

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

the class KieHelloWorldTest method createKieProjectWithPackages.

private KieModuleModel createKieProjectWithPackages(KieServices ks, String pkg) {
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel().setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).addPackage(pkg);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("realtime")).setDefault(true);
    return kproj;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 23 with KieModuleModel

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

the class LifecycleTest method initSession.

@Before
public void initSession() {
    String drlString = "package org.jboss.brms\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role( event )\n" + "    @expires( 4s )\n" + "end\n" + "rule \"TestEventReceived\"\n" + "    when\n" + "        $event : StockTick() over window:time(4s) from entry-point EventStream\n" + "    then\n" + "        // do something;\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    final Resource drl = ks.getResources().newByteArrayResource(drlString.getBytes()).setTargetPath("org/jboss/brms/lifecycle.drl");
    kfs.write(drl);
    KieModuleModel kmoduleModel = ks.newKieModuleModel();
    kmoduleModel.newKieBaseModel("defaultKBase").addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("defaultKieSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kmoduleModel.toXML());
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size());
    this.kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Before(org.junit.Before)

Example 24 with KieModuleModel

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

the class ListenersTest method prepareKieModule.

/**
 * Inserts a new KieModule containing single KieBase and a stateful and stateless KieSessions with listeners
 * into KieRepository.
 *
 * @return created KIE module ReleaseId
 */
private ReleaseId prepareKieModule() {
    final KieServices ks = KieServices.Factory.get();
    KieModuleModel module = ks.newKieModuleModel();
    KieBaseModel baseModel = module.newKieBaseModel("defaultKBase");
    baseModel.setDefault(true);
    baseModel.addPackage("*");
    KieSessionModel sessionModel = baseModel.newKieSessionModel("defaultKSession");
    sessionModel.setDefault(true);
    sessionModel.setType(KieSessionModel.KieSessionType.STATEFUL);
    sessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER);
    sessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER);
    KieSessionModel statelessSessionModel = baseModel.newKieSessionModel("defaultStatelessKSession");
    statelessSessionModel.setDefault(true);
    statelessSessionModel.setType(KieSessionModel.KieSessionType.STATELESS);
    statelessSessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER);
    statelessSessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER);
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(module.toXML());
    kfs.generateAndWritePomXML(RELEASE_ID);
    kfs.write("src/main/resources/" + PACKAGE_PATH + "/test.drl", ResourceFactory.newByteArrayResource(DRL.getBytes()));
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals("Unexpected compilation errors", 0, builder.getResults().getMessages().size());
    ks.getRepository().addKieModule(builder.getKieModule());
    return RELEASE_ID;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 25 with KieModuleModel

use of org.kie.api.builder.model.KieModuleModel 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)

Aggregations

KieModuleModel (org.kie.api.builder.model.KieModuleModel)126 KieFileSystem (org.kie.api.builder.KieFileSystem)78 KieServices (org.kie.api.KieServices)76 Test (org.junit.Test)67 KieBuilder (org.kie.api.builder.KieBuilder)56 KieBaseModel (org.kie.api.builder.model.KieBaseModel)55 KieContainer (org.kie.api.runtime.KieContainer)50 ReleaseId (org.kie.api.builder.ReleaseId)38 KieSession (org.kie.api.runtime.KieSession)27 KieSessionModel (org.kie.api.builder.model.KieSessionModel)25 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)18 InternalKieContainer (org.drools.core.impl.InternalKieContainer)14 KieModule (org.kie.api.builder.KieModule)14 KieBase (org.kie.api.KieBase)11 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)10 Results (org.kie.api.builder.Results)10 Resource (org.kie.api.io.Resource)9 ArrayList (java.util.ArrayList)8 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)8 Before (org.junit.Before)7