Search in sources :

Example 21 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class TimerAndCalendarWithPseudoTimeTest method testExpiredPropagations.

@Test
public void testExpiredPropagations() {
    // DROOLS-244
    final String drl = "package org.drools.test;\n" + "\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + "    @role( event )\n" + "    @timestamp( time )\n" + "end\n" + "\n" + "declare window ATicks\n" + " StockTick( company == \"AAA\" ) over window:time( 1s ) " + " from entry-point \"AAA\"\n" + "end\n" + "\n" + "declare window BTicks\n" + " StockTick( company == \"BBB\" ) over window:time( 1s ) " + " from entry-point \"BBB\"\n" + "end\n" + "\n" + "rule Ticks \n" + " when\n" + " String()\n" + " accumulate( $x : StockTick() from window ATicks, $a : count( $x ) )\n" + " accumulate( $y : StockTick() from window BTicks, $b : count( $y ) )\n" + " accumulate( $z : StockTick() over window:time( 1s ), $c : count( $z ) )\n" + " then\n" + " list.add( $a );\n" + " list.add( $b );\n" + " list.add( $c );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("timer-and-calendar-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final ArrayList list = new ArrayList();
        ksession.setGlobal("list", list);
        final SessionPseudoClock clock = ksession.getSessionClock();
        clock.advanceTime(1100, TimeUnit.MILLISECONDS);
        final StockTick tick = new StockTick(0, "AAA", 1.0, 0);
        final StockTick tock = new StockTick(1, "BBB", 1.0, 2500);
        final StockTick tack = new StockTick(1, "CCC", 1.0, 2700);
        final EntryPoint epa = ksession.getEntryPoint("AAA");
        final EntryPoint epb = ksession.getEntryPoint("BBB");
        epa.insert(tick);
        epb.insert(tock);
        ksession.insert(tack);
        FactHandle handle = ksession.insert("go1");
        ksession.fireAllRules();
        assertEquals(asList(0L, 1L, 1L), list);
        list.clear();
        ksession.delete(handle);
        clock.advanceTime(2550, TimeUnit.MILLISECONDS);
        handle = ksession.insert("go2");
        ksession.fireAllRules();
        assertEquals(asList(0L, 0L, 1L), list);
        list.clear();
        ksession.delete(handle);
        clock.advanceTime(500, TimeUnit.MILLISECONDS);
        handle = ksession.insert("go3");
        ksession.fireAllRules();
        assertEquals(asList(0L, 0L, 0L), list);
        list.clear();
        ksession.delete(handle);
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 22 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class ConstraintsTest method testConnectorsAndOperators.

@Test
public void testConnectorsAndOperators() {
    final String drl = "package org.drools.compiler\n" + "\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "\n" + "declare StockTick\n" + "    @role( event )\n" + "    @timestamp( dateTimestamp )\n" + "end\n" + "\n" + "rule \"operator\"\n" + "    when\n" + "        $t1 : StockTick( company == \"RHT\" )\n" + "        $t2 : StockTick( company == \"IBM\", this after $t1 || before $t1 )\n" + "    then\n" + "        // do something\n" + "end  ";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("constraints-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(new StockTick(1, "RHT", 10, 1000));
        ksession.insert(new StockTick(2, "IBM", 10, 1100));
        assertEquals(1, ksession.fireAllRules());
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 23 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class StreamsTest method testMultipleWindows.

@Test(timeout = 10000)
public void testMultipleWindows() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule FaultsCoincide\n" + "when\n" + "   f1 : StockTick( company == \"RHT\" ) over window:length( 1 )\n" + "   f2 : StockTick( company == \"JBW\" ) over window:length( 1 )\n" + "then\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final StockTick st1 = new StockTick(1, "RHT", 10, 1000);
        ksession.insert(st1);
        final StockTick st2 = new StockTick(2, "JBW", 10, 1000);
        ksession.insert(st2);
        ksession.fireAllRules();
        final ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
        verify(ael, times(1)).afterMatchFired(captor.capture());
        final AfterMatchFiredEvent aafe = captor.getValue();
        assertThat(aafe.getMatch().getDeclarationValue("f1"), CoreMatchers.is(st1));
        assertThat(aafe.getMatch().getDeclarationValue("f2"), CoreMatchers.is(st2));
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 24 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class StreamsTest method testModifyOnEntryPointFacts.

@Test
public void testModifyOnEntryPointFacts() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule R1 salience 100\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep1\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n" + "rule R2 salience 90\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep2\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n" + "rule R3 salience 80\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep3\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
        ksession.addEventListener(ael);
        final EntryPoint ep1 = ksession.getEntryPoint("ep1");
        final EntryPoint ep2 = ksession.getEntryPoint("ep2");
        final EntryPoint ep3 = ksession.getEntryPoint("ep3");
        ep1.insert(new StockTick(1, "RHT", 10, 1000));
        ep2.insert(new StockTick(1, "RHT", 10, 1000));
        ep3.insert(new StockTick(1, "RHT", 10, 1000));
        final int rulesFired = ksession.fireAllRules();
        assertEquals(3, rulesFired);
        final ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
        verify(ael, times(3)).afterMatchFired(captor.capture());
        final List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
        assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
        assertThat(aafe.get(1).getMatch().getRule().getName(), is("R2"));
        assertThat(aafe.get(2).getMatch().getRule().getName(), is("R3"));
    } finally {
        ksession.dispose();
    }
}
Also used : AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 25 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class AbstractCepEspTest method testDuplicateFiring2.

@Test
public void testDuplicateFiring2() {
    final String drl = "package org.test;\n" + "import " + StockTick.class.getCanonicalName() + ";\n " + "" + "global java.util.List list \n" + "" + "declare StockTick @role(event) end \n" + "" + "rule Tick when $s : StockTick() then System.out.println( $s ); end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + "\t$n: Number ( intValue > 0 ) from accumulate ( $e: StockTick() over window:time(3s), count($e))\n" + "then\n" + "  list.add( $n ); \n" + "  System.out.println( \"Events in last 3 seconds: \" + $n );\n" + "end" + "";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    final ArrayList list = new ArrayList();
    try {
        final SessionPseudoClock clock = ksession.getSessionClock();
        ksession.setGlobal("list", list);
        // insert events
        for (int i = 1; i < 3; i++) {
            final StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
            clock.advanceTime(1001, TimeUnit.MILLISECONDS);
            ksession.insert(event);
            ksession.fireAllRules();
        }
        clock.advanceTime(3001, TimeUnit.MILLISECONDS);
        final StockTick event = new StockTick(3, "XXX", 1.0, 0);
        ksession.insert(event);
        ksession.fireAllRules();
        clock.advanceTime(3001, TimeUnit.MILLISECONDS);
        final StockTick event2 = new StockTick(3, "XXX", 1.0, 0);
        ksession.insert(event2);
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
        if (kieBaseTestConfiguration.isIdentity()) {
            assertEquals(Arrays.asList(1L, 2L, 1L, 1L), list);
        } else {
            assertEquals(Arrays.asList(1L, 2L, 1L), list);
        }
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Aggregations

StockTick (org.drools.testcoverage.common.model.StockTick)53 KieBase (org.kie.api.KieBase)52 Test (org.junit.Test)51 KieSession (org.kie.api.runtime.KieSession)51 ArrayList (java.util.ArrayList)31 EntryPoint (org.kie.api.runtime.rule.EntryPoint)19 List (java.util.List)15 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 InternalFactHandle (org.drools.core.common.InternalFactHandle)12 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)9 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)9 IOException (java.io.IOException)8 ParseException (java.text.ParseException)8 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)7 EventFactHandle (org.drools.core.common.EventFactHandle)5 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)4 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)3 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)3 RuleRuntimeEventListener (org.kie.api.event.rule.RuleRuntimeEventListener)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2