Search in sources :

Example 11 with WorkingMemoryEntryPoint

use of org.drools.core.WorkingMemoryEntryPoint 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 12 with WorkingMemoryEntryPoint

use of org.drools.core.WorkingMemoryEntryPoint in project drools by kiegroup.

the class EventListDataStream method insertAndAdvanceClock.

private void insertAndAdvanceClock(T t, DataProcessor subscriber) {
    EventFactHandle fh = (EventFactHandle) subscriber.insert(null, t);
    long timestamp = fh.getStartTimestamp();
    WorkingMemoryEntryPoint ep = fh.getEntryPoint(null);
    SessionPseudoClock clock = (SessionPseudoClock) ep.getReteEvaluator().getSessionClock();
    long advanceTime = timestamp - clock.getCurrentTime();
    if (advanceTime > 0) {
        clock.advanceTime(advanceTime, TimeUnit.MILLISECONDS);
    } else if (advanceTime < 0) {
        LOGGER.warn("Received an event with a timestamp that is " + (-advanceTime) + " milliseconds in the past. " + "Evaluation of out of order events could lead to unpredictable results.");
    }
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) EventFactHandle(org.drools.core.common.EventFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint)

Example 13 with WorkingMemoryEntryPoint

use of org.drools.core.WorkingMemoryEntryPoint in project drools by kiegroup.

the class RuleUnitExecutorImpl method initInitialFact.

private void initInitialFact(RuleBase kBase) {
    WorkingMemoryEntryPoint defaultEntryPoint = entryPointsManager.getDefaultEntryPoint();
    InternalFactHandle handle = getFactHandleFactory().newInitialFactHandle(defaultEntryPoint);
    ObjectTypeNode otn = defaultEntryPoint.getEntryPointNode().getObjectTypeNodes().get(InitialFact_ObjectType);
    if (otn != null) {
        PropagationContextFactory ctxFact = RuntimeComponentFactory.get().getPropagationContextFactory();
        PropagationContext pctx = ctxFact.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, handle, defaultEntryPoint.getEntryPoint(), null);
        otn.assertInitialFact(handle, pctx, this);
    }
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PhreakPropagationContext(org.drools.core.common.PhreakPropagationContext) PropagationContext(org.drools.core.spi.PropagationContext) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalFactHandle(org.drools.core.common.InternalFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint)

Example 14 with WorkingMemoryEntryPoint

use of org.drools.core.WorkingMemoryEntryPoint in project drools by kiegroup.

the class FactHandleMarshallingTest method createEventFactHandle.

private InternalFactHandle createEventFactHandle(StatefulKnowledgeSessionImpl wm, InternalKnowledgeBase kBase) {
    // EntryPointNode
    Rete rete = kBase.getRete();
    NodeFactory nFacotry = CoreComponentFactory.get().getNodeFactoryService();
    RuleBasePartitionId partionId = RuleBasePartitionId.MAIN_PARTITION;
    EntryPointNode entryPointNode = nFacotry.buildEntryPointNode(1, partionId, false, rete, EntryPointId.DEFAULT);
    WorkingMemoryEntryPoint wmEntryPoint = new NamedEntryPoint(EntryPointId.DEFAULT, entryPointNode, wm);
    EventFactHandle factHandle = new EventFactHandle(1, new Person(), 0, (new Date()).getTime(), 0, wmEntryPoint);
    return factHandle;
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) Rete(org.drools.core.reteoo.Rete) NodeFactory(org.drools.core.reteoo.builder.NodeFactory) RuleBasePartitionId(org.drools.core.common.RuleBasePartitionId) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) EventFactHandle(org.drools.core.common.EventFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) Person(org.drools.mvel.compiler.Person) Date(java.util.Date)

Example 15 with WorkingMemoryEntryPoint

use of org.drools.core.WorkingMemoryEntryPoint in project drools by kiegroup.

the class StatefulKnowledgeSessionImpl method reset.

public void reset(long handleId, long handleCounter, long propagationCounter) {
    if (nodeMemories != null) {
        nodeMemories.clear();
    }
    this.agenda.clear();
    for (WorkingMemoryEntryPoint ep : this.entryPointsManager.getEntryPoints()) {
        // clear the state for each entry point
        ep.reset();
    }
    this.handleFactory.clear(handleId, handleCounter);
    this.propagationIdCounter = new AtomicLong(propagationCounter);
    this.lastIdleTimestamp.set(-1);
// TODO should these be cleared?
// we probably neeed to do CEP and Flow timers too
// this.processInstanceManager.clear()
// this.workItemManager.clear();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint)

Aggregations

WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)31 InternalFactHandle (org.drools.core.common.InternalFactHandle)14 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)8 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)6 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)6 PropagationContext (org.drools.core.spi.PropagationContext)6 EventFactHandle (org.drools.core.common.EventFactHandle)5 EntryPointId (org.drools.core.rule.EntryPointId)5 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)5 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)4 EntryPointNode (org.drools.core.reteoo.EntryPointNode)4 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 EqualityKey (org.drools.core.common.EqualityKey)2 ObjectStore (org.drools.core.common.ObjectStore)2