KIE Internal 6.0.0.Beta1

org.kie.internal.runtime
Interface StatefulKnowledgeSession

All Superinterfaces:
org.kie.api.runtime.CommandExecutor, org.kie.api.runtime.KieRuntime, org.kie.api.event.KieRuntimeEventManager, org.kie.api.runtime.KieSession, KnowledgeRuntime, KnowledgeRuntimeEventManager, org.kie.api.event.process.ProcessEventManager, org.kie.api.runtime.process.ProcessRuntime, org.kie.api.runtime.rule.Session, org.kie.api.runtime.rule.SessionEntryPoint, org.kie.api.runtime.process.StatefulProcessSession, org.kie.api.runtime.rule.StatefulRuleSession, org.kie.api.event.rule.WorkingMemoryEventManager

public interface StatefulKnowledgeSession
extends org.kie.api.runtime.KieSession, KnowledgeRuntime, KnowledgeRuntimeEventManager

StatefulKnowledgeSession is the most common way to interact with the engine. A StatefulKnowledgeSession allows the application to establish an iterative conversation with the engine, where the state of the session is kept across invocations. The reasoning process may be triggered multiple times for the same set of data. After the application finishes using the session, though, it must call the dispose() method in order to free the resources and used memory.

Simple example showing a stateful session executing rules for a given collection of java objects.

 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
 kbuilder.add( ResourceFactory.newFileSystemResource( fileName ), ResourceType.DRL );
 assertFalse( kbuilder.hasErrors() );
 if (kbuilder.hasErrors() ) {
     System.out.println( kbuilder.getErrors() );
 }
 KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
 kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
 
 StatefulKnowledgeSession ksession = kbase.newKieSession();
 for( Object fact : facts ) {
     ksession.insert( fact );
 }
 ksession.fireAllRules();
 ksession.dispose();
 

Simple example showing a stateful session executing processes.

 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
 kbuilder.add( ResourceFactory.newFileSystemResource( fileName ), ResourceType.BPMN2 );
 KnowledgeBase kbase = kbuilder.newKnowledgeBase();
 StatefulKnowledgeSession ksession = kbase.newKieSession();
 ksession.startProcess("com.sample.processid");
 ksession.signalEvent("SomeEvent", null);
 ksession.startProcess("com.sample.processid");
 ksession.dispose();
 

StatefulKnowledgeSessions support globals. Globals are used to pass information into the engine (like data or service callbacks that can be used in your rules and processes), but they should not be used to reason over. If you need to reason over your data, make sure you insert it as a fact, not a global.

Globals are shared among ALL your rules and processes, so be especially careful of (and avoid as much as possible) mutable globals. Also, it is a good practice to set your globals before inserting your facts or starting your processes. Rules engines evaluate rules at fact insertion time, and so, if you are using a global to constraint a fact pattern, and the global is not set, you may receive a NullPointerException.

Globals can be resolved in two ways. The StatefulKnowledgeSession supports getGlobals() which returns the internal Globals, which itself can take a delegate. Calling of setGlobal(String, Object) will set the global on an internal Collection. Identifiers in this internal Collection will have priority over the externally supplied Globals delegate. If an identifier cannot be found in the internal Collection, it will then check the externally supplied Globals delegate, if one has been set.

Code snippet for setting a global:

 StatefulKnowledgeSession ksession = kbase.newKieSession();
 ksession.setGlobal( "hbnSession", hibernateSession ); // sets a global hibernate session, that can be used for DB interactions in the rules.
 for( Object fact : facts ) {
     ksession.insert( fact );
 }
 ksession.fireAllRules(); // this will now execute and will be able to resolve the "hbnSession" identifier.
 ksession.dispose();
 

Like StatelessKnowledgeSession this also implements CommandExecutor which can be used to script a StatefulKnowledgeSession. See CommandExecutor for more details.

See Also:
Globals

Method Summary
 KnowledgeBase getKieBase()
           
 
Methods inherited from interface org.kie.api.runtime.KieSession
destroy, dispose, getId
 
Methods inherited from interface org.kie.api.runtime.rule.StatefulRuleSession
fireAllRules, fireAllRules, fireAllRules, fireAllRules, fireUntilHalt, fireUntilHalt
 
Methods inherited from interface org.kie.api.runtime.CommandExecutor
execute
 
Methods inherited from interface org.kie.api.runtime.KieRuntime
getCalendars, getChannels, getEnvironment, getGlobal, getGlobals, getSessionClock, getSessionConfiguration, registerChannel, setGlobal, unregisterChannel
 
Methods inherited from interface org.kie.api.runtime.rule.Session
getAgenda, getEntryPoint, getEntryPoints, getQueryResults, halt, openLiveQuery
 
Methods inherited from interface org.kie.api.runtime.rule.SessionEntryPoint
delete, getEntryPointId, getFactCount, getFactHandle, getFactHandles, getFactHandles, getObject, getObjects, getObjects, insert, retract, update
 
Methods inherited from interface org.kie.api.runtime.process.ProcessRuntime
abortProcessInstance, createProcessInstance, getProcessInstance, getProcessInstance, getProcessInstances, getWorkItemManager, signalEvent, signalEvent, startProcess, startProcess, startProcessInstance
 
Methods inherited from interface org.kie.api.event.rule.WorkingMemoryEventManager
addEventListener, addEventListener, getAgendaEventListeners, getWorkingMemoryEventListeners, removeEventListener, removeEventListener
 
Methods inherited from interface org.kie.api.event.process.ProcessEventManager
addEventListener, getProcessEventListeners, removeEventListener
 

Method Detail

getKieBase

KnowledgeBase getKieBase()
Specified by:
getKieBase in interface org.kie.api.runtime.KieRuntime

KIE Internal 6.0.0.Beta1

Copyright © 2001-2013 JBoss by Red Hat. All Rights Reserved.