use of org.drools.compiler.StockTick in project drools by kiegroup.
the class StreamsTest method testEventDoesNotExpireIfNotInPattern.
@Test(timeout = 10000)
public void testEventDoesNotExpireIfNotInPattern() throws Exception {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase("test_EventExpiration.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);
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);
verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
assertThat(ksession.getObjects().size(), equalTo(2));
assertThat((Collection<Object>) ksession.getObjects(), hasItems((Object) st1, st2));
ksession.fireAllRules();
clock.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertThat(ksession.getObjects().size(), equalTo(0));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class StreamsTest method testEntryPointWithAccumulateAndMVEL.
@Test(timeout = 10000)
public void testEntryPointWithAccumulateAndMVEL() throws Exception {
String str = "package org.drools.compiler\n" + "rule R1 dialect 'mvel'\n" + " when\n" + " $n : Number() from accumulate( \n" + " StockTick() from entry-point ep1,\n" + " count(1))" + " then\n" + "end\n";
// read in the source
KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, str);
KieSession ksession = createKnowledgeSession(kbase);
org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
ksession.addEventListener(ael);
EntryPoint ep1 = ksession.getEntryPoint("ep1");
ep1.insert(new StockTick(1, "RHT", 10, 1000));
int rulesFired = ksession.fireAllRules();
assertEquals(1, rulesFired);
ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(1)).afterMatchFired(captor.capture());
List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class StreamsTest method testModifyRetracOnEntryPointFacts.
@Test(timeout = 10000)
public void testModifyRetracOnEntryPointFacts() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_modifyRetractEntryPoint.drl");
KieSession session = kbase.newKieSession();
final List<? extends Number> results = new ArrayList<Number>();
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();
System.out.println(results);
assertEquals(2, results.size());
assertEquals(30, ((Number) results.get(0)).intValue());
assertEquals(110, ((Number) results.get(1)).intValue());
// the 3 non-matched facts continue to exist in the entry point
assertEquals(3, entry.getObjects().size());
// but no fact was inserted into the main session
assertEquals(0, session.getObjects().size());
}
use of org.drools.compiler.StockTick 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.StockTick 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));
}
Aggregations