use of org.drools.compiler.StockTickInterface in project drools by kiegroup.
the class StreamsTest method testEventExpirationSetToZero.
@Test(timeout = 10000)
public void testEventExpirationSetToZero() throws Exception {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase("test_EventExpirationSetToZero.drl", kconf);
KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(ksessionConfig, null);
RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
ksession.addEventListener(wml);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
final StockTickInterface st1 = new StockTick(1, "RHT", 100, 1000);
final StockTickInterface st2 = new StockTick(2, "RHT", 100, 1000);
ksession.insert(st1);
ksession.insert(st2);
assertThat(ksession.fireAllRules(), equalTo(2));
verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
assertThat(ksession.getObjects().size(), equalTo(2));
assertThat((Collection<Object>) ksession.getObjects(), hasItems((Object) st1, st2));
clock.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertThat(ksession.getObjects().size(), equalTo(0));
}
use of org.drools.compiler.StockTickInterface in project drools by kiegroup.
the class StreamsTest method testEntryPointReference.
// (timeout=10000)
@Test
public void testEntryPointReference() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_EntryPointReference.drl");
KieSession session = kbase.newKieSession();
final List<StockTick> results = new ArrayList<StockTick>();
session.setGlobal("results", results);
StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
EntryPoint entry = session.getEntryPoint("stream1");
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.StockTickInterface in project drools by kiegroup.
the class PseudoClockEventsTest method processStocks.
private int processStocks(int stockCount, AgendaEventListener agendaEventListener, String drlContentString) throws DroolsParserException, IOException, Exception {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kconf, drlContentString);
KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
ksessionConfig.setProperty("keep.reference", "true");
final KieSession ksession = kbase.newKieSession(ksessionConfig, null);
ksession.addEventListener(agendaEventListener);
PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
Runnable fireUntilHaltRunnable = new Runnable() {
public void run() {
ksession.fireUntilHalt();
}
};
Thread fireUntilHaltThread = new Thread(fireUntilHaltRunnable, "Engine's thread");
fireUntilHaltThread.start();
Thread.currentThread().setName("Feeding thread");
for (int stIndex = 1; stIndex <= stockCount; stIndex++) {
clock.advanceTime(20, TimeUnit.SECONDS);
Thread.sleep(100);
final StockTickInterface st = new StockTick(stIndex, "RHT", 100 * stIndex, 100 * stIndex);
ksession.insert(st);
Thread.sleep(100);
}
Thread.sleep(100);
ksession.halt();
fireUntilHaltThread.join(5000);
return stockCount;
}
Aggregations