Search in sources :

Example 36 with StockTick

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

the class CepEspTest method testDeserializationWithTrackableTimerJob.

@Test
public void testDeserializationWithTrackableTimerJob() throws InterruptedException {
    final String drl = "package org.drools.test;\n" + "import " + StockTick.class.getCanonicalName() + "; \n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + "  @role( event )\n" + "  @expires( 1s )\n" + "end\n" + "\n" + "rule \"One\"\n" + "when\n" + "  StockTick( $id : seq, company == \"AAA\" ) over window:time( 1s )\n" + "then\n" + "  list.add( $id ); \n" + "end\n" + "\n" + "rule \"Two\"\n" + "when\n" + "  StockTick( $id : seq, company == \"BBB\" ) \n" + "then\n" + "  System.out.println( $id ); \n" + "  list.add( $id );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSessionConfiguration kieSessionConfiguration = KieSessionTestConfiguration.STATEFUL_REALTIME.getKieSessionConfiguration();
    kieSessionConfiguration.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ks = kbase.newKieSession(kieSessionConfiguration, null);
    try {
        ks.insert(new StockTick(2, "BBB", 1.0, 0));
        Thread.sleep(1100);
        try {
            ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
        } catch (final Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        ks.addEventListener(new DebugAgendaEventListener());
        final ArrayList list = new ArrayList();
        ks.setGlobal("list", list);
        ks.fireAllRules();
        ks.insert(new StockTick(3, "BBB", 1.0, 0));
        ks.fireAllRules();
        assertEquals(2, list.size());
        assertEquals(Arrays.asList(2L, 3L), list);
    } finally {
        ks.dispose();
    }
}
Also used : DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with StockTick

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

the class CepEspTest method AfterOperatorInCEPQueryTest.

@Test
public void AfterOperatorInCEPQueryTest() {
    final String drl = "package org.drools;\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "\n" + "query EventsBeforeNineSeconds\n" + "   $event : StockTick() from entry-point EStream\n" + "   $result : StockTick ( this after [0s, 9s] $event) from entry-point EventStream\n" + "end\n" + "\n" + "query EventsBeforeNineteenSeconds\n" + "   $event : StockTick() from entry-point EStream\n" + "   $result : StockTick ( this after [0s, 19s] $event) from entry-point EventStream\n" + "end\n" + "\n" + "query EventsBeforeHundredSeconds\n" + "   $event : StockTick() from entry-point EStream\n" + "   $result : StockTick ( this after [0s, 100s] $event) from entry-point EventStream\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final SessionPseudoClock clock = ksession.getSessionClock();
        final EntryPoint ePoint = ksession.getEntryPoint("EStream");
        final EntryPoint entryPoint = ksession.getEntryPoint("EventStream");
        ePoint.insert(new StockTick(0L, "zero", 0.0, 0));
        entryPoint.insert(new StockTick(1L, "one", 0.0, 0));
        clock.advanceTime(10, TimeUnit.SECONDS);
        entryPoint.insert(new StockTick(2L, "two", 0.0, 0));
        clock.advanceTime(10, TimeUnit.SECONDS);
        entryPoint.insert(new StockTick(3L, "three", 0.0, 0));
        QueryResults results = ksession.getQueryResults("EventsBeforeNineSeconds");
        assertEquals(0, results.size());
        results = ksession.getQueryResults("EventsBeforeNineteenSeconds");
        assertEquals(0, results.size());
        results = ksession.getQueryResults("EventsBeforeHundredSeconds");
        assertEquals(1, results.size());
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 38 with StockTick

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

the class CepEspTest method testEventAssertionWithDateTimestamp.

@Test(timeout = 10000)
public void testEventAssertionWithDateTimestamp() {
    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" + "    @timestamp( dateTimestamp )\n" + "end\n" + "\n" + "rule \"Check event\"\n" + "when\n" + "    $st : StockTick( company == \"ACME\" )\n" + "then\n" + "    results.add( $st );\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-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, 10000, 5);
        final StockTick tick2 = new StockTick(2, "ACME", 10, 11000, 10);
        final StockTick tick3 = new StockTick(3, "ACME", 10, 12000, 8);
        final StockTick tick4 = new StockTick(4, "DROO", 50, 13000, 7);
        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());
        final EventFactHandle eh1 = (EventFactHandle) handle1;
        final EventFactHandle eh2 = (EventFactHandle) handle2;
        final EventFactHandle eh3 = (EventFactHandle) handle3;
        final EventFactHandle eh4 = (EventFactHandle) handle4;
        assertEquals(tick1.getTime(), eh1.getStartTimestamp());
        assertEquals(tick2.getTime(), eh2.getStartTimestamp());
        assertEquals(tick3.getTime(), eh3.getStartTimestamp());
        assertEquals(tick4.getTime(), eh4.getStartTimestamp());
        session.fireAllRules();
        assertEquals(2, results.size());
    } finally {
        session.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 39 with StockTick

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

the class CepEspTest method testDeserializationWithExpiringEventAndAccumulate.

@Test
public void testDeserializationWithExpiringEventAndAccumulate() throws InterruptedException {
    final String drl = "package org.drools.test;\n" + "import " + StockTick.class.getCanonicalName() + "; \n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + "  @role( event )\n" + "  @expires( 1s )\n" + "end\n" + "\n" + "rule R\n" + "when\n" + "  accumulate ( StockTick( company == \"BBB\", $p : price), " + "              $sum : sum( $p );" + "              $sum > 0 )\n" + "then\n" + "  list.add( $sum ); \n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    KieSession ks = kbase.newKieSession();
    try {
        ks.insert(new StockTick(1, "BBB", 1.0, 0));
        Thread.sleep(1000);
        ks.insert(new StockTick(2, "BBB", 2.0, 0));
        Thread.sleep(100);
        try {
            ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
        } catch (final Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        final List<Double> list = new ArrayList<>();
        ks.setGlobal("list", list);
        ks.fireAllRules();
        ks.insert(new StockTick(3, "BBB", 3.0, 0));
        ks.fireAllRules();
        assertEquals(2, list.size());
        assertEquals(Arrays.asList(2.0, 5.0), list);
    } finally {
        ks.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 40 with StockTick

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

the class CepEspTest method testSalienceWithEventsRealtimeClock.

@Test(timeout = 10000)
public void testSalienceWithEventsRealtimeClock() throws InterruptedException {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getName() + "\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule R1 salience 1000\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT' )\n" + "        $s2 : StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + "    then\n" + "end\n" + "rule R2 salience 1000\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT' )\n" + "        not StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + "    then\n" + "end\n" + "rule R3 salience 100\n" + "    when\n" + "        $s2 : StockTick( company == 'ACME' )\n" + "    then\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        ksession.insert(new StockTick(1, "RHT", 10, 1000));
        ksession.insert(new StockTick(2, "RHT", 10, 1000));
        ksession.insert(new StockTick(3, "RHT", 10, 1000));
        // sleep for 2 secs
        Thread.sleep(2000);
        ksession.insert(new StockTick(4, "ACME", 10, 1000));
        // sleep for 1 sec
        Thread.sleep(1000);
        final int rulesFired = ksession.fireAllRules();
        assertEquals(4, rulesFired);
        final ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        verify(ael, times(4)).afterMatchFired(captor.capture());
        final List<AfterMatchFiredEvent> aafe = captor.getAllValues();
        assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
        assertThat(aafe.get(1).getMatch().getRule().getName(), is("R1"));
        assertThat(aafe.get(2).getMatch().getRule().getName(), is("R1"));
        assertThat(aafe.get(3).getMatch().getRule().getName(), is("R3"));
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) 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