Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This sample demonstrates a service for specifying the charge rates for calls through the use of business rules.

...


Prerequisites

...

To run this sample

...

Sample Rule Definition

Rules

...

:

Sample Configuration
 

Sample Rule Definition
 

Rules

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

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

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

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


Facts

There is one fact and one result as "named A call has been taken (CallLog)" and ". a charge should be calculated for the call (CallCharge)" is the result of the rule execution.

 

Code Block
languagejava
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)

An in-line rule set is used within the service.rsl file as follows.

 

Code Block
languagehtml/xml
    <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>
    </operation>
</ruleService>

 

Executing the Sample

To execute the sample, run the ant command from the  <BRS_Home>/samples/callcharging.service directory to run the CallCharging Service.

Info

Before executing this sample, 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

1.
  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 
  1. in section  Exposing Rules as Services .
2.
  1.  
  2. After deployment, click on List under Services in the main tab of the management console. The service will appear in
the
  1. 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
  1.  page.
  2. Click CallChargingService to access the dashboard of the service.
  3. Click Try this service in the Client Operations widget of the dashboard to invoke the Try-it tool
 to test the service
  1. .
4. In the service's Try-it window, issue
  1. Issue a request similar to the following in the Try the CallCharging service window.

 

Code Block
languagehtml/xml
<type>idd</type> 
<period>12</period>

Similarly send another request:

Code Block
languagehtml/xml
<type>local</type>
<period>12</period>

5. Alternatively, you can use Generate Client 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".

...