[Documentation Index]

WSO2 BRS - Samples : Call Charging Service

This guide demonstrates a service for specifying the charge rates for calls. This service uses business rules.

Please follow the User Guide before proceeding with this sample, if you have not already followed the User Guide.

Contents

Call Charging Service

Rules

Rule 1 : If the local calls were taken during daytime, then, the call rate is two dollars per minute.

Rule 2 : If the local calls were taken during night, then, the call rate is one dollar per minute.

Rule 3 : If the international were taken during daytime, then, the call rate is six dollars per minute.

Rule 4 : If the international were taken during night, then, the call rate is four dollars per minute.

Facts

There are a one fact and a one result - A call has been taken (CallLog) and a charge should be calculated for the call (CallCharge).

              
package samples.callcharging;

import java.util.Calendar;

/**
 * CallLog fact
 */
public class CallLog {

    private String type;

    private double period;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getPeriod() {
        return period;
    }

    public void setPeriod(double period) {
        this.period = period;
    }

    public int now() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.HOUR_OF_DAY);
    }
}

package samples.callcharging;

/**
 * CallCharge fact
 */
public class CallCharge {

    private double amount;

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }
}  

Rule Service Configuration - service.rsl

    <ruleService
        name="CallChargingService"
        xmlns="http://wso2.org/carbon/rules"
        targetNamespace="http://com.test/callcharging">
    <ruleSet>
        <rule resourceType="regular" sourceType="inline">
            <![CDATA[
                package callcharging

                import samples.callcharging.CallLog;
                import samples.callcharging.CallCharge;

                rule "Local Call During Daytime"
                when
                      callLog : CallLog( type == "local")
                           eval((6 < callLog.now()) && (callLog.now()< 18))
                then
                           CallCharge c = new CallCharge();
                           c.setAmount(callLog.getPeriod() * 2);
                           insertLogical(c);
                end

                rule "Local Call During Night"
                when
                      callLog : CallLog( type == "local")
                           eval((18 < callLog.now()) || (callLog.now()< 5))
                then
                           CallCharge c = new CallCharge();
                           c.setAmount(callLog.getPeriod() * 1);
                           insertLogical(c);
                end

                rule "IDD Call During Daytime"
                when
                      callLog : CallLog( type == "idd")
                           eval((6 < callLog.now()) && (callLog.now()< 18))
                then
                           CallCharge c = new CallCharge();
                           c.setAmount(callLog.getPeriod() * 6);
                           insertLogical(c);
                end

                rule "IDD Call During Night"
                when
                      callLog : CallLog( type == "idd")
                           eval((18 < callLog.now()) || (callLog.now()< 5))
                then
                           CallCharge c = new CallCharge();
                           c.setAmount(callLog.getPeriod() * 4);
                           insertLogical(c);
                end

            ]]>
        </rule>
    </ruleSet>
    <operation name="charge">
        <input wrapperElementName="callCharge" namespace="http://com.test/callCharge">
            <fact elementName="callLog" namespace="http://com.test/callCharge" type="samples.callcharging.CallLog"></fact>
        </input>
        <output wrapperElementName="callChargeRespone" namespace="http://com.test/callCharge">
            <fact elementName="callCharge" namespace="http://com.test/callCharge" type="samples.callcharging.CallCharge"></fact>
        </output>
    </operation>
</ruleService>
            

Deploying Service

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.

Testing Sample

You can use Try-it tool with the requests similar to the following.

<type>idd</type>
<period>12</period>

Another request

<type>local</type>
<period>12</period>

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 test.com.Client;

    import org.apache.axis2.AxisFault;
    import test.com.callcharge.CallCharge;
    import test.com.callcharge.CallChargeE;
    import test.com.callcharge.CallLog;
    import test.com.callcharging.CallChargingServiceStub;

    import java.rmi.RemoteException;

    public class CallChargingTestCase {
        public static void main(String[] args) {
            try {
                CallChargingServiceStub callChargingServiceStub = new CallChargingServiceStub("http://localhost:9763/services/CallChargingService");

                CallChargeE callChargeRequest = new CallChargeE();
                CallLog callLog = new CallLog();
                callLog.setPeriod(200);
                callLog.setType("local");
                CallLog[] callLogs = new CallLog[1];
                callLogs[0] = callLog;

                callChargeRequest.setCallLog(callLogs);

                CallCharge[] callCharges = callChargingServiceStub.charge(callLogs);
                if(callCharges != null){
                    for (CallCharge callCharge : callCharges){
                        System.out.println("Tha charge for the call : " + callCharge.getAmount());

                    }
                }
            } catch (AxisFault axisFault) {
                axisFault.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }
    }

References