Este contenido no está disponible en el idioma seleccionado.
Chapter 12. Modes and Methods
12.1. Sequential Mode Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Using sequential mode in JBoss Rules allows you to utilize the engine in a more simplified way. It allows for rules to be used without needing to be re-evaluated if a user is working with a stateless session and no more data can be asserted or modified after the initial data set.
12.2. Sequential Mode Options Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
These are some of the options you can choose to utilize when using the sequential mode:
- Order the Rules by salience and position in the ruleset (by setting a sequence attribute on the rule terminal node).
- Create an array (one element for each possible rule activation). Element position indicates firing order.
- Turn off all node memories, except the right-input Object memory.
- Disconnect the Left Input Adapter Node propagation and let the Object and the Node be referenced in a Command object. This is added to a list in the Working Memory for later execution.
- Assert all objects. When all assertions are finished and the right-input node memories are populated, you can check the Command list and execute each in turn.
- All resulting Activations should be placed in the array, based upon the determined sequence number of the Rule. Record the first and last populated elements, to reduce the iteration range.
- Iterate the array of Activations, executing populated element in turn.
- If there is a maximum number of allowed rule executions, exit the network evaluations early to fire all the rules in the array.
12.3. Activating Sequential Mode Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Procedure 12.1. Task
- Start a stateless session.
- The sequential mode will be turned off by default. To turn it on, call
RuleBaseConfiguration.setSequential(true). Alternatively, set the rulebase configuration propertydrools.sequentialto true. - To allow sequential mode to fall back to a dynamic agenda, call
setSequentialAgendawithSequentialAgenda.DYNAMIC. - Optionally, set the
JBossRules.sequential.agendaproperty tosequentialordynamic.
12.4. The CommandFactory Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The
CommandFactory object allows for commands to be executed on stateless sessions. Upon its conclusion, the factory will execute fireAllRules() before disposing the session.
12.5. Supported CommandFactory Options Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
All of these options are compatible with the CommandFactory:
- FireAllRules
- GetGlobal
- SetGlobal
- InsertObject
- InsertElements
- Query
- StartProcess
- BatchExecution
12.6. The Insert Command Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
InsertObject will insert a single object with an optional "out" identifier. InsertElements will iterate an Iterable, inserting each of the elements. This allows a Stateless Knowledge Session to process or execute queries in any order.
12.7. Insert Command Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession(); ExecutionResults bresults = ksession.execute( CommandFactory.newInsert( new Car( "sedan" ), "sedan_id" ) ); Sedan sedan = bresults.getValue( "sedan_id" );
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
ExecutionResults bresults =
ksession.execute( CommandFactory.newInsert( new Car( "sedan" ), "sedan_id" ) );
Sedan sedan = bresults.getValue( "sedan_id" );
12.8. The Execute Method Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The execute method is used to execute commands one at a time. It always returns an
ExecutionResults instance, which allows access to any command results if they specify an out identifier such as stilton_id.
12.9. Execute Method Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
12.10. The BatchExecution Command Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The
BatchExecution command allows you to execute multiple commands at once. It represents a composite command that is created from a list of commands. Execute will iterate over the list and execute each command in turn. This means you can insert some objects, start a process, call fireAllRules and execute a query, all in a single execute(...) call.
12.11. The FireAllRules Command Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The
FireAllRules command disables the automatic execution of rules at the end. It is a type of manual override function.
12.12. Out Identifiers Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Commands support out identifiers. Any command that has an out identifier set on it will add its results to the returned ExecutionResults instance.
12.13. Out Identifier Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This example will use the BatchExecution command to show how out identifiers work:
In the above example multiple commands are executed, two of which populate the
ExecutionResults. The query command defaults to use the same identifier as the query name, but it can also be mapped to a different identifier.
12.14. Execution XML Examples Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
A custom XStream marshaler can be used with the JBoss Rules Pipeline to achieve XML scripting, which is perfect for services. Here are two examples of this:
BatchExecution XML:
ExecutionResults XML:
12.15. Execution Marshalling Examples Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is an example of BatchExecution marshalled to XML
The
CommandExecutor returns an ExecutionResults, and this is handled by the pipeline code snippet as well. A similar output for the <batch-execution> XML sample above would be:
12.16. Batch-execution and Command Examples Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- There is currently no XML schema to support schema validation. This is the basic format. The root element is <batch-execution> and it can contain zero or more commands elements:
<batch-execution> ... </batch-execution>
<batch-execution> ... </batch-execution>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The insert element features an "out-identifier" attribute so the inserted object will be returned as part of the result payload:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - It's also possible to insert a collection of objects using the <insert-elements> element. This command does not support an out-identifier. The
org.domain.UserClassis just an illustrative user object that XStream would serialize:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The
<set-global>element sets a global for the session:Copy to Clipboard Copied! Toggle word wrap Toggle overflow <set-global>also supports two other optional attributes:outandout-identifier. A true value for the booleanoutwill add the global to the<batch-execution-results>payload, using the name from theidentifierattribute.out-identifierworks likeoutbut additionally allows you to override the identifier used in the<batch-execution-results>payload:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - There is a
<get-global>element without contents. It only has anout-identifierattribute. There is no need for anoutattribute because retrieving the value is the sole purpose of a<get-global>element:<batch-execution> <get-global identifier='userVar1' /> <get-global identifier='userVar2' out-identifier='alternativeUserVar2'/> </batch-execution>
<batch-execution> <get-global identifier='userVar1' /> <get-global identifier='userVar2' out-identifier='alternativeUserVar2'/> </batch-execution>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The query command supports both parameter and parameterless queries. The
nameattribute is the name of the query to be called, and theout-identifieris the identifier to be used for the query results in the<execution-results>payload:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The
<start-process>command accepts optional parameters:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The signal event command allows you to identify processes:
<signal-event process-instance-id='1' event-type='MyEvent'> <string>MyValue</string> </signal-event>
<signal-event process-instance-id='1' event-type='MyEvent'> <string>MyValue</string> </signal-event>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The complete work item command notifies users when a process is completed:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The abort work item command lets you cancel a process while it is running:
<abort-work-item id='21' />
<abort-work-item id='21' />Copy to Clipboard Copied! Toggle word wrap Toggle overflow
12.17. The MarshallerFactory Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The
MarshallerFactory is used to marshal and unmarshal Stateful Knowledge Sessions.
12.18. Marshaller Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is what a marsheller looks like in practice:
12.19. Marshalling Options Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
| Option | Description |
|---|---|
| ObjectMarshallingStrategy | This interface provides implementations for marshalling and allows for greater flexibility. |
| SerializeMarshallingStrategy |
This is the default strategy for calling the
Serializable or Externalizable methods on a user instance.
|
| IdentityMarshallingStrategy |
This strategy creates an integer id for each user object and stores them in a Map, while the id is written to the stream.
When unmarshalling it accesses the
IdentityMarshallingStrategy map to retrieve the instance. This means that if you use the IdentityMarshallingStrategy, it is stateful for the life of the Marshaller instance and will create ids and keep references to all objects that it attempts to marshal.
|
12.20. IdentityMarshallingStrategy Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is the code for using the IdentityMarshallingStrategy:
12.21. The ObjectMarshallingStrategyAcceptor Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The
ObjectMarshallingStrategyAcceptor is the interface that each Object Marshalling Strategy contains. The Marshaller has a chain of strategies. When it attempts to read or write a user object, it uses the ObjectMarshallingStrategyAcceptor to determine if they are to be used for marshalling the user object.
12.22. The ClassFilterAcceptor Implementation Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The
ClassFilterAcceptor implementation allows strings and wild cards to be used to match class names. The default is "*.*".
12.23. IdentityMarshallingStrategy with Acceptor Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is an example of using the IdentityMarshallingStrategy with the acceptor. Note that the acceptance checking order is in the natural order of the supplied array:
12.24. Persistence and Transactions in JBoss Rules Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Long-term out of the box persistence with Java Persistence API (JPA) is possible with JBoss Rules. You will need to have some implementation of the Java Transaction API (JTA) installed. For development purposes you can use the Bitronix Transaction Manager as it's simple to set up and works embedded. For production use, JBoss Transactions is recommended.
12.25. Transaction Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is what performing a transaction looks like:
12.26. Using a JPA Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Procedure 12.2. Task
- Make sure the environment is set with both the
EntityManagerFactoryand theTransactionManager. - Launch the JPA from your GUI or command line.
- Use the id to load a previously persisted Stateful Knowledge Session. If rollback occurs the ksession state is also rolled back, you can continue to use it after a rollback.
12.27. Loading a StatefulKnowledgeSession with JPA Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is the code for loading a StatefulKnowledgeSession implementing the JPA:
StatefulKnowledgeSession ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( sessionId, kbase, null, env );
StatefulKnowledgeSession ksession =
JPAKnowledgeService.loadStatefulKnowledgeSession( sessionId, kbase, null, env );
12.28. Configuring JPA Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
To enable persistence several classes must be added to your persistence.xml, as in the example below:
12.29. Configuring JTA DataSource Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is the code for configuring the JTA DataSource:
12.30. JNDI Properties Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Bitronix also provides a simple embedded JNDI service, ideal for testing. To use it add a jndi.properties file to your META-INF and add the following line to it:
java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory
java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory
12.31. KnowledgeBase Namespaces Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
This is a list of namespaces you can attach to the KnowledgeBase for building purposes:
- deftemplate
- defrule
- deffunction
- and/or/not/exists/test conditional elements
- Literal, Variable, Return Value and Predicate field constraints