This sample demonstrates how to use the BRS for a simple insurance service which uses business rules.
Prerequisites
To run this sample:
- Windows, Linux or Solaris operating system should exist.
- WSO2 BRS should be installed. To install the BRS, refer Installing the Product . This also includes installing Apache Ant as one of the Installation Prerequisites.
- WSO2 BRS should be started as described in Running the Product.
Sample configuration
Sample rule definition
Rules
A 20% increase in the insurance premium would apply if all the following conditions are met with regard to a car and a driver.
- The car is red.
- The car is in a sport class.
- The driver is male.
- The age of the driver is 16-25.
Facts
There are two facts, which are the Driver and Car and one result, which is the Policy.
There are two facts named Driver
and Car
. Policy
is the result of the rule execution.
package samples.insurance; /** * Car */ public class Car { private String style; private String color; public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } package samples.insurance; /** * Driver */ public class Driver { private int age; private String name; private boolean male; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isMale() { return male; } public void setMale(boolean male) { this.male = male; } } package samples.insurance; /** * Policy */ public class Policy { private double premium = 100000; public double getPremium() { return premium; } public void setPremium(double premium) { this.premium = premium; } public void increasePremium(double percentage) { setPremium((percentage + 1) * premium); } public void decreasePremium(double percentage) { setPremium((1 - percentage) * premium); } }
Rule service configuration (service.rsl)
An in-line rule set is used within the service.rsl file as follows.
<ruleService name="InsuranceService" xmlns="http://wso2.org/carbon/rules" targetNamespace="http://com.test/insurance"> <ruleSet> <rule resourceType="regular" sourceType="inline"> <![CDATA[ package insurance import samples.insurance.Driver; import samples.insurance.Car; import samples.insurance.Policy; rule "High Risk" when Driver( ( male == true) && ( age >= 16 ) && ( age <= 25 )) Car ( ( style == "sport") && ( color=="red" ) ) then Policy p = new Policy (); p.increasePremium(0.2); insertLogical(p); end ]]> </rule> </ruleSet> <operation name="applyForInsurance"> <input wrapperElementName="applyForInsurance" namespace="http://com.test/applyForInsurance"> <fact elementName="carInsurance" namespace="http://com.test/applyForInsurance" type="samples.insurance.Car"></fact> <fact elementName="driverInsurance" namespace="http://com.test/applyForInsurance" type="samples.insurance.Driver"></fact> </input> <output wrapperElementName="insurancerApplicationRespone" namespace="http://com.test/applyForInsurance"> <fact elementName="insurancePolicy" namespace="http://com.test/applyForInsurance" type="samples.insurance.Policy"></fact> </output> </operation> </ruleService>
Executing the service
To execute the service, run the ant
command from the <PRODUCT_HOME>/samples/insurance.service
directory to run the Insurance Service.
Before executing this service, it is recommended that you refer Exposing Rules as Services which explains in detail the process of writing and deploying a business rule.
Deploying and testing the service
- Deploy the rule service through the BRS management console. You can follow either of the two methods:
- Bundle all artifacts in an .aar file and upload it (Rule Service -> Upload menu).
- Create using the Rule Service wizard UI (Rule Service -> Create menu).
The above steps are discussed in detail in section Exposing Rules as Services .
- After deployment, click on List under Services in the main tab of the management console. The service will appear in the Deployed Services page.
- Click Insurance Service to access the dashboard of the service.
- Click Try this service in the Client Operations widget of the dashboard to invoke the Try-it tool.
Issue a request similar to the following in the Try-it tool.
The Car:<color>red</color> <style>sport</style>
The Driver:
<age>20</age> <name>your name</name> <male>true</male>
You would get the following result:<premium>120000.0</premium>
Alternatively, you can use Generate Axis2 Client link in the
Client Operations
widget of the dashboard to invoke the service. A client using generated stub codes is shown below where the codes were generated with the Unpacks the data binding classes check box checked.package org.wso2.carbon.samples; import org.apache.axis2.AxisFault; import org.wso2.carbon.samples.insuranceService.insurance.ApplyForInsurance; import org.wso2.carbon.samples.insuranceService.insurance.Car; import org.wso2.carbon.samples.insuranceService.insurance.Driver; import org.wso2.carbon.samples.insuranceService.insurance.Policy; import org.wso2.carbon.samples.insuranceService.stub.InsuranceServiceStub; import java.rmi.RemoteException; public class InsuranceServiceTestCase { public static void main(String[] args) { try { InsuranceServiceStub insuranceServiceStub = new InsuranceServiceStub("http://localhost:9763/services/InsuranceService"); ApplyForInsurance applyForInsurance = new ApplyForInsurance(); Car car = new Car(); car.setColor("red"); car.setStyle("sport"); Car[] cars = new Car[1]; cars[0] = car; Driver driver = new Driver(); driver.setName("your name"); driver.setAge(24); driver.setMale(true); Driver[] drivers = new Driver[1]; drivers[0] = driver; applyForInsurance.setCarInsurance(cars); applyForInsurance.setDriverInsurance(drivers); Policy[] policies = insuranceServiceStub.applyForInsurance(cars, drivers); double result = policies[0].getPremium(); System.out.println(result); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } }