Search in sources :

Example 1 with WorkingMemoryFileLogger

use of org.drools.core.audit.WorkingMemoryFileLogger in project drools by kiegroup.

the class CepEspTest method testCollectWithWindows.

@Test(timeout = 10000)
public void testCollectWithWindows() throws Exception {
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_CollectWithWindows.drl");
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger(ksession);
    File testTmpDir = new File("target/test-tmp/");
    testTmpDir.mkdirs();
    logger.setFileName("target/test-tmp/testCollectWithWindows-audit");
    List<Number> timeResults = new ArrayList<Number>();
    List<Number> lengthResults = new ArrayList<Number>();
    ksession.setGlobal("timeResults", timeResults);
    ksession.setGlobal("lengthResults", lengthResults);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    try {
        // First interaction
        clock.advanceTime(5, // 5 seconds
        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
        clock.advanceTime(10, // 10 seconds
        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
        clock.advanceTime(10, // 10 seconds
        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
        clock.advanceTime(10, // 10 seconds
        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
        clock.advanceTime(5, // 10 seconds
        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();
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) WorkingMemoryFileLogger(org.drools.core.audit.WorkingMemoryFileLogger) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) OrderEvent(org.drools.compiler.OrderEvent) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 2 with WorkingMemoryFileLogger

use of org.drools.core.audit.WorkingMemoryFileLogger in project drools by kiegroup.

the class CepEspTest method testIdleTimeAndTimeToNextJob.

@Test(timeout = 10000)
public void testIdleTimeAndTimeToNextJob() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_SimpleTimeWindow.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) createKnowledgeSession(kbase, sconf);
    WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger((WorkingMemory) wm);
    File testTmpDir = new File("target/test-tmp/");
    testTmpDir.mkdirs();
    logger.setFileName("target/test-tmp/testIdleTimeAndTimeToNextJob-audit");
    try {
        List results = new ArrayList();
        wm.setGlobal("results", results);
        // how to initialize the clock?
        // how to configure the clock?
        SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
        clock.advanceTime(5, // 5 seconds
        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
        clock.advanceTime(10, // 10 seconds
        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
        clock.advanceTime(10, // 10 seconds
        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
        clock.advanceTime(10, // 10 seconds
        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
        clock.advanceTime(10, // 10 seconds
        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();
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) WorkingMemoryFileLogger(org.drools.core.audit.WorkingMemoryFileLogger) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) OrderEvent(org.drools.compiler.OrderEvent) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 3 with WorkingMemoryFileLogger

use of org.drools.core.audit.WorkingMemoryFileLogger in project drools by kiegroup.

the class WorkingMemoryLoggerTest method testOutOfMemory.

@Test
public void testOutOfMemory() throws Exception {
    final KieBase kbase = loadKnowledgeBase("empty.drl");
    for (int i = 0; i < 10000; i++) {
        final KieSession session = createKnowledgeSession(kbase);
        final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger((WorkingMemory) session);
        session.fireAllRules();
        session.dispose();
    }
}
Also used : WorkingMemoryFileLogger(org.drools.core.audit.WorkingMemoryFileLogger) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

WorkingMemoryFileLogger (org.drools.core.audit.WorkingMemoryFileLogger)3 Test (org.junit.Test)3 KieBase (org.kie.api.KieBase)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 OrderEvent (org.drools.compiler.OrderEvent)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 KieSession (org.kie.api.runtime.KieSession)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)2 List (java.util.List)1 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)1