This guide demonstrates a simple insurance service which uses business rules.
Please follow the User Guide before proceeding with this sample, if you have not already followed the User Guide.
Given a car and a driver, if all of the following conditions are met :
There are two facts - Driver and Car , and a one result - Policy.
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);
}
}<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>You can either create an .aar file and upload or create a rule service using rule service wizard UI. Please refer the User Guide for more information.
Put following in the car
<color>red</color> <style>sport</style>
Put following in the Driver
<age>20</age> <name>your name</name> <male>true</male>
You can also use the code generation. There is an option for code generation in the services management page. A client using generated stub codes is shown bellow. Codes was generated with option - Unpacks the databinding classes.
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();
}
}
}