Car Rental Service
This sample demonstrates a car rental 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
Rule 1: A 3-dollar-per-mile fee will be charged for any additional miles.
Rule 2 : If the car reservation is daily, then the charge for a mile is 3 dollars.
Rule 3 : If the car reservation is weekly, then the charge for a mile is 2.5 dollars.
Rule 4 : If the car reservation is unlimited, then the charge for a mile is 2 dollars.
Facts
There are two facts named Reservation
and Charge
.
package samples.carrental; /** * Reservation */ public class Reservation { private double MPD; private String type; public double getMPD() { return MPD; } public void setMPD(double MPD) { this.MPD = MPD; } public String getType() { return type; } public void setType(String type) { this.type = type; } } package samples.carrental; /** * Charge */ public class Charge { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
Rule service Configuration (service.rsl)
An in-line rule set is used within the service.rsl file as follows.
<ruleService name="CarRentalService" xmlns="http://wso2.org/carbon/rules" targetNamespace="http://com.test/carrental"> <ruleSet> <rule resourceType="regular" sourceType="inline"> <![CDATA[ package carrental import samples.carrental.Reservation; import samples.carrental.Charge; rule "Daily Reservation" when r : Reservation( ( type == "daily" ) && ( MPD <= 18 ) ) then Charge charge = new Charge(); double total = r.getMPD() * 3; charge.setMessage("The charge for a mile is 3 dollars. The total charge is " + total ); insertLogical(charge); end rule "Daily Reservation with additional miles" when r : Reservation( (type == "daily") && ( MPD > 18 ) ) then Charge charge = new Charge(); double total = (r.getMPD() - 18) * 3 + r.getMPD() * 3; charge.setMessage("The charge for a mile is 3 dollars and the amount of 3 dollar a per mile fee will be charged for any additional miles. The total charge is " + total ); insertLogical(charge); end rule "Weekly Reservation" when r : Reservation( (type == "weekly") && ( MPD <= 18 ) ) then Charge charge = new Charge(); double total = r.getMPD() * 2.5; charge.setMessage("The charge for a mile is 2.5 dollars. The total charge is " + total ); insertLogical(charge); end rule "Weekly Reservation with additional miles" when r : Reservation( (type == "weekly") && ( MPD > 18 ) ) then Charge charge = new Charge(); double total = (r.getMPD() - 18) * 3 + r.getMPD() * 2.5; charge.setMessage("The charge for a mile is 2.5 dollars and the amount of 3 dollar a per mile fee will be charged for any additional miles. The total charge is " + total ); insertLogical(charge); end rule "Unlimited Reservation" when r : Reservation( (type == "unlimited") && ( MPD <= 18 ) ) then Charge charge = new Charge(); double total = r.getMPD() * 2; charge.setMessage("The charge for a mile is 2 dollars. The total charge is " + total ); insertLogical(charge); end rule "Unlimited Reservation with additional miles" when r : Reservation( (type == "unlimited") && ( MPD > 18 ) ) then Charge charge = new Charge(); double total = (r.getMPD() - 18) * 3 + r.getMPD() * 2; charge.setMessage("The charge for a mile is 2 dollars and the amount of 3 dollar a per mile fee will be charged for any additional miles. The total charge is " + total ); insertLogical(charge); end ]]> </rule> </ruleSet> <operation name="rent"> <input wrapperElementName="carReservation" namespace="http://com.test/carReservation"> <fact elementName="reserve" namespace="http://com.test/carReservation" type="samples.carrental.Reservation"></fact> </input> <output wrapperElementName="carReservationRespone" namespace="http://com.test/carReservation"> <fact elementName="charge" namespace="http://com.test/carReservation" type="samples.carrental.Charge"></fact> </output> </operation> </ruleService>
Executing the service
To execute the service, run the ant
command from the <PRODUCT_HOME>/samples/carrental.service
directory to run the Car Rental 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 CarRentalService 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:
<type>unlimited</type> <MPD>18.5</MPD>
You would get the following response:
<message>The charge for a mile is 2 dollars and the amount of 3 dollar a per mile fee will be charged for any additional miles. The total charge is 38.5</message>
Issue another request as follows:
<type>daily</type> <MPD>18</MPD>
You would get the following response:<message>The charge for a mile is 3 dollars. The total charge is 54.0</message>
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.
package org.wso2.carbon.samples; import org.apache.axis2.AxisFault; import org.wso2.carbon.samples.carRentalService.reservation.CarReservation; import org.wso2.carbon.samples.carRentalService.reservation.Charge; import org.wso2.carbon.samples.carRentalService.reservation.Reservation; import org.wso2.carbon.samples.carRentalService.stub.CarRentalServiceStub; import java.rmi.RemoteException; public class CarRentalTestCase { public static void main(String[] args) { try { CarRentalServiceStub carRentalServiceStub = new CarRentalServiceStub("http://localhost:9763/services/CarRentalService"); CarReservation carReservation = new CarReservation(); Reservation reservation = new Reservation(); reservation.setMPD(18.5); reservation.setType("weekly"); Reservation[] reservations = new Reservation[1]; reservations[0] = reservation; carReservation.setReserve(reservations); Charge[] charges = carRentalServiceStub.rent(reservations); String result = charges[0].getMessage(); System.out.println(result); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } }