Search in sources :

Example 26 with StockTick

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

the class AbstractCepEspTest method testAssertBehaviorOnEntryPoints.

@Test(timeout = 10000)
public void testAssertBehaviorOnEntryPoints() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_AssertBehaviorOnEntryPoints.drl");
    final KieSession ksession = kbase.newKieSession();
    try {
        final StockTick st1 = new StockTick(1, "RHT", 10, 10);
        final StockTick st2 = new StockTick(1, "RHT", 10, 10);
        final StockTick st3 = new StockTick(2, "RHT", 15, 20);
        final AgendaEventListener ael1 = mock(AgendaEventListener.class);
        ksession.addEventListener(ael1);
        final EntryPoint ep1 = ksession.getEntryPoint("stocktick stream");
        final FactHandle fh1 = ep1.insert(st1);
        final FactHandle fh1_2 = ep1.insert(st1);
        final FactHandle fh2 = ep1.insert(st2);
        final FactHandle fh3 = ep1.insert(st3);
        if (kieBaseTestConfiguration.isIdentity()) {
            assertSame(fh1, fh1_2);
            assertNotSame(fh1, fh2);
            assertNotSame(fh1, fh3);
            assertNotSame(fh2, fh3);
            ksession.fireAllRules();
            // must have fired 3 times, one for each event identity
            verify(ael1, times(3)).afterMatchFired(any(AfterMatchFiredEvent.class));
        } else {
            assertSame(fh1, fh1_2);
            assertSame(fh1, fh2);
            assertNotSame(fh1, fh3);
            ksession.fireAllRules();
            // must have fired 2 times, one for each event equality
            verify(ael1, times(2)).afterMatchFired(any(AfterMatchFiredEvent.class));
        }
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 27 with StockTick

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

the class DRLCepTest method testEventsInDifferentPackages.

@Test
public void testEventsInDifferentPackages() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "rule r1\n" + "when\n" + "then\n" + "    StockTick st = new StockTick();\n" + "    st.setCompany(\"RHT\");\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("drl-cep-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        Assertions.assertThat(ksession.fireAllRules()).isEqualTo(1);
    } 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 28 with StockTick

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

the class CepEspTest method testTemporalOperators.

@Test(timeout = 10000)
public void testTemporalOperators() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick \n" + "    @role( event )\n" + "    @timestamp( time )\n" + "end\n" + "\n" + "rule \"2 operators\"\n" + "when\n" + "    $a : StockTick( company == \"A\" )\n" + "    not( StockTick( company == \"B\", this after[0,20s] $a ) )\n" + "    not( StockTick( company == \"C\", this after[0,20s] $a ) )\n" + "then\n" + "    // do something\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(new StockTick(1, "A", 10, 1000));
    } 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 29 with StockTick

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

the class CepEspTest method testDuplicateFiring1.

@Test
public void testDuplicateFiring1() {
    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 \"slidingTimeCount\"\n" + "when\n" + "  accumulate ( $e: StockTick() over window:time(300ms) from entry-point SensorEventStream, " + "              $n : count( $e );" + "              $n > 0 )\n" + "then\n" + "  list.add( $n ); \n" + "  System.out.println( \"Events in last 3 seconds: \" + $n );\n" + "end" + "" + "\n" + "rule \"timerRuleAfterAllEvents\"\n" + "        timer ( int: 2s )\n" + "when\n" + "        $room : String( )\n" + "then\n" + "  list.add( -1 ); \n" + "  System.out.println(\"2sec after room was modified\");\n" + "end " + "";
    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 ArrayList list = new ArrayList();
        ksession.setGlobal("list", list);
        // entry point for sensor events
        final EntryPoint sensorEventStream = ksession.getEntryPoint("SensorEventStream");
        ksession.insert("Go");
        // insert events
        for (int i = 2; i < 8; i++) {
            final StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
            sensorEventStream.insert(event);
            ksession.fireAllRules();
            clock.advanceTime(105, TimeUnit.MILLISECONDS);
        }
        // let thread sleep for another 1m to see if dereffered rules fire (timers, (not) after rules)
        clock.advanceTime(100 * 40, TimeUnit.MILLISECONDS);
        ksession.fireAllRules();
        assertEquals(Arrays.asList(1L, 2L, 3L, 3L, 3L, 3L, -1), list);
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 30 with StockTick

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

the class AccumulateTest method testAccumulateNonExistingFunction.

@Test(timeout = 10000)
public void testAccumulateNonExistingFunction() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "rule \"Accumulate non existing function - Java\"\n" + "    dialect \"java\"\n" + "    when\n" + "        $val : Number() from accumulate( $st : StockTick(),\n" + "                                         nonExistingFunction( 1 ) );\n" + "    then\n" + "        // no-op\n" + "end  \n" + "\n" + "rule \"Accumulate non existing function - MVEL\"\n" + "    dialect \"mvel\"\n" + "    when\n" + "        $val : Number() from accumulate( $st : StockTick(),\n" + "                                         nonExistingFunction( 1 ) );\n" + "    then\n" + "        // no-op\n" + "end";
    final KieBuilder kieBuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, drl);
    Assertions.assertThat(kieBuilder.getResults().getMessages()).isNotEmpty();
    Assertions.assertThat(kieBuilder.getResults().getMessages()).extracting(Message::getText).anySatisfy(text -> Assertions.assertThat(text).contains("Unknown accumulate function: 'nonExistingFunction' on rule 'Accumulate non existing function - Java'."));
    Assertions.assertThat(kieBuilder.getResults().getMessages()).extracting(Message::getText).anySatisfy(text -> Assertions.assertThat(text).contains("Unknown accumulate function: 'nonExistingFunction' on rule 'Accumulate non existing function - MVEL'."));
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBuilder(org.kie.api.builder.KieBuilder) 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