use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ParallelEvaluationTest method testEventsExpiration.
@Test(timeout = 10000L)
public void testEventsExpiration() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
sb.append("import " + MyEvent.class.getCanonicalName() + ";\n");
sb.append("declare MyEvent @role( event ) @expires( 20ms ) @timestamp( timestamp ) end\n");
for (int i = 0; i < 10; i++) {
sb.append(getRuleWithEvent(i));
}
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(0);
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
ksession.insert(new MyEvent(i, i * 2L));
}
ksession.fireAllRules();
assertEquals(10, list.size());
assertEquals(10L, ksession.getFactCount());
sessionClock.advanceTime(29, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(5L, ksession.getFactCount());
sessionClock.advanceTime(12, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0L, ksession.getFactCount());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ParallelEvaluationTest method testFireUntilHaltWithExpiration.
@Test(timeout = 10000L)
public void testFireUntilHaltWithExpiration() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
sb.append("import " + MyEvent.class.getCanonicalName() + ";\n");
sb.append("declare MyEvent @role( event ) @expires( 20ms ) @timestamp( timestamp ) end\n");
for (int i = 0; i < 10; i++) {
sb.append(getRuleWithEventForExpiration(i));
}
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(0);
DebugList<Integer> list = new DebugList<Integer>();
CountDownLatch done1 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 10) {
done1.countDown();
}
});
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
ksession.insert(new MyEvent(i, i * 2L));
}
new Thread(() -> ksession.fireUntilHalt()).start();
try {
done1.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(10, list.size());
list.clear();
CountDownLatch done2 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 5) {
done2.countDown();
}
});
ksession.insert(1);
sessionClock.advanceTime(29, TimeUnit.MILLISECONDS);
try {
done2.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(5, list.size());
list.clear();
CountDownLatch done3 = new CountDownLatch(1);
list.onItemAdded = (l -> {
if (l.size() == 5) {
done3.countDown();
}
});
sessionClock.advanceTime(12, TimeUnit.MILLISECONDS);
try {
done3.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertEquals(5, list.size());
ksession.halt();
ksession.dispose();
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ParallelEvaluationTest method testFireUntilHaltWithExpiration2.
@Test(timeout = 100000L)
public void testFireUntilHaltWithExpiration2() {
String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $A: A( $Aid : value > 0 )\n" + " $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + " counter.incrementAndGet();\n" + "end\n" + "rule R1 when\n" + " $A: A( $Aid: value > 1 )\n" + " $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + " counter.incrementAndGet();\n" + "end\n" + "rule R2 when\n" + " $A: A( $Aid: value > 2 )\n" + " $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + " counter.incrementAndGet();\n" + "end\n" + "rule R3 when\n" + " $A: A( $Aid: value > 3 )\n" + " $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + " counter.incrementAndGet();\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(0);
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
new Thread(() -> ksession.fireUntilHalt()).start();
int eventsNr = 5;
for (int i = 0; i < eventsNr; i++) {
ksession.insert(new A(i + 4));
ksession.insert(new B(i + 4));
sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ksession.halt();
ksession.dispose();
assertEquals(eventsNr * 4, counter.get());
}
use of org.drools.core.time.impl.PseudoClockScheduler 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.core.time.impl.PseudoClockScheduler 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));
}
Aggregations