use of org.drools.serialization.protobuf.ProtobufMessages.Timers in project drools by kiegroup.
the class ProtobufOutputMarshaller method serializeSession.
private static ProtobufMessages.KnowledgeSession serializeSession(MarshallerWriteContext context) throws IOException {
StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) context.getWorkingMemory();
try {
wm.getLock().lock();
for (EntryPoint ep : wm.getEntryPoints()) {
if (ep instanceof NamedEntryPoint) {
((NamedEntryPoint) ep).lock();
}
}
evaluateRuleActivations(wm);
ProtobufMessages.RuleData.Builder _ruleData = ProtobufMessages.RuleData.newBuilder();
long time = 0;
if (context.getWorkingMemory().getTimerService() instanceof PseudoClockScheduler) {
time = context.getClockTime();
}
_ruleData.setLastId(wm.getFactHandleFactory().getId());
_ruleData.setLastRecency(wm.getFactHandleFactory().getRecency());
InternalFactHandle handle = context.getWorkingMemory().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.getEntryPoints()) {
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.isMarshalProcessInstances()) {
context.setParameterObject(_pdata);
processMarshaller.writeProcessInstances(context);
}
if (context.isMarshalWorkItems()) {
context.setParameterObject(_pdata);
processMarshaller.writeWorkItems(context);
}
// this now just assigns the writer, it will not write out any timer information
context.setParameterObject(_pdata);
processMarshaller.writeProcessTimers(context);
_session.setProcessData(_pdata.build());
}
Timers _timers = writeTimers(context.getWorkingMemory().getTimerJobInstances(context.getWorkingMemory().getIdentifier()), context);
if (_timers != null) {
_session.setTimers(_timers);
}
return _session.build();
} finally {
for (EntryPoint ep : wm.getEntryPoints()) {
if (ep instanceof NamedEntryPoint) {
((NamedEntryPoint) ep).unlock();
}
}
wm.getLock().unlock();
}
}
use of org.drools.serialization.protobuf.ProtobufMessages.Timers in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeTimers.
private static ProtobufMessages.Timers writeTimers(Collection<TimerJobInstance> timers, MarshallerWriteContext outCtx) {
if (!timers.isEmpty()) {
List<TimerJobInstance> sortedTimers = new ArrayList<TimerJobInstance>(timers);
Collections.sort(sortedTimers, new Comparator<TimerJobInstance>() {
public int compare(TimerJobInstance o1, TimerJobInstance o2) {
return (int) (o1.getJobHandle().getId() - o2.getJobHandle().getId());
}
});
ProtobufMessages.Timers.Builder _timers = ProtobufMessages.Timers.newBuilder();
for (TimerJobInstance timer : sortedTimers) {
JobContext jctx = timer.getJobContext();
if (jctx instanceof SelfRemovalJobContext) {
jctx = ((SelfRemovalJobContext) jctx).getJobContext();
}
if (jctx instanceof ObjectTypeNode.ExpireJobContext && !((ObjectTypeNode.ExpireJobContext) jctx).getExpireAction().getFactHandle().isValid()) {
continue;
}
TimersOutputMarshaller writer = (TimersOutputMarshaller) outCtx.getWriterForClass(jctx.getClass());
Timer _timer = writer.serialize(jctx, outCtx);
if (_timer != null) {
_timers.addTimer(_timer);
}
}
return _timers.build();
}
return null;
}
Aggregations