Search in sources :

Example 6 with StockTick

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

the class CepEspTest method testDeserializationWithCompositeTrigger.

@Test
public void testDeserializationWithCompositeTrigger() {
    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" + "  $event : StockTick( )\n" + "  not StockTick( company == \"BBB\", this after[0,96h] $event )\n" + "  not StockTick( company == \"CCC\", this after[0,96h] $event )\n" + "then\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, "AAA", 1.0, 0));
        try {
            ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
        } catch (final Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    } finally {
        ks.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) 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 7 with StockTick

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

the class CepEspTest method testPastEventExipration.

@Test
public void testPastEventExipration() {
    // DROOLS-257
    final String drl = "package org.test;\n" + "import " + StockTick.class.getCanonicalName() + ";\n " + "" + "global java.util.List list; \n" + "" + "declare StockTick @role(event) @timestamp( time ) @expires( 200ms ) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + "  accumulate ( $e: StockTick() over window:length(10), $n : count($e) )\n" + "then\n" + "  list.add( $n ); \n" + "  System.out.println( \"Events in last X seconds: \" + $n );\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);
        final long now = 0;
        final StockTick event1 = new StockTick(1, "XXX", 1.0, now);
        final StockTick event2 = new StockTick(2, "XXX", 1.0, now + 240);
        final StockTick event3 = new StockTick(2, "XXX", 1.0, now + 380);
        final StockTick event4 = new StockTick(2, "XXX", 1.0, now + 500);
        ksession.insert(event1);
        ksession.insert(event2);
        ksession.insert(event3);
        ksession.insert(event4);
        clock.advanceTime(220, TimeUnit.MILLISECONDS);
        ksession.fireAllRules();
        clock.advanceTime(400, TimeUnit.MILLISECONDS);
        ksession.fireAllRules();
        assertEquals(Arrays.asList(3L, 1L), 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) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 8 with StockTick

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

the class CepEspTest method testSerializationWithWindowLengthAndLiaSharing.

@Test
public void testSerializationWithWindowLengthAndLiaSharing() {
    // DROOLS-953
    final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "\n" + "rule ReportLastEvent when\n" + "    $e : StockTick() over window:length(1)\n" + "then\n" + "    list.add($e.getCompany());\n" + "end\n" + "\n" + "rule ReportEventInserted when\n" + "   $e : StockTick()\n" + "then\n" + "   System.out.println(\"Event Insert : \" + $e);\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        List<String> list = new ArrayList<>();
        ksession.setGlobal("list", list);
        ksession.insert(new StockTick(1, "ACME", 50));
        ksession.insert(new StockTick(2, "DROO", 50));
        ksession.insert(new StockTick(3, "JBPM", 50));
        ksession.fireAllRules();
        assertEquals(1, list.size());
        assertEquals("JBPM", list.get(0));
        try {
            ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        } catch (final Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        list = new ArrayList<>();
        ksession.setGlobal("list", list);
        ksession.fireAllRules();
        assertEquals(0, list.size());
    } finally {
        ksession.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 9 with StockTick

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

the class CepEspTest method testComplexOperator.

@Test(timeout = 10000)
public void testComplexOperator() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_ComplexOperator.drl");
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final PseudoClockScheduler clock = ksession.getSessionClock();
        clock.setStartupTime(1000);
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final StockTick tick1 = new StockTick(1, "DROO", 50, 0, 3);
        final StockTick tick2 = new StockTick(2, "ACME", 10, 4, 3);
        final StockTick tick3 = new StockTick(3, "ACME", 10, 8, 3);
        final StockTick tick4 = new StockTick(4, "DROO", 50, 12, 5);
        final StockTick tick5 = new StockTick(5, "ACME", 10, 12, 5);
        final StockTick tick6 = new StockTick(6, "ACME", 10, 13, 3);
        final StockTick tick7 = new StockTick(7, "ACME", 10, 13, 5);
        final StockTick tick8 = new StockTick(8, "ACME", 10, 15, 3);
        ksession.insert(tick1);
        ksession.insert(tick2);
        ksession.insert(tick3);
        ksession.insert(tick4);
        ksession.insert(tick5);
        ksession.insert(tick6);
        ksession.insert(tick7);
        ksession.insert(tick8);
        ksession.fireAllRules();
        assertEquals(1, list.size());
        final StockTick[] stocks = (StockTick[]) list.get(0);
        assertSame(tick4, stocks[0]);
        assertSame(tick2, stocks[1]);
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) 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) List(java.util.List) ArrayList(java.util.ArrayList) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 10 with StockTick

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

the class CepEspTest method testEventExpiration4.

@Test(timeout = 10000)
public void testEventExpiration4() {
    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" + "    @expires( 10s )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "no-loop\n" + "when\n" + "   $st1 : StockTick( company == \"ACME\" ) over window:time( 10s ) from entry-point \"Event Stream\"\n" + "then\n" + "   results.add( $st1 );\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 EntryPoint eventStream = ksession.getEntryPoint("Event Stream");
        final SessionPseudoClock clock = ksession.getSessionClock();
        final List results = new ArrayList();
        ksession.setGlobal("results", results);
        final EventFactHandle handle1 = (EventFactHandle) eventStream.insert(new StockTick(1, "ACME", 50, System.currentTimeMillis(), 3));
        ksession.fireAllRules();
        clock.advanceTime(11, TimeUnit.SECONDS);
        // clock.advance() will put the event expiration in the queue to be executed,
        // but it has to wait for a "thread" to do that
        // so we fire rules again here to get that
        // alternative could run fireUntilHalt()
        ksession.fireAllRules();
        assertEquals(1, results.size());
        assertTrue(handle1.isExpired());
        assertFalse(ksession.getFactHandles().contains(handle1));
    } 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) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) 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