Interface KieSession
- All Superinterfaces:
AutoCloseable,CommandExecutor,EntryPoint,KieRuntime,KieRuntimeEventManager,ProcessEventManager,ProcessRuntime,RuleRuntime,RuleRuntimeEventManager,RuntimeSession,StatefulProcessSession,StatefulRuleSession
dispose() method in order to free the resources and used memory.
Simple example showing a KieSession executing rules for a given collection of java objects.
KieServices kieServices = KieServices.Factory.get();
KieContainer kContainer = kieServices.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession();
for( Object fact : facts ) {
kSession.insert( fact );
}
kSession.fireAllRules();
kSession.dispose();
Simple example showing a stateful session executing processes.
KieSession kSession = kbase.newKieSession();
kSession.startProcess("com.sample.processid");
kSession.signalEvent("SomeEvent", null);
kSession.startProcess("com.sample.processid");
kSession.dispose();
KieSession 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 KieSession 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:
KieSession 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 StatelessKieSession this also implements CommandExecutor which can be used to script a KieSession. See CommandExecutor for more details.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceAn action that will be executed atomically on this session. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidclose()Disposes the KieSession when used as AutoClosable.voiddestroy()Destroys session permanently.voiddispose()Releases all the current session resources, setting up the session for garbage collection.intgetId()Deprecated.long<T> TgetKieRuntime(Class<T> cls) Returns a runtime for the given interface.voidsubmit(KieSession.AtomicAction action) Submit an action that will be executed atomically on this session.Methods inherited from interface org.kie.api.runtime.CommandExecutor
executeMethods inherited from interface org.kie.api.runtime.rule.EntryPoint
delete, delete, getEntryPointId, getFactCount, getFactHandle, getFactHandles, getFactHandles, getInstancesOf, getObject, getObjects, getObjects, getSingleInstanceOf, insert, retract, update, updateMethods inherited from interface org.kie.api.runtime.KieRuntime
getCalendars, getEnvironment, getGlobal, getSessionClock, getSessionConfigurationMethods inherited from interface org.kie.api.event.KieRuntimeEventManager
getLoggerMethods inherited from interface org.kie.api.event.process.ProcessEventManager
addEventListener, getProcessEventListeners, removeEventListenerMethods inherited from interface org.kie.api.runtime.process.ProcessRuntime
abortProcessInstance, createProcessInstance, getProcessInstance, getProcessInstance, getProcessInstances, getWorkItemManager, signalEvent, signalEvent, startProcess, startProcess, startProcess, startProcess, startProcessFromNodeIds, startProcessInstanceMethods inherited from interface org.kie.api.runtime.rule.RuleRuntime
getAgenda, getEntryPoint, getEntryPoints, getQueryResults, halt, openLiveQueryMethods inherited from interface org.kie.api.event.rule.RuleRuntimeEventManager
addEventListener, addEventListener, getAgendaEventListeners, getRuleRuntimeEventListeners, removeEventListener, removeEventListenerMethods inherited from interface org.kie.api.runtime.RuntimeSession
getChannels, getGlobals, getKieBase, registerChannel, setGlobal, unregisterChannelMethods inherited from interface org.kie.api.runtime.rule.StatefulRuleSession
fireAllRules, fireAllRules, fireAllRules, fireAllRules, fireUntilHalt, fireUntilHalt
-
Method Details
-
getId
Deprecated.Deprecated. usegetIdentifier()instead- Returns:
- id of this session
-
getIdentifier
long getIdentifier() -
dispose
void dispose()Releases all the current session resources, setting up the session for garbage collection. This method must always be called after finishing using the session, or the engine will not free the memory used by the session. If a logger has been registered on this session it will be automatically closed. -
close
default void close()Disposes the KieSession when used as AutoClosable. Wrapper method that callsdispose(). To see more details, please see documentation on the methoddispose().- Specified by:
closein interfaceAutoCloseable
-
destroy
void destroy()Destroys session permanently. In case of session state being persisted in data store it will be removed from it otherwise it falls back to default dispose() method. NOTE: Name and location of this method will most likely change before 6.0.Final as it applies only to persistent sessions -
submit
Submit an action that will be executed atomically on this session. This is useful when using fireUntilHalt to avoid evaluations against partially modified objects. -
getKieRuntime
Returns a runtime for the given interface. This method is used to retrieve runtime extensions to the engine. It is used as the hook point for the custom pluggable knowledge extensions like the bayes engine and the DMN engine. E.g.:DMNRuntime dmnRuntime = session.getKieRuntime( DMNRuntime.class );- Parameters:
cls- the runtime interface for the extension- Returns:
- the runtime instance for the extension
-