This documentation is for WSO2 Business Rules Server 2.1.0.View documentation for the latest release.
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »


This sample demonstrates how to use the BRS for a simple greetings service.


To run this sample:


Sample Configuration

Sample Rule Definition


Rule 1 : If the time is between 12 and 18, say good afternoon.

Rule 2 : If the time is between 6 and 12, say good morning.

Rule 3 : If the time is between 18 and 24, say good night.


There is one fact named A user logged in into the system and a result named GreetingMessage.

package samples.greeting;
import java.util.Calendar;

 * User
public class User {
    private String name ;

    public String getName() {
        return name;
    public void setName(String name) { = name;
    public int now(){
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.HOUR_OF_DAY);
package samples.greeting;
 * greeting message
public class GreetingMessage {
    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.

       <rule resourceType="regular" sourceType="inline">
               package greeting

               import samples.greeting.GreetingMessage;
               import samples.greeting.User;

               rule "Is Morning" no-loop true
                   user : User()
                   eval((6 < && (< 12))
                   GreetingMessage msg = new GreetingMessage();
                   msg.setMessage("Good Morning  " + user.getName() + " !!! ");

               rule "Is afternoon" no-loop true
                   user : User()
                   eval((12 <= && ( < 18))
                   GreetingMessage msg = new GreetingMessage();
                   msg.setMessage("Good Afternoon  " + user.getName() + " !!! ");

               rule "Is Night" no-loop true
                   user : User()
                   eval( (18 <= && ( < 24))
                   GreetingMessage msg = new GreetingMessage();
                   msg.setMessage("Good Night  " + user.getName() + " !!! ");
    <operation name="greetMe">
       <input wrapperElementName="user" namespace="http://com.test/greeting">
           <fact elementName="user" namespace="http://com.test/greeting" type="samples.greeting.User"></fact>
       <output wrapperElementName="greetingMessage" namespace="http://com.test/greeting">
           <fact elementName="greetingMessage" namespace="http://com.test/greeting" type="samples.greeting.GreetingMessage"></fact>


Executing the sample

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

Deploying the sample

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.


<name>your name</name>

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 java.lang.String;
import org.wso2.carbon.samples.greetingService.greeting.*;
import org.apache.axis2.AxisFault;
import java.rmi.RemoteException;

public class GreetingServiceTestCase {

    public static void main(String[] args) {

        try {
            GreetingServiceStub greetingServiceStub = new GreetingServiceStub("http://localhost:9763/services/GreetingService");

            UserE userRequest = new UserE();
            User user = new User();
            user.setName("your name");
            User[] users = new User[1];
            users[0] = user;

            GreetingMessage[] greetingMessages = greetingServiceStub.greetMe(users);
            String result = greetingMessages[0].getMessage();

        } catch (AxisFault axisFault) {
        } catch (RemoteException e) {
  • No labels