use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testAfterOnArbitraryDates.
@Test(timeout = 10000)
public void testAfterOnArbitraryDates() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_AfterOperatorDates.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, // 4 seconds after DROO
104000, 3);
InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
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 testEventAssertionWithDateTimestamp.
@Test(timeout = 10000)
public void testEventAssertionWithDateTimestamp() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertionWithDateTimestamp.drl");
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession session = createKnowledgeSession(kbase, conf);
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000, 5);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 11000, 10);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 12000, 8);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 13000, 7);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
EventFactHandle eh1 = (EventFactHandle) handle1;
EventFactHandle eh2 = (EventFactHandle) handle2;
EventFactHandle eh3 = (EventFactHandle) handle3;
EventFactHandle eh4 = (EventFactHandle) handle4;
assertEquals(tick1.getTime(), eh1.getStartTimestamp());
assertEquals(tick2.getTime(), eh2.getStartTimestamp());
assertEquals(tick3.getTime(), eh3.getStartTimestamp());
assertEquals(tick4.getTime(), eh4.getStartTimestamp());
session.fireAllRules();
assertEquals(2, results.size());
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class MultithreadTest method testSlidingTimeWindows.
@Test(timeout = 1000000)
public void testSlidingTimeWindows() {
final String str = "package org.drools\n" + "global java.util.List list; \n" + "import " + StockTick.class.getCanonicalName() + "; \n" + "" + "declare StockTick @role(event) end\n" + "" + "rule R\n" + "when\n" + " accumulate( $st : StockTick() over window:time(400ms)\n" + " from entry-point X,\n" + " $c : count(1) )" + "then\n" + " list.add( $c ); \n" + "end \n";
final List<Exception> errors = new ArrayList<Exception>();
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(kbconf, str);
final KieSession ksession = kbase.newKieSession();
final EntryPoint ep = ksession.getEntryPoint("X");
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Executor executor = Executors.newCachedThreadPool(new ThreadFactory() {
public Thread newThread(final Runnable r) {
final Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
});
// runs for 10 seconds
final int RUN_TIME = 5000;
final int THREAD_NR = 2;
final CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
ecs.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
try {
ksession.fireUntilHalt();
return true;
} catch (final Exception e) {
errors.add(e);
e.printStackTrace();
return false;
}
}
});
for (int i = 0; i < THREAD_NR; i++) {
ecs.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
try {
final String s = Thread.currentThread().getName();
final long endTS = System.currentTimeMillis() + RUN_TIME;
int j = 0;
long lastTimeInserted = -1;
while (System.currentTimeMillis() < endTS) {
final long currentTimeInMillis = System.currentTimeMillis();
if (currentTimeInMillis > lastTimeInserted) {
lastTimeInserted = currentTimeInMillis;
ep.insert(new StockTick(j++, s, 0.0, 0));
}
}
return true;
} catch (final Exception e) {
errors.add(e);
e.printStackTrace();
return false;
}
}
});
}
boolean success = true;
for (int i = 0; i < THREAD_NR; i++) {
try {
success = ecs.take().get() && success;
} catch (final Exception e) {
errors.add(e);
}
}
ksession.halt();
try {
success = ecs.take().get() && success;
} catch (final Exception e) {
errors.add(e);
}
for (final Exception e : errors) {
e.printStackTrace();
}
Assertions.assertThat(errors).isEmpty();
Assertions.assertThat(success).isTrue();
ksession.dispose();
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class StreamsTest method testEventAssertion.
@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_EntryPoint.drl");
// final RuleBase ruleBase = loadRuleBase( reader );
KieSessionConfiguration conf = SessionConfiguration.newInstance();
((SessionConfiguration) conf).setClockType(ClockType.PSEUDO_CLOCK);
KieSession session = kbase.newKieSession(conf, null);
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis());
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(0, results.size());
StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick7 = new StockTick(7, "ACME", 15, System.currentTimeMillis());
StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
EntryPoint entry = session.getEntryPoint("StockStream");
InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
assertNotNull(handle5);
assertNotNull(handle6);
assertNotNull(handle7);
assertNotNull(handle8);
assertTrue(handle5.isEvent());
assertTrue(handle6.isEvent());
assertTrue(handle7.isEvent());
assertTrue(handle8.isEvent());
session.fireAllRules();
assertEquals(1, results.size());
assertSame(tick7, results.get(0));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class StreamsTest method testWindowDeclaration.
public void testWindowDeclaration() throws Exception {
String drl = "package org.drools.compiler\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "declare window RedHatTicks\n" + " StockTick( company == 'RHT' )\n" + " over window:length(5)\n" + " from entry-point ticks\n" + "end\n" + "rule X\n" + "when\n" + " accumulate( $s : StockTick( price > 20 ) from window RedHatTicks,\n" + " $sum : sum( $s.getPrice() ),\n" + " $cnt : count( $s ) )\n" + "then\n" + "end\n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
KieSession ksession = kbase.newKieSession();
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
EntryPoint ep = ksession.getEntryPoint("ticks");
// not in the window
ep.insert(new StockTick(1, "ACME", 20, 1000));
// not > 20
ep.insert(new StockTick(2, "RHT", 20, 1000));
ep.insert(new StockTick(3, "RHT", 30, 1000));
// not in the window
ep.insert(new StockTick(4, "ACME", 30, 1000));
ep.insert(new StockTick(5, "RHT", 25, 1000));
// not in the window
ep.insert(new StockTick(6, "ACME", 10, 1000));
// not > 20
ep.insert(new StockTick(7, "RHT", 10, 1000));
ep.insert(new StockTick(8, "RHT", 40, 1000));
ksession.fireAllRules();
ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(1)).afterMatchFired(captor.capture());
AfterMatchFiredEvent aafe = captor.getValue();
assertThat(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(95));
assertThat(((Number) aafe.getMatch().getDeclarationValue("$cnt")).intValue(), is(3));
}
Aggregations