Versions Compared

Key

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

This sample demonstrates a simple greeting service the stateful behavior of a rule session through a simple shopping application which uses business rules. This sample should be deployed in a stateful axis2 session - i.e. session scope or transport scope.

Info
titleNote

Before executing this sample, it is recommended that you refer to section Exposing Rules as Services which explains in detail the process of writing and deploying a business rule.

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, which is "A user logged-in into the system". Also, "GreetingMessage" is used to present the result of the rule executionApply 10% discount if total purchases is over 100.

Facts

There are two facts, which are "A customer made a purchase" and "a product is added".

 

Code Block
languagejava
package samples.greeting;
import java.util.Calendarshopping;

/**
 * UserProduct
 */
public class UserProduct {

    private String name;
    private float price;

    public String getName() {
        return name;
    }

    public voidfloat setNamegetPrice(String name) {
        this.name = namereturn price;
    }

    public intvoid nowsetName(String name) {
        Calendarthis.name cal = Calendar.getInstance();name;
    }

    public returnvoid cal.get(Calendar.HOUR_OF_DAY);
setPrice(float price) {
   } }    this.price = package samples.greetingprice;
/**  * greeting message  }
}

package samples.shopping;

/**
 * Customer
 */
public class GreetingMessageCustomer {

    private String message name;

   public Stringprivate getMessage() {int discount;

    public Customer(String name,
return message;     }     public void setMessage(String message) {     int discount) {
  this.message = message;    this.name }
}

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;
       name="GreetingService" this.discount = discount;
    }

 xmlns="http://wso2.org/carbon/rules"   public String getName() {
 targetNamespace="http://com.test/greeting">     <ruleSet>  return name;
    <rule}
resourceType="regular"
sourceType="inline">    public int getDiscount() {
    <![CDATA[    return discount;
    }

    public void package greeting
setDiscount(int discount) {
        this.discount = discount;
    import samples.greeting.GreetingMessage;}

    public void setName(String name) {
      import samples.greeting.User;

 this.name = name;
    }
}

package samples.shopping;

/**
 * Discount
rule "Is Morning" no-loop true
*/
public class Discount {

    private Customer customer;
    private int whenamount;

    public Discount(Customer customer,
           user : User()       int amount) {
        this.customer  eval((6 < user.now()) && (user.now()< 12))= customer;
        this.amount = amount;
    }

   then public Customer getCustomer() {
        return customer;
    }
GreetingMessage
msg = new GreetingMessage(); public int getAmount() {
        return amount;
    }
msg.setMessage("Good
Morning  " + user.getName() + " !!! ");
public void setCustomer(Customer customer) {
        this.customer = customer;
    }

 insertLogical(msg);   public void setAmount(int amount) {
        endthis.amount = amount;
    }
}

package samples.shopping;

/**
 * Purchase
rule "Is afternoon" no-loop true
*/
public class Purchase {

    private String customer;
    private String whenproduct;

    public String getCustomer() {
        return customer;
user : User()  }

    public void setCustomer(String customer) {
       eval((12 <= user.now()) && (user.now() < 18))
 this.customer = customer;
    }

    public String getProduct() {
     then   return product;
    }

    public void setProduct(String product) {
GreetingMessage msg = new GreetingMessage();    this.product = product;
    }
}

package samples.shopping;

/**
  msg.setMessage("Good Afternoon  " + user.getName() + " !!! ");* Purchase Log
 */
public class PurchaseLog {

    private String customer;

    private Product product;

    public insertLogicalPurchaseLog(msg);String customer, Product product) {
        this.customer = customer;
end        this.product = product;
    }

rule "Is Night" no-loop truepublic String getCustomer() {
        return customer;
  when  }

    public void setCustomer(String customer) {
       user : User()
this.customer = customer;
    }

    public Product getProduct() {
    eval( (18 <= user.now()) && (user.now() < 24)) return product;
    }

    public void setProduct(Product product) {
  then      this.product = product;
    }
}

Rule Service Configuration (service.rsl)

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

 

Code Block
languagehtml/xml
<ruleService
      GreetingMessage msg name="ShoppingService"
new GreetingMessage();       xmlns="http://wso2.org/carbon/rules"
            msg.setMessage("Good Night  " + user.getName() + " !!! ");targetNamespace="http://com.test/shopping"
	    scope="transportsession">
    <ruleSet>
        <rule resourceType="regular" sourceType="inline">
   insertLogical(msg);         <![CDATA[
      end          package  ]]>shopping

      </rule>     </ruleSet>     <operationdialect name="greetMe">mvel"

           <input wrapperElementName="user" namespace="http://com.test/greeting">    import samples.shopping.Customer;
              <fact elementName="user" namespace="http://com.test/greeting" type="samples.greeting.User"></fact> import samples.shopping.Product;
       </input>        <output wrapperElementName="greetingMessage" namespace="http://com.test/greeting">import samples.shopping.Purchase;
              <fact elementName="greetingMessage" namespace="http://com.test/greeting" type="samples.greeting.GreetingMessage"></fact> import samples.shopping.Discount;
       </output>         </operation>
</ruleService>

 

Deploying and Testing the Service

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.

 

Code Block
languagehtml/xml
<name>your name</name>

Image Removed

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".

 

...

languagejava

...

import samples.shopping.PurchaseLog;

                rule "Purchase notification new customer"
                    salience 11 no-loop true
                when
                    $p : Purchase()
                    not Customer(name == $p.customer)
                then
                    System.out.println( "New Customer : " + $p.customer);
                    insert(new Customer($p.customer, 0));
                end

                rule "Purchase notification"
                    salience 10 no-loop true
                when
                    $c : Customer()
                    $purchase : Purchase( customer == $c.name)
                    $product : Product( name == $purchase.product)
                then
                    insert(new PurchaseLog($c.name, $product));
                    modify($c){
                     setDiscount(0)
                    }
                    System.out.println( "Customer " + $c.name + " just purchased " + $product.name );
                end

                rule "Apply 10% discount if total purchases is over 100" salience 9  no-loop true dialect "java"
                    when
                            $p : Purchase()
                $c : Customer(name == $p.customer)
                $i : Double(doubleValue  > 100) from accumulate ( PurchaseLog( customer == $c.name, $price : product.price ),
                                                                            sum( $price ) )
                    then
                  $c.setDiscount( 10 );
                insert( new Discount($c, 10) );
                System.out.println( "Customer " + $c.getName() + " now has a shopping total of " + $i );
                end

                rule "cleanUp" salience 8
                when
                 $p : Purchase()
                then
                 retract($p);
                end
            ]]>
        </rule>
    </ruleSet>

 	<operation name="purchase">
        <input wrapperElementName="purchaseOrder" namespace="http://com.test/purchaseOrder">
            <fact elementName="purchase" namespace="http://com.test/purchaseOrder" type="samples.shopping.Purchase"></fact>
        </input>
        <output wrapperElementName="purchaseOrderRespone" namespace="http://com.test/purchaseOrder">
            <fact elementName="discount" namespace="http://com.test/purchaseOrder" type="samples.shopping.Discount"></fact>
        </output>
    </operation>
    <operation name="addProduct">
        <input wrapperElementName="addProduct" namespace="http://com.test/addProduct">
            <fact elementName="addProduct" namespace="http://com.test/addProduct" type="samples.shopping.Product"></fact>
        </input>
    </operation>
