Search in sources :

Example 11 with StockTick

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());
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 12 with StockTick

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());
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieSession(org.kie.api.runtime.KieSession) SpecialString(org.drools.mvel.compiler.SpecialString) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 13 with StockTick

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();
    }
}
Also used : ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) EntryPoint(org.kie.api.runtime.rule.EntryPoint) StockTick(org.drools.mvel.compiler.StockTick) KieBase(org.kie.api.KieBase) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

StockTick (org.drools.mvel.compiler.StockTick)13 Test (org.junit.Test)11 KieSession (org.kie.api.runtime.KieSession)10 EntryPoint (org.kie.api.runtime.rule.EntryPoint)7 ArrayList (java.util.ArrayList)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)5 KieBase (org.kie.api.KieBase)5 KiePackage (org.kie.api.definition.KiePackage)5 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)3 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)2 List (java.util.List)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)1 PackageDescrBuilder (org.drools.drl.ast.dsl.PackageDescrBuilder)1 DrlDumper (org.drools.mvel.DrlDumper)1 SpecialString (org.drools.mvel.compiler.SpecialString)1 StockTickInterface (org.drools.mvel.compiler.StockTickInterface)1