Search in sources :

Example 51 with StockTick

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

the class CepEspTest method testEventAssertionWithDuration.

@Test(timeout = 10000)
public void testEventAssertionWithDuration() {
    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" + "    @duration( duration )\n" + "    @timestamp( time )\n" + "end\n" + "\n" + "rule \"Check event\"\n" + "when\n" + "    $st : StockTick( 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 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());
        assertEquals(tick1.getDuration(), eh1.getDuration());
        assertEquals(tick2.getDuration(), eh2.getDuration());
        assertEquals(tick3.getDuration(), eh3.getDuration());
        assertEquals(tick4.getDuration(), eh4.getDuration());
        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 52 with StockTick

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

the class CepEspTest method testArbitraryDates.

private void testArbitraryDates(final String drlClasspathResource, final long tick1Time, final long tick2Time) {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, drlClasspathResource);
    final KieSession wm = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List<?> results = new ArrayList<>();
        wm.setGlobal("results", results);
        final StockTick tick1 = new StockTick(1, "DROO", 50, tick1Time, 3);
        final StockTick tick2 = new StockTick(2, "ACME", 10, tick2Time, 3);
        final InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
        final InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
        assertNotNull(handle1);
        assertNotNull(handle2);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        wm.fireAllRules();
        assertEquals(4, results.size());
        assertEquals(tick1, results.get(0));
        assertEquals(tick2, results.get(1));
        assertEquals(tick1, results.get(2));
        assertEquals(tick2, results.get(3));
    } 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) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 53 with StockTick

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

the class CepEspTest method testEventDeclarationForInterfaces.

@Test(timeout = 10000)
public void testEventDeclarationForInterfaces() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + "\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" + "    // no-op\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession();
    try {
        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);
        final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
        final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
        final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        assertTrue(handle3.isEvent());
        assertTrue(handle4.isEvent());
    } finally {
        session.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) 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