Testing Oracle Business Rules using Rules Designer

The JDeveloper 11G Rules editor allows developers to test business rules directly from within the Rules Designer.

The "User's Guide for Oracle Business Rules" does a nice job of introducing testing fundamentals such as creating the test function and passing a simple fact.

This posting presents a more complex example illustrating;

- How to create test data for input data structures marked as a list or a tree.
- How to create test data for both Java and XML facts.

Our example scenario involves checking line items in an order to determine if additional shipping charges apply.


An Order is represented by an XML document that contains a list of order Items.
An Order and its line items are represented by XML facts using the following XML Schema

Order.xsd

<?xml version= '1.0' encoding= 'UTF-8' ?>
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.rules.example"
            targetNamespace="http://www.rules.example"  elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Order" type="OrderT"/>
    <xs:element name="item" type="ItemT"/>
    <xs:complexType name="OrderT">
        <xs:sequence>
            <xs:element name="orderId" type="xs:string"/>
            <xs:element name="items" type="OrderItemCollection"/>
        </xs:sequence>
    </xs:complexType>
     <xs:complexType name="OrderItemCollection">
        <xs:sequence>
            <xs:element name="item" type="ItemT" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
     <xs:complexType name="ItemT">
        <xs:sequence>
            <xs:element name="productId" type="xs:string"/>
            <xs:element name="quantity" type="xs:integer"/>
            <xs:element name="unitPrice" type="xs:decimal"/>
            <xs:element name="extraCharge" type="xs:boolean"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

To determine Shipping charges we consult a list of Java Facts.

The ShipCode class contains the shipping cost category for a given product.
The ShipCodeList contains a list of all possible ShipCodes.

ShipCode.java

package rulesproject;

public class ShipCode {

public ShipCode() {

super();

}

private String productId;

private String shipCode;

public void setProductId(String productId) {

this.productId = productId;

}

public String getProductId() {

return productId;

}

public void setShipCode(String shipCode) {

this.shipCode = shipCode;

}

public String getShipCode() {

return shipCode;

}

}

ShipCodeList.java

package rulesproject;

import java.util.ArrayList;

import java.util.List;

public class ShipCodeList {

public ShipCodeList() {

super();

shipList = new ArrayList<shipcode>();

}

private List<shipcode> shipList;

public void setShipList(List<shipcode> shipList) {

this.shipList = shipList;

}

public List<shipcode> getShipList() {

if(shipList == null) shipList = new ArrayList<shipcode>();

return shipList;

For our simple example we use a single rule that checks if one of the Items asserted in the order matches a ShipCode for the same product.  If we find a match, we set the extraCharge variable to true for the corresponding order item


The ShippingRules are exposed using a Decision Function.
Our “DetermineShippingCosts” Decision Function takes two inputs.

The first input is an OrderT fact based on our XML Facts. This parameter is marked as a tree structure.
This ensure that the entire XML data structure is asserted into the engine.

The second input is List of ShipCode Java based facts.   We indicate a parameter of  type ShipCode, but since the List checkbox is selected we will provide an input object of type ShipCodeList.

The output for our decision function is a modified order with  line items that require additional shipping charges marked accordingly.

To test this decision function we will create three different functions.

Our first function is the top level test function “testShipRateRules”,  We will execute this function later to perform the test.  By standard the top level function must be defined to return a boolean.  In our case the boolean returned is not relevant. When we run the test later our results will be displayed in a popup dialog.

Within the body of the function we call our Decision Function named DetermineShippingCosts. The decision function we defined takes two parameters.  We create two other functions, createOrderTestData and createShipRateData to create the test data for these parameters.

The next two screen snapshots show the creatOrderTestData() and createShipRateData() functions.

In the body of both these functions we use assign and modify commands to create our test objects. 
We then create an instance of our list object and add the test objects to the list.

The key point to understand is that both XML and Java fact lists are represented under the covers by the java.util.List interface.   The Rules Designer normally assists the user with the creation of Rule statements, but to add the objects to a list you will need to author the line without assistance.  Choose “expression” from the drop down list presented when entering the beginning of each source line.  Invoke the underlying methods to get the list and to add() items to the list.

Finally we are ready to execute our test.
We run the test by selecting the testShipRateRules function and clicking the test link in the upper right corner.   Note that this link will not be enabled if there are any rule validation errors.

When we run the test, our results are output to a dialog window.

We requested this output by including the RL.watch.all()  statement.  

Our test data is designed to find one match for item 001 and no match for item 123.

In the bottom 4 lines of the output we see that the rule has fired and our order line for product 001 has a been modified to set the value of the extraCharge property to true.

The complete example can be downloaded in a zip file from github here

 

Comments:

Hi Bob,

I have gone through the Blog it was very useful to understand the Business rules.In our current project we have a requirement where we need to update the Business Rules through java API not manually.So is this a valid scenario does API supports it.If so please guide us since were not able to get the proper methods that needed to be used in java program.

Raghu

Posted by Raghu on February 05, 2013 at 03:26 AM EST #

Creation and modification of rules is possible using the Rules SDK.
The javadoc for the 11g API is here
http://docs.oracle.com/cd/E23943_01/apirefs.1111/e10663/toc.htm

The 10g version of the Rules User guide provided a nice overview of the basics of using the SDK to create rules.
Unfortunately this section does not appear in the 11g manual.
Here is the link to the 10g guide which should be correct
http://docs.oracle.com/cd/E11036_01/web.1013/b28965/sdk.htm#BJFHJBIC

Bob

Posted by Bob Webster on February 05, 2013 at 11:25 AM EST #

Post a Comment:
Comments are closed for this entry.
About

Picture of Bob

I am an Oracle Architect specializing in Service Oriented Architecture and Business Process Management.


Any code presented is for educational purposes only, no warranty or support is implied.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today