com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_link3' is unknown.

Insurance Service

This guide demonstrates a simple insurance service which uses business rules.

Note

Before executing this sample, it is recommended that you refer to section Exposing Rules as Services which explains in detail the process of writing and deploying a business rule.

Sample Rule Definition

Rules

Given a car and a driver, if all of the following conditions are met, the consequence is a 20% insurance premium increase.

  • The car is red.
  • The car is in a sport class.
  • The driver is male.
  • The driver is between the age of 16-25.

Facts

There are two facts, which are the Driver and Car and one result, which is the 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);
    }
}

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>

 

Deploying and Testing the Service

1. 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.

2. After deployment, the service will appear in the "Deployed Services" window of the BRS management console. Access the service's dashboard by clicking on it.

3. In the "Client Operations" widget of the dashboard, invoke the Try-it tool to test the service.

4. In the service's TryIt window, issue a request similar to the following.

The Car:

 

<color>red</color>
<style>sport</style>
The Driver:

 

<age>20</age>
<name>your name</name>
<male>true</male>

5. Alternatively, you can use code generation 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 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();
        }
    }
}
com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links2' is unknown.