LinkedIn

Tuesday, April 19, 2011

Using and Testing Complex Business Rules in Oracle BPM 11g

A Slight Briefing

Oracle Business Rules is a high performance and lightweight business rules product that is part of the Oracle Fusion Middleware Suite that can be used in both SOA and BPM suite.

To have a business process more agile and coherent with the changing demands of Business, Oracle Business rules is a must for any design. Also it should act as a central component where all process rules are located.

With OBR 11g one added advantage of business rules is that they can be exposed as any other web service. This makes it an instant hit as it becomes hot pluggable.

Here in this example blog i would show how to create a complex rule in JDeveloper and test it out through multiple ways. This is intended to be a zero lecture hands on so i would skip the talk.

Prerequisites
The Scenario

A high school needs a web service implemented as a rule in Oracle that calculates the grades of students.

The service would take some basic candidate information and an array of subjects/marks that the candidate has obtained. The rules engine will have to allocate the candidate Grades on the basis of the following logic.

Average Marks Grade Allotted
<40 FAIL
40-60 FAIR
60-80 GOOD
80-90 VERY GOOD
90-100 OUTSTANDING

For the sake of this demonstration we would use an XSD definition for CandidateInformation and CandidateGrade.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org" targetNamespace="http://www.example.org" elementFormDefault="qualified">
<xsd:element name="CandidateInformation" type="CandidateInformationType"/>
<xsd:element name="CandidateGrade" type="CandidateGradeType"/>
<xsd:complexType name="CandidateInformationType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="rollNumber" type="xsd:string"/>
<xsd:element name="class" type="xsd:string"/>
<xsd:element name="section" type="xsd:string"/>
<xsd:element name="remarks" type="xsd:string"/>
<xsd:element name="subject" type="SubjectType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="CandidateGradeType">
<xsd:sequence>
<xsd:element name="overallGrade" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="SubjectType">
<xsd:sequence>
<xsd:element name="subjectName" type="xsd:string"/>
<xsd:element name="subjectCode" type="xsd:string"/>
<xsd:element name="subjectMark" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

Save it somewhere as GradesAllocation.xsd

The Solution
  • Open JDeveloper and Create a New SOA Application.

image
  • Name the application as BusinessRulesApplication and click on Next.

image
  • Name the project as BusinessRulesProject and again click Next. Make sure ‘SOA’ is selected under ‘Project Technologies

image
  • Choose ‘Composite with Business Rule and click on ‘Finish.
  • You would see that a window pops out to create a rule and specify the inputs and output for the rule.
  • Name the rule as ‘GradeAllocationRule and click on the ‘+’ icon  to specify the Input and Output types for the rule. Select ‘CandidateInformation from the GradeAllocation.xsd as the input and CandidateGrade as the output.

image

image
  • Wait for the wizard to create the rule definition. Click on the Ruleset at the top and rename it to GradeAllocation’.

image
  • Click on ‘Bucketsets link in the left most panel and Add a ‘List of Ranges’.

image
  • Name the bucketset as ‘markRange and create a list of ranges as under.

image
  • Click OK to save changes to the bucketset and click on GradeAllocation ruleset.
  • Select ‘Create Decision Table from the two options. Remember that we can either create an ‘if-then-else’ rule or a decision table. A decision table is implicitly evaluated as an ‘of-then-else’ rule only but gives a better manageability to rule definitions.

image
  • Name the decision function as ‘decideGrade’ and check ‘Advanced Mode’ to be true.

image
  • Click on Insert Pattern’ in the workspace below.

image
  • Right Click on the ‘variable block and click on ‘Surround with’ and click on ‘Pattern Block’

image

image
  • Click on the auto generated expression and from the dropdown select ‘aggregate

image
  • Now click on variabe and define a variable named ‘averageMarks’, Click on ‘functions to select ‘average from the dropdown. Click on ‘fact type’ to select ‘SubjectType element as the fact. Name this as ‘subjectType. Click on ‘expression now to select ‘subjectType.subjectMarkas we are interested in the average marks across all subjects. The overall construct should look like below :

