Monday, May 31, 2010

JRules exposed via Web Services

Typically you have a RuleApp, which contains several RuleSets.

When you produce a RuleSet, you can write several BusinessRules in it.

Here http://publib.boulder.ibm.com/infocenter/brjrules/v7r0m2/index.jsp?topic=/com.ibm.websphere.ilog.jrules.doc/Content/Business_Rules/Documentation/_pubskel/JRules/ps_JRules_Global800.html it says:

The decision service automatically generates a Web Services Description Language (WSDL) file for each deployed ruleset archive

In our example, helloWorldRuleApp contains helloWorldRuleApp,
and helloWorldRuleApp contains 2 BusinessRule: greetingsItalian and greetingsEnglish.
They will return respectively Buongiorno and Good morning!.

JRules  exposes them automatically via Web Services, the endpoint looks like:

http://localhost:8080/DecisionService/ws/helloWorldRuleApp/1.0/greetingsRuleset/1.0

This will fire all the Rules inside the greetingsRuleset.


The result will be:

<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>Buongiorno
Good morning!</ilog.rules.outputString>
         <ilog.rules.firedRulesCount>2</ilog.rules.firedRulesCount>
      </DecisionServiceResponse>
   </soapenv:Body>
</soapenv:Envelope>


that is:
ilog.rules.outputString is the output of all the Rules invoked
ilog.rules.firedRulesCount is the count of the rules fired


The WSDL associated to the Web Service is:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://www.ilog.com/rules/DecisionService" xmlns:tns="http://www.ilog.com/rules/DecisionService" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.ilog.com/rules/DecisionService" xmlns:param="http://www.ilog.com/rules/param" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:import namespace="http://www.ilog.com/rules/param"/>
<xsd:element name="DecisionServiceRequest">
<xsd:complexType>
<xsd:sequence>
</xsd:sequence>
</xsd:complexType>

</xsd:element>
<xsd:element name="DecisionServiceResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="ilog.rules.outputString" type="xsd:string"/>
<xsd:element name="ilog.rules.firedRulesCount" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="DecisionServiceException">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="exception" nillable="false" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xsd:schema targetNamespace="http://www.ilog.com/rules/param" xmlns:ns0="http://www.ilog.com/rules/param"/>

</wsdl:types>
<wsdl:message name="DecisionServiceSoapResponse">
<wsdl:part name="parameters" element="tns:DecisionServiceResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="DecisionServiceSoapFault">
<wsdl:part name="fault" element="tns:DecisionServiceException">
</wsdl:part>
</wsdl:message>

<wsdl:message name="DecisionServiceSoapRequest">
<wsdl:part name="parameters" element="tns:DecisionServiceRequest">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="DecisionServiceJRulesPVHelloWorld">
<wsdl:operation name="executeDecisionService">
<wsdl:input message="tns:DecisionServiceSoapRequest">
</wsdl:input>
<wsdl:output message="tns:DecisionServiceSoapResponse">

</wsdl:output>
<wsdl:fault name="DecisionServiceSoapFault" message="tns:DecisionServiceSoapFault">
</wsdl:fault>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="DecisionServiceSOAP" type="tns:DecisionServiceJRulesPVHelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="executeDecisionService">
<soap:operation soapAction="executeDecisionService" style="document"/>

<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
<wsdl:fault name="DecisionServiceSoapFault">
<soap:fault name="DecisionServiceSoapFault" use="literal"/>
</wsdl:fault>

</wsdl:operation>
</wsdl:binding>
<wsdl:service name="DecisionServiceJRulesPVHelloWorld">
<wsdl:port name="DecisionServiceSOAPesp50ws17206.crb.acme.net" binding="tns:DecisionServiceSOAP">
<soap:address location="http://localhost:8080/DecisionService/ws/helloWorldRuleApp/1.0/greetingsRuleset/1.0"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


Let's watch an example where you pass an XML parameter (thank you David Selman for your excellent presentation, but next time can you ask a beautiful girl to show her face instead of yours :O)? We are all men in this business and seing a girl once in a while would help...):




In fact it's all about invoking a Web Service, the video shows how to do it with a Eclipse Plugin and Axis2 Java generated code.


The way I see it is that there is a DecisionService servlet serving all RuleApps and acting as a Proxy to the Rules Engine.

No comments: