use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testDelayingNot.
@Test(timeout = 10000)
public void testDelayingNot() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_DelayingNot.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession wm = createKnowledgeSession(kbase, sconf);
final RuleImpl rule = (RuleImpl) kbase.getRule("org.drools.compiler", "Delaying Not");
assertEquals(10000, ((DurationTimer) rule.getTimer()).getDuration());
final List results = new ArrayList();
wm.setGlobal("results", results);
SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
clock.advanceTime(10, TimeUnit.SECONDS);
StockTickInterface st1O = new StockTick(1, "DROO", 100, clock.getCurrentTime());
EventFactHandle st1 = (EventFactHandle) wm.insert(st1O);
wm.fireAllRules();
// should not fire, because it must wait 10 seconds
assertEquals(0, results.size());
clock.advanceTime(5, TimeUnit.SECONDS);
EventFactHandle st2 = (EventFactHandle) wm.insert(new StockTick(1, "DROO", 80, clock.getCurrentTime()));
wm.fireAllRules();
// should still not fire, because it must wait 5 more seconds, and st2 has lower price (80)
assertEquals(0, results.size());
// assert new data
wm.fireAllRules();
clock.advanceTime(6, TimeUnit.SECONDS);
wm.fireAllRules();
// should fire, because waited for 10 seconds and no other event arrived with a price increase
assertEquals(1, results.size());
assertEquals(st1O, results.get(0));
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testTimeWindowWithPastEvents.
@Test(timeout = 10000)
public void testTimeWindowWithPastEvents() throws Exception {
// JBRULES-2258
String drl = "package org.drools.compiler;\n" + "\n" + "import java.util.List\n" + "\n" + "global List timeResults;\n" + "\n" + "declare StockTick\n" + " @role( event )\n" + " @timestamp( time ) \n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " accumulate(\n" + " $o : StockTick() over window:time(10ms)," + " $tot : count( $o );" + " $tot > 0 )\n" + "then\n" + " System.out.println( $tot ); \n" + " timeResults.add( $tot );\n" + "end\n";
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(kbconf, drl);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, ksconf);
List<Number> timeResults = new ArrayList<Number>();
ksession.setGlobal("timeResults", timeResults);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
int count = 0;
StockTick tick1 = new StockTick(count++, "X", 0.0, 1);
StockTick tick2 = new StockTick(count++, "X", 0.0, 3);
StockTick tick3 = new StockTick(count++, "X", 0.0, 7);
StockTick tick4 = new StockTick(count++, "X", 0.0, 9);
StockTick tick5 = new StockTick(count++, "X", 0.0, 15);
clock.advanceTime(30, TimeUnit.MILLISECONDS);
ksession.insert(tick1);
ksession.insert(tick2);
ksession.insert(tick3);
ksession.insert(tick4);
ksession.insert(tick5);
ksession.fireAllRules();
System.out.println(timeResults);
assertTrue(timeResults.isEmpty());
clock.advanceTime(0, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertTrue(timeResults.isEmpty());
clock.advanceTime(3, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertTrue(timeResults.isEmpty());
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertTrue(timeResults.isEmpty());
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testTemporalOperators2.
@Test(timeout = 10000)
public void testTemporalOperators2() throws Exception {
// read in the source
final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators2.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, sconf);
List list = new ArrayList();
ksession.setGlobal("list", list);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
EntryPoint ep = ksession.getEntryPoint("X");
clock.advanceTime(1000, TimeUnit.SECONDS);
ep.insert(new StockTick(1, "A", 10, clock.getCurrentTime()));
clock.advanceTime(8, TimeUnit.SECONDS);
ep.insert(new StockTick(2, "B", 10, clock.getCurrentTime()));
clock.advanceTime(8, TimeUnit.SECONDS);
ep.insert(new StockTick(3, "B", 10, clock.getCurrentTime()));
clock.advanceTime(8, TimeUnit.SECONDS);
int rules = ksession.fireAllRules();
// assertEquals( 2,
// rules );
// assertEquals( 1, list.size() );
// StockTick[] stocks = ( StockTick[] ) list.get(0);
// assertSame( tick4, stocks[0]);
// assertSame( tick2, stocks[1]);
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testDuplicateFiring1.
@Test
public void testDuplicateFiring1() throws InterruptedException {
String drl = "package org.test;\n" + "import org.drools.compiler.StockTick;\n " + "" + "global java.util.List list \n" + "" + "declare StockTick @role(event) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + " accumulate ( $e: StockTick() over window:time(300ms) from entry-point SensorEventStream, " + " $n : count( $e );" + " $n > 0 )\n" + "then\n" + " list.add( $n ); \n" + " System.out.println( \"Events in last 3 seconds: \" + $n );\n" + "end" + "" + "\n" + "rule \"timerRuleAfterAllEvents\"\n" + " timer ( int: 2s )\n" + "when\n" + " $room : String( )\n" + "then\n" + " list.add( -1 ); \n" + " System.out.println(\"2sec after room was modified\");\n" + "end " + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
// Check the builder for errors
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
// configure knowledge base
KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
baseConfig.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
kbase.addPackages(kbuilder.getKnowledgePackages());
// init session clock
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
// init stateful knowledge session
KieSession ksession = kbase.newKieSession(sessionConfig, null);
SessionPseudoClock clock = ksession.getSessionClock();
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
// entry point for sensor events
EntryPoint sensorEventStream = ksession.getEntryPoint("SensorEventStream");
ksession.insert("Go");
System.out.println("1. fireAllRules()");
// insert events
for (int i = 2; i < 8; i++) {
StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
sensorEventStream.insert(event);
System.out.println(i + ". fireAllRules()");
ksession.fireAllRules();
clock.advanceTime(105, TimeUnit.MILLISECONDS);
}
// let thread sleep for another 1m to see if dereffered rules fire (timers, (not) after rules)
clock.advanceTime(100 * 40 * 1, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(Arrays.asList(1L, 2L, 3L, 3L, 3L, 3L, -1), list);
ksession.dispose();
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testEventTimestamp2.
@Test
public void testEventTimestamp2() {
// DROOLS-268
String drl = "\n" + "import org.drools.compiler.integrationtests.CepEspTest.Event; \n" + "global java.util.List list; \n" + "global org.kie.api.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" + " $event0 : Event( type == 0 )\n" + " $event1 : Event( type == 1 )\n" + " $event2: Event( type == 2 )\n" + " not Event( type == 3, this after [0, 1000ms] $event1 ) \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);
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);
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
ksession.setGlobal("clock", clock);
ksession.insert(new Event(0, 0, clock.getCurrentTime()));
clock.advanceTime(100, TimeUnit.MILLISECONDS);
ksession.insert(new Event(1, 0, clock.getCurrentTime()));
clock.advanceTime(600, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
ksession.insert(new Event(2, 0, clock.getCurrentTime()));
clock.advanceTime(600, TimeUnit.MILLISECONDS);
ksession.insert(new Event(3, 0, clock.getCurrentTime()));
ksession.fireAllRules();
assertFalse(list.isEmpty());
assertEquals(1, list.size());
long time = (Long) list.get(0);
assertEquals(1300, time);
ksession.dispose();
}
Aggregations