Search in sources :

Example 1 with WorkingMemoryFileLogger

use of org.drools.kiesession.audit.WorkingMemoryFileLogger 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)

Example 2 with WorkingMemoryFileLogger

use of org.drools.kiesession.audit.WorkingMemoryFileLogger 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)

Aggregations

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