This guide demonstrates a simple greeting service which uses business rules.
Please follow the User Guide before proceeding with this sample, if you have not already followed the User Guide.
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 - A user logged-in into the system. Furthermore , GreetingMessage is used to present the result of the rule execution
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;
}
}<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>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.
<name>your name</name>
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 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;
userRequest.setUser(users);
GreetingMessage[] greetingMessages = greetingServiceStub.greetMe(users);
String result = greetingMessages[0].getMessage();
System.out.println(result);
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}