All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.runtime.StatefulKnowledgeSession Maven / Gradle / Ivy

There is a newer version: 5.1.1
Show newest version
package org.drools.runtime;

import org.drools.runtime.process.StatefulProcessSession;
import org.drools.runtime.rule.StatefulRuleSession;

/**
 * StatefulKnowledgeSession is the most common way to interact with a rules engine. A StatefulKnowledgeSession
 * allows the application to establish an iterative conversation with the engine, where 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 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.newStatefulKnowledgeSession();
 * for( Object fact : facts ) {
 *     ksession.insert( fact );
 * }
 * ksession.fireAllRules();
 * ksession.dispose();
 * 
* *

* StatefulKnowledgeSessions support globals. Globals are used to pass information into the engine and receive callbacks * from your rules, 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, 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. 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 setGlobalResolver() and setGlobal(). * 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 GlobalResolver. If an identifier cannot be found in * the internal Collection, it will then check the externally supplied Global Resolver, if one has been set. *

* *

Code snippet for setting a global:

*
 * StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
 * 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();
 * 
* * @see org.drools.runtime.GlobalResolver */ public interface StatefulKnowledgeSession extends StatefulRuleSession, StatefulProcessSession, KnowledgeRuntime { /** * 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. */ void dispose(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy