use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class FireAllRulesCommandTest method oneRuleFiredTest.
@Test
public void oneRuleFiredTest() {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " System.out.println($c); \n";
str += "end \n";
StatelessKieSession ksession = getSession(str);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert(new Cheese("stilton")));
commands.add(CommandFactory.newFireAllRules("num-rules-fired"));
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
assertEquals(1, fired);
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class FireAllRulesCommandTest method infiniteLoopTerminatesAtMaxTest.
@Test
public void infiniteLoopTerminatesAtMaxTest() {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " update($c); \n";
str += "end \n";
StatelessKieSession ksession = getSession(str);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert(new Cheese("stilton")));
FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
farc.setOutIdentifier("num-rules-fired");
commands.add(farc);
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
assertEquals(10, fired);
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class FireAllRulesCommandTest method oneRuleFiredWithDefinedMaxTest.
@Test
public void oneRuleFiredWithDefinedMaxTest() {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " System.out.println($c); \n";
str += "end \n";
StatelessKieSession ksession = getSession(str);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert(new Cheese("stilton")));
FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
farc.setOutIdentifier("num-rules-fired");
commands.add(farc);
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
assertEquals(1, fired);
}
use of org.kie.api.runtime.StatelessKieSession 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());
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class RegisterWorkItemHandlerTest method testRegisterWorkItemHandlerWithStatelessSession.
@Test
public void testRegisterWorkItemHandlerWithStatelessSession() {
String str = "package org.kie.workitem.test \n" + "import " + DefaultWorkItemManager.class.getCanonicalName() + "\n" + "import " + WorkItem.class.getCanonicalName() + "\n" + "import " + WorkItemImpl.class.getCanonicalName() + "\n" + "rule r1 when \n" + "then \n" + " WorkItem wi = new WorkItemImpl(); \n" + " wi.setName( \"wihandler\" ); \n" + " DefaultWorkItemManager wim = ( DefaultWorkItemManager ) kcontext.getKieRuntime().getWorkItemManager(); \n" + " wim.internalExecuteWorkItem(wi); \n" + "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final boolean[] answer = new boolean[] { false };
StatelessKieSession ks = kbase.newStatelessKieSession();
ks.execute(CommandFactory.newRegisterWorkItemHandlerCommand(new WorkItemHandler() {
public void executeWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
answer[0] = true;
}
public void abortWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
// TODO Auto-generated method stub
}
}, "wihandler"));
assertTrue(answer[0]);
}
Aggregations