Executing ODM business rules by using a JavaCompute node
You can use a JavaCompute node to execute IBM Operational Decision Manager (ODM) business rules.
Before you begin
- Read Using Operational Decision Manager (ODM) business rules.
- Create a message flow containing a JavaCompute node.
About this task
You can use a JavaCompute node to execute rules contained in a ruleset, by using an embedded rule engine.
Procedure
To use a JavaCompute node to execute rules contained in a ruleset, complete the following steps:
Obtaining a connection to an ODM server
About this task
onSetup
method of
a JavaCompute node, by calling the
getODMServer
method and supplying the name of an ODMServer
policy:
String odmServerPolicyName = "{myPolicyProject}:myODMServerPolicy";
MbODMServer odmServer = getODMServer(odmServerPolicyName);
This method initializes a connection to the ODM Rule Execution server, which must be running when
the message flow that references the JavaCompute node is
deployed or started. The getODMServer
method can be called only from within the
onSetup
method. MbODMServer objects must not be shared between JavaCompute nodes and must not be cached for longer than the
lifetime of the current node. MbODMServer objects that are obtained in the onSetup
method can be used in the evaluate
method to look up rulesets.
Obtaining a ruleset
About this task
When an MbODMServer object has been retrieved, it can be used to retrieve a ruleset from the ODM
server for execution when the message flow is running. To retrieve a ruleset, call the
getRuleset
method on the MbODMServer object and supply the fully qualified path to
the ruleset, in the form
'/RuleAppName/RuleAppVersion/rulesetName/rulesetVersion'. You can specify
either a particular version or latest
to use the most recent version. For
example:
MbODMRuleset odmRuleset = odmServer.getRuleset("/myRuleApp/1.0/my_ruleset/1.0");
MbODMRuleset odmRuleset = odmServer.getRuleset("/myRuleApp/Latest/my_ruleset/latest");
MbODMRuleset odmRuleset = odmServer.getRuleset("/myRuleApp/1.0/my_ruleset/latest");
When it is first referenced, the Latest
version of a ruleset or RuleApp is
loaded and cached at the application level. As a result, all message flows and nodes inside the
application have the same Latest
version, regardless of when they are first
referenced. The latest version is re-evaluated when the application is stopped and restarted.
The getRuleset
method retrieves the ruleset from the ODM server and makes it
available in the embedded rule engine, ready for execution.
Rulesets define ruleset parameters, which are used to pass data between the ruleset and the
calling application. The ruleset can have parameters of type Java
or
XML
, and rulesets that support both parameter types can be called from a JavaCompute node. If the ruleset has parameters of type
Java
, the classes that define the Java objects for those parameters must be made
available to the JavaCompute node at development time and at
runtime. If you use the JavaCompute node wizard to query the
ODM server and generate template Java code, you also have the option of downloading the class files
that define the parameters and make them available to the Java project that contains the JavaCompute node.
The getRuleset
method can be called during the JavaCompute node's onSetup
or
evaluate
methods, and if the same rule is requested multiple times, all calls after
the first one return the version that has been downloaded and cached. MbODMRuleset objects must not
be shared between JavaCompute nodes and must not be cached
for longer than the lifetime of the current node. MbODMRulesets can be retrieved during a JavaCompute node's onSetup
method and then used
by its evaluate
method, to avoid delaying the execution of the first message.
Executing a ruleset
About this task
When executing a ruleset, you must build up a map of parameters to supply to the
execute
method of the MbODMRuleset. The map must contain an entry for each
parameter defined as an IN or INOUT parameter in the ruleset definition. For parameters of type
XML
, you must supply a Java string that contains an XML representation of the
xsd
type (simple or complex) defined for that parameter. For parameters of type
Java
, you must supply an instance of the Java class defined for that parameter. To
execute the ruleset, call the execute
method of the MbODMRuleset, passing in the
map of parameters:
Map<String, Object> ruleInParameters = new HashMap<String, Object>();
MyObject myObj1 = new MyObject();
ruleInParameters.put("ruleParameter_Java_1", myObj1);
ruleInParameters.put("ruleParameter_XML_2", "<Customer><name>John Doe</name><age>33</age></Customer>");
MbODMRulesetExecutionResponse ruleResponse = odmRuleset.execute(ruleInParameters);
The result of calling the execute
method is an instance of an
MbODMRulesetExecutionResponse. This object gives access to the map of OUT and INOUT parameters of
the ruleset through the getOutputParameters
method, and to some execution
statistics:
Map<String, Object> ruleOutParameters = ruleResponse.getOutputParameters();
MyResultObject myResult1 = (MyResultObject)ruleOutParameters.get("ruleResponseParameter1");
long rulesFired = ruleResponse.getTotalRulesFired();
You can use normal message tree access and modification techniques to retrieve data from the
incoming message tree to build the parameters supplied to the ruleset execute
method, and to build or modify the outgoing message tree with details from the result parameters.
The execute
method of the MbODMRuleset can be called multithreaded, and it does not
need to be looked up for each flow instance.
Executing rulesets by using a JavaCompute node
About this task
When you create a JavaCompute node, you are presented with a series of templates, one of which is Process via Operational Decision Manager ruleset. Select this option to query the available rulesets in an ODM Rule Execution server; the wizard guides you through selecting a ruleset and then generates template Java code with details about the rule and how to call it. For more information, see Creating ODM classes by using the Process via Operational Decision Manager ruleset wizard.