Tuesday, June 1, 2010

JRules: deploying a Ruleset as a Web Service

Something cool is "Import-Rule Studio Samples and Tutorials", a nice one is webdecisionservice. unfortunately it looks like the example is in Java binding, and not XML binding.... If I want to expose the Ruleset as a Web Service I need to use XML binding (is this true?).
XML handling is done with IlrXmlDataDriver.

Create a Java Project, call it PV01_JRulesResources, create a folder xsds and put inside this file:


New, Rule Project with a BOM, call it PV01_RuleProject
specify the Employee XSD as XML XOM:

Add the Employee and EmployeeResponse XSDs as a parameter to the Ruleset (right click on PV01_RuleProject, properties, Ruleset Parameters, add employee as a IN parameter and verbalization employee, add employeeResponse as an OUT parameter and verbalization employeeResponse)
Don't forget to set the default value of the response to
new com.acme.employee.EmployeeResponse()

otherwise you will get a Null Pointer Exception when accessing it.

4) Code this business rule

if the name of employeeRequest is "Pierre" then
 set the fired of employeeResponse to "TRUE";

else set the fired of employeeResponse to "FALSE";

5) Create a Rule App project, add to it the RuleSet Project

6) Export the RuleApp project to a JAR, open the console http://localhost:8080/res and do "Deploy RuleApp Archive" and install the Rules.

7) click on "Get HTDS WSDL for this ruleset version", copy the WLSD link, go to SOAP UI and create a new Web Services Client project using the WSDL URL

8) you are now ready to test your rule from SOAP-UI! Time to celebrate.

In our case, we will get this SOAP message for test:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dec="http://www.ilog.com/rules/DecisionService" xmlns:par="http://www.ilog.com/rules/param" xmlns:emp="http://www.acme.com/Employee">

We can see that we have a operation DecisionServiceRequest which contains a node par:employeeRequest which contains the value EmployeeRequest.

The generic request will be:




you should get this response:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.ilog.com/rules/DecisionService">
<DecisionServiceResponse xmlns="http://www.ilog.com/rules/DecisionService">
<employeeResponse xmlns="http://www.ilog.com/rules/param"><EmployeeResponse xmlns="http://www.acme.com/Employee" xmlns:ns0="http://www.acme.com/Employee">
<fired xmlns="http://www.acme.com/Employee">TRUE</fired>

and the WSDL is here:


In case of fault, you get this:

 <faultstring>Error when executing the ruleset /PV01_RuleApp/1.0/PV01_RuleProject/1.0</faultstring>
       <ds:exception>ilog.rules.res.decisionservice.IlrDecisionServiceException: Error when executing the ruleset /PV01_RuleApp/1.0/PV01_RuleProject/1.0
at ilog.rules.res.decisionservice.web.IlrDecisionServiceController.executeRuleset(IlrDecisionServiceController.java:125)

No comments: