use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ExpirationTest method testSoftExpiration.
@Test
public void testSoftExpiration() {
// DROOLS-1483
String drl = "import " + ExpiringEventA.class.getCanonicalName() + "\n" + "import " + ExpiringEventB.class.getCanonicalName() + "\n" + "import " + ExpiringEventC.class.getCanonicalName() + "\n" + "rule Ra when\n" + " $e : ExpiringEventA() over window:time(20s)\n" + "then end\n " + "rule Rb when\n" + " $e : ExpiringEventB() over window:time(20s)\n" + "then end\n " + "rule Rc when\n" + " $e : ExpiringEventC()\n" + "then end\n";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
ksession.insert(new ExpiringEventA());
ksession.insert(new ExpiringEventB());
ksession.insert(new ExpiringEventC());
ksession.fireAllRules();
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=15 -> hard expiration of A
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=25 -> implicit expiration of B
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=35 -> soft expiration of C
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ExpirationTest method testAlpha.
@Test
public void testAlpha() {
String drl = "import " + A.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $a: A( $Aid: id > 0 )\n" + "then\n" + " System.out.println(\"[\" + $a + \"]\");" + " counter.incrementAndGet();\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler sessionClock = ksession.getSessionClock();
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
ksession.insert(new A(1));
sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.insert(new A(2));
sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(2, counter.get());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ExpirationTest method testBeta.
@Test
public void testBeta() {
// DROOLS-1329
String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $a: A( $Aid: id > 0 )\n" + " $b: B( ($Bid: id <= $Aid) && (id > ($Aid - 1 )))\n" + "then\n" + " System.out.println(\"[\" + $a + \",\" + $b + \"]\");" + " counter.incrementAndGet();\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler sessionClock = ksession.getSessionClock();
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
ksession.insert(new A(1));
ksession.insert(new B(1));
sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.insert(new A(2));
ksession.insert(new B(2));
sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(2, counter.get());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testDeleteExpiredEvent.
@Test
public void testDeleteExpiredEvent() throws Exception {
// BZ-1274696
String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "no-loop\n" + "when\n" + " $st1 : StockTick( company == \"ACME\" )\n" + " not ( StockTick( this != $st1, this after[0s, 1s] $st1) )\n" + "then\n" + " delete($st1);\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50));
ksession.fireAllRules();
clock.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testCancelActivationWithExpiredEvent.
@Test
public void testCancelActivationWithExpiredEvent() {
// RHBRMS-2463
String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "import " + AtomicInteger.class.getCanonicalName() + "\n" + "declare MyEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 10ms )\n" + "end\n" + "\n" + "rule R\n" + " timer (int: 0 1; start=$startTime, repeat-limit=0 )\n" + " when\n" + " $event: MyEvent ($startTime : timestamp)\n" + " $counter : AtomicInteger(get() > 0)\n" + " then\n" + " System.out.println(\"RG_TEST_TIMER WITH \" + $event + \" AND \" + $counter);\n" + " modify($counter){\n" + " decrementAndGet()\n" + " }\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
long now = System.currentTimeMillis();
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(now - 10000);
AtomicInteger counter = new AtomicInteger(1);
MyEvent event1 = new MyEvent(now - 8000);
MyEvent event2 = new MyEvent(now - 7000);
MyEvent event3 = new MyEvent(now - 6000);
ksession.insert(counter);
ksession.insert(event1);
ksession.insert(event2);
ksession.insert(event3);
// Nothing Happens
ksession.fireAllRules();
assertEquals(1, counter.get());
sessionClock.advanceTime(10000, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0, counter.get());
}
Aggregations