This guide demonstrates a part of a Car Rental Service which uses business rules.
Please follow the User Guide before proceeding with this sample, if you have not already followed the User Guide.
Rule 1: The amount of 3 dollar a 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.
There are two facts - 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; } }
<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>
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.
<type>unlimited</type> <MPD>18.5</MPD>
Another request
<type>daily</type> <MPD>18</MPD>
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.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(); } } }