This sample demonstrates a simple greeting car rental service which uses business rules.
Info | ||
---|---|---|
| ||
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
Rule 1: A 3-dollar-per-mile fee will be charged for any additional miles.
Rule 2 : If the time is between 12 and 18, say good afternooncar reservation is daily, then the charge for a mile is 3 dollars.
Rule 2 3 : If the time is between 6 and 12, say good morningcar reservation is weekly, then the charge for a mile is 2.5 dollars.
Rule 3 4 : If the time is between 18 and 24, say good nightcar reservation is unlimited, then the charge for a mile is 2 dollars.
Facts
There is one fact, which is "A user logged-in into the system". Also, "GreetingMessage" is used to present the result of the rule execution.are two facts - Reservation and Charge.
Code Block | ||
---|---|---|
| ||
package samples.greeting; import java.util.Calendarcarrental; /** * UserReservation */ public class UserReservation { private double MPD; private String name type; public Stringdouble getNamegetMPD() { return nameMPD; } public void setNamesetMPD(Stringdouble nameMPD) { this.nameMPD = nameMPD; } public intString nowgetType() { Calendar cal = Calendar.getInstance(); return type; } public void setType(String type) { return cal.get(Calendar.HOUR_OF_DAY)this.type = type; } } package samples.greetingcarrental; /** * greeting messageCharge */ public class GreetingMessageCharge { 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.
Code Block | ||
---|---|---|
| ||
<ruleService name="GreetingServiceCarRentalService" xmlns="http://wso2.org/carbon/rules" targetNamespace="http://com.test/greetingcarrental"> <ruleSet> <rule resourceType="regular" sourceType="inline"> <![CDATA[ package carrental greeting import samples.greetingcarrental.GreetingMessageReservation; import samples.greetingcarrental.UserCharge; rule "IsDaily MorningReservation" no-loop true when when userr : User() Reservation( ( type == "daily" ) && ( MPD <= 18 ) ) then Charge charge = eval((6 < user.now()) && (user.now()< 12))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 GreetingMessageCharge msgcharge = new GreetingMessageCharge(); msg.setMessage("Good Morning " + user.getName() + " !!! "); 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(msgcharge); end rule "IsWeekly afternoonReservation" no-loop true when when userr : User() Reservation( (type == "weekly") && ( MPD <= 18 ) ) eval((12 <= user.now()) && (user.now() < 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 GreetingMessage Charge msgcharge = new GreetingMessageCharge(); msg.setMessage("Good Afternoon " + user.getName() + " !!! "); 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(msgcharge); end rule "IsUnlimited NightReservation" no-loop true when when userr : User()Reservation( (type == "unlimited") && ( MPD <= 18 ) ) then Charge charge = new Charge(); double total = eval( (18 <= user.now()) && (user.now() < 24))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 GreetingMessageCharge msgcharge = new GreetingMessageCharge(); msg.setMessage("Good Night " double total = (r.getMPD() - 18) * 3 + userr.getNamegetMPD() + " !!! " * 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(msgcharge); end ]]> </rule> </ruleSet> <operation name="greetMerent"> <input wrapperElementName="usercarReservation" namespace="http://com.test/greetingcarReservation"> <fact elementName="userreserve" namespace="http://com.test/greetingcarReservation" type="samples.greetingcarrental.UserReservation"></fact> </input> <output wrapperElementName="greetingMessagecarReservationRespone" namespace="http://com.test/greetingcarReservation"> <fact elementName="greetingMessagecharge" namespace="http://com.test/greetingcarReservation" type="samples.greetingcarrental.GreetingMessageCharge"></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.
Code Block | ||
---|---|---|
|
<name>your name</name>
<type>unlimited</type>
<MPD>18.5</MPD> |
Similarly, send another request:
Code Block | ||
---|---|---|
| ||
<type>daily</type>
<MPD>18</MPD> |
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".
Code Block | ||
---|---|---|
| ||
package org.wso2.carbon.samples; import org.apache.axis2.AxisFault; import import java.lang.Stringorg.wso2.carbon.samples.carRentalService.reservation.CarReservation; import org.wso2.carbon.samples.carRentalService.reservation.Charge; import org.wso2.carbon.samples.greetingServicecarRentalService.greetingreservation.*Reservation; import org.apache.axis2.AxisFault.wso2.carbon.samples.carRentalService.stub.CarRentalServiceStub; import java.rmi.RemoteException; public class GreetingServiceTestCaseCarRentalTestCase { public static void main(String[] args) { try { GreetingServiceStubCarRentalServiceStub greetingServiceStubcarRentalServiceStub = new GreetingServiceStubCarRentalServiceStub("http://localhost:9763/services/GreetingServiceCarRentalService"); CarReservation UserE userRequestcarReservation = new UserECarReservation(); UserReservation userreservation = new User(Reservation(); reservation.setMPD(18.5); userreservation.setNamesetType("your nameweekly"); UserReservation[] usersreservations = new UserReservation[1]; usersreservations[0] = userreservation; userRequestcarReservation.setUsersetReserve(usersreservations); GreetingMessageCharge[] greetingMessagescharges = greetingServiceStubcarRentalServiceStub.greetMerent(usersreservations); String result = greetingMessagescharges[0].getMessage(); System.out.println(result); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } } |
Excerpt | ||
---|---|---|
| ||
Greeting Car Rental service sample of the WSO2 Business Rules Server samples guide. |