Search in sources :

Example 1 with StockTick

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

the class CepEspTest method testIdleTime.

@Test(timeout = 10000)
public void testIdleTime() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleEventAssertion.drl");
    final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final SessionPseudoClock clock = session.getSessionClock();
        final List results = new ArrayList();
        session.setGlobal("results", results);
        final StockTick tick1 = new StockTick(1, "DROO", 50, 10000);
        final StockTick tick2 = new StockTick(2, "ACME", 10, 10010);
        final StockTick tick3 = new StockTick(3, "ACME", 10, 10100);
        final StockTick tick4 = new StockTick(4, "DROO", 50, 11000);
        assertEquals(0, ((InternalWorkingMemory) session).getIdleTime());
        final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
        clock.advanceTime(10, TimeUnit.SECONDS);
        assertEquals(10000, ((InternalWorkingMemory) session).getIdleTime());
        final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
        assertEquals(0, ((InternalWorkingMemory) session).getIdleTime());
        clock.advanceTime(15, TimeUnit.SECONDS);
        assertEquals(15000, ((InternalWorkingMemory) session).getIdleTime());
        clock.advanceTime(15, TimeUnit.SECONDS);
        assertEquals(30000, ((InternalWorkingMemory) session).getIdleTime());
        final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
        assertEquals(0, ((InternalWorkingMemory) session).getIdleTime());
        clock.advanceTime(20, TimeUnit.SECONDS);
        final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
        clock.advanceTime(10, TimeUnit.SECONDS);
        assertNotNull(handle1);
        assertNotNull(handle2);
        assertNotNull(handle3);
        assertNotNull(handle4);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        assertTrue(handle3.isEvent());
        assertTrue(handle4.isEvent());
        assertEquals(10000, ((InternalWorkingMemory) session).getIdleTime());
        session.fireAllRules();
        assertEquals(0, ((InternalWorkingMemory) session).getIdleTime());
        assertEquals(2, ((List) session.getGlobal("results")).size());
    } finally {
        session.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) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 2 with StockTick

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

the class CepEspTest method testAnnotatedEventAssertion.

@Test(timeout = 10000)
public void testAnnotatedEventAssertion() {
    final String drl = "package org.drools.compiler.test;\n" + "\n" + "import " + StockTickEvent.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "rule \"Check event\"\n" + "when\n" + "    $st : StockTickEvent( company == \"ACME\" )\n" + "then\n" + "    results.add( $st );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final SessionPseudoClock clock = session.getSessionClock();
        final List results = new ArrayList();
        session.setGlobal("results", results);
        final StockTick tick1 = new StockTickEvent(1, "DROO", 50, 10000);
        final StockTick tick2 = new StockTickEvent(2, "ACME", 10, 10010);
        final StockTick tick3 = new StockTickEvent(3, "ACME", 10, 10100);
        final StockTick tick4 = new StockTickEvent(4, "DROO", 50, 11000);
        final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
        clock.advanceTime(10, TimeUnit.SECONDS);
        final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
        clock.advanceTime(30, TimeUnit.SECONDS);
        final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
        clock.advanceTime(20, TimeUnit.SECONDS);
        final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
        clock.advanceTime(10, TimeUnit.SECONDS);
        assertNotNull(handle1);
        assertNotNull(handle2);
        assertNotNull(handle3);
        assertNotNull(handle4);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        assertTrue(handle3.isEvent());
        assertTrue(handle4.isEvent());
        session.fireAllRules();
        assertEquals(2, ((List) session.getGlobal("results")).size());
    } finally {
        session.dispose();
    }
}
Also used : StockTickEvent(org.drools.testcoverage.common.model.StockTickEvent) 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) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 3 with StockTick

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

the class CepEspTest method testEventAssertion.

@Test(timeout = 10000)
public void testEventAssertion() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleEventAssertion.drl");
    final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final SessionPseudoClock clock = session.getSessionClock();
        final List results = new ArrayList();
        session.setGlobal("results", results);
        final StockTick tick1 = new StockTick(1, "DROO", 50, 10000);
        final StockTick tick2 = new StockTick(2, "ACME", 10, 10010);
        final StockTick tick3 = new StockTick(3, "ACME", 10, 10100);
        final StockTick tick4 = new StockTick(4, "DROO", 50, 11000);
        final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
        clock.advanceTime(10, TimeUnit.SECONDS);
        final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
        clock.advanceTime(30, TimeUnit.SECONDS);
        final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
        clock.advanceTime(20, TimeUnit.SECONDS);
        final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
        clock.advanceTime(10, TimeUnit.SECONDS);
        assertNotNull(handle1);
        assertNotNull(handle2);
        assertNotNull(handle3);
        assertNotNull(handle4);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        assertTrue(handle3.isEvent());
        assertTrue(handle4.isEvent());
        session.fireAllRules();
        assertEquals(2, ((List) session.getGlobal("results")).size());
    } finally {
        session.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) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 4 with StockTick

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

the class CepEspTest method testSalienceWithEventsPseudoClock.

@Test(timeout = 10000)
public void testSalienceWithEventsPseudoClock() {
    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(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final SessionPseudoClock clock = ksession.getSessionClock();
        clock.advanceTime(1000000, TimeUnit.MILLISECONDS);
        ksession.insert(new StockTick(1, "RHT", 10, 1000));
        clock.advanceTime(5, TimeUnit.SECONDS);
        ksession.insert(new StockTick(2, "RHT", 10, 1000));
        clock.advanceTime(5, TimeUnit.SECONDS);
        ksession.insert(new StockTick(3, "RHT", 10, 1000));
        clock.advanceTime(5, TimeUnit.SECONDS);
        ksession.insert(new StockTick(4, "ACME", 10, 1000));
        clock.advanceTime(5, TimeUnit.SECONDS);
        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) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) 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)

Example 5 with StockTick

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

the class CepEspTest method testDeleteExpiredEventWithTimestampAndEqualityKey.

@Test
public void testDeleteExpiredEventWithTimestampAndEqualityKey() {
    // DROOLS-1017
    final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + "    @role( event )\n" + "    @timestamp( time )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "when\n" + "  $st1 : StockTick( company == \"ACME\" )\n" + "  not ( StockTick( this != $st1, this after[0s, 1s] $st1) )\n" + "then\n" + "  delete($st1);\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final PseudoClockScheduler clock = ksession.getSessionClock();
        clock.setStartupTime(5000L);
        final EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50, 0L));
        clock.advanceTime(2, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertTrue(handle1.isExpired());
        assertFalse(ksession.getFactHandles().contains(handle1));
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) 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