Search in sources :

Example 31 with StockTick

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

the class StreamsTest method testWindowDeclaration.

@Test
public void testWindowDeclaration() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "declare window RedHatTicks\n" + "    StockTick( company == 'RHT' )\n" + "               over window:length(5)\n" + "               from entry-point ticks\n" + "end\n" + "rule X\n" + "when\n" + "    accumulate( $s : StockTick( price > 20 ) from window RedHatTicks,\n" + "                $sum : sum( $s.getPrice() ),\n" + "                $cnt : count( $s ) )\n" + "then\n" + "end\n";
    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 EntryPoint ep = ksession.getEntryPoint("ticks");
        // not in the window
        ep.insert(new StockTick(1, "ACME", 20, 1000));
        // not > 20
        ep.insert(new StockTick(2, "RHT", 20, 1000));
        ep.insert(new StockTick(3, "RHT", 30, 1000));
        // not in the window
        ep.insert(new StockTick(4, "ACME", 30, 1000));
        ep.insert(new StockTick(5, "RHT", 25, 1000));
        // not in the window
        ep.insert(new StockTick(6, "ACME", 10, 1000));
        // not > 20
        ep.insert(new StockTick(7, "RHT", 10, 1000));
        ep.insert(new StockTick(8, "RHT", 40, 1000));
        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(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(95));
        assertThat(((Number) aafe.getMatch().getDeclarationValue("$cnt")).intValue(), is(3));
    } finally {
        ksession.dispose();
    }
}
Also used : 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) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 32 with StockTick

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

the class StreamsTest method testEventAssertion.

@Test(timeout = 10000)
public void testEventAssertion() {
    final String drl = "package org.drools.compiler\n" + "\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "\n" + "rule \"Test entry point\"\n" + "when\n" + "    $st : StockTick( company == \"ACME\", price > 10 ) from entry-point StockStream\n" + "then\n" + "    results.add( $st );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List results = new ArrayList();
        session.setGlobal("results", results);
        final StockTick tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis());
        final StockTick tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis());
        final StockTick tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis());
        final StockTick tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis());
        final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
        final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
        final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
        final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
        assertNotNull(handle1);
        assertNotNull(handle2);
        assertNotNull(handle3);
        assertNotNull(handle4);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        assertTrue(handle3.isEvent());
        assertTrue(handle4.isEvent());
        session.fireAllRules();
        assertEquals(0, results.size());
        final StockTick tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
        final StockTick tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
        final StockTick tick7 = new StockTick(7, "ACME", 15, System.currentTimeMillis());
        final StockTick tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
        final EntryPoint entry = session.getEntryPoint("StockStream");
        final InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
        final InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
        final InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
        final 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));
    } finally {
        session.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 33 with StockTick

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

the class StreamsTest method testEntryPointWithAccumulateAndMVEL.

@Test(timeout = 10000)
public void testEntryPointWithAccumulateAndMVEL() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\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";
    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");
        ep1.insert(new StockTick(1, "RHT", 10, 1000));
        final int rulesFired = ksession.fireAllRules();
        assertEquals(1, rulesFired);
        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 List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
        assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
    } 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 34 with StockTick

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

the class StreamsTest method testModifyRetracOnEntryPointFacts.

@Test(timeout = 10000)
public void testModifyRetracOnEntryPointFacts() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "\n" + "rule \"Test entry point 1\"\n" + "when\n" + "    $st : StockTick( company == \"ACME\", price > 10 && < 100 ) from entry-point \"stream1\"\n" + "then\n" + "    results.add( Double.valueOf( $st.getPrice() ) );\n" + "    modify( $st ) { setPrice( 110 ) }\n" + "end\n" + "\n" + "rule \"Test entry point 2\"\n" + "when\n" + "    $st : StockTick( company == \"ACME\", price > 100 ) from entry-point \"stream1\"\n" + "then\n" + "    results.add( Double.valueOf( $st.getPrice() ) );\n" + "    delete( $st );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession();
    try {
        final List<? extends Number> results = new ArrayList<>();
        session.setGlobal("results", results);
        final StockTick tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
        final StockTick tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
        final StockTick tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
        final StockTick tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
        final EntryPoint entry = session.getEntryPoint("stream1");
        final InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
        final InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
        final InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
        final 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(2, results.size());
        assertEquals(30, results.get(0).intValue());
        assertEquals(110, 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());
    } finally {
        session.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 35 with StockTick

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

the class StreamsTest method testEventDoesNotExpireIfNotInPattern.

@Test(timeout = 10000)
public void testEventDoesNotExpireIfNotInPattern() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + "    @role( event )\n" + "    @expires( 1s )\n" + "end\n" + "\n" + "rule X\n" + "when\n" + "    eval( true )\n" + "then \n" + "    // no-op\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
        ksession.addEventListener(wml);
        final PseudoClockScheduler clock = ksession.getSessionClock();
        final StockTick st1 = new StockTick(1, "RHT", 100, 1000);
        final StockTick 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(), CoreMatchers.hasItems(st1, st2));
        ksession.fireAllRules();
        clock.advanceTime(3, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertThat(ksession.getObjects().size(), equalTo(0));
    } finally {
        ksession.dispose();
    }
}
Also used : RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) 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