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 9 Next »

Introduction

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

Prerequisites

Before building the sample, the following needs to be done:

Sample Rule Definition

Rules

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.

Facts

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) {
        this.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.

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

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

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

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

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

 

Building the sample

 

Executing the sample

To execute the sample:

  1. Install and run the WSO2 BRS using instructions in the Installation Guide.This includes downloading and installing Apache Ant as one of the Installation Prerequisites.
  2. Run the ant command from the <BRS_Home>/samples/greeting.service directory to run the Greetings Service.

Analyzing the output

  • No labels