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

Introduction

This sample demonstrates an order approval process which uses business rules to check whether the required conditions for approving an order are met.

Prerequisites

To run this sample:

Sample configuration


Sample rule definition

Rules

Rule 1 : An order for stocks of Company IBM is accepted only if the number of stocks is higher than 10.
Rule 2 : An order for stocks of Company SUN is accepted only if the stock price is higher than 100 $.
Rule 3 : An order for stocks of Company MSFT is accepted only if the stock price is higher than 50 $ and the number of stocks is lower than 200.

Facts

There is one fact named Order. OrderAccept and OrderReject are used to capture the result of the service.

Rule service configuration (service.rsl) 

service.rsl
<ruleService
        name="OrderApprovalService"
        xmlns="http://wso2.org/carbon/rules"
        targetNamespace="http://com.test/orderApproval">
  <ruleSet>
        <rule resourceType="regular" sourceType="file">orderApprovalRules.drl</rule>
    </ruleSet>
    <operation name="placeOrder">
        <input wrapperElementName="placeOrder" namespace="http://com.test/placeorder">
            <fact elementName="order" namespace="http://com.test/placeorder" type="samples.userguide.PlaceOrder"></fact>
        </input>
        <output wrapperElementName="placeOrderRespone" namespace="http://com.test/placeorder">
            <fact elementName="orderAccept" namespace="http://com.test/placeorder" type="samples.userguide.OrderAccept"></fact>
            <fact elementName="orderReject" namespace="http://com.test/placeorder" type="samples.userguide.OrderReject"></fact>
        </output>
    </operation>
</ruleService>


Executing the service

To execute the service, run the ant command from the  <PRODUCT_HOME>/samples/orderApproval.service directory to run the OrderApproval 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 OrderApproval 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. Issue a request similar to the following in the Try-it tool.
     

    <price>10</price>
    <quantity>9</quantity>
    <symbol>IBM</symbol> 


    You would get the following response:

      <orderReject>
          <reason>An Order for stocks of IBM is accepted only if the number of stocks is higher than 10.</reason>
      </orderReject>
  6. Enter another request as follows:

    <price>120</price>
    <quantity>100</quantity>
    <symbol>SUN</symbol> 

    You would get the following response

     <orderAccept>
          <message>Accepted order for: 100 stocks of SUN at$ 120.0</message>
       </orderAccept>
  7. Enter another request as follows:

    <price>50</price>
    <quantity>160</quantity>
    <symbol>MSFT</symbol> 


    You would get the following result: 

      <orderReject>
          <reason>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.</reason>
       </orderReject>
  8. 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.wso2.carbon.samples.orderApprovalService.order.OrderAccept;
    import org.wso2.carbon.samples.orderApprovalService.order.PlaceOrder;
    import org.wso2.carbon.samples.orderApprovalService.order.PlaceOrderE;
    import org.wso2.carbon.samples.orderApprovalService.order.PlaceOrderRespone;
    import org.wso2.carbon.samples.orderApprovalService.stub.OrderApprovalServiceCallbackHandler;
    import org.wso2.carbon.samples.orderApprovalService.stub.OrderApprovalServiceStub;
    import java.rmi.RemoteException;
    
    public class PlaceOrderTestCase {
    
        public static void main(String[] args) {
    
            try {
                OrderApprovalServiceStub orderApprovalServiceStub = new OrderApprovalServiceStub("http://localhost:9763/services/OrderApprovalService");
                PlaceOrderE placeOrderE = new PlaceOrderE();
                PlaceOrder placeOrder = new PlaceOrder();
                placeOrder.setSymbol("Company A");
                placeOrder.setPrice(150);
                placeOrder.setQuantity(128);
                PlaceOrder[] placeOrders = new PlaceOrder[1];
                placeOrders[0] = placeOrder;
                placeOrderE.setOrder(placeOrders);
    
                PlaceOrderRespone placeOrderRespone = null; //new PlaceOrderRespone();
                try {
                    placeOrderRespone = orderApprovalServiceStub.placeOrder(placeOrders);
                } catch (RemoteException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                OrderAccept[] orderAccepts = placeOrderRespone.getOrderAccept();
                String result = orderAccepts[0].getMessage();
                System.out.println(result);
    
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • No labels