use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class TimerAndCalendarTest method testDurationMemoryLeakonRepeatedUpdate.
@Test(timeout = 10000)
public void testDurationMemoryLeakonRepeatedUpdate() throws Exception {
String str = "";
str += "package org.drools.compiler.test\n";
str += "import org.drools.compiler.Alarm\n";
str += "global java.util.List list;";
str += "rule \"COMPTEUR\"\n";
str += " timer (int: 50s)\n";
str += " when\n";
str += " $alarm : Alarm( number < 5 )\n";
str += " then\n";
str += " $alarm.incrementNumber();\n";
str += " list.add( $alarm );\n";
str += " update($alarm);\n";
str += "end\n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Alarm());
ksession.fireAllRules();
for (int i = 0; i < 6; i++) {
timeService.advanceTime(55, TimeUnit.SECONDS);
ksession.fireAllRules();
}
assertEquals(5, list.size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class TimerAndCalendarTest method testNoProtocolIntervalTimer.
@Test(timeout = 10000)
public void testNoProtocolIntervalTimer() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " duration (30s 10s) ";
str += "when \n";
str += "then \n";
str += " list.add(\"fired\"); \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(15, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
timeService.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class TimerAndCalendarTest method testCalendarsWithIntervalsAndStartAndEnd.
@Test(timeout = 10000)
public void testCalendarsWithIntervalsAndStartAndEnd() throws Exception {
String str = "";
str += "package org.simple \n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += " calendars \"cal1\"\n";
// int: protocol is assumed
str += " timer (0d 1d; start=3-JAN-2010, end=5-JAN-2010) ";
str += "when \n";
str += "then \n";
str += " list.add(\"fired\"); \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
List list = new ArrayList();
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
Date date = df.parse("1-JAN-2010");
Calendar cal1 = new Calendar() {
public boolean isTimeIncluded(long timestamp) {
return true;
}
};
long oneDay = 60 * 60 * 24;
ksession.getCalendars().set("cal1", cal1);
ksession.setGlobal("list", list);
timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
// day 3
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
// day 5
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
timeService.advanceTime(oneDay, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class RuleTest method testTimeMachine.
@Test
public void testTimeMachine() {
SessionConfiguration conf = SessionConfiguration.newInstance();
conf.setClockType(ClockType.PSEUDO_CLOCK);
WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession(conf, null);
final Calendar future = Calendar.getInstance();
((PseudoClockScheduler) wm.getSessionClock()).setStartupTime(future.getTimeInMillis());
final RuleImpl rule = new RuleImpl("myrule");
rule.setEnabled(EnabledBoolean.ENABLED_TRUE);
assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
future.setTimeInMillis(future.getTimeInMillis() + 100000000);
rule.setDateEffective(future);
assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
((PseudoClockScheduler) wm.getSessionClock()).advanceTime(1000000000000L, TimeUnit.MILLISECONDS);
assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ProtobufOutputMarshaller method serializeSession.
private static ProtobufMessages.KnowledgeSession serializeSession(MarshallerWriteContext context) throws IOException {
StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) context.wm;
try {
wm.getLock().lock();
for (WorkingMemoryEntryPoint ep : wm.getWorkingMemoryEntryPoints().values()) {
if (ep instanceof NamedEntryPoint) {
((NamedEntryPoint) ep).lock();
}
}
evaluateRuleActivations(wm);
ProtobufMessages.RuleData.Builder _ruleData = ProtobufMessages.RuleData.newBuilder();
long time = 0;
if (context.wm.getTimerService() instanceof PseudoClockScheduler) {
time = context.clockTime;
}
_ruleData.setLastId(wm.getFactHandleFactory().getId());
_ruleData.setLastRecency(wm.getFactHandleFactory().getRecency());
InternalFactHandle handle = context.wm.getInitialFactHandle();
if (handle != null) {
// can be null for RETE, if fireAllRules has not yet been called
ProtobufMessages.FactHandle _ifh = ProtobufMessages.FactHandle.newBuilder().setType(ProtobufMessages.FactHandle.HandleType.INITIAL_FACT).setId(handle.getId()).setRecency(handle.getRecency()).build();
_ruleData.setInitialFact(_ifh);
}
writeAgenda(context, _ruleData);
writeNodeMemories(context, _ruleData);
for (EntryPoint wmep : wm.getWorkingMemoryEntryPoints().values()) {
org.drools.core.marshalling.impl.ProtobufMessages.EntryPoint.Builder _epb = ProtobufMessages.EntryPoint.newBuilder();
_epb.setEntryPointId(wmep.getEntryPointId());
writeObjectTypeConfiguration(context, ((WorkingMemoryEntryPoint) wmep).getObjectTypeConfigurationRegistry(), _epb);
writeFactHandles(context, _epb, ((NamedEntryPoint) wmep).getObjectStore());
writeTruthMaintenanceSystem(context, wmep, _epb);
_ruleData.addEntryPoint(_epb.build());
}
writeActionQueue(context, _ruleData);
ProtobufMessages.KnowledgeSession.Builder _session = ProtobufMessages.KnowledgeSession.newBuilder().setMultithread(false).setTime(time).setRuleData(_ruleData.build());
if (processMarshaller != null) {
Builder _pdata = ProtobufMessages.ProcessData.newBuilder();
if (context.marshalProcessInstances) {
context.parameterObject = _pdata;
processMarshaller.writeProcessInstances(context);
}
if (context.marshalWorkItems) {
context.parameterObject = _pdata;
processMarshaller.writeWorkItems(context);
}
// this now just assigns the writer, it will not write out any timer information
context.parameterObject = _pdata;
processMarshaller.writeProcessTimers(context);
_session.setProcessData(_pdata.build());
}
Timers _timers = writeTimers(context.wm.getTimerService().getTimerJobInstances(context.wm.getIdentifier()), context);
if (_timers != null) {
_session.setTimers(_timers);
}
return _session.build();
} finally {
for (WorkingMemoryEntryPoint ep : wm.getWorkingMemoryEntryPoints().values()) {
if (ep instanceof NamedEntryPoint) {
((NamedEntryPoint) ep).unlock();
}
}
wm.getLock().unlock();
}
}
Aggregations