Search in sources :

Example 1 with Timers

use of org.drools.core.marshalling.impl.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.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();
    }
}
Also used : NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Builder(org.drools.core.marshalling.impl.ProtobufMessages.ProcessData.Builder) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) Timers(org.drools.core.marshalling.impl.ProtobufMessages.Timers)

Example 2 with Timers

use of org.drools.core.marshalling.impl.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 = ((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

Timers (org.drools.core.marshalling.impl.ProtobufMessages.Timers)2 ArrayList (java.util.ArrayList)1 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)1 FactHandle (org.drools.core.marshalling.impl.ProtobufMessages.FactHandle)1 Builder (org.drools.core.marshalling.impl.ProtobufMessages.ProcessData.Builder)1 Timer (org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)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