use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method AfterOperatorInCEPQueryTest.
@Test
public void AfterOperatorInCEPQueryTest() {
String drl = "package org.drools;\n" + "import org.drools.compiler.StockTick; \n" + "\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "query EventsBeforeNineSeconds\n" + " $event : StockTick() from entry-point EStream\n" + " $result : StockTick ( this after [0s, 9s] $event) from entry-point EventStream\n" + "end\n" + "\n" + "query EventsBeforeNineteenSeconds\n" + " $event : StockTick() from entry-point EStream\n" + " $result : StockTick ( this after [0s, 19s] $event) from entry-point EventStream\n" + "end\n" + "\n" + "query EventsBeforeHundredSeconds\n" + " $event : StockTick() from entry-point EStream\n" + " $result : StockTick ( this after [0s, 100s] $event) from entry-point EventStream\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);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
EntryPoint ePoint = ksession.getEntryPoint("EStream");
EntryPoint entryPoint = ksession.getEntryPoint("EventStream");
ePoint.insert(new StockTick(0L, "zero", 0.0, 0));
entryPoint.insert(new StockTick(1L, "one", 0.0, 0));
clock.advanceTime(10, TimeUnit.SECONDS);
entryPoint.insert(new StockTick(2L, "two", 0.0, 0));
clock.advanceTime(10, TimeUnit.SECONDS);
entryPoint.insert(new StockTick(3L, "three", 0.0, 0));
QueryResults results = ksession.getQueryResults("EventsBeforeNineSeconds");
assertEquals(0, results.size());
results = ksession.getQueryResults("EventsBeforeNineteenSeconds");
assertEquals(0, results.size());
results = ksession.getQueryResults("EventsBeforeHundredSeconds");
assertEquals(1, results.size());
ksession.dispose();
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testCoincidesOnArbitraryDates.
@Test(timeout = 10000)
public void testCoincidesOnArbitraryDates() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_CoincidesOperatorDates.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession wm = createKnowledgeSession(kbase, sconf);
final List<?> results = new ArrayList<Object>();
wm.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
100000, 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 50 milliseconds after DROO
100050, 3);
InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
assertNotNull(handle1);
assertNotNull(handle2);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
// wm = SerializationHelper.serializeObject(wm);
wm.fireAllRules();
assertEquals(4, results.size());
assertEquals(tick1, results.get(0));
assertEquals(tick2, results.get(1));
assertEquals(tick1, results.get(2));
assertEquals(tick2, results.get(3));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testEventDeclarationForInterfaces.
@Test(timeout = 10000)
public void testEventDeclarationForInterfaces() throws Exception {
// read in the source
final KieBase kbase = loadKnowledgeBase("test_CEP_EventInterfaces.drl");
KieSession session = createKnowledgeSession(kbase);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testDuplicateFiring2.
@Test
public void testDuplicateFiring2() 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 Tick when $s : StockTick() then System.out.println( $s ); end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + "\t$n: Number ( intValue > 0 ) from accumulate ( $e: StockTick() over window:time(3s), count($e))\n" + "then\n" + " list.add( $n ); \n" + " System.out.println( \"Events in last 3 seconds: \" + $n );\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.CLOUD);
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);
// insert events
for (int i = 1; i < 3; i++) {
StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
clock.advanceTime(1001, TimeUnit.MILLISECONDS);
ksession.insert(event);
System.out.println(i + ". rule invocation");
ksession.fireAllRules();
}
clock.advanceTime(3001, TimeUnit.MILLISECONDS);
StockTick event = new StockTick(3, "XXX", 1.0, 0);
System.out.println("3. rule invocation");
ksession.insert(event);
ksession.fireAllRules();
clock.advanceTime(3001, TimeUnit.MILLISECONDS);
StockTick event2 = new StockTick(3, "XXX", 1.0, 0);
System.out.println("4. rule invocation");
ksession.insert(event2);
ksession.fireAllRules();
ksession.dispose();
assertEquals(Arrays.asList(1L, 2L, 1L, 1L), list);
}
use of org.drools.compiler.StockTick 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));
}
Aggregations