Search in sources :

Example 1 with TupleKey

use of org.drools.core.marshalling.TupleKey in project drools by kiegroup.

the class ProtobufInputMarshaller method readSession.

public static StatefulKnowledgeSessionImpl readSession(ProtobufMessages.KnowledgeSession _session, StatefulKnowledgeSessionImpl session, InternalAgenda agenda, ProtobufMarshallerReaderContext 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);
    }
    context.setWorkingMemory(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));
        context.getHandles().put(session.getInitialFactHandle().getId(), session.getInitialFactHandle());
    }
    for (ProtobufMessages.EntryPoint _ep : _session.getRuleData().getEntryPointList()) {
        EntryPoint wmep = context.getWorkingMemory().getEntryPoint(_ep.getEntryPointId());
        readFactHandles(context, _ep, ((WorkingMemoryEntryPoint) wmep).getObjectStore(), pctxs);
        context.getWorkingMemory().getFactHandleFactory().doRecycleIds(context.getHandles().keySet());
        context.getFilter().fireRNEAs(context.getWorkingMemory());
        readTruthMaintenanceSystem(context, wmep, _ep, pctxs);
        context.getWorkingMemory().getFactHandleFactory().stopRecycleIds();
    }
    cleanReaderContexts(pctxs);
    readActionQueue(context, _session.getRuleData());
    if (processMarshaller != null) {
        if (_session.hasProcessData()) {
            context.setParameterObject(_session.getProcessData());
            processMarshaller.readProcessInstances(context);
            context.setParameterObject(_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.setParameterObject(_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.kiesession.entrypoints.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) TupleKey(org.drools.core.marshalling.TupleKey) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) GlobalResolver(org.drools.core.spi.GlobalResolver) Timer(org.drools.serialization.protobuf.ProtobufMessages.Timers.Timer)

Example 2 with TupleKey

use of org.drools.core.marshalling.TupleKey in project drools by kiegroup.

the class ProtobufMarshallerReaderContext method createQueryResultHandle.

@Override
public QueryElementFactHandle createQueryResultHandle(Tuple leftTuple, Object[] objects, int nodeId) {
    ProtobufMessages.FactHandle handle = null;
    Map<TupleKey, ProtobufInputMarshaller.QueryElementContext> map = (Map<TupleKey, ProtobufInputMarshaller.QueryElementContext>) getNodeMemories().get(nodeId);
    if (map != null) {
        ProtobufInputMarshaller.QueryElementContext queryElementContext = map.get(TupleKey.createTupleKey(leftTuple));
        if (queryElementContext != null) {
            handle = queryElementContext.results.removeFirst();
        }
    }
    return handle != null ? new QueryElementFactHandle(objects, handle.getId(), handle.getRecency()) : null;
}
Also used : QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) TupleKey(org.drools.core.marshalling.TupleKey) RightTupleKey(org.drools.serialization.protobuf.marshalling.RightTupleKey) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TupleKey

use of org.drools.core.marshalling.TupleKey in project drools by kiegroup.

the class ProtobufMarshallerReaderContext method createAsyncNodeHandle.

@Override
public InternalFactHandle createAsyncNodeHandle(Tuple leftTuple, ReteEvaluator reteEvaluator, Object object, int nodeId, ObjectTypeConf objectTypeConf) {
    ProtobufMessages.FactHandle _handle = null;
    Map<TupleKey, List<ProtobufMessages.FactHandle>> map = (Map<TupleKey, List<ProtobufMessages.FactHandle>>) getNodeMemories().get(nodeId);
    if (map != null) {
        TupleKey key = TupleKey.createTupleKey(leftTuple);
        List<ProtobufMessages.FactHandle> list = map.get(key);
        if (list != null && !list.isEmpty()) {
            // it is a linked list, so the operation is fairly efficient
            _handle = ((java.util.LinkedList<ProtobufMessages.FactHandle>) list).removeFirst();
            if (list.isEmpty()) {
                map.remove(key);
            }
        }
    }
    InternalFactHandle handle = null;
    if (_handle != null) {
        // create a handle with the given id
        handle = reteEvaluator.getFactHandleFactory().newFactHandle(_handle.getId(), object, _handle.getRecency(), objectTypeConf, reteEvaluator, null);
    }
    return handle;
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) List(java.util.List) TupleKey(org.drools.core.marshalling.TupleKey) RightTupleKey(org.drools.serialization.protobuf.marshalling.RightTupleKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with TupleKey

use of org.drools.core.marshalling.TupleKey in project drools by kiegroup.

the class ProtobufMarshallerReaderContext method createAccumulateHandle.

@Override
public InternalFactHandle createAccumulateHandle(EntryPointId entryPointId, ReteEvaluator reteEvaluator, LeftTuple leftTuple, Object result, int nodeId) {
    InternalFactHandle handle = null;
    ProtobufMessages.FactHandle _handle = null;
    Map<TupleKey, ProtobufMessages.FactHandle> map = (Map<TupleKey, ProtobufMessages.FactHandle>) getNodeMemories().get(nodeId);
    if (map != null) {
        _handle = map.get(TupleKey.createTupleKey(leftTuple));
    }
    if (_handle != null) {
        // create a handle with the given id
        handle = reteEvaluator.getFactHandleFactory().newFactHandle(_handle.getId(), result, _handle.getRecency(), reteEvaluator.getDefaultEntryPoint().getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(entryPointId, result), reteEvaluator, // so far, result is not an event
        null);
    }
    return handle;
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle) TupleKey(org.drools.core.marshalling.TupleKey) RightTupleKey(org.drools.serialization.protobuf.marshalling.RightTupleKey) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with TupleKey

use of org.drools.core.marshalling.TupleKey in project drools by kiegroup.

the class PhreakTimerNode method scheduleLeftTuple.

private void scheduleLeftTuple(final TimerNode timerNode, final TimerNodeMemory tm, final PathMemory pmem, final SegmentMemory smem, final LeftTupleSink sink, final ActivationsManager activationsManager, final Timer timer, final TimerService timerService, final long timestamp, final String[] calendarNames, final Calendars calendars, final LeftTuple leftTuple, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
    ReteEvaluator reteEvaluator = activationsManager.getReteEvaluator();
    if (leftTuple.getPropagationContext().getReaderContext() == null) {
        final Trigger trigger = createTrigger(timerNode, reteEvaluator, timer, timestamp, calendarNames, calendars, leftTuple);
        // regular propagation
        scheduleTimer(timerNode, tm, smem, sink, reteEvaluator, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, trigger);
    } else {
        // de-serializing, so we need to correlate timers before scheduling them
        Scheduler scheduler = new Scheduler() {

            @Override
            public void schedule(Trigger t) {
                scheduleTimer(timerNode, tm, smem, sink, reteEvaluator, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, t);
                evaluate(pmem, activationsManager, sink, tm, trgLeftTuples);
            }

            @Override
            public Trigger getTrigger() {
                return createTrigger(timerNode, reteEvaluator, timer, timestamp, calendarNames, calendars, leftTuple);
            }
        };
        TupleKey key = TupleKey.createTupleKey(leftTuple);
        leftTuple.getPropagationContext().getReaderContext().addTimerNodeScheduler(timerNode.getId(), key, scheduler);
        leftTuple.setContextObject(key);
    }
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Trigger(org.drools.core.time.Trigger) TupleKey(org.drools.core.marshalling.TupleKey)

Aggregations

TupleKey (org.drools.core.marshalling.TupleKey)7 HashMap (java.util.HashMap)3 Map (java.util.Map)3 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)3 RightTupleKey (org.drools.serialization.protobuf.marshalling.RightTupleKey)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 PropagationContext (org.drools.core.spi.PropagationContext)2 Trigger (org.drools.core.time.Trigger)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)1 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 PhreakTimerNode (org.drools.core.phreak.PhreakTimerNode)1 Scheduler (org.drools.core.phreak.PhreakTimerNode.Scheduler)1 LeftTuple (org.drools.core.reteoo.LeftTuple)1 GlobalResolver (org.drools.core.spi.GlobalResolver)1 TimerService (org.drools.core.time.TimerService)1 DefaultJobHandle (org.drools.core.time.impl.DefaultJobHandle)1 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)1 TupleList (org.drools.core.util.index.TupleList)1