use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testEventOffsetExpirationOverflow.
@Test
public void testEventOffsetExpirationOverflow() {
// DROOLS-455
String drl = "\n" + "import java.util.*; " + "" + "declare LongLastingEvent \n" + " @role( event )" + " @timestamp( start ) " + " @duration( duration ) " + " start : long " + " duration : long " + "end \n" + "" + "rule Insert " + " when " + " then " + " insert( new LongLastingEvent( 100, Long.MAX_VALUE ) ); " + " end " + " " + " " + "rule Collect \n" + "when\n" + " accumulate( $x: LongLastingEvent() over window:time(1h), $num : count($x) ) \n" + "then " + "end " + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
baseConfig.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
// init stateful knowledge session
KieSession ksession = kbase.newKieSession(sessionConfig, null);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
// generate the event
ksession.fireAllRules();
// move on..
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// The event should still be there...
assertEquals(1, ksession.getObjects().size());
ksession.dispose();
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testAnnotatedEventAssertion.
@Test(timeout = 10000)
public void testAnnotatedEventAssertion() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleAnnotatedEventAssertion.drl");
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieSession session = createKnowledgeSession(kbase, conf);
SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTickEvent(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTickEvent(2, "ACME", 10, 10010);
StockTickInterface tick3 = new StockTickEvent(3, "ACME", 10, 10100);
StockTickInterface tick4 = new StockTickEvent(4, "DROO", 50, 11000);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
clock.advanceTime(30, TimeUnit.SECONDS);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
clock.advanceTime(20, TimeUnit.SECONDS);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(2, ((List) session.getGlobal("results")).size());
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class TimerAndCalendarTest method testTimerRuleAfterCronReloadSession.
@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterCronReloadSession() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = createSession(kbase);
// must advance time or it won't save.
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
// if we do not call 'ksession.fireAllRules()', this test will run successfully.
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
// build timer rule, if the rule is fired, the list size will increase every 300ms
String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + " timer (cron: * * * * * ?) \n" + "when \n" + "then \n" + " list.add(list.size()); \n" + " end";
Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
kbase.addPackages(kpackages);
List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
ksession.setGlobal("list", list);
ksession.setGlobal("list", list);
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
ksession.setGlobal("list", list);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
Assert.assertEquals(1, list.size());
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(3, TimeUnit.SECONDS);
Assert.assertEquals(4, list.size());
ksession = disposeAndReloadSession(ksession, kbase);
ksession.setGlobal("list", list);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(2, TimeUnit.SECONDS);
// if the rule is fired, the list size will greater than one.
Assert.assertEquals(6, list.size());
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepTest method testNegatedAfter.
@Test
public void testNegatedAfter() throws Exception {
String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R when\n" + " $a : StockTick( company == \"DROO\" )\n" + " $b : StockTick( company == \"ACME\", this not after[5s,8s] $a )\n" + "then\n" + " System.out.println(\"fired\");\n" + "end\n";
KieSession ksession = getKieSession(getCepKieModuleModel(), str);
SessionPseudoClock clock = ksession.getSessionClock();
ksession.insert(new StockTick("DROO"));
clock.advanceTime(6, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(0, ksession.fireAllRules());
clock.advanceTime(4, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(1, ksession.fireAllRules());
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepTest method testAfter.
@Test
public void testAfter() throws Exception {
String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R when\n" + " $a : StockTick( company == \"DROO\" )\n" + " $b : StockTick( company == \"ACME\", this after[5s,8s] $a )\n" + "then\n" + " System.out.println(\"fired\");\n" + "end\n";
KieSession ksession = getKieSession(getCepKieModuleModel(), str);
SessionPseudoClock clock = ksession.getSessionClock();
ksession.insert(new StockTick("DROO"));
clock.advanceTime(6, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(1, ksession.fireAllRules());
clock.advanceTime(4, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(0, ksession.fireAllRules());
}
Aggregations