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.


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

http://sites.google.com/site/javamonamour/xsds/Employee.xsd?attredirects=0&d=1




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


3)
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">
<soapenv:Header/>
<soapenv:Body>
<dec:DecisionServiceRequest>
<par:employeeRequest>
<emp:EmployeeRequest>
<emp:SSN>1234-56-7890</emp:SSN>
<emp:name>Pierre</emp:name>
<emp:dateOfBirth>1961-02-11</emp:dateOfBirth>
<emp:employeeType>none</emp:employeeType>
<emp:salary>20</emp:salary>
</emp:EmployeeRequest>
</par:employeeRequest>
</dec:DecisionServiceRequest>
</soapenv:Body>
</soapenv:Envelope>


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:


<soapenv:Body>
<dec:DecisionServiceRequest>
<par:parameter1Name>

</par:parameter1Name><par:parameter2Name>


</par:parameter2Name>
</dec:DecisionServiceRequest>
</soapenv:Body>


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">
<soapenv:Body>
<DecisionServiceResponse xmlns="http://www.ilog.com/rules/DecisionService">
<ilog.rules.outputString></ilog.rules.outputString>
<ilog.rules.firedRulesCount>1</ilog.rules.firedRulesCount>
<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>
</EmployeeResponse>
</employeeResponse>
</DecisionServiceResponse>
</soapenv:Body>
</soapenv:Envelope>


and the WSDL is here:

http://sites.google.com/site/javamonamour/xsds/rulesetwsdl.wsdl?attredirects=0&d=1


In case of fault, you get this:

<soapenv:Fault>
 <faultcode>soapenv:Server</faultcode>
 <faultstring>Error when executing the ruleset /PV01_RuleApp/1.0/PV01_RuleProject/1.0</faultstring>
 <faultactor>http://www.ilog.com/rules/DecisionService</faultactor>
 <detail>
    <ds:DecisionServiceException>
       <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: