use of org.drools.compiler.StockTickInterface 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.drools.compiler.StockTickInterface in project drools by kiegroup.
the class CepEspTest method testBeforeOnArbitraryDates.
@Test(timeout = 10000)
public void testBeforeOnArbitraryDates() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperatorDates.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
104000, 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 4 seconds after DROO
100000, 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.StockTickInterface in project drools by kiegroup.
the class CepEspTest method testBeforeOperator.
@Test(timeout = 10000)
public void testBeforeOperator() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperator.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, sconf);
final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
clock.setStartupTime(1000);
List list = new ArrayList();
ksession.setGlobal("list", list);
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);
ksession.insert(tick1);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick2);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick3);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick4);
ksession.insert(tick5);
clock.advanceTime(1, TimeUnit.MILLISECONDS);
ksession.insert(tick6);
ksession.insert(tick7);
clock.advanceTime(2, TimeUnit.MILLISECONDS);
ksession.insert(tick8);
ksession.fireAllRules();
assertEquals(1, list.size());
StockTick[] stocks = (StockTick[]) list.get(0);
assertSame(tick4, stocks[0]);
assertSame(tick2, stocks[1]);
}
use of org.drools.compiler.StockTickInterface in project drools by kiegroup.
the class CepEspTest method testMetByOperator.
@Test(timeout = 10000)
public void testMetByOperator() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_MetByOperator.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, sconf);
final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<PseudoClockScheduler>getSessionClock();
clock.setStartupTime(1000);
List list = new ArrayList();
ksession.setGlobal("list", list);
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 fh1 = (InternalFactHandle) ksession.insert(tick1);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
InternalFactHandle fh2 = (InternalFactHandle) ksession.insert(tick2);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick3);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick4);
ksession.insert(tick5);
clock.advanceTime(1, TimeUnit.MILLISECONDS);
ksession.insert(tick6);
ksession.insert(tick7);
clock.advanceTime(2, TimeUnit.MILLISECONDS);
ksession.insert(tick8);
ksession.fireAllRules();
assertEquals(1, list.size());
StockTick[] stocks = (StockTick[]) list.get(0);
assertSame(tick1, stocks[0]);
assertSame(tick2, stocks[1]);
}
use of org.drools.compiler.StockTickInterface in project drools by kiegroup.
the class CepEspTest method testEqualityAssertBehaviorOnEntryPoints.
@Test(timeout = 10000)
public void testEqualityAssertBehaviorOnEntryPoints() throws IOException, ClassNotFoundException {
StockTickInterface st1 = new StockTick(1, "RHT", 10, 10);
StockTickInterface st2 = new StockTick(1, "RHT", 10, 10);
StockTickInterface st3 = new StockTick(2, "RHT", 15, 20);
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
kbconf.setOption(EqualityBehaviorOption.EQUALITY);
final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
final KieSession ksession1 = kbase1.newKieSession();
AgendaEventListener ael1 = mock(AgendaEventListener.class);
ksession1.addEventListener(ael1);
EntryPoint ep1 = ksession1.getEntryPoint("stocktick stream");
FactHandle fh1 = ep1.insert(st1);
FactHandle fh1_2 = ep1.insert(st1);
FactHandle fh2 = ep1.insert(st2);
FactHandle fh3 = ep1.insert(st3);
assertSame(fh1, fh1_2);
assertSame(fh1, fh2);
assertNotSame(fh1, fh3);
ksession1.fireAllRules();
// must have fired 2 times, one for each event equality
verify(ael1, times(2)).afterMatchFired(any(AfterMatchFiredEvent.class));
ksession1.dispose();
}
Aggregations