Search in sources :

Example 1 with Timer

use of org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer in project drools by kiegroup.

the class ProtobufInputMarshaller method readSession.

public static StatefulKnowledgeSessionImpl readSession(ProtobufMessages.KnowledgeSession _session, StatefulKnowledgeSessionImpl session, InternalAgenda agenda, MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    GlobalResolver globalResolver = (GlobalResolver) context.env.get(EnvironmentName.GLOBALS);
    if (globalResolver != null) {
        session.setGlobalResolver(globalResolver);
    }
    if (session.getTimerService() instanceof PseudoClockScheduler) {
        PseudoClockScheduler clock = (PseudoClockScheduler) session.getTimerService();
        clock.advanceTime(_session.getTime(), TimeUnit.MILLISECONDS);
    }
    // RuleFlowGroups need to reference the session
    // for ( InternalAgendaGroup group : agenda.getAgendaGroupsMap().values() ) {
    // ((RuleFlowGroupImpl) group).setWorkingMemory( session );
    // }
    context.wm = session;
    // need to read node memories before reading the fact handles
    // because this data is required during fact propagation
    readNodeMemories(context, _session.getRuleData());
    List<PropagationContext> pctxs = new ArrayList<PropagationContext>();
    if (_session.getRuleData().hasInitialFact()) {
        session.setInitialFactHandle(session.initInitialFact(context.kBase, context));
        context.handles.put(session.getInitialFactHandle().getId(), session.getInitialFactHandle());
    }
    for (ProtobufMessages.EntryPoint _ep : _session.getRuleData().getEntryPointList()) {
        EntryPoint wmep = ((StatefulKnowledgeSessionImpl) context.wm).getEntryPointMap().get(_ep.getEntryPointId());
        readFactHandles(context, _ep, ((WorkingMemoryEntryPoint) wmep).getObjectStore(), pctxs);
        context.filter.fireRNEAs(context.wm);
        readTruthMaintenanceSystem(context, wmep, _ep, pctxs);
    }
    cleanReaderContexts(pctxs);
    readActionQueue(context, _session.getRuleData());
    if (processMarshaller != null) {
        if (_session.hasProcessData()) {
            context.parameterObject = _session.getProcessData();
            processMarshaller.readProcessInstances(context);
            context.parameterObject = _session.getProcessData();
            processMarshaller.readWorkItems(context);
            // This actually does ALL timers, due to backwards compatability issues
            // It will read in old JBPM binaries, but always write to the new binary format.
            context.parameterObject = _session.getProcessData();
            processMarshaller.readProcessTimers(context);
        }
    } else {
        if (_session.hasProcessData()) {
            throw new IllegalStateException("No process marshaller, unable to unmarshall process data.");
        }
    }
    if (_session.hasTimers()) {
        for (ProtobufMessages.Timers.Timer _timer : _session.getTimers().getTimerList()) {
            readTimer(context, _timer);
        }
    }
    // need to process any eventual left over timer node timers
    if (!context.timerNodeSchedulers.isEmpty()) {
        for (Map<TupleKey, Scheduler> schedulers : context.timerNodeSchedulers.values()) {
            for (Scheduler scheduler : schedulers.values()) {
                scheduler.schedule(scheduler.getTrigger());
            }
        }
        context.timerNodeSchedulers.clear();
    }
    // remove the activations filter
    agenda.setActivationsFilter(null);
    return session;
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) Scheduler(org.drools.core.phreak.PhreakTimerNode.Scheduler) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) ArrayList(java.util.ArrayList) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) GlobalResolver(org.drools.core.spi.GlobalResolver) Timer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer)

Example 2 with Timer

use of org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer 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 = ((SelfRemovalJobContext) timer.getJobContext()).getJobContext();
            if (jctx instanceof ObjectTypeNode.ExpireJobContext && !((ObjectTypeNode.ExpireJobContext) jctx).getExpireAction().getFactHandle().isValid()) {
                continue;
            }
            TimersOutputMarshaller writer = outCtx.writersByClass.get(jctx.getClass());
            Timer _timer = writer.serialize(jctx, outCtx);
            if (_timer != null) {
                _timers.addTimer(_timer);
            }
        }
        return _timers.build();
    }
    return null;
}
Also used : TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) SelfRemovalJobContext(org.drools.core.time.SelfRemovalJobContext) Timer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer) JobContext(org.drools.core.time.JobContext) SelfRemovalJobContext(org.drools.core.time.SelfRemovalJobContext) Timers(org.drools.core.marshalling.impl.ProtobufMessages.Timers)

Aggregations

ArrayList (java.util.ArrayList)2 Timer (org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer)2 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 Timers (org.drools.core.marshalling.impl.ProtobufMessages.Timers)1 Scheduler (org.drools.core.phreak.PhreakTimerNode.Scheduler)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 GlobalResolver (org.drools.core.spi.GlobalResolver)1 PropagationContext (org.drools.core.spi.PropagationContext)1 JobContext (org.drools.core.time.JobContext)1 SelfRemovalJobContext (org.drools.core.time.SelfRemovalJobContext)1 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)1 TimerJobInstance (org.drools.core.time.impl.TimerJobInstance)1 EntryPoint (org.kie.api.runtime.rule.EntryPoint)1