Search in sources :

Example 1 with GlobalResolver

use of org.drools.core.spi.GlobalResolver in project drools by kiegroup.

the class StatefulKnowledgeSessionImpl method init.

private void init(SessionConfiguration config, Environment environment, long propagationContext) {
    this.config = config;
    this.environment = environment;
    this.propagationIdCounter = new AtomicLong(propagationContext);
    Globals globals = (Globals) this.environment.get(EnvironmentName.GLOBALS);
    if (globals != null) {
        if (!(globals instanceof GlobalResolver)) {
            this.globalResolver = new GlobalsAdapter(globals);
        } else {
            this.globalResolver = (GlobalResolver) globals;
        }
    } else {
        this.globalResolver = new MapGlobalResolver();
    }
    this.kieBaseEventListeners = new LinkedList<KieBaseEventListener>();
    this.lock = new ReentrantLock();
    this.timerService = TimerServiceFactory.getTimerService(this.config);
    this.opCounter = new AtomicLong(0);
    this.lastIdleTimestamp = new AtomicLong(-1);
}
Also used : Globals(org.kie.api.runtime.Globals) ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicLong(java.util.concurrent.atomic.AtomicLong) KieBaseEventListener(org.kie.api.event.kiebase.KieBaseEventListener) MapGlobalResolver(org.drools.core.base.MapGlobalResolver) GlobalResolver(org.drools.core.spi.GlobalResolver) MapGlobalResolver(org.drools.core.base.MapGlobalResolver)

Example 2 with GlobalResolver

use of org.drools.core.spi.GlobalResolver 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 3 with GlobalResolver

use of org.drools.core.spi.GlobalResolver in project drools by kiegroup.

the class ReteooWorkingMemoryTest method testGlobalResolver.

@Test
public void testGlobalResolver() {
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("global1", "value1");
    map.put("global2", "value2");
    final GlobalResolver resolver = new MapGlobalResolver(map);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    ksession.setGlobalResolver(resolver);
    assertEquals("value1", ksession.getGlobal("global1"));
    assertEquals("value2", ksession.getGlobal("global2"));
}
Also used : HashMap(java.util.HashMap) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) GlobalResolver(org.drools.core.spi.GlobalResolver) MapGlobalResolver(org.drools.core.base.MapGlobalResolver) MapGlobalResolver(org.drools.core.base.MapGlobalResolver) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

GlobalResolver (org.drools.core.spi.GlobalResolver)3 MapGlobalResolver (org.drools.core.base.MapGlobalResolver)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)1 Timer (org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer)1 Scheduler (org.drools.core.phreak.PhreakTimerNode.Scheduler)1 PropagationContext (org.drools.core.spi.PropagationContext)1 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)1 Test (org.junit.Test)1 KieBaseEventListener (org.kie.api.event.kiebase.KieBaseEventListener)1 Globals (org.kie.api.runtime.Globals)1 EntryPoint (org.kie.api.runtime.rule.EntryPoint)1