Search in sources :

Example 1 with OrderEvent

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

the class CepEspTest method testTimeAndLengthWindowConflict.

@Test(timeout = 10000)
public void testTimeAndLengthWindowConflict() {
    // JBRULES-3671
    final String drl = "package org.drools.compiler;\n" + "import java.util.List\n" + "import " + OrderEvent.class.getCanonicalName() + "\n" + "\n" + "global List timeResults;\n" + "global List lengthResults;\n" + "\n" + "declare OrderEvent\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " $list : List( empty == false ) from collect(\n" + " $o : OrderEvent() over window:time(30s) )\n" + "then\n" + " timeResults.add( $list.size() );\n" + "end\n" + "\n" + "rule \"collect with length window\"\n" + "when\n" + " accumulate (\n" + " $o : OrderEvent( $tot : total ) over window:length(3)," + " $avg : average( $tot ) )\n" + "then\n" + " lengthResults.add( $avg );\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<>();
        final List<Number> lengthResults = new ArrayList<>();
        ksession.setGlobal("timeResults", timeResults);
        ksession.setGlobal("lengthResults", lengthResults);
        final SessionPseudoClock clock = ksession.getSessionClock();
        // 5 seconds
        clock.advanceTime(5, TimeUnit.SECONDS);
        ksession.insert(new OrderEvent("1", "customer A", 70));
        ksession.fireAllRules();
        System.out.println(lengthResults);
        assertTrue(lengthResults.contains(70.0));
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        ksession.insert(new OrderEvent("2", "customer A", 60));
        ksession.fireAllRules();
        System.out.println(lengthResults);
        assertTrue(lengthResults.contains(65.0));
        // 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();
        System.out.println(lengthResults);
        assertTrue(lengthResults.contains(60.0));
        // Fourth interaction: advance clock and assert new data
        // 60 seconds
        clock.advanceTime(60, TimeUnit.SECONDS);
        ksession.insert(new OrderEvent("4", "customer A", 25));
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
}
Also used : 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) Test(org.junit.Test)

Example 2 with OrderEvent

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

the class CepEspTest method testSimpleTimeWindow.

@Test(timeout = 10000)
public void testSimpleTimeWindow() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleTimeWindow.drl");
    final KieSession wm = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List results = new ArrayList();
        wm.setGlobal("results", results);
        final SessionPseudoClock clock = wm.getSessionClock();
        // 5 seconds
        clock.advanceTime(5, TimeUnit.SECONDS);
        final EventFactHandle handle1 = (EventFactHandle) wm.insert(new OrderEvent("1", "customer A", 70));
        assertEquals(5000, handle1.getStartTimestamp());
        assertEquals(0, handle1.getDuration());
        wm.fireAllRules();
        assertEquals(1, results.size());
        assertEquals(70, ((Number) results.get(0)).intValue());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle2 = (EventFactHandle) wm.insert(new OrderEvent("2", "customer A", 60));
        assertEquals(15000, handle2.getStartTimestamp());
        assertEquals(0, handle2.getDuration());
        wm.fireAllRules();
        assertEquals(2, results.size());
        assertEquals(65, ((Number) results.get(1)).intValue());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle3 = (EventFactHandle) wm.insert(new OrderEvent("3", "customer A", 50));
        assertEquals(25000, handle3.getStartTimestamp());
        assertEquals(0, handle3.getDuration());
        wm.fireAllRules();
        assertEquals(3, results.size());
        assertEquals(60, ((Number) results.get(2)).intValue());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle4 = (EventFactHandle) wm.insert(new OrderEvent("4", "customer A", 25));
        assertEquals(35000, handle4.getStartTimestamp());
        assertEquals(0, handle4.getDuration());
        wm.fireAllRules();
        // first event should have expired, making average under the rule threshold, so no additional rule fire
        assertEquals(3, results.size());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle5 = (EventFactHandle) wm.insert(new OrderEvent("5", "customer A", 70));
        assertEquals(45000, handle5.getStartTimestamp());
        assertEquals(0, handle5.getDuration());
        wm.fireAllRules();
        // still under the threshold, so no fire
        assertEquals(3, results.size());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle6 = (EventFactHandle) wm.insert(new OrderEvent("6", "customer A", 115));
        assertEquals(55000, handle6.getStartTimestamp());
        assertEquals(0, handle6.getDuration());
        wm.fireAllRules();
        assertEquals(4, results.size());
        assertEquals(70, ((Number) results.get(3)).intValue());
    } finally {
        wm.dispose();
    }
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) OrderEvent(org.drools.testcoverage.common.model.OrderEvent) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with OrderEvent

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

the class CepEspTest method testSimpleLengthWindowWithQueue.

@Test(timeout = 10000)
public void testSimpleLengthWindowWithQueue() throws Exception {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleLengthWindow.drl");
    KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List results = new ArrayList();
        ksession.setGlobal("results", results);
        ksession.insert(new OrderEvent("1", "customer A", 80));
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        // assert new data
        ksession.insert(new OrderEvent("2", "customer A", 70));
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        // assert new data
        ksession.insert(new OrderEvent("3", "customer A", 60));
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        // assert new data
        ksession.insert(new OrderEvent("4", "customer A", 50));
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        assertEquals(1, results.size());
        assertEquals(60, ((Number) results.get(0)).intValue());
        // assert new data
        ksession.insert(new OrderEvent("5", "customer A", 10));
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        assertEquals(1, results.size());
        ksession.insert(new OrderEvent("6", "customer A", 90));
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        assertEquals(2, results.size());
        assertEquals(50, ((Number) results.get(1)).intValue());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) OrderEvent(org.drools.testcoverage.common.model.OrderEvent) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with OrderEvent

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

