KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption( ClockTypeOption.get( "pseudo" ) );
KnowledgeBase kbase = loadKnowledgeBaseFromString(str );
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = ( PseudoClockScheduler ) ksession.<SessionClock>getSessionClock();
DateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
Date date = df.parse( "2009-01-01T00:00:00.000-0000" );
timeService.advanceTime( date.getTime(), TimeUnit.MILLISECONDS );
final Date date1 = new Date( date.getTime() + (15 * 1000) );
final Date date2 = new Date( date1.getTime() + (60 * 1000) );
final Date date3 = new Date( date2.getTime() + (60 * 1000) );
final Date date4 = new Date( date3.getTime() + (60 * 1000) );
Calendar cal1 = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
if ( timestamp == date1.getTime() ) {
return true;
} else if ( timestamp == date4.getTime() ) {
return false;
} else {
return true;
}
}
};
Calendar cal2 = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
if ( timestamp == date2.getTime() ) {
return false;
} else if ( timestamp == date3.getTime() ) {
return true;
} else {
return true;
}
}
};
ksession.getCalendars().set( "cal1", cal1 );
ksession.getCalendars().set( "cal2", cal2 );
ksession.setGlobal( "list", list );
ksession.fireAllRules();
timeService.advanceTime( 20, TimeUnit.SECONDS );
ksession.fireAllRules();
assertEquals( 1, list.size() );
timeService.advanceTime( 60, TimeUnit.SECONDS );
ksession.fireAllRules();
assertEquals( 1, list.size() );
timeService.advanceTime( 60, TimeUnit.SECONDS );
ksession.fireAllRules();
assertEquals( 2, list.size() );
timeService.advanceTime( 60, TimeUnit.SECONDS );
ksession.fireAllRules();
assertEquals( 2, list.size() );
timeService.advanceTime( 60, TimeUnit.SECONDS );
ksession.fireAllRules();
assertEquals( 3, list.size() );
timeService.advanceTime( 60, TimeUnit.SECONDS );
ksession.fireAllRules();
assertEquals( 4, list.size() );
}