com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_link3' is unknown.

Car Rental Service

This sample demonstrates a car rental service which uses business rules. 

Note

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 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 - 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>

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.

<type>unlimited</type> 
<MPD>18.5</MPD>

Similarly, send another request:

<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".

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();
        }
    }
}
com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links2' is unknown.