use of org.drools.mvel.compiler.StockTick in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleAdditionsWithEntryPoints.
@Test(timeout = 10000)
public void testDynamicRuleAdditionsWithEntryPoints() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration);
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicWithEntryPoint.drl").getKiePackages();
KieSession ksession = kbase.newKieSession();
// now lets add some knowledge to the kbase
kbase.addPackages(kpkgs);
List<StockTick> results = new ArrayList<StockTick>();
ksession.setGlobal("results", results);
EntryPoint ep = ksession.getEntryPoint("in-channel");
ep.insert(new StockTick(1, "RHT", 20, 10000));
ep.insert(new StockTick(2, "RHT", 21, 15000));
ep.insert(new StockTick(3, "RHT", 22, 20000));
ksession.fireAllRules();
assertEquals(3, results.size());
}
use of org.drools.mvel.compiler.StockTick in project drools by kiegroup.
the class FirstOrderLogicTest method testForallWithSlidingWindow.
@Test
public void testForallWithSlidingWindow() throws Exception {
final KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieBaseTestConfiguration streamConfig = TestParametersUtil.getStreamInstanceOf(kieBaseTestConfiguration);
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), streamConfig, "test_ForallSlidingWindow.drl");
KieSession ksession = kbase.newKieSession(conf, null);
final SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
List<String> results = new ArrayList<String>();
ksession.setGlobal("results", results);
// advance time... no events, so forall should fire
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
int seq = 1;
// advance time... there are matching events now, but forall still not fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 60
clock.getCurrentTime()));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
ksession.insert(new StockTick(seq++, "RHT", 10, // 65
clock.getCurrentTime()));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... there are non-matching events now, so forall de-activates
ksession.insert(new StockTick(seq++, "IBM", 10, // 70
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... there are non-matching events now, so forall is still deactivated
ksession.insert(new StockTick(seq++, "RHT", 10, // 80
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... non-matching event expires now, so forall should fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 90
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
// advance time... forall still matches and should not fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 100
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
// advance time... forall still matches and should not fire
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
}
use of org.drools.mvel.compiler.StockTick in project drools by kiegroup.
the class MultithreadTest method testSlidingTimeWindows.
@Test(timeout = 2000000)
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<>();
KieBaseTestConfiguration streamConfig = TestParametersUtil.getStreamInstanceOf(kieBaseTestConfiguration);
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", streamConfig, str);
final KieSession ksession = kbase.newKieSession();
final EntryPoint ep = ksession.getEntryPoint("X");
final List list = new ArrayList();
ksession.setGlobal("list", list);
final int THREAD_NR = 2;
final ExecutorService executor = Executors.newFixedThreadPool(THREAD_NR, r -> {
final Thread t = new Thread(r);
t.setDaemon(true);
return t;
});
try {
final CompletionService<Boolean> ecs = new ExecutorCompletionService<>(executor);
ecs.submit(() -> {
try {
ksession.fireUntilHalt();
return true;
} catch (final Exception e) {
errors.add(e);
e.printStackTrace();
return false;
}
});
// runs for 10 seconds
final int RUN_TIME = 5000;
boolean success = false;
try {
for (int i = 0; i < THREAD_NR; i++) {
ecs.submit(() -> {
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;
}
});
}
success = true;
for (int i = 0; i < THREAD_NR; i++) {
try {
success = ecs.take().get() && success;
} catch (final Exception e) {
errors.add(e);
}
}
} finally {
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();
}
} finally {
executor.shutdownNow();
}
}
Aggregations