Search in sources :

Example 1 with MapGlobalResolver

use of org.drools.core.base.MapGlobalResolver 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 MapGlobalResolver

use of org.drools.core.base.MapGlobalResolver in project drools by kiegroup.

the class TransactionTestCommand method initializeEnvironment.

private Environment initializeEnvironment() {
    Environment env = EnvironmentFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(EnvironmentName.GLOBALS, new MapGlobalResolver());
    env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManager.transactionManager());
    return env;
}
Also used : Environment(org.kie.api.runtime.Environment) MapGlobalResolver(org.drools.core.base.MapGlobalResolver)

Example 3 with MapGlobalResolver

use of org.drools.core.base.MapGlobalResolver in project drools by kiegroup.

the class DroolsPersistenceUtil method createEnvironment.

public static Environment createEnvironment(Map<String, Object> context) {
    Environment env = EnvironmentFactory.newEnvironment();
    UserTransaction ut = (UserTransaction) context.get(TRANSACTION);
    if (ut != null) {
        env.set(TRANSACTION, ut);
    }
    env.set(ENTITY_MANAGER_FACTORY, context.get(ENTITY_MANAGER_FACTORY));
    env.set(GLOBALS, new MapGlobalResolver());
    return env;
}
Also used : UserTransaction(javax.transaction.UserTransaction) Environment(org.kie.api.runtime.Environment) MapGlobalResolver(org.drools.core.base.MapGlobalResolver)

Example 4 with MapGlobalResolver

use of org.drools.core.base.MapGlobalResolver in project drools by kiegroup.

the class GlobalTest method testGlobalAccess.

@Test
public void testGlobalAccess() {
    final String drl = "import org.drools.core.base.MapGlobalResolver;\n" + "global java.lang.String myGlobal;\n" + "global String unused; \n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession session1 = kbase.newKieSession();
    final String sample = "default string";
    // Testing 1.
    System.out.println("Start testing 1.");
    session1.setGlobal("myGlobal", "Testing 1");
    session1.insert(sample);
    session1.fireAllRules();
    final Map.Entry[] entries1 = ((MapGlobalResolver) session1.getGlobals()).getGlobals();
    assertEquals(1, entries1.length);
    assertEquals(entries1[0].getValue(), "Testing 1");
    assertEquals(1, session1.getGlobals().getGlobalKeys().size());
    assertTrue(session1.getGlobals().getGlobalKeys().contains("myGlobal"));
    session1.dispose();
    // Testing 2.
    System.out.println("Start testing 2.");
    final StatelessKieSession session2 = session1.getKieBase().newStatelessKieSession();
    session2.setGlobal("myGlobal", "Testing 2");
    session2.execute(sample);
    final Map.Entry[] entries2 = ((MapGlobalResolver) session2.getGlobals()).getGlobals();
    assertEquals(1, entries2.length);
    assertEquals(entries2[0].getValue(), "Testing 2");
    assertEquals(1, session2.getGlobals().getGlobalKeys().size());
    assertTrue(session2.getGlobals().getGlobalKeys().contains("myGlobal"));
    // Testing 3.
    System.out.println("Start testing 3.");
    final StatefulKnowledgeSession session3 = ((StatelessKnowledgeSessionImpl) session2).newWorkingMemory();
    session3.insert(sample);
    session3.fireAllRules();
    Map.Entry[] entries3 = ((MapGlobalResolver) session3.getGlobals()).getGlobals();
    assertEquals(1, entries3.length);
    assertEquals(entries3[0].getValue(), "Testing 2");
    assertEquals(1, session3.getGlobals().getGlobalKeys().size());
    assertTrue(session3.getGlobals().getGlobalKeys().contains("myGlobal"));
    session3.setGlobal("myGlobal", "Testing 3 Over");
    entries3 = ((MapGlobalResolver) session3.getGlobals()).getGlobals();
    assertEquals(1, entries3.length);
    assertEquals(entries3[0].getValue(), "Testing 3 Over");
    assertEquals(1, session3.getGlobals().getGlobalKeys().size());
    assertTrue(session3.getGlobals().getGlobalKeys().contains("myGlobal"));
    session3.dispose();
    // Testing 4.
    System.out.println("Start testing 4.");
    final StatefulKnowledgeSession session4 = ((StatelessKnowledgeSessionImpl) session2).newWorkingMemory();
    session4.setGlobal("myGlobal", "Testing 4");
    session4.insert(sample);
    session4.fireAllRules();
    final Map.Entry[] entries4 = ((MapGlobalResolver) session4.getGlobals()).getGlobals();
    assertEquals(1, entries4.length);
    assertEquals(entries4[0].getValue(), "Testing 4");
    assertEquals(1, session4.getGlobals().getGlobalKeys().size());
    assertTrue(session4.getGlobals().getGlobalKeys().contains("myGlobal"));
    session4.dispose();
}
Also used : StatelessKnowledgeSessionImpl(org.drools.core.impl.StatelessKnowledgeSessionImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) MapGlobalResolver(org.drools.core.base.MapGlobalResolver) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 5 with MapGlobalResolver

use of org.drools.core.base.MapGlobalResolver 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

MapGlobalResolver (org.drools.core.base.MapGlobalResolver)5 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 GlobalResolver (org.drools.core.spi.GlobalResolver)2 Test (org.junit.Test)2 Environment (org.kie.api.runtime.Environment)2 HashMap (java.util.HashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 UserTransaction (javax.transaction.UserTransaction)1 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)1 StatelessKnowledgeSessionImpl (org.drools.core.impl.StatelessKnowledgeSessionImpl)1 KieBaseEventListener (org.kie.api.event.kiebase.KieBaseEventListener)1 Globals (org.kie.api.runtime.Globals)1 KieSession (org.kie.api.runtime.KieSession)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)1