image
  • Click on ‘insert condition’ in the panel below and then click ‘edit condition’ to select ‘averageMarks from the option. Choose the bucketset ‘marksRangefrom the dropdown ‘Local List of Ranges’.

image
  • Click on the +’ icon adjacent to the range dropdown and keep adding a rule for each of the range. Remember you have to add six distinct rule. Select a distinct value defined in the bucketset each time. Here is how to define the rules.

image
  • Now go to the ‘Actions panel and click oninsert action’. Select ‘Assert new’. Double click on the action and click on CandidateGradeTypeunder Facts. Check the option Parameterized’ for the propertyoverallGrade’

image
  • Now click on each of the option box in the Grid to define a value for the outcome. Since overallGrade is a String type, assign a grade in string for each of the condition as under

image
  • Save all projects and files in JDeveloper. With this we are done with the Rules creation part. So here is a summary of what we did.
    • Created a Bucketset for a list of mark ranges and grade type associated with them.
    • Created a decision table for a set of rules. Initialized a variable for ‘averageMarks and using a pattern block assigned it as an average of all subject marks.
    • Now for each condition for the rule asserted the outcome for ‘CandidateGrade.overallGrade’ with the grade that has to be assigned.
    • Pretty simple. Isn't it?
Testing the Rules

Creating business rules isn't just enough. There has to be a mechanism to test them. Remember Oracle Rules engine is a inference based rules engine i.e rules are all evaluated at runtime. For more information on how rules are evaluated refer to Oracle Business rules architecture. Here I would show how rules can be tested using three ways.

Testing Rules by Creating a Debug RL function
  • Click on the Functions link and add a new function and name it to DebugRule’. Select boolean under both Return Type and Bucketset.
  • For the body part of the function we would right an RL construct to initialize CandidateInformationType (input to the rule) and pass some dummy values to it.
  • Check the screen snap below and create a body exactly like the one below

image

image
  • Now if you are familiar with any programming language understanding the above construct should be like a cakewalk.
  • You would now see that the Test’ link for the function becomes enabled. Click on it to test the rule output.

image
  • Here you go. You can see that the output grade isOUTSTANDING’. Has to be since the dummy value of marks assigned were 100 and 100. You can now change the marks in the subjects or add a new subject type to test the rule again for a different output.

image
  • You can create as many Rulesets as you may for evaluating more complex conditions and add them to the Decision Functions in the order you would need their evaluation to come up with complex business scenarios.

Testing Rules from EM console
  • Deploy the BusinessRulesProject to a domain server extended with soa suite. Boot up the em console and browse to the project composite.

image
  • Click on the ‘Test’ icon for the composite to launch the EM test wizard for the composite.

image
  • You would see a Tree View for the request message for the composite wherein you can input sample values

image
  • Fill in any random values for the type bpelInstance. The only important value would be the attribute ‘NCName’. Make sure you put the name of your Decision Function there.
  • Fill the request wizard with CandidateInformation as under

image
  • Click on Test Web Service button on top left of the page to test the Rules decision service.

image
  • Expand the CandidateGrade in the Response tab to see the overallGrade for the student.

image
  • You can click on ‘Launch Flow Trace’ to view the execution trace for the Decision Service.

image
  • You can see how easily we can test out our Business rules from the EM console.

Testing Rules from SOAP UI

More than often in real life scenarios we would like to create some kind of a unit testing suite for out business rules. Ag you might have already made note that in SOA suite 11g Business rules are exposed as standard web services that can be invoked from anywhere. See the demonstration below to see how Business rules can be externalized as web service and invoked through third parties even.
  • Go to the BusinessRulesProject composite in the EM console.
  • Click on theService Icon’ to copy the WSDL endpoint for the rules service.

image
  • Create a SOAPUI project based on this WSDL.

image
  • Fill the mock service request with actual values

image
  • Run the test and you should see the outcome from the Rules decision function.

image

Now you can build a test suite to create mock requests for various scenarios and assert the responses.

The JDeveloper project used in this example can be downloaded from here.

No comments:

Post a Comment