This section explains numerous methods of exposing a rule set as a Web service through a simple, example usecase: "Order Approval" sample. It is only a part of a large rule set of an order processing application.
Sample Rule Definition
Rules
Rule 1 : An order for stocks of Company A is accepted only if the number of stocks is higher than 10. Rule 2 : An order for stocks of Company B is accepted only if the stock prize is higher than 100 $. Rule 3 : An order for stocks of Company C is accepted only if the stock prize is higher than 50 $ and the number of stocks is lower than 200.
Facts
The three rules above use validating orders. All three rules are based on a customer placing an order, and can be captured using a Java bean as follows.
package samples.userguide; /** * Represents the fact of a customer places an order */ public class PlaceOrder { String symbol; int quantity; double price; public String getSymbol() { return symbol; } public void setSymbol(String symbol) { this.symbol = symbol; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
The result of the execution of the three rules above is either an OrderAccept or OrderReject, which can be captured using Java beans as follows.
package samples.userguide; /** * Order accept notification */ public class OrderAccept { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } package samples.userguide; /** * Order Reject Notification */ public class OrderReject { private String reason; public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } }
Using the three rules, fact and result definitions above and a rule language of a rule engine, you can define new rules. WSO2 BRS is based on the JSR 94 Java Rule Specification and uses the open source Drools rule engine as the default JSR 94 provider. Shown below is how to write the rules of our example usecase using Drools rule language.
Drools Rule File
package OrderApproval; import samples.userguide.OrderAccept; import samples.userguide.OrderReject; import samples.userguide.PlaceOrder; rule "Order Approval Rule" dialect "mvel" no-loop true salience 4 when $placeOrder : PlaceOrder( ( symbol == "IBM" && quantity > 10 ) || ( symbol == "SUN" && price > 100 ) || ( symbol == "MSFT" && price > 50 && quantity < 200 ) ) then OrderAccept orderAccept = new OrderAccept(); orderAccept.setMessage("Accepted order for: "+ $placeOrder.quantity + " stocks of "+ $placeOrder.symbol +" at$ " + $placeOrder.price); insertLogical(orderAccept); end rule "IBM Order Deny Rule" dialect "mvel" no-loop true salience 3 when not ( OrderAccept()) $placeOrder : PlaceOrder( symbol == "IBM" ) then retract($placeOrder); OrderReject orderReject = new OrderReject(); orderReject.setReason("An Order for stocks of IBM is accepted only if the number of stocks is higher than 10."); insertLogical(orderReject); end rule "SUN Order Deny Rule" dialect "mvel" no-loop true salience 2 when not ( OrderAccept()) $placeOrder : PlaceOrder( symbol == "SUN" ) then retract($placeOrder); OrderReject orderReject = new OrderReject(); orderReject.setReason("An Order for stocks of SUN is accepted only if the stock price is higher than 100 $."); insertLogical(orderReject); end rule "MSFT Order Deny Rule" dialect "mvel" no-loop true salience 1 when not ( OrderAccept()) $placeOrder : PlaceOrder( symbol == "MSFT" ) then retract($placeOrder); OrderReject orderReject = new OrderReject(); orderReject.setReason("An Order for stocks of MSFT is accepted only if the stock price is higher than 50 $ and the number of stocks is lower than 200."); insertLogical(orderReject); end
Creating and Deploying the Rule Service
There are two ways to create a rule service using the WSO2 BRS management console as follows:
- Rule Service Upload
- Rule Service Wizard
Rule Service Upload
This demonstrates how to use the rule service upload UI for deploying a rule service.
1. Create a valid jar containing the java classes of facts and results defined above for the Order Approval sample usecase.
2. Create a rule service configuration using the instruction in section BRS Configuration Language. The configuration file used for the sample is shown below, which is named as service.rsl (Rule Service Language).
<ruleService name="OrderApprovalService" xmlns="http://wso2.org/carbon/rules" targetNamespace="http://com.test/orderApproval"> <ruleSet> <rule resourceType="regular" sourceType="file">orderApprovalRules.drl</rule> </ruleSet> <operation name="placeOrder"> <input wrapperElementName="placeOrder" namespace="http://com.test/placeorder"> <fact elementName="order" namespace="http://com.test/placeorder" type="samples.userguide.PlaceOrder"></fact> </input> <output wrapperElementName="placeOrderRespone" namespace="http://com.test/placeorder"> <fact elementName="orderAccept" namespace="http://com.test/placeorder" type="samples.userguide.OrderAccept"></fact> <fact elementName="orderReject" namespace="http://com.test/placeorder" type="samples.userguide.OrderReject"></fact> </output> </operation> </ruleService>
Infor
- To learn how to create an Axis2 service using WSO2 Carbon Studio, refer to: http://markmail.org/download.xqy?id=tsctqe7x5rnwexls&number=1
- To learn how to create an Axis2 service using Eclipse, refer to: http://wso2.org/library/tutorials/create-axis2-web-service-in-3-steps-using-eclipse
5. Log in to the WSO2 BRS management console and select "Rule Service -> Upload" in the "Main" menu.
6. The "Upload Rule Service" window opens. Upload the file created in step 4 and click "Upload". For example,
7. The "Deployed Services" window opens with the newly uploaded business rules exposed as a service. Click on it to access its dashboard and select the TryIt option on the "Client Operations" panel in the service dashboard.
8. The Try-It tool can be used to test the sample through a request similar to the one shown bellow.
<symbol>Company A</symbol> <quantity>223</quantity> <price>14</price>
9. Each rule can be tested by changing values in the symbol, price and quantity. For the request given above, the response will be as follows.
<brs:placeOrderResponse> <brs:result> <brs:OrderAccept xsi:type="ax2277:OrderAccept"> <ax2275:message>Accepted order for: 223 stocks of IBM at$ 14.0</ax2275:message> </brs:OrderAccept> </brs:result> </brs:placeOrderResponse>
10. Also, the WSDL of the service can be used to generate client-side code (stub) required for the service invocation. There is an option for code generation in the services management page. A client using generated stub codes is shown below and the codes are generated with option - "Unpacks the databinding classes".
package org.wso2.carbon.samples; import org.wso2.carbon.samples.orderApprovalService.order.OrderAccept; import org.wso2.carbon.samples.orderApprovalService.order.PlaceOrder; import org.wso2.carbon.samples.orderApprovalService.order.PlaceOrderE; import org.wso2.carbon.samples.orderApprovalService.order.PlaceOrderRespone; import org.wso2.carbon.samples.orderApprovalService.stub.OrderApprovalServiceCallbackHandler; import org.wso2.carbon.samples.orderApprovalService.stub.OrderApprovalServiceStub; import java.rmi.RemoteException; public class PlaceOrderTestCase { public static void main(String[] args) { try { OrderApprovalServiceStub orderApprovalServiceStub = new OrderApprovalServiceStub("http://localhost:9763/services/OrderApprovalService"); PlaceOrderE placeOrderE = new PlaceOrderE(); PlaceOrder placeOrder = new PlaceOrder(); placeOrder.setSymbol("IBM"); placeOrder.setPrice(150); placeOrder.setQuantity(128); PlaceOrder[] placeOrders = new PlaceOrder[1]; placeOrders[0] = placeOrder; placeOrderE.setOrder(placeOrders); PlaceOrderRespone placeOrderRespone = null; //new PlaceOrderRespone(); try { placeOrderRespone = orderApprovalServiceStub.placeOrder(placeOrders); } catch (RemoteException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } OrderAccept[] orderAccepts = placeOrderRespone.getOrderAccept(); String result = orderAccepts[0].getMessage(); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
Rule Service Wizard
A rules service can be created through the rule service wizard of the WSO2 BRS management console, as follow.
1. Log in to the WSO2 BRS management console and select "Rule Service -> Create" in the "Main" menu.
2. The "Service Information" window opens. Enter the required information and click "Next". The service name has to be unique. For example,
3. The "Rule Set Information" window appears.
There are four ways to specify a rule set or a rule script.
- In-lined : Script is provided in the window itself
- Registry Key: Include the rule script in the system's governance registry. (Multiple scripts can be added here).
- Rule File: Upload a Drools rules file from a folder.
- URL: Upload from a url