the class CepEspTest method testSimpleLengthWindow.

@Test(timeout = 10000)
public void testSimpleLengthWindow() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleLengthWindow.drl");
    final KieSession wm = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List results = new ArrayList();
        wm.setGlobal("results", results);
        wm.insert(new OrderEvent("1", "customer A", 70));
        wm.fireAllRules();
        assertEquals(1, results.size());
        assertEquals(70, ((Number) results.get(0)).intValue());
        // assert new data
        wm.insert(new OrderEvent("2", "customer A", 60));
        wm.fireAllRules();
        assertEquals(2, results.size());
        assertEquals(65, ((Number) results.get(1)).intValue());
        // assert new data
        wm.insert(new OrderEvent("3", "customer A", 50));
        wm.fireAllRules();
        assertEquals(3, results.size());
        assertEquals(60, ((Number) results.get(2)).intValue());
        // assert new data
        wm.insert(new OrderEvent("4", "customer A", 25));
        wm.fireAllRules();
        // first event should have expired, making average under the rule threshold, so no additional rule fire
        assertEquals(3, results.size());
        // assert new data
        wm.insert(new OrderEvent("5", "customer A", 70));
        wm.fireAllRules();
        // still under the threshold, so no fire
        assertEquals(3, results.size());
        // assert new data
        wm.insert(new OrderEvent("6", "customer A", 115));
        wm.fireAllRules();
        assertEquals(4, results.size());
        assertEquals(70, ((Number) results.get(3)).intValue());
    } finally {
        wm.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) OrderEvent(org.drools.testcoverage.common.model.OrderEvent) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with OrderEvent

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

the class CepEspTest method testIdleTimeAndTimeToNextJob.

@Test(timeout = 10000)
public void testIdleTimeAndTimeToNextJob() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleTimeWindow.drl");
    final StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger((WorkingMemory) wm);
        final File testTmpDir = new File("target/test-tmp/");
        testTmpDir.mkdirs();
        logger.setFileName("target/test-tmp/testIdleTimeAndTimeToNextJob-audit");
        try {
            final List results = new ArrayList();
            wm.setGlobal("results", results);
            // how to initialize the clock?
            // how to configure the clock?
            final SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
            // 5 seconds
            clock.advanceTime(5, TimeUnit.SECONDS);
            // there is no next job, so returns -1
            assertEquals(-1, wm.getTimeToNextJob());
            wm.insert(new OrderEvent("1", "customer A", 70));
            wm.fireAllRules();
            assertEquals(0, wm.getIdleTime());
            // now, there is a next job in 30 seconds: expire the event
            assertEquals(30000, wm.getTimeToNextJob());
            wm.fireAllRules();
            assertEquals(1, results.size());
            assertEquals(70, ((Number) results.get(0)).intValue());
            // advance clock and assert new data
            // 10 seconds
            clock.advanceTime(10, TimeUnit.SECONDS);
            // next job is in 20 seconds: expire the event
            assertEquals(20000, wm.getTimeToNextJob());
            wm.insert(new OrderEvent("2", "customer A", 60));
            wm.fireAllRules();
            assertEquals(2, results.size());
            assertEquals(65, ((Number) results.get(1)).intValue());
            // advance clock and assert new data
            // 10 seconds
            clock.advanceTime(10, TimeUnit.SECONDS);
            // next job is in 10 seconds: expire the event
            assertEquals(10000, wm.getTimeToNextJob());
            wm.insert(new OrderEvent("3", "customer A", 50));
            wm.fireAllRules();
            assertEquals(3, results.size());
            assertEquals(60, ((Number) results.get(2)).intValue());
            // advance clock and assert new data
            // 10 seconds
            clock.advanceTime(10, TimeUnit.SECONDS);
            // advancing clock time will cause events to expire
            assertEquals(0, wm.getIdleTime());
            // next job is in 10 seconds: expire another event
            // assertEquals( 10000, iwm.getTimeToNextJob());
            wm.insert(new OrderEvent("4", "customer A", 25));
            wm.fireAllRules();
            // first event should have expired, making average under the rule threshold, so no additional rule fire
            assertEquals(3, results.size());
            // advance clock and assert new data
            // 10 seconds
            clock.advanceTime(10, TimeUnit.SECONDS);
            wm.insert(new OrderEvent("5", "customer A", 70));
            assertEquals(0, wm.getIdleTime());
            // wm  = SerializationHelper.serializeObject(wm);
            wm.fireAllRules();
            // still under the threshold, so no fire
            assertEquals(3, results.size());
        } finally {
            logger.writeToDisk();
        }
    } finally {
        wm.dispose();
    }
}
Also used : WorkingMemoryFileLogger(org.drools.kiesession.audit.WorkingMemoryFileLogger) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) OrderEvent(org.drools.testcoverage.common.model.OrderEvent) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) 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