Versions Compared

Key

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

...

Sample

...

configuration

Table of Contents
maxLevel4
minLevel4

Sample

...

rule definition

Rules

Rule 1: If the customer has bronze status, then the item costs five dollars.

Rule 2 : If the customer has silver status, then the item costs four dollars.

Rule 3 : If the customer has gold status, then the item costs three dollars.

Facts

There is a fact named A customer made a get quote request . Quotation is used to capture the result of the rules execution.

Code Block
languagejava
package samples.quotation;

 /**
  * Customer fact
 */
 public class Customer {

    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
 }

 package samples.quotation;

 /**
  * Quotation result
 */
 public class Quotation {

    private int price;

    public int getPrice() {
        return price;
    }

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

Rule

Service Configuration

service configuration (service.rsl)

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

 

Code Block
languagehtml/xml
<ruleService
        name="GetQuoteService"
        xmlns="http://wso2.org/carbon/rules"
        targetNamespace="http://com.test/quotation">
    <ruleSet>
        <rule resourceType="regular" sourceType="inline">
            <![CDATA[
                package quotation

                import samples.quotation.Customer;
                import samples.quotation.Quotation;

                rule "Gold Customer"
                when
                      Customer( status == "gold" )
                then
                      Quotation q = new Quotation();
                      q.setPrice(3);
                      insertLogical(q);
                end

                rule "Silver Customer"
                when
                      Customer( status == "silver" )
                then
                      Quotation q = new Quotation();
                      q.setPrice(4);
                      insertLogical(q);
                end

                rule "Bronze Customer"
                when
                      Customer( status == "bronze" )
                then
                      Quotation q = new Quotation();
                      q.setPrice(5);
                      insertLogical(q);
                end
            ]]>
        </rule>
    </ruleSet>
    <operation name="getQuote">
        <input wrapperElementName="placeCustomerDetail" namespace="http://com.test/placeCustomerDetail">
            <fact elementName="customerDetail" namespace="http://com.test/placeCustomerDetail" type="samples.quotation.Customer"></fact>
        </input>
        <output wrapperElementName="placeCustomerDetailRespone" namespace="http://com.test/placeCustomerDetail">
            <fact elementName="getQuotation" namespace="http://com.test/placeCustomerDetail" type="samples.quotation.Quotation"></fact>
        </output>
    </operation>
</ruleService>

 

Executing the

sample

service

To execute the sampleservice, run the ant command from the  <PRODUCT_HOME>/samples/quotation.service directory to run the GetQuote Service.

Info

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

 

Deploying and

Testing

testing the

Service

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 GetQuote 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. Issue a request similar to the following in the Try-it tool.:
     

    Code Block
    languagehtml/xml
    <status>gold</status>


    You would get the following response:

    Code Block
    languagehtml/xml
    <price>3</price>
  6. 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.

    Code Block
    languagejava
    package org.wso2.carbon.samples;
    
    import org.apache.axis2.AxisFault;
    import org.wso2.carbon.samples.quoteService.customerDetail.Customer;
    import org.wso2.carbon.samples.quoteService.customerDetail.PlaceCustomerDetail;
    import org.wso2.carbon.samples.quoteService.customerDetail.Quotation;
    import org.wso2.carbon.samples.quoteService.stub.GetQuoteServiceStub;
    import java.rmi.RemoteException;
    
    public class GetQuoteServiceTestCase {
    
        public static void main(String[] args) {
    
            try {
                GetQuoteServiceStub getQuoteServiceStub = new GetQuoteServiceStub("http://localhost:9763/services/GetQuoteService");
    
                PlaceCustomerDetail placeCustomerDetail = new PlaceCustomerDetail();
                Customer customer = new Customer();
                customer.setStatus("gold");
                Customer[] customers = new Customer[1];
                customers[0] = customer;
                placeCustomerDetail.setCustomerDetail(customers);
    
                Quotation[] quotations = getQuoteServiceStub.getQuote(customers);
                int result = quotations[0].getPrice();
                System.out.println(result);
            } catch (AxisFault axisFault) {
                axisFault.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }
    Excerpt
    hiddentrue

    GetQuote service sample of the WSO2 Business Rules Server samples guide.

...