@Test public void testEventTimestamp() { // DROOLS-268 String drl = "\n" + "import org.drools.reteoo.integrationtests.CepEspTest.Event; \n" + "global java.util.List list; \n" + "global org.drools.core.time.SessionPseudoClock clock; \n" + "" + "declare Event \n" + " @role( event )\n" + " @timestamp( time ) \n" + " @expires( 10000000 ) \n" + "end \n" + "" + "" + "rule \"inform about E1\"\n" + "when\n" + " $event1 : Event( type == 1 )\n" + " //there is an event (T2) with value 0 between 0,2m after doorClosed\n" + " $event2: Event( type == 2, value == 1, this after [0, 1200ms] $event1, $timestamp : time )\n" + " //there is no newer event (T2) within the timeframe\n" + " not Event( type == 2, this after [0, 1200ms] $event1, time > $timestamp ) \n" + "then\n" + " list.add( clock.getCurrentTime() ); \n " + "end\n" + "\n"; 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); baseConfig.setOption(RuleEngineOption.RETEOO); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId())); // init stateful knowledge session StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(sessionConfig, null); ArrayList list = new ArrayList(); ksession.setGlobal("list", list); SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock(); ksession.setGlobal("clock", clock); ksession.insert(new Event(1, -1, clock.getCurrentTime())); // 0 clock.advanceTime(600, TimeUnit.MILLISECONDS); ksession.fireAllRules(); ksession.insert(new Event(2, 0, clock.getCurrentTime())); // 600 clock.advanceTime(100, TimeUnit.MILLISECONDS); ksession.fireAllRules(); ksession.insert(new Event(2, 0, clock.getCurrentTime())); // 700 clock.advanceTime(300, TimeUnit.MILLISECONDS); ksession.fireAllRules(); ksession.insert(new Event(2, 0, clock.getCurrentTime())); // 1000 clock.advanceTime(100, TimeUnit.MILLISECONDS); ksession.fireAllRules(); ksession.insert(new Event(2, 1, clock.getCurrentTime())); // 1100 clock.advanceTime(100, TimeUnit.MILLISECONDS); ksession.fireAllRules(); clock.advanceTime(100, TimeUnit.MILLISECONDS); ksession.fireAllRules(); ksession.insert(new Event(2, 0, clock.getCurrentTime())); // 1300 clock.advanceTime(1000, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertFalse(list.isEmpty()); assertEquals(1, list.size()); Long time = (Long) list.get(0); assertTrue(time > 1000 && time < 1500); ksession.dispose(); }
@Test @Ignore("This test requires us to fix the propagation order") public void testForallWithSlidingWindow() throws Exception { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newInputStreamResource( getClass().getResourceAsStream("test_ForallSlidingWindow.drl")), ResourceType.DRL); assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors()); final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId())); final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(conf, null); final SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock(); List<String> results = new ArrayList<String>(); ksession.setGlobal("results", results); // advance time... no events, so forall should fire clock.advanceTime(60, TimeUnit.SECONDS); ksession.fireAllRules(); assertEquals(1, results.size()); int seq = 1; // advance time... there are matching events now, but forall still not fire ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 60 clock.advanceTime(5, TimeUnit.SECONDS); ksession.fireAllRules(); assertEquals(1, results.size()); ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 65 clock.advanceTime(5, TimeUnit.SECONDS); ksession.fireAllRules(); assertEquals(1, results.size()); // advance time... there are non-matching events now, so forall de-activates ksession.insert(new StockTick(seq++, "IBM", 10, clock.getCurrentTime())); // 70 clock.advanceTime(10, TimeUnit.SECONDS); ksession.fireAllRules(); assertEquals(1, results.size()); // advance time... there are non-matching events now, so forall is still deactivated ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 80 clock.advanceTime(10, TimeUnit.SECONDS); ksession.fireAllRules(); assertEquals(1, results.size()); // advance time... non-matching event expires now, so forall should fire ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 90 clock.advanceTime(10, TimeUnit.SECONDS); ksession.fireAllRules(); assertEquals(2, results.size()); // advance time... forall still matches and should not fire ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 100 clock.advanceTime(10, TimeUnit.SECONDS); ksession.fireAllRules(); assertEquals(2, results.size()); // advance time... forall still matches and should not fire clock.advanceTime(60, TimeUnit.SECONDS); ksession.fireAllRules(); assertEquals(2, results.size()); }