conf.setOption( EventProcessingOption.STREAM );
final KnowledgeBase 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,
TimeUnit.SECONDS ); // 5 seconds
// there is no next job, so returns -1
assertEquals( -1,
wm.getTimeToNextJob() );
wm.insert( new OrderEvent( "1",
"customer A",
70 ) );
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,
TimeUnit.SECONDS ); // 10 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,
TimeUnit.SECONDS ); // 10 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,
TimeUnit.SECONDS ); // 10 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,
TimeUnit.SECONDS ); // 10 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 {