use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class FactHandleMarshallingTest method createWorkingMemory.
private StatefulKnowledgeSessionImpl createWorkingMemory(InternalKnowledgeBase kBase) {
// WorkingMemoryEntryPoint
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get("pseudo"));
SessionConfiguration sessionConf = ((SessionConfiguration) ksconf);
StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase, true, sessionConf, EnvironmentFactory.newEnvironment());
return wm;
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class MarshallingTest method testMarshallWithTimedRule.
@Test
public void testMarshallWithTimedRule() {
// DROOLS-795
String drl = "rule \"Rule A Timeout\"\n" + "when\n" + " String( this == \"ATrigger\" )\n" + "then\n" + " insert (new String( \"A-Timer\") );\n" + "end\n" + "\n" + "rule \"Timer For rule A Timeout\"\n" + " timer ( int: 5s )\n" + "when\n" + " String( this == \"A-Timer\")\n" + "then\n" + " delete ( \"A-Timer\" );\n" + " delete ( \"ATrigger\" );\n" + "end\n";
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(EqualityBehaviorOption.EQUALITY, DeclarativeAgendaOption.ENABLED, EventProcessingOption.STREAM);
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(sessionConfig, null);
ksession.insert("ATrigger");
assertEquals(1, ksession.getFactCount());
ksession.fireAllRules();
assertEquals(2, ksession.getFactCount());
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(4, TimeUnit.SECONDS);
assertEquals(2, ksession.getFactCount());
ksession.fireAllRules();
assertEquals(2, ksession.getFactCount());
ksession = marshallAndUnmarshall(kbase, ksession, sessionConfig);
clock = ksession.getSessionClock();
clock.advanceTime(4, TimeUnit.SECONDS);
assertEquals(2, ksession.getFactCount());
ksession.fireAllRules();
assertEquals(0, ksession.getFactCount());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class TimerAndCalendarTest method testIntervalTimer.
@Test(timeout = 10000)
public void testIntervalTimer() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " timer (int:30s 10s) ";
str += "when \n";
str += "then \n";
str += " list.add(\"fired\"); \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(15, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class TimerAndCalendarTest method testCronTimer.
@Test(timeout = 10000)
public void testCronTimer() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " timer (cron:15 * * * * ?) ";
str += "when \n";
str += "then \n";
str += " list.add(\"fired\"); \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = df.parse("2009-01-01T00:00:00.000-0000");
timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(30, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(30, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class TimerAndCalendarTest method testIntervalTimerWithoutFire.
@Test(timeout = 10000)
public void testIntervalTimerWithoutFire() throws Exception {
String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + " timer (int:30s 10s) " + "when \n" + "then \n" + " list.add(\"fired\"); \n" + "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
conf.setOption(TimedRuleExecutionOption.YES);
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(35, TimeUnit.SECONDS);
assertEquals(1, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
assertEquals(2, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
assertEquals(3, list.size());
}
Aggregations