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);
}
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;
}
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;
}
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();
}
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"));
}
Aggregations