||
Skip to end of metadata
Go to start of metadata

This sample demonstrates 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.

Prerequisites

To run this sample:

Sample configuration

Sample rule definition

Rules

Rule 1: Apply a 10% discount if the value of total purchases is over 100.

Facts

There are two facts named A customer made a purchase and A product is added.

 

package samples.shopping;

/**
 * Product
 */
public class Product {

    private String name;
    private float price;

    public String getName() {
        return name;
    }

    public float getPrice() {
        return price;
    }

    public void setName(String name) {
        this.name = name;
    }

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

package samples.shopping;

/**
 * Customer
 */
public class Customer {

    private String name;

    private int discount;

    public Customer(String name,
                    int discount) {
        this.name = name;
        this.discount = discount;
    }

    public String getName() {
        return name;
    }

    public int getDiscount() {
        return discount;
    }

    public void setDiscount(int discount) {
        this.discount = discount;
    }

    public void setName(String name) {
        this.name = name;
    }
}

package samples.shopping;

/**
 * Discount
 */
public class Discount {

    private Customer customer;
    private int amount;

    public Discount(Customer customer,
                    int amount) {
        this.customer = customer;
        this.amount = amount;
    }

    public Customer getCustomer() {
        return customer;
    }

    public int getAmount() {
        return amount;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
}

package samples.shopping;

/**
 * Purchase
 */
public class Purchase {

    private String customer;
    private String product;

    public String getCustomer() {
        return customer;
    }

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

    public String getProduct() {
        return product;
    }

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

package samples.shopping;

/**
 * Purchase Log
 */
public class PurchaseLog {

    private String customer;

    private Product product;

    public PurchaseLog(String customer, Product product) {
        this.customer = customer;
        this.product = product;
    }

    public String getCustomer() {
        return customer;
    }

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

    public Product getProduct() {
        return product;
    }

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

Rule service configuration (service.rsl)

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

 

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

                dialect "mvel"

                import samples.shopping.Customer;
                import samples.shopping.Product;
                import samples.shopping.Purchase;
                import samples.shopping.Discount;
                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)

 

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

 

Executing the service

To execute the service, run the ant command from the  <PRODUCT_HOME>/samples/shopping.service directory to run the Shopping Service.

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

 

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, click on List under Services in the main tab of the management console. The service will appear in the Deployed Services page.
  3. Click Shopping Service to access the dashboard of the service.
  4. Click Try this service in the Client Operations widget of the dashboard to invoke the Try-it tool.
  5. Click addProduct in the left pane of the Try-it tool and issue a request similar to the following in the Try-it tool to add a product:

    <name>product name</name>
    <price>12.34</price>


    You would get the following result:
     

    <axis2ns11:addProductResponse xmlns:axis2ns11="http://com.test/shopping"/>


  6. Click purchase in the left pane of the Try-it Tool and issue another request similar to the following to make a purchase:

    <customer>Tester</customer>
    <product>Test Product</product>

     

    The following would be logged in the terminal:

     New Customer : Tester
    
    Customer Tester just purchased Test Product

    Repeat this step ten times. You would get the following result in the Try-it tool:

     <discount>10</discount>
     <name>Tester</name>
  7. Alternatively, you can use Generate Axis2 Client 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 the Unpacks the data binding classes check box checked.

    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;
    
                shoppingServiceStub.addProduct(products);
    
                PurchaseOrder purchaseOrder = new PurchaseOrder();
                Purchase purchase = new Purchase();
                purchase.setCustomer("Ishara");
                purchase.setProduct("toy");
    
                Purchase[] purchases = new Purchase[1];
                purchases[0] = purchase;
    
                purchaseOrder.setPurchase(purchases);
                Discount[] discounts = shoppingServiceStub.purchase(purchases);
                int result = discounts[0].getAmount();
                System.out.println(result);
    
            } catch (AxisFault axisFault) {
                axisFault.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

 

 

  • No labels