Create Oracle Business Rules using the Java API


SOA Suite and BPM Suite 11g developers and end users normally develop and edit Rule sets
using graphical tools such as the JDeveloper Rule editor and the SOA Composer Web Application.

But Rules can also be created and manipulated using the Oracle Business Rules Java API,
in fact both of the graphical editors leverage the API to perform their work.

This blog post provides a Rules API Example that illustrates how to:

The Javadoc for the 11g Rules API can be found here Java Business Rules 11g API


Get the Sample Code

Here is the sample code.

RuleSDKExample.java

ItemT.java


Create a JDeveloper project to test the example

1) Install Oracle JDeveloper 11g

2) Add the SOA Editor expansion pack 

    To edit business rules, the Oracle SOA Composite Editor extension is required.
    In JDeveloper choose Help->Check for updates an select the 'Oracle SOA Composite Editor

3) Create a new JDeveloper Application

    From the JDeveloper main menu, Choose Application-> New Application
    Set the application name to   RulesSDKApp
    Select 'SOA Application' from the Application template window.

    Press Next and enter the value RulesProject the name of a new Project  
 
   
Press the Finish button

4) Add additional required libraries to the project

    Right click on the RulesProject in the Application Navigator and
    select Project Properties
    Select the --  Libraries and Classpath   node
    Click the Add Library button and expand the Extensions section

    Select   -- Oracle Rules from the list and press the OK button.
    Repeat the steps to add the following libraries...
               -- Oracle XML Parser V2
               -- ADF Model Runtime
               -- Oracle JDBC

4) Add a new Rules Dictionary to the project

    Right click on the RulesProject in the Application Navigator window and choose New.
    Select the 'Current Project Technologies Tab'
    Expand the 'SOA Tier' node
    Select 'Service Components' and select Business Rules in the items section.


   New Business Rules

    In the Create Business Rules dialog,
    change the default dictionary name from
    OracleRules1  to  MyRulesDictionary
    Press the OK button to create the Business Rules Dictionary

5) Add a new Java Class for the java fact java code

    Right click on the RulesProject in the Application Navigator and choose New
    Select the 'Current Project Technologies' tab and select the General Node
    Select 'Java Class' from the items list and press the OK button.
    Set the new class name to  ItemT
    Set the class package to    rulesproject
    Press the OK button

    Open the ItemT.java code contained in the link above in an editor.
    Cut and paste the code into the new ItemT.java window.
    Save the file.


6) Add a new Java Class for the SDKExample java code

    Right click on the RulesProject in the Application Navigator and choose New
    Select the 'Current Project Technologies' tab and select the General Node
    Select 'Java Class' from the items list and press the OK button.
    Set the new class name to  SDKExample
    Set the class package to    rulesproject
    Press the OK button

    Open the SDKExample.java code contained in the link above in an editor.
    Cut and paste the code into the new SDKExample window.
    Save the File

7) At this point the project is complete and should be successfully compiled,

Completeed App


You can also download a zip of the completed JDeveloper project from GitHub here


Run the Example

Right click on RulesSDKExample.java file  in the JDeveloper Application Navigator and
choose Run

In the MyRulesDicationary

Right click on RulesSDKExample.java and choose Run

In the Running window at the bottom of jdeveloper, the following output should appear

    Removed old ruleset
    RuleTable size is 1
    Rules can be added? false
    Updated dictionary
    Rules can be added true
    Added new rule
    Updated dictionary
    Rules can be added true
    Added new rule
    Updated dictionary
    Rule table now contains 3 rules
    Updated dictionary
    Wrote dictionary to filesystem
    Process exited with exit code 0
.

You can view the new Ruleset in JDeveloper by opening the rules file.
If the .rules file is already open, close the window and reopen it reload the modified dictionary from the file system.

After execution, a new java fact named ItemT should be visible in the Data Model as shown below.


Click the edit pencil icon on the upper right to view the Fact Details,
Note the BucketSet ShipWeightType is associated to the fact property named productWeight


A new list of values bucket set is also added


Click the edit pencil icon on the upper right to view the BucketSet Details,


Within the Rule Set a new If-Then Rule exists

And a New Decision Table, change the view drop down to select the new Decision Table


Finally, here is a copy of the Rules class diagram developed during development of the example