Este contenido no está disponible en el idioma seleccionado.
Chapter 20. The Java Rule Engine Application Programming Interface
20.1. JSR94 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
JSR94 is an API used in JBoss Rules. It allows for support of multiple rule engines from a single API. JSR94 does not deal in any way with the rule language itself.
The JSR94 standard represents the "least common denominator" in features across rule engines. This means that there is less functionality in the JSR94 API than in the standard Knowledge API. By using JSR94, you cannot use all the capabilities of JBoss Rules. You should use globals and support for DRL, DSL and XML, via property maps in conjunction with JSR94. This introduces non-portable functionality.
Note
As JSR94 does not provide a rule language, you are only solving a small fraction of the complexity of switching rule engines with very little gain. Red Hat recommends you program against the Knowledge (JBoss Rules and jBPM) API.
20.2. Javax.rules Interfaces Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
HandleThe Handle is used to retrieve an Object back from theWorkingMemorywhich was added in aStatefulRuleSession. With theHandleyou can modify or remove anObjectfrom theWorkingMemory. To modify an Object callupdateObject()from theStatefulRuleSession. To remove it, callremoveObject()with theHandleas the Parameter. Inside of the implementation of the Java Rule Engine API will be called themodifyObject()andretractObject()methods of the encapsulated Knowledge (Drools and jBPM) API.ObjectFilterThis interface is used to filter objects for RuleSession.RuleExecutionSetMetadataThe RuleExecutionSetMetadata is used to store name, description and URI for a RuleExecutionSet.RuleRuntimeThe RuleRuntime is the key to a RuleSession. The RuleRuntime obtained from the RuleServiceProvider.If you retrieve a RuleRuntime call createRuleSession() to open a RuleSession.Through the RuleRuntime you can retrieve a list of URIs of all RuleExecutionSets, which were registered by a RuleAdministrator. You need the URI as a String to open a RuleSession to the rule engine. The rule engine will use the rules of the RuleExecutionSet inside of the RuleSession.The Map is used for Globals. Globals were formerly called ApplicationData (in Drools 2.x). The key needs to be the identifier of the Global and the Value the object you want to use as a Global.RuleSessionThe RuleSession is the object you are working with if you want to contact the rule engine.If you are getting a RuleSession from the RuleRuntime, then it will be either a StatefulRuleSession or a StatelessRuleSession.Call the release()-method so that all resources will be freed.StatefulRuleSessionIf you need to run the rule engine more than once, run a StatefulRuleSession. You can assert objects, execute rules and so on.You will get a Handle for every object which you are asserting to the Rule Session. Do not lose it, you will need it, to retract or modify objects in the Working Memory. You are having no direct contact to Drools´ Working Memory which is used inside the implementation, for this you got the RuleSession.StatelessRuleSessionA StatelessRuleSession means that you are having only one contact to the rule engine. You are giving a list of objects to the rule engine and the rule engine asserts them all and starts execution immediately. The result is a list of objects. The content of the result list depends on your rules. If your rules are not modifying or retracting any objects from the Working Memory, you should get all objects you re-added.You can use the ObjectFilter which will filter the resulting list of objects before you get it.
20.3. Javax.rules Classes Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
RuleServiceProviderThe RuleServiceProvider gives you access to the RuleAdministrator or a RuleRuntime, which you need to open a new Rule Session. To get the RuleServiceProvider call RuleServiceProviderManager.getRuleServiceProvider().In a J2EE environment you can bind the RuleServiceProvider to the JNDI and create a lookup to place it in all your applications.RuleServiceProviderManagerThe RuleServiceProvider is often compared with the DriverManager, which you use in JDBC. It works like setting up the Driver for a DataBase.
20.4. Javax.rules Exceptions Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
ConfigurationExceptionThis exception is thrown when a user configuration error has been made.InvalidHandleExceptionThis exception is thrown when a client passes an invalid Handle to the rule engine.InvalidRuleSessionExceptionThe InvalidRuleSessionException should be thrown when a method is invoked on a RuleSession and the internal state of the RuleSession is invalid. This may have occurred because a StatefulRuleSession has been serialized and external resources can no longer be accessed. This exception is also used to signal that a RuleSession is in an invalid state (such as an attempt to use it after the release method has been called) (Taken from JCP API Documentation).RuleExceptionBase class for all Exception classes in the javax.rules package.RuleExecutionExceptionThis exception is not thrown in the Drools 3 JSR 94 implementationRuleExecutionSetNotFoundExceptionThis exception is thrown if a client requests a RuleExecutionSet from the RuleRuntime and the URI or RuleExecutionSet cannot be found (Taken from JCP API Documentation).RuleSessionCreateExceptionThis exception is thrown when a client requests a RuleSession from the RuleRuntime and an error occurs that prevents a RuleSession from being returned (Taken from JCP API Documentation).RuleSessionTypeUnsupportedExceptionThis exception is thrown when a client requests a RuleSession and the vendor does not support the given type (defined in the RuleRuntime) or the RuleExecutionSet itself does not support the requested mode (Taken from JCP API Documentation).
20.5. Using a Rule Service Provider Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Procedure 20.1. Task
- Use the following code to load the JBoss Rules rule service provider:
Class ruleServiceProviderClass = Class.forName("org.drools.jsr94.rules.RuleServiceProviderImpl"); - Use the following code to register it:
RuleServiceProviderManager.registerRuleServiceProvider( "http://jboss.com/products/rules", ruleServiceProviderClass); - Call to the RuleServiceProvider using the following code:
RuleServiceProviderManager.getRuleServiceProvider("http://jboss.com/products/rules"); - To stop the rule service, deregister it with this code:
RuleServiceProviderManager.deregisterRuleServiceProvider( "http://jboss.com/products/rules");
20.6. Javax.rules.admin Interfaces Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
LocalRuleExecutionSetProviderRuleRuleAdministratorRuleExecutionSetRuleExecutionSetProvider
20.7. Javax.rules.admin Exceptions Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
RuleAdministrationExceptionBase class for all administration RuleException classes in the javax.rules.admin package (Taken from JCP API Documentation).RuleExecutionSetCreateExceptionOccurs when there is an error in creating a rule execution set.RuleExecutionSetDeregistrationExceptionOccurs if there is an error upon attempting to unregister a rule execution set from a URI.RuleExecutionSetRegisterExceptionOccurs if there is an error upon attempting to register a rule execution set to a URI.
20.8. The RuleServiceProvider Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The RuleServiceProvider provides access to the RuleRuntime and RuleAdministrator APIs. The RuleAdministrator provides an administration API for the management of RuleExecutionSet objects, making it possible to register a RuleExecutionSet that can then be retrieved via the RuleRuntime.
20.9. The RuleServiceProviderManager Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The RuleServiceProviderManager manages the registration and retrieval of RuleServiceProviders. The JBossRules RuleServiceProvider implementation is automatically registered via a static block when the class is loaded using Class.forName, in much the same way as JDBC drivers.
20.10. Automatic RuleServiceProvider Registration Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is an example of registering the automatic RuleServiceProvider:
20.11. Registering a LocalRuleExecutionSet with the RuleAdministrator API Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Procedure 20.2. Task
- Create a RuleExecutionSet. You can do so by using the RuleAdministrator which provides factory methods to return an empty LocalRuleExecutionSetProvider or RuleExecutionSetProvider.
- Specify the name for the RuleExecutionSet.
- Register the RuleExecutionSet as shown below:
// Register the RuleExecutionSet with the RuleAdministrator String uri = ruleExecutionSet.getName(); ruleAdministrator.registerRuleExecutionSet(uri, ruleExecutionSet, null);
// Register the RuleExecutionSet with the RuleAdministrator String uri = ruleExecutionSet.getName(); ruleAdministrator.registerRuleExecutionSet(uri, ruleExecutionSet, null);Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use the LocalRuleExecutionSetProvider to load a RuleExecutionSets from local sources that are not serializable, like Streams.
- Use the RuleExecutionSetProvider to load RuleExecutionSets from serializable sources, like DOM Elements or Packages. Both the "ruleAdministrator.getLocalRuleExecutionSetProvider( null );" and the "ruleAdministrator.getRuleExecutionSetProvider( null );" (use null as a parameter).
- The example below shoes you how to register the LocalRuleExecutionSet:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - You can use the "ruleExecutionSetProvider.createRuleExecutionSet( reader, null )" property to provide configuration for the incoming source. When null is passed the default is used to load the input as a drl. Allowed keys for a map are "source" and "dsl". The key "source" takes "drl" or "xml" as its value.
- Set "source" to "drl" to load a DRL, or to "xml" to load an XML source. "xml" will ignore any "dsl" key/value settings. The "dsl" key can take a Reader or a String (the contents of the dsl) as a value. See the following dsl example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
20.12. Using Stateful and Stateless RuleSessions Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Procedure 20.3. Task
- Get the runtime by accessing the RuleServiceProvider as shown:
RuleRuntime ruleRuntime = ruleServiceProvider.getRuleRuntime();
RuleRuntime ruleRuntime = ruleServiceProvider.getRuleRuntime();Copy to Clipboard Copied! Toggle word wrap Toggle overflow - To create a rule session, use one of the two RuleRuntime public constants. These are "RuleRuntime.STATEFUL_SESSION_TYPE" and "RuleRuntime.STATELESS_SESSION_TYPE", accompanying the URI to the RuleExecutionSet you wish to instantiate a RuleSession for.
- Optionally, access the properties to specify globals.
- The createRuleSession(...) method will return a RuleSession instance. You should cast it to StatefulRuleSession or StatelessRuleSession:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - When using a StatelessRuleSession, you can only call executeRules(List list) passing a list of objects, and an optional filter, the resulting objects are then returned:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
20.13. Using Globals with JSR94 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
JSR94 supports globals (in a manner that is not portable) by using the properties map passed to the RuleSession factory method. Globals must be defined in the DRL or XML file first, otherwise an exception will be thrown. The key represents the identifier declared in the DRL or XML, and the value is the instance you wish to be used in the execution.
20.14. Using Globals with JSR94 Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Here is an example of implementing a global in JSR94:
Do not forget to declare the global "list" in your DRL:
20.15. Further Reading About JSR94 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
If you need more information on JSR94, please refer to the following links:
- Official JCP Specification for Java Rule Engine API (JSR 94)
- The Java Rule Engine API documentation
- The Logic From The Bottom Line: An Introduction to The Drools Project. By N. Alex Rupp, published on TheServiceSide.com in 2004
- Getting Started With the Java Rule Engine API (JSR 94): Toward Rule-Based Applications. By Dr. Qusay H. Mahmoud, published on Sun Developer Network in 2005
- Jess and the javax.rules API. By Ernest Friedman-Hill, published on TheServerSide.com in 2003