This documentation is for WSO2 Business Rules Server 2.0.0.View documentation for the latest release.

Versions Compared

Key

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

...

Facts

The three rules above use validating orders. All three rules are based on a customer placing an order, and can be captured using a Java bean as follows.

 

Code Block
languagejava
package samples.userguide;
/**
* Represents the fact of a customer places an order 
*/
public class PlaceOrder {

       String symbol;
       int quantity;
       double price;

       public String getSymbol() {
           return symbol;
       }

       public void setSymbol(String symbol) {
           this.symbol = symbol;
       }

       public int getQuantity() {
           return quantity;
       }

       public void setQuantity(int quantity) {
           this.quantity = quantity;
       }

       public double getPrice() {
           return price;
       }

       public void setPrice(double price) {
           this.price = price;
       }
}

 

The result of the execution of the three rules above is either an OrderAccept or OrderReject, which can be captured using Java beans as follows.

Code Block
languagejava
package samples.userguide;
/**
* Order accept notification
*/
public class OrderAccept {

       private String message;

       public String getMessage() {
           return message;
       }

       public void setMessage(String message) {
           this.message = message;
       }
}            

package samples.userguide;
/**
* Order Reject Notification
*/
       public class OrderReject {

       private String reason;

       public String getReason() {
            return reason;
       }

       public void setReason(String reason) {
            this.reason = reason;
       }
}

 

Using above the three rules, fact and result definitions , we can now define our rules using above and a rule language of a rule engine, you can define new rules. WSO2 BRS is based on the JSR 94 Java Rule Specification and uses the open source Drools rule engine as the default JSR 94 provider. Let us Shown below is how to write the rules of our example usecase using Drools rule language.

Drools Rule File

Code Block
languagenone
package OrderApproval;

import samples.userguide.OrderAccept;
import samples.userguide.OrderReject;
import samples.userguide.PlaceOrder;

rule "Order Approval Rule" dialect "mvel" no-loop true salience 4

when
$placeOrder : PlaceOrder( ( symbol == "IBM" && quantity > 10 ) || ( symbol == "SUN" && price > 100 ) || ( symbol == "MSFT" && price > 50 && quantity < 200 ) )
then

OrderAccept orderAccept = new OrderAccept();
orderAccept.setMessage("Accepted order for: "+ $placeOrder.quantity + " stocks of "+
$placeOrder.symbol +" at$ " + $placeOrder.price);
insertLogical(orderAccept);

end

rule "IBM Order Deny Rule" dialect "mvel" no-loop true salience 3

when
not ( OrderAccept())
$placeOrder : PlaceOrder( symbol == "IBM" )
then
retract($placeOrder);
OrderReject orderReject = new OrderReject();
orderReject.setReason("An Order for stocks of IBM is accepted only if the number of stocks is higher than 10.");
insertLogical(orderReject);
end

rule "SUN Order Deny Rule" dialect "mvel" no-loop true salience 2
when
not ( OrderAccept())
$placeOrder : PlaceOrder( symbol == "SUN" )
then
retract($placeOrder);
OrderReject orderReject = new OrderReject();
orderReject.setReason("An Order for stocks of SUN is accepted only if the stock price is higher than 100 $.");
insertLogical(orderReject);
end

rule "MSFT Order Deny Rule" dialect "mvel" no-loop true salience 1
when
not ( OrderAccept())
$placeOrder : PlaceOrder( symbol == "MSFT" )
then
retract($placeOrder);
OrderReject orderReject = new OrderReject();
orderReject.setReason("An Order for stocks of MSFT is accepted only if the stock price is higher than 50 $ and the number of stocks is lower than 200.");
insertLogical(orderReject);
end

 

 

Creating and Deploying the Rule Service

There are two ways for creating to create a rule service using Admin Console : the WSO2 BRS management console as follows:

...

...

Rule Service Upload

This demonstrates how to use the rule service upload UI for deploying a rule service.

Step 1 : Create a valid jar containing the java classes of facts and results defined in the Order Approval Sample presented above.

...