Search in sources :

Example 6 with OrderEvent

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

the class CepEspTest method testCollectWithWindows.

@Test(timeout = 10000)
public void testCollectWithWindows() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_CollectWithWindows.drl");
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger(ksession);
        final File testTmpDir = new File("target/test-tmp/");
        testTmpDir.mkdirs();
        logger.setFileName("target/test-tmp/testCollectWithWindows-audit");
        final List<Number> timeResults = new ArrayList<>();
        final List<Number> lengthResults = new ArrayList<>();
        ksession.setGlobal("timeResults", timeResults);
        ksession.setGlobal("lengthResults", lengthResults);
        final SessionPseudoClock clock = ksession.getSessionClock();
        try {
            // First interaction
            // 5 seconds
            clock.advanceTime(5, TimeUnit.SECONDS);
            ksession.insert(new OrderEvent("1", "customer A", 70));
            ksession.fireAllRules();
            assertEquals(1, timeResults.size());
            assertEquals(1, timeResults.get(0).intValue());
            assertEquals(1, lengthResults.size());
            assertEquals(1, lengthResults.get(0).intValue());
            // Second interaction: advance clock and assert new data
            // 10 seconds
            clock.advanceTime(10, TimeUnit.SECONDS);
            ksession.insert(new OrderEvent("2", "customer A", 60));
            ksession.fireAllRules();
            assertEquals(2, timeResults.size());
            assertEquals(2, timeResults.get(1).intValue());
            assertEquals(2, lengthResults.size());
            assertEquals(2, lengthResults.get(1).intValue());
            // Third interaction: advance clock and assert new data
            // 10 seconds
            clock.advanceTime(10, TimeUnit.SECONDS);
            ksession.insert(new OrderEvent("3", "customer A", 50));
            ksession.fireAllRules();
            assertEquals(3, timeResults.size());
            assertEquals(3, timeResults.get(2).intValue());
            assertEquals(3, lengthResults.size());
            assertEquals(3, lengthResults.get(2).intValue());
            // Fourth interaction: advance clock and assert new data
            // 10 seconds
            clock.advanceTime(10, TimeUnit.SECONDS);
            ksession.insert(new OrderEvent("4", "customer A", 25));
            ksession.fireAllRules();
            // first event should have expired now
            assertEquals(4, timeResults.size());
            assertEquals(3, timeResults.get(3).intValue());
            assertEquals(4, lengthResults.size());
            assertEquals(3, lengthResults.get(3).intValue());
            // Fifth interaction: advance clock and assert new data
            // 10 seconds
            clock.advanceTime(5, TimeUnit.SECONDS);
            ksession.insert(new OrderEvent("5", "customer A", 70));
            ksession.fireAllRules();
            assertEquals(5, timeResults.size());
            assertEquals(4, timeResults.get(4).intValue());
            assertEquals(5, lengthResults.size());
            assertEquals(3, lengthResults.get(4).intValue());
        } finally {
            logger.writeToDisk();
        }
    } finally {
        ksession.dispose();
    }
}
Also used : WorkingMemoryFileLogger(org.drools.kiesession.audit.WorkingMemoryFileLogger) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) OrderEvent(org.drools.testcoverage.common.model.OrderEvent) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) Test(org.junit.Test)

Example 7 with OrderEvent

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

the class CepEspTest method testTwoWindowsInsideCEAndOut.

@Test(timeout = 10000)
public void testTwoWindowsInsideCEAndOut() {
    final String drl = "package org.drools.compiler;\n" + "import java.util.List\n" + "import " + OrderEvent.class.getCanonicalName() + ";\n" + "\n" + "global List timeResults;\n" + "\n" + "declare " + OrderEvent.class.getCanonicalName() + "\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"r1\"\n" + "when\n" + "    $o1 : OrderEvent() over window:length(3) \n" + "        accumulate(  $o2 : OrderEvent() over window:length(3);\n" + "                     $avg : average( $o2.getTotal() ) )\n" + "then\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession wm = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        wm.insert(new OrderEvent("1", "customer A", 70));
        wm.insert(new OrderEvent("2", "customer A", 60));
        wm.insert(new OrderEvent("3", "customer A", 50));
        wm.insert(new OrderEvent("4", "customer A", 40));
        wm.insert(new OrderEvent("5", "customer A", 30));
        wm.insert(new OrderEvent("6", "customer A", 20));
        wm.insert(new OrderEvent("7", "customer A", 10));
        wm.fireAllRules();
    } finally {
        wm.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) OrderEvent(org.drools.testcoverage.common.model.OrderEvent) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

OrderEvent (org.drools.testcoverage.common.model.OrderEvent)7 Test (org.junit.Test)7 KieBase (org.kie.api.KieBase)7 ArrayList (java.util.ArrayList)6 KieSession (org.kie.api.runtime.KieSession)6 List (java.util.List)4 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)4 File (java.io.File)2 WorkingMemoryFileLogger (org.drools.kiesession.audit.WorkingMemoryFileLogger)2 EventFactHandle (org.drools.core.common.EventFactHandle)1 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)1