Search in sources :

Example 41 with StockTick

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

the class CepEspTest method testExpireEventOnEndTimestamp.

@Test(timeout = 10000)
public void testExpireEventOnEndTimestamp() {
    // DROOLS-40
    final String drl = "package org.drools.compiler;\n" + "import " + StockTick.class.getName() + "\n" + "global java.util.List resultsAfter;\n" + "\n" + "declare StockTick\n" + "    @role( event )\n" + "    @duration( duration )\n" + "end\n" + "\n" + "rule \"after[60,80]\"\n" + "when\n" + "$a : StockTick( company == \"DROO\" )\n" + "$b : StockTick( company == \"ACME\", this after[60,80] $a )\n" + "then\n" + "       resultsAfter.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 PseudoClockScheduler clock = ksession.getSessionClock();
        final List<StockTick> resultsAfter = new ArrayList<>();
        ksession.setGlobal("resultsAfter", resultsAfter);
        // inserting new StockTick with duration 30 at time 0 => rule
        // after[60,80] should fire when ACME lasts at 100-120
        ksession.insert(new StockTick(1, "DROO", 0, 0, 30));
        clock.advanceTime(100, TimeUnit.MILLISECONDS);
        ksession.insert(new StockTick(2, "ACME", 0, 0, 20));
        ksession.fireAllRules();
        assertEquals(1, resultsAfter.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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 42 with StockTick

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

the class CepEspTest method testMetByOperator.

@Test(timeout = 10000)
public void testMetByOperator() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_MetByOperator.drl");
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final PseudoClockScheduler clock = ksession.getSessionClock();
        clock.setStartupTime(1000);
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        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);
        ksession.insert(tick1);
        clock.advanceTime(4, TimeUnit.MILLISECONDS);
        ksession.insert(tick2);
        clock.advanceTime(4, TimeUnit.MILLISECONDS);
        ksession.insert(tick3);
        clock.advanceTime(4, TimeUnit.MILLISECONDS);
        ksession.insert(tick4);
        ksession.insert(tick5);
        clock.advanceTime(1, TimeUnit.MILLISECONDS);
        ksession.insert(tick6);
        ksession.insert(tick7);
        clock.advanceTime(2, TimeUnit.MILLISECONDS);
        ksession.insert(tick8);
        ksession.fireAllRules();
        assertEquals(1, list.size());
        final StockTick[] stocks = (StockTick[]) list.get(0);
        assertSame(tick1, 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) 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 43 with StockTick

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

the class CepEspTest method testTimeWindowWithPastEvents.

@Test(timeout = 10000)
public void testTimeWindowWithPastEvents() {
    // JBRULES-2258
    final String drl = "package org.drools.compiler;\n" + "import java.util.List\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "\n" + "global List timeResults;\n" + "\n" + "declare StockTick\n" + " @role( event )\n" + " @timestamp( time ) \n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " accumulate(\n" + " $o : StockTick() over window:time(10ms)," + " $tot : count( $o );" + " $tot > 0 )\n" + "then\n" + " timeResults.add( $tot );\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 List<Number> timeResults = new ArrayList<>();
        ksession.setGlobal("timeResults", timeResults);
        final SessionPseudoClock clock = ksession.getSessionClock();
        int count = 0;
        final StockTick tick1 = new StockTick(count++, "X", 0.0, 1);
        final StockTick tick2 = new StockTick(count++, "X", 0.0, 3);
        final StockTick tick3 = new StockTick(count++, "X", 0.0, 7);
        final StockTick tick4 = new StockTick(count++, "X", 0.0, 9);
        final StockTick tick5 = new StockTick(count, "X", 0.0, 15);
        clock.advanceTime(30, TimeUnit.MILLISECONDS);
        ksession.insert(tick1);
        ksession.insert(tick2);
        ksession.insert(tick3);
        ksession.insert(tick4);
        ksession.insert(tick5);
        ksession.fireAllRules();
        assertTrue(timeResults.isEmpty());
        clock.advanceTime(0, TimeUnit.MILLISECONDS);
        ksession.fireAllRules();
        assertTrue(timeResults.isEmpty());
        clock.advanceTime(3, TimeUnit.MILLISECONDS);
        ksession.fireAllRules();
        assertTrue(timeResults.isEmpty());
        clock.advanceTime(10, TimeUnit.MILLISECONDS);
        ksession.fireAllRules();
        assertTrue(timeResults.isEmpty());
    } 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) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 44 with StockTick

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

the class CepEspTest method testDeserializationWithTrackableTimerJobShortExpiration.

@Test
public void testDeserializationWithTrackableTimerJobShortExpiration() {
    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( 1ms )\n" + "end\n" + "rule \"Two\"\n" + "when\n" + "  StockTick( $id : seq, company == \"BBB\" ) \n" + "then\n" + "  System.out.println( $id ); \n" + "  list.add( $id );\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, "BBB", 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 45 with StockTick

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

the class StreamsTest method testEntryPointReference.

@Test
public void testEntryPointReference() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("stream-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_EntryPointReference.drl");
    final KieSession session = kbase.newKieSession();
    try {
        final List<StockTick> results = new ArrayList<>();
        session.setGlobal("results", results);
        final StockTick tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
        final StockTick tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
        final StockTick tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
        final StockTick tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
        final EntryPoint entry = session.getEntryPoint("stream1");
        final InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
        final InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
        final InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
        final InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
        assertNotNull(handle5);
        assertNotNull(handle6);
        assertNotNull(handle7);
        assertNotNull(handle8);
        assertTrue(handle5.isEvent());
        assertTrue(handle6.isEvent());
        assertTrue(handle7.isEvent());
        assertTrue(handle8.isEvent());
        session.fireAllRules();
        assertEquals(1, results.size());
        assertSame(tick7, results.get(0));
    } finally {
        session.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) 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