This sample demonstrates a service for calculating Mortgage Insurance Premium (PMIMIP) using business rules.
...
Prerequisites
To run this sample
...
Sample Rule Definition
Rules
...
:
- 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
Table of Contents | ||||
---|---|---|---|---|
|
Sample rule definition
Rules
The mortgage insurance rate is 0.5 for MIP insurance and 1.5 for FHA loans.
Facts
There is one fact and a result as Client and named Client. The result of the rule execution is Mortgage Insurance Premium.
Code Block | ||
---|---|---|
| ||
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 Configurationservice configuration (service.rsl)
An in-line rule set is used within the service.rsl file as follows.
Code Block | ||
---|---|---|
| ||
<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> |
Executing the service
Deploying and Testing the Service
1. To execute the service, run the ant
command from the <PRODUCT_HOME>/samples/greeting.service
directory to run the InsuranceCalculation Service.
Info |
---|
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
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.
...
- page.
- Click InsuranceCalculation 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 as follows in the Try-it tool:
Code Block language html/xml <loanType>FHA</loanType> <mortgageValue>100000</mortgageValue> <downPayment>90000</downPayment>
...
...
You would get the following result:
Code Block language html/xml <annualMIP>1500.0</annualMIP>
Issue another request as follows:
Code Block language html/xml <loanType>PMI</loanType> <mortgageValue>100000</mortgageValue> <downPayment>90000</downPayment>
...
...
You would get the following result:
Code Block language html/xml <annualMIP>500.0</annualMIP>
Alternatively, you can
...
click Generate Axis2 Client 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 databinding classes
...
check box checked.
Code Block language java 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(); } } }
Excerpt hidden true Mortgage insurance premium calculation service sample of the WSO2 Business Rules Server samples guide.