[Documentation Index]

WSO2 BRS - Samples : Calculating Mortgage Insurance Premium

This guide demonstrates a service for calculating Mortgage Insurance Premium (PMI) using business rules.

Please follow the User Guide before proceeding with this sample, if you have not already followed the User Guide.

Contents

Calculating Mortgage Insurance Premium

Rules

Rule 1 : The mortgage insurance rate for PMI insurance is 0.5 and for FHA loans, it is 1.5.

Facts

There are one fact and one result - Client and Mortgage Insurance Premium.

              
package samples.MIPCalculate;

/**
 * Client fact
 */
public class Client {

    private String loanType;

    private double mortgageValue;

    private double downPayment;

    public String getLoanType() {
        return loanType;
    }

    public void setLoanType(String loanType) {
        this.loanType = loanType;
    }

    public double getMortgageValue() {
        return mortgageValue;
    }

    public void setMortgageValue(double mortgageValue) {
        this.mortgageValue = mortgageValue;
    }

    public double getDownPayment() {
        return downPayment;
    }

    public void setDownPayment(double downPayment) {
        this.downPayment = downPayment;
    }
}

package samples.MIPCalculate;

/**
 * MIP - Mortgage Insurance Premium
 */
public class MIP {

    private double annualMIP;

    public double getAnnualMIP() {
        return annualMIP;
    }

    public void setAnnualMIP(double annualMIP) {
        this.annualMIP = annualMIP;
    }
}

            

Rule Service Configuration - service.rsl

<ruleService
        name="MIPCalculateService"
        xmlns="http://wso2.org/carbon/rules"
        targetNamespace="http://com.test/MIPCalculateService">
    <ruleSet>
        <rule resourceType="regular" sourceType="inline">
            <![CDATA[
                package MIPCalculateService

                import samples.MIPCalculate.Client;
                import samples.MIPCalculate.MIP;

                rule "Calculate the MIP for PMI " no-loop true
                when
                    user : Client(loanType == "PMI")
                then
                    MIP mip = new MIP();
                    double loanAmount = user.getMortgageValue() - user.getDownPayment();
                    double lvt = loanAmount / user.getMortgageValue();
                    double lvtAmount = lvt * user.getMortgageValue();
                    mip.setAnnualMIP(lvtAmount * 0.05);
                    insertLogical(mip);

                end

                rule "Calculate the MIP for FHA loan" no-loop true
                when
                    user : Client(loanType == "FHA")
                then
                    MIP mip = new MIP();
                    double loanAmount = user.getMortgageValue() - user.getDownPayment();
                    double lvt = loanAmount / user.getMortgageValue();
                    double lvtAmount = lvt * user.getMortgageValue();
                    mip.setAnnualMIP(lvtAmount * 0.15);
                    insertLogical(mip);
                end
            ]]>
        </rule>
    </ruleSet>
    <operation name="calculate">
        <input wrapperElementName="placeClientDetail" namespace="http://com.test/placeClientDetail">
            <fact elementName="clientDetail" namespace="http://com.test/placeClientDetail" type="samples.MIPCalculate.Client"></fact>
        </input>
        <output wrapperElementName="placeClientDetailRespone" namespace="http://com.test/placeClientDetail">
            <fact elementName="MIP" namespace="http://com.test/placeClientDetail" type="samples.MIPCalculate.MIP"></fact>
        </output>
    </operation>
</ruleService>

Deploying Service

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.

Testing Sample

You can use Try-it tool with the requests similar to the following.

<loanType>FHA</loanType>
<mortgageValue>100000</mortgageValue>
<downPayment>90000</downPayment>

Another request

<loanType>PMI</loanType>
<mortgageValue>100000</mortgageValue>
<downPayment>90000</downPayment>

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.mipCalculateService.clientDetail.Client;
import org.wso2.carbon.samples.mipCalculateService.clientDetail.MIP;
import org.wso2.carbon.samples.mipCalculateService.clientDetail.PlaceClientDetail;
import org.wso2.carbon.samples.mipCalculateService.stub.MIPCalculateServiceStub;
import java.rmi.RemoteException;

public class MIPCalculateServiceTestCase {

    public static void main(String[] args) {

        try {
            MIPCalculateServiceStub mipCalculateServiceStub =
                    new MIPCalculateServiceStub("http://localhost:9763/services/MIPCalculateService");
            PlaceClientDetail placeClientDetail = new PlaceClientDetail();

            Client client = new Client();
            client.setLoanType("FHA");
            client.setDownPayment(8000);
            client.setMortgageValue(90000);
            Client[] clients = new Client[1];
            clients[0] = client;

            MIP[] mips = mipCalculateServiceStub.calculate(clients);
            double result = mips[0].getAnnualMIP();
            System.out.println(result);

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

References