@Test(timeout = 10000) public void testExprTimeRescheduled() throws Exception { String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "rule r1 timer (expr: f1.field2, f1.field4)\n" + "when\n" + " f1 : FactA() \n" + "then\n" + " list.add( f1 );\n" + "end\n" + "\n"; KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption(ClockTypeOption.get("pseudo")); KnowledgeBase kbase = loadKnowledgeBaseFromString(text); KieSession ksession = createKnowledgeSession(kbase, conf); PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock(); timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS); List list = new ArrayList(); ksession.setGlobal("list", list); FactA fact1 = new FactA(); fact1.setField1("f1"); fact1.setField2(500); fact1.setField4(1000); FactHandle fh = (FactHandle) ksession.insert(fact1); ksession.fireAllRules(); assertEquals(0, list.size()); timeService.advanceTime(1100, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals(fact1, list.get(0)); timeService.advanceTime(1100, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(2, list.size()); assertEquals(fact1, list.get(1)); timeService.advanceTime(400, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(3, list.size()); assertEquals(fact1, list.get(2)); list.clear(); fact1.setField2(300); fact1.setField4(2000); ksession.update(fh, fact1); // 100 has passed of the 1000, from the previous schedule // so that should be deducted from the 300 delay above, meaning // we only need to increment another 250 timeService.advanceTime(250, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals(fact1, list.get(0)); list.clear(); timeService.advanceTime(1000, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(0, list.size()); timeService.advanceTime(700, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(0, list.size()); timeService.advanceTime(300, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(1, list.size()); }
@Test(timeout = 10000) public void testIntervalTimerExpressionWithOr() throws Exception { String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + Foo.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "rule r1 timer (expr: f1.field2, f1.field2; repeat-limit=3)\n" + "when\n" + " foo: Foo()\n" + " ( Pet() and f1 : FactA( field1 == 'f1') ) or \n" + " f1 : FactA(field1 == 'f2') \n" + "then\n" + " list.add( f1 );\n" + " foo.setId( 'xxx' );\n" + "end\n" + "\n"; KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption(ClockTypeOption.get("pseudo")); KnowledgeBase kbase = loadKnowledgeBaseFromString(text); KieSession ksession = createKnowledgeSession(kbase, conf); PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock(); timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS); List list = new ArrayList(); ksession.setGlobal("list", list); ksession.insert(new Foo(null, null)); ksession.insert(new Pet(null)); FactA fact1 = new FactA(); fact1.setField1("f1"); fact1.setField2(250); FactA fact3 = new FactA(); fact3.setField1("f2"); fact3.setField2(1000); ksession.insert(fact1); ksession.insert(fact3); ksession.fireAllRules(); assertEquals(0, list.size()); timeService.advanceTime(300, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals(fact1, list.get(0)); timeService.advanceTime(300, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(2, list.size()); assertEquals(fact1, list.get(1)); timeService.advanceTime(300, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(2, list.size()); // did not change, repeat-limit kicked in timeService.advanceTime(300, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(3, list.size()); assertEquals(fact3, list.get(2)); timeService.advanceTime(1000, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(4, list.size()); assertEquals(fact3, list.get(3)); timeService.advanceTime(1000, TimeUnit.MILLISECONDS); ksession.fireAllRules(); assertEquals(4, list.size()); // did not change, repeat-limit kicked in }