Search in sources :

Example 11 with StockTick

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

the class CepEspTest method testWindowExpireActionDeserialization.

@Test
public void testWindowExpireActionDeserialization() 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" + "end\n" + "\n" + "rule \"One\"\n" + "when\n" + "  StockTick( $id : seq, company == \"BBB\" ) over window:time( 1s )\n" + "then\n" + "  list.add( $id );\n" + "end\n" + "\n" + "";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    KieSession ks = kbase.newKieSession();
    try {
        ks.insert(new StockTick(2, "BBB", 1.0, 0));
        Thread.sleep(1500);
        try {
            ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
        } catch (final Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        final ArrayList list = new ArrayList();
        ks.setGlobal("list", list);
        ks.fireAllRules();
        ks.insert(new StockTick(3, "BBB", 1.0, 0));
        ks.fireAllRules();
        assertEquals(1, list.size());
        assertEquals(Collections.singletonList(3L), 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 12 with StockTick

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

the class CepEspTest method testTimeRelationalOperators.

@Test(timeout = 10000)
public void testTimeRelationalOperators() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_TimeRelationalOperators.drl");
    final KieSession wm = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final PseudoClockScheduler clock = wm.getSessionClock();
        clock.setStartupTime(1000);
        final List results_coincides = new ArrayList();
        final List results_before = new ArrayList();
        final List results_after = new ArrayList();
        final List results_meets = new ArrayList();
        final List results_met_by = new ArrayList();
        final List results_overlaps = new ArrayList();
        final List results_overlapped_by = new ArrayList();
        final List results_during = new ArrayList();
        final List results_includes = new ArrayList();
        final List results_starts = new ArrayList();
        final List results_started_by = new ArrayList();
        final List results_finishes = new ArrayList();
        final List results_finished_by = new ArrayList();
        wm.setGlobal("results_coincides", results_coincides);
        wm.setGlobal("results_before", results_before);
        wm.setGlobal("results_after", results_after);
        wm.setGlobal("results_meets", results_meets);
        wm.setGlobal("results_met_by", results_met_by);
        wm.setGlobal("results_overlaps", results_overlaps);
        wm.setGlobal("results_overlapped_by", results_overlapped_by);
        wm.setGlobal("results_during", results_during);
        wm.setGlobal("results_includes", results_includes);
        wm.setGlobal("results_starts", results_starts);
        wm.setGlobal("results_started_by", results_started_by);
        wm.setGlobal("results_finishes", results_finishes);
        wm.setGlobal("results_finished_by", results_finished_by);
        final StockTick tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
        final StockTick tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
        final StockTick tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
        final StockTick tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
        final StockTick tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
        final StockTick tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
        final StockTick tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
        final StockTick tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
        final InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
        clock.advanceTime(4, TimeUnit.MILLISECONDS);
        final InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
        clock.advanceTime(4, TimeUnit.MILLISECONDS);
        final InternalFactHandle handle3 = (InternalFactHandle) wm.insert(tick3);
        clock.advanceTime(4, TimeUnit.MILLISECONDS);
        final InternalFactHandle handle4 = (InternalFactHandle) wm.insert(tick4);
        final InternalFactHandle handle5 = (InternalFactHandle) wm.insert(tick5);
        clock.advanceTime(1, TimeUnit.MILLISECONDS);
        final InternalFactHandle handle6 = (InternalFactHandle) wm.insert(tick6);
        final InternalFactHandle handle7 = (InternalFactHandle) wm.insert(tick7);
        clock.advanceTime(2, TimeUnit.MILLISECONDS);
        final InternalFactHandle handle8 = (InternalFactHandle) wm.insert(tick8);
        assertNotNull(handle1);
        assertNotNull(handle2);
        assertNotNull(handle3);
        assertNotNull(handle4);
        assertNotNull(handle5);
        assertNotNull(handle6);
        assertNotNull(handle7);
        assertNotNull(handle8);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        assertTrue(handle3.isEvent());
        assertTrue(handle4.isEvent());
        assertTrue(handle6.isEvent());
        assertTrue(handle7.isEvent());
        assertTrue(handle8.isEvent());
        wm.fireAllRules();
        assertEquals(1, results_coincides.size());
        assertEquals(tick5, results_coincides.get(0));
        assertEquals(1, results_before.size());
        assertEquals(tick2, results_before.get(0));
        assertEquals(1, results_after.size());
        assertEquals(tick3, results_after.get(0));
        assertEquals(1, results_meets.size());
        assertEquals(tick3, results_meets.get(0));
        assertEquals(1, results_met_by.size());
        assertEquals(tick2, results_met_by.get(0));
        assertEquals(1, results_met_by.size());
        assertEquals(tick2, results_met_by.get(0));
        assertEquals(1, results_overlaps.size());
        assertEquals(tick4, results_overlaps.get(0));
        assertEquals(1, results_overlapped_by.size());
        assertEquals(tick8, results_overlapped_by.get(0));
        assertEquals(1, results_during.size());
        assertEquals(tick6, results_during.get(0));
        assertEquals(1, results_includes.size());
        assertEquals(tick4, results_includes.get(0));
        assertEquals(1, results_starts.size());
        assertEquals(tick6, results_starts.get(0));
        assertEquals(1, results_started_by.size());
        assertEquals(tick7, results_started_by.get(0));
        assertEquals(1, results_finishes.size());
        assertEquals(tick8, results_finishes.get(0));
        assertEquals(1, results_finished_by.size());
        assertEquals(tick7, results_finished_by.get(0));
    } finally {
        wm.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) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 13 with StockTick

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

the class CepEspTest method testSerializationWithWindowLength.

@Test
public void testSerializationWithWindowLength() {
    // 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";
    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 14 with StockTick

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

the class CepEspTest method testPackageSerializationWithEvents.

@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testPackageSerializationWithEvents() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleEventAssertion.drl");
    final KieSession session = kbase.newKieSession();
    try {
        final List<StockTick> 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 InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
        final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
        assertNotNull(handle1);
        assertNotNull(handle2);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        session.fireAllRules();
        assertEquals(1, results.size());
        assertEquals(tick2, results.get(0));
    } finally {
        session.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) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 15 with StockTick

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

the class CepEspTest method testTemporalOperatorsInfinity.

@Test(timeout = 10000)
public void testTemporalOperatorsInfinity() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "\n" + "declare StockTick \n" + "    @role( event )\n" + "end\n" + "\n" + "rule \"infinity\"\n" + "when\n" + "    $a : StockTick( ) from entry-point \"X\"\n" + "    $b : StockTick( this after[5s,*] $a ) from entry-point \"X\"\n" + "then\n" + "    list.add( $a );\n" + "    list.add( $b );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final SessionPseudoClock clock = ksession.getSessionClock();
        final EntryPoint ep = ksession.getEntryPoint("X");
        clock.advanceTime(1000, TimeUnit.SECONDS);
        ep.insert(new StockTick(1, "A", 10, clock.getCurrentTime()));
        clock.advanceTime(8, TimeUnit.SECONDS);
        ep.insert(new StockTick(2, "B", 10, clock.getCurrentTime()));
        clock.advanceTime(8, TimeUnit.SECONDS);
        ep.insert(new StockTick(3, "B", 10, clock.getCurrentTime()));
        clock.advanceTime(8, TimeUnit.SECONDS);
        assertEquals(3, ksession.fireAllRules());
    } 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) 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