Ce contenu n'est pas disponible dans la langue sélectionnée.
8.5. Rules
8.5.1. Rules Component
java:comp/BeanManager
lookup via JNDI is not supported. There is no supported alternative.
8.5.2. Create a Rules Service
Prerequisites
- File Name: the name of the file that is used to create a new template rules definition.
- Service Name: the name of the service that your rules provide.
- Interface Type: the contract for the service being provided. Rules services support Java and WSDL contract types.
- Package Name: package name used for the new Rules file.
Procedure 8.10. Create a Rules Service
- Create a new SwitchYard Rules file in the SwitchYard Editor JBoss Developer Studio plug-in.
- The MyService interface can be as simple as this, with no SwitchYard-specific imports:
package com.example.switchyard.docs; public interface Example { public void process(MyData data); }
- The generated rule template looks like this:
package com.example.switchyard.docs import org.switchyard.Message global Message message rule "RulesExample" when // insert conditional here then // insert consequence here System.out.println("service: ExampleService, payload: " + message.getContent()); end
- Input the values into the SwitchYard Editor's SwitchYard Rules File screen.
- Click Finish.
8.5.3. Stateless and Stateful Rules Executions
Introduction
By default, service method invocation creates a new Drools knowledge session, execute it given the passed-in domain data and then be disposed cleanly.
8.5.4. Stateless Knowledge Session
- Validation
- Is this person eligible for a mortgage?
- Calculation
- Compute a mortgage premium.
- Routing and Filtering
- Filter incoming messages, such as emails, into folders.Send incoming messages to a destination.
8.5.5. Stateful Knowledge Session
the dispose()
method must be called afterwards to ensure there are no memory leaks, as the Knowledge Base contains references to Stateful Knowledge Sessions when they are created. StatefulKnowledgeSession also supports the BatchExecutor
interface, like StatelessKnowledgeSession, the only difference being that the FireAllRules command is not automatically called at the end for a Stateful Session.
- Monitoring
- Stock market monitoring and analysis for semi-automatic buying.
- Diagnostics
- Fault finding and medical diagnostics
- Logistics
- Parcel tracking and delivery provisioning
- Compliance
- Validation of legality for market trades.
8.5.6. Mapping Global Variables
- To map the variables, hover the mouse over the Rules component in
switchyard.xml
and click Properties icon.Figure 8.3. Properties dialog for Rules Component
- In the Properties dialog, click Implementation. From the right hand side panel, click Operations tab to view Operation Mapping tooling window.
Figure 8.4. Operations Mapping
8.5.7. Map Global Variables
- Configure the rules implementation.NoteYour expression can use the variables exchange (
org.switchyard.Exchange
), context (org.switchyard.Context
) or message (org.switchyard.Message
).Context can be accessed in the expression as a java.util.Map. When accessing it as such, the default properties Scope is IN. This can be overridden using the contextScope attribute by changing it to OUT or EXCHANGE - Use these global variables in your JBoss Rules Drools Rule Language file:
package example global java.lang.String service global java.lang.String messageId global com.example.Payload payload rule "Example" when ... then ... System.out.println("service: " + service + ", messageId: " + messageId + ", payload: " + payload); end
NoteIn a more realistic scenario, the payload would be accessed in rule firing because it was inserted into the session directly by the RulesExchangeHandler, and thus evaluated (rather than being accessed as a global).
8.5.8. Mapping Facts
8.5.9. Notes About Mapping Facts
- If you specify your own fact mappings, the SwitchYard message's content is not inserted as a fact. You can add a fact mapping with an expression of "message.content" if you want to still include it.
- There is no point in specifying the "variable" attribute of the mappings (as done for global mappings), as the result of each expression is inserted as a nameless fact.For stateless execution, the full list of facts is passed to the StatelessKnowledgeSessions'
execute(Iterable)
method.For stateful execution, each fact is individually inserted into the StatefulKnowledgeSession. - If the result of the mapping expression implements Iterable (for example, a Collection), then the result is iterated over, and each iteration is inserted as a separate fact, rather than the parent Iterable itself being inserted. This is not recursive behavior (because it is only done once).