Search in sources :

Example 1 with Globals

use of org.kie.api.runtime.Globals 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 Globals

use of org.kie.api.runtime.Globals in project drools by kiegroup.

the class StatelessKnowledgeSessionImpl method newWorkingMemory.

public StatefulKnowledgeSession newWorkingMemory() {
    this.kBase.readLock();
    try {
        StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) wmFactory.createWorkingMemory(this.kBase.nextWorkingMemoryCounter(), this.kBase, (SessionConfiguration) this.conf, this.environment);
        StatefulKnowledgeSessionImpl ksessionImpl = (StatefulKnowledgeSessionImpl) ksession;
        ((Globals) ksessionImpl.getGlobalResolver()).setDelegate(this.sessionGlobals);
        registerListeners(ksessionImpl);
        for (Map.Entry<String, Channel> entry : this.channels.entrySet()) {
            ksession.registerChannel(entry.getKey(), entry.getValue());
        }
        wmCreated.incrementAndGet();
        return ksession;
    } finally {
        this.kBase.readUnlock();
    }
}
Also used : Globals(org.kie.api.runtime.Globals) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Channel(org.kie.api.runtime.Channel) HashMap(java.util.HashMap) Map(java.util.Map) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 3 with Globals

use of org.kie.api.runtime.Globals in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeSameSessionRemovingGlobal.

@Test
public void testKJarUpgradeSameSessionRemovingGlobal() throws Exception {
    // DROOLS-752
    String drl1 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "global java.lang.String bar\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "global java.lang.String baz\n" + "rule R2 when\n" + "   $m : Message( )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.setGlobal("foo", "foo");
    ksession.setGlobal("bar", "bar");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl2);
    kc.updateToVersion(releaseId2);
    ksession.setGlobal("baz", "baz");
    Globals globals = ksession.getGlobals();
    assertEquals(2, globals.getGlobalKeys().size());
    assertEquals("foo", ksession.getGlobal("foo"));
    assertNull(ksession.getGlobal("bar"));
    assertEquals("baz", ksession.getGlobal("baz"));
}
Also used : Globals(org.kie.api.runtime.Globals) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 4 with Globals

use of org.kie.api.runtime.Globals in project drools by kiegroup.

the class RuleUnitExecutorSession method bindRuleUnit.

private RuleUnitDescr bindRuleUnit(RuleUnit ruleUnit) {
    suspended.set(false);
    currentRuleUnit = ruleUnit;
    currentRuleUnit.onStart();
    factHandlesMap.computeIfAbsent(ruleUnit.getClass(), x -> session.getEntryPoint(RULE_UNIT_ENTRY_POINT).insert(ruleUnit));
    RuleUnitDescr ruDescr = session.kBase.getRuleUnitRegistry().getRuleUnitDescr(ruleUnit);
    ruDescr.bindDataSources(session, ruleUnit);
    ((Globals) session.getGlobalResolver()).setDelegate(new RuleUnitGlobals(ruDescr, ruleUnit));
    InternalAgendaGroup unitGroup = (InternalAgendaGroup) session.getAgenda().getAgendaGroup(ruleUnit.getClass().getName());
    unitGroup.setAutoDeactivate(false);
    unitGroup.setFocus();
    return ruDescr;
}
Also used : Globals(org.kie.api.runtime.Globals) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr)

Example 5 with Globals

use of org.kie.api.runtime.Globals in project drools by kiegroup.

the class MarshallingTest method marsallStatefulKnowledgeSession.

private KieSession marsallStatefulKnowledgeSession(KieSession ksession) throws IOException, ClassNotFoundException {
    Globals globals = ksession.getGlobals();
    KieBase kbase = ksession.getKieBase();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    MarshallerFactory.newMarshaller(kbase).marshall(out, ksession);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    Environment env = EnvironmentFactory.newEnvironment();
    env.set(EnvironmentName.GLOBALS, globals);
    ksession = MarshallerFactory.newMarshaller(kbase).unmarshall(new ByteArrayInputStream(out.toByteArray()), ksconf, env);
    return ksession;
}
Also used : Globals(org.kie.api.runtime.Globals) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

Aggregations

Globals (org.kie.api.runtime.Globals)5 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1 SessionConfiguration (org.drools.core.SessionConfiguration)1 MapGlobalResolver (org.drools.core.base.MapGlobalResolver)1 InternalAgendaGroup (org.drools.core.common.InternalAgendaGroup)1 RuleUnitDescr (org.drools.core.ruleunit.RuleUnitDescr)1 GlobalResolver (org.drools.core.spi.GlobalResolver)1 Test (org.junit.Test)1 KieBase (org.kie.api.KieBase)1 KieServices (org.kie.api.KieServices)1 KieModule (org.kie.api.builder.KieModule)1 ReleaseId (org.kie.api.builder.ReleaseId)1 KieBaseEventListener (org.kie.api.event.kiebase.KieBaseEventListener)1