use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testTimeRelationalOperators.
@Test(timeout = 10000)
public void testTimeRelationalOperators() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_TimeRelationalOperators.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession wm = createKnowledgeSession(kbase, sconf);
final PseudoClockScheduler clock = (PseudoClockScheduler) wm.getSessionClock();
clock.setStartupTime(1000);
final List results_coincides = new ArrayList();
final List results_before = new ArrayList();
final List results_after = new ArrayList();
final List results_meets = new ArrayList();
final List results_met_by = new ArrayList();
final List results_overlaps = new ArrayList();
final List results_overlapped_by = new ArrayList();
final List results_during = new ArrayList();
final List results_includes = new ArrayList();
final List results_starts = new ArrayList();
final List results_started_by = new ArrayList();
final List results_finishes = new ArrayList();
final List results_finished_by = new ArrayList();
wm.setGlobal("results_coincides", results_coincides);
wm.setGlobal("results_before", results_before);
wm.setGlobal("results_after", results_after);
wm.setGlobal("results_meets", results_meets);
wm.setGlobal("results_met_by", results_met_by);
wm.setGlobal("results_overlaps", results_overlaps);
wm.setGlobal("results_overlapped_by", results_overlapped_by);
wm.setGlobal("results_during", results_during);
wm.setGlobal("results_includes", results_includes);
wm.setGlobal("results_starts", results_starts);
wm.setGlobal("results_started_by", results_started_by);
wm.setGlobal("results_finishes", results_finishes);
wm.setGlobal("results_finished_by", results_finished_by);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
StockTickInterface tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
StockTickInterface tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
InternalFactHandle handle3 = (InternalFactHandle) wm.insert(tick3);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
InternalFactHandle handle4 = (InternalFactHandle) wm.insert(tick4);
InternalFactHandle handle5 = (InternalFactHandle) wm.insert(tick5);
clock.advanceTime(1, TimeUnit.MILLISECONDS);
InternalFactHandle handle6 = (InternalFactHandle) wm.insert(tick6);
InternalFactHandle handle7 = (InternalFactHandle) wm.insert(tick7);
clock.advanceTime(2, TimeUnit.MILLISECONDS);
InternalFactHandle handle8 = (InternalFactHandle) wm.insert(tick8);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertNotNull(handle5);
assertNotNull(handle6);
assertNotNull(handle7);
assertNotNull(handle8);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
assertTrue(handle6.isEvent());
assertTrue(handle7.isEvent());
assertTrue(handle8.isEvent());
// wm = SerializationHelper.serializeObject(wm);
wm.fireAllRules();
assertEquals(1, results_coincides.size());
assertEquals(tick5, results_coincides.get(0));
assertEquals(1, results_before.size());
assertEquals(tick2, results_before.get(0));
assertEquals(1, results_after.size());
assertEquals(tick3, results_after.get(0));
assertEquals(1, results_meets.size());
assertEquals(tick3, results_meets.get(0));
assertEquals(1, results_met_by.size());
assertEquals(tick2, results_met_by.get(0));
assertEquals(1, results_met_by.size());
assertEquals(tick2, results_met_by.get(0));
assertEquals(1, results_overlaps.size());
assertEquals(tick4, results_overlaps.get(0));
assertEquals(1, results_overlapped_by.size());
assertEquals(tick8, results_overlapped_by.get(0));
assertEquals(1, results_during.size());
assertEquals(tick6, results_during.get(0));
assertEquals(1, results_includes.size());
assertEquals(tick4, results_includes.get(0));
assertEquals(1, results_starts.size());
assertEquals(tick6, results_starts.get(0));
assertEquals(1, results_started_by.size());
assertEquals(tick7, results_started_by.get(0));
assertEquals(1, results_finishes.size());
assertEquals(tick8, results_finishes.get(0));
assertEquals(1, results_finished_by.size());
assertEquals(tick7, results_finished_by.get(0));
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testCEPNamedConsTimers.
@Test
public void testCEPNamedConsTimers() throws InterruptedException {
String drl = "package org.drools " + "global java.util.List list; " + "declare Msg " + " @role( event ) " + " sender : String @key " + "end " + "rule Init " + "when " + " $s : String() " + "then " + " System.out.println( 'Msg ' + $s ); " + " insert( new Msg( $s ) ); " + "end " + "rule 'Viol' " + "when " + " $trigger : Msg( 'John' ; ) " + " not Msg( 'Peter' ; this after[0, 100ms] $trigger ) do[viol]" + "then " + " list.add( 0 ); " + "then[viol] " + " list.add( -2 ); " + "end " + "";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieSession ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("John");
ksession.fireAllRules();
assertTrue(list.isEmpty());
((PseudoClockScheduler) ksession.getSessionClock()).advanceTime(1000, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertTrue(list.contains(-2));
assertTrue(list.contains(0));
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testDeleteExpiredEventWithTimestampAndEqualityKey.
@Test
public void testDeleteExpiredEventWithTimestampAndEqualityKey() throws Exception {
// DROOLS-1017
String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + " @role( event )\n" + " @timestamp( time )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\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, EqualityBehaviorOption.EQUALITY);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
clock.setStartupTime(5000L);
EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50, 0L));
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 testLeftTupleExpirationWithNot.
@Test
public void testLeftTupleExpirationWithNot() {
// RHBRMS-2463
String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "import " + AtomicInteger.class.getCanonicalName() + "\n" + "global AtomicInteger counter;\n" + "declare MyEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 10ms )\n" + "end\n" + "\n" + "rule R when\n" + " MyEvent()\n" + " Boolean()\n" + " not Integer()\n" + " then\n" + " 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();
sessionClock.setStartupTime(0);
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
ksession.insert(true);
FactHandle iFh = ksession.insert(1);
ksession.insert(new MyEvent(0));
ksession.insert(new MyEvent(15));
ksession.fireAllRules();
assertEquals(0, counter.get());
sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
ksession.delete(iFh);
// MyEvent is expired
ksession.fireAllRules();
assertEquals(1, counter.get());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testTimerWithMillisPrecision.
@Test
public void testTimerWithMillisPrecision() {
// RHBRMS-2627
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);
// System.currentTimeMillis();
long now = 1000;
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(now - 10);
AtomicInteger counter = new AtomicInteger(1);
MyEvent event1 = new MyEvent(now - 8);
MyEvent event2 = new MyEvent(now - 7);
MyEvent event3 = new MyEvent(now - 6);
ksession.insert(counter);
ksession.insert(event1);
ksession.insert(event2);
ksession.insert(event3);
// Nothing Happens
ksession.fireAllRules();
assertEquals(1, counter.get());
sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0, counter.get());
}
Aggregations