</ruleService>

Axis2 Service Configuration (services.xml)

 

Code Block
languagehtml/xml
<service name="ShoppingService" scope="transportsession">   
    <operation name="purchase"/>
    <operation name="addProduct" mep="http://www.w3.org/ns/wsdl/in-only"/>
</service>

 

Deploying and Testing the Service

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. First add a product before buying it.

Adding a product:

 

Code Block
languagehtml/xml
<name>product name</name>
<price>12.34</price>

 

Doing a purchase:

 

Code Block
languagehtml/xml
<customer>your name </customer>
<product>product name</product>

 

 

5. Alternatively, you can use code generation link in the  "Client Operations" widget of the dashboard to invoke the service. However, in the axis2 stub-based clients, there is a limitation in managing session with in-only operations such as 'addProduct'. Therefore, prior to use the code generation, you have to remove mep=http://www.w3.org/ns/wsdl/in-only from the service xml and redeploy the service. This makes the 'addProduct' method return an empty result.

 

A client using generated stub codes is shown below where the codes were generated with option "Unpacks the databinding classes".

 

Code Block
languagejava
package org.wso2.carbon.samples;

import org.apache.axis2.AxisFault;
import org.wso2.carbon.samples.shoppingService.product.AddProduct;
import org.wso2.carbon.samples.shoppingService.product.Product;
import org.wso2.carbon.samples.shoppingService.purchaseOrder.Discount;
import org.wso2.carbon.samples.shoppingService.purchaseOrder.Purchase;
import org.wso2.carbon.samples.shoppingService.purchaseOrder.PurchaseOrder;
import org.wso2.carbon.samples.shoppingService.stub.ShoppingServiceStub;
import java.rmi.RemoteException;

public class ShoppingServiceTestCase {

    public static void main(String[] args) {

        ShoppingServiceStub shoppingServiceStub = null;
        try {
            shoppingServiceStub = new ShoppingServiceStub("http://localhost:9763/services/ShoppingService");
            shoppingServiceStub._getServiceClient().getOptions().setManageSession(true);

            AddProduct addProduct = new AddProduct();
            Product product = new Product();
            product.setName("toy");
            product.setPrice(200);
            Product[] products = new Product[1];
            products[0] = product;

            try {shoppingServiceStub.addProduct(products);

            GreetingServiceStubPurchaseOrder greetingServiceStubpurchaseOrder = new GreetingServiceStub("http://localhost:9763/services/GreetingService"PurchaseOrder();
            Purchase UserE userRequestpurchase = new UserEPurchase();
  ;
         User user = new User( purchase.setCustomer("Ishara");
            userpurchase.setNamesetProduct("your nametoy");

            UserPurchase[] userspurchases = new UserPurchase[1];
            userspurchases[0] = userpurchase;

            userRequestpurchaseOrder.setUsersetPurchase(userspurchases);
             GreetingMessageDiscount[] greetingMessagesdiscounts = greetingServiceStubshoppingServiceStub.greetMepurchase(userspurchases);
            Stringint result = greetingMessagesdiscounts[0].getMessagegetAmount();
            System.out.println(result);

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
Excerpt
hiddentrue

Greeting Shopping service sample of the WSO2 Business Rules Server samples guide.