Oracle Business Rules

First Created: 08/25/2005
Last Updated:  01/27/2005
Author:  Lance Zaklan

How-To: Building a Rules Enabled Java Application - Car Rental with Java Bean Business Objects

Introduction


  • Shows you how to create a Java rules enabled application. This application opens an Oracle Business Rules dictionary, processes the rules from the dictionary, and runs the rules to obtain results. The rules in the dictionary use Java Beans for business objects (as Facts).
  • Build, deploy, and run a sample rules enabled Java application that uses Java Facts in an Oracle Business Rules rule set.

Prerequisites

OC4J Documentation on OTN.

What you need to know

  • To understand this How-To, you should be familiar with the following technologies: Java programming language, Java Beans.
  • For more information on Java, see other  OC4J Howto's on OTN.

Software Requirements

You need the following to build and run this Oracle Business Rules application:

Notations

  • %ORACLE_HOME% - The directory where you installed Oracle Application Server.
    • %ORACLE_HOME%/lib provides access to xmlparserv2.jar
    • %ORACLE_HOME%/rules/lib provides access to rl.jar.
    • %ORACLE_HOME%/rules/lib provides access to rulesdk.jar
  • %JAVA_HOME% - The directory where your JDK is installed.
  • %HOWTO_HOME% - The directory where this How-To is unzipped.
    • %HOWTO_HOME%/lib provides access to car.jar

Stand Alone Environment Modifications


  • %RULES_HOME% - the directory in which rules was installed and that directory is not %ORACLE_HOME%
    • %RULES_HOME%/rules/lib provides access to rl.jar.
    • %RULES_HOME%/rules/lib provides access to rulesdk.jar


 

Working with the Rule Enabled Application

Examining the Sample File Directories

This section shows the contents of the ZIP file:

  • how-to-rules-java/src/ - contains all Java source code for the example.
    • carrental/ 
      • TestMain.java - Test application for the car rental example.
      • Driver.java - java bean that provides car rental facts.
  • how-to-rules-java/dict
    •  CarRepository contains the repository which in turn contains a dictionary expected by the application
  • how-to-rules-java/lib
    • car.jar -   jar containing TestMain.class and Driver.class for running the sample.
    • car-objs.jar - jar containing Driver.class for import into dictionary.
  • how-to-rules-java/docs
    • Readme.html - This document.
  • how-to-rules-java/build.xml- An Ant build file to compile TestMain.java and create a new jar file. This is only needed if you want to modify and build the code provided in the src directory.
  • how-to-rules-java/setCP - unix shell script to set classpath.
  • how-to-rules-java/setCP .cmd - windows shell script to set classpath.

Examining the Rule Enabled Application Code






Importing the Rules SDK and Rules RL Classes



import java.util.Date;
import oracle.rules.sdk.ruleset.RuleSet;
import oracle.rules.sdk.repository.RuleRepository;
import oracle.rules.sdk.repository.RepositoryManager;
import oracle.rules.sdk.repository.RepositoryType;
import oracle.rules.sdk.repository.RepositoryContext;
import oracle.rules.sdk.repository.RepositoryException;

import oracle.rules.sdk.dictionary.RuleDictionary;

import oracle.rules.rl.RuleSession;

Connecting to a Repository and loading a Dictionary with Rules SDK




  • Create a String to denote the type of repository by using the appropriate repository store package
  • Create an instance of RepositoryType using the repository package String
  • Create an instance of Repository by invoking the RepositoryManager class, createRuleRepositoryInstance method
    • createRuleRepositoryInstance requires a RepositoryType object
  • Create a RepositoryContext object and set the required properties (repository path for file repositories)
  • Connect to the Repository by invoking the init method on the RuleRepository object
    • init requires a RepositoryContext





    try
    {
        //connect to repository
        String repoPath = "dict" + fs + "CarRepository";


        final String jarstoreKey = "oracle.rules.sdk.store.jar";
         
        RepositoryType jarType = RepositoryManager.getRegisteredRepositoryType( jarstoreKey );
        RuleRepository repo = RepositoryManager.createRuleRepositoryInstance( jarType );
           
        //fill in init property values
        RepositoryContext jarCtx = new RepositoryContext();
        jarCtx.setProperty( oracle.rules.sdk.store.jar.Constants.I_PATH_BASE, repoPath );

        //init the repository instance.  If the init is successful,
        //we shall get a useable repository instance
        repo.init( jarCtx );

        //load the dicionary/version       
        RuleDictionary dict = repo.loadDictionary( "CarRental", "HowTo" );
     }
 
NOTE

Specifying a Rule Set and Generating RL with Rules SDK



//init a rule session
String rsname = "vehicleRent";
String dmrl = dict.dataModelRL();
String rsrl = dict.ruleSetRL( rsname );

Initializing and Executing a Rule Session






RuleSession session = new RuleSession();
session.executeRuleset( dmrl );
session.executeRuleset( rsrl );




Asserting Business Objects Within a Rule Session



// Driver d1 record
Date d1LicIssueDate = getDate( "10/1/1969" );
Driver d1 = new Driver( "d111", "Dave", 50, "sports", "full",
                                      d1LicIssueDate, 0, 1, true );

// Driver d2 record
Date d2LicIssueDate = getDate( "8/1/2004" );
Driver d2 = new Driver( "d222", "Qun", 15, "truck", "provisional",
                                    d2LicIssueDate, 0, 0, true );

//Driver d3 record
Date d3LicIssueDate = getDate( "6/1/2004" );
Driver d3 = new Driver( "d333", "Lance", 44, "motorcycle", "full",
                                    d3LicIssueDate, 0, 1, true );

session.callFunctionWithArgument( "assert", d1 );
session.callFunctionWithArgument( "assert", d2 );
session.callFunctionWithArgument( "assert", d3 );

Using the Run Function with a Rule Session




session.callFunctionWithArgument( "run", rsname);

 

Running the Rule Enabled Application and Examining Results

Run the setCP Command Script

The setCP, or setCP.cmd scripts set the appropriate CLASSPATH environment  variable for running the TestMain program from this directory.  If you need to run TestMain from a different directory, then modify the source code in %HOWTO_HOME%/src/carrental/TestMain.java.  The dictionary is currently loaded from a relative path that assumes the program is running from %HOWTO_HOME%.  Either make the path for the repository to be an absolute path or modify the source code retrieve the repository path from a command line argument or the environment.

To run the test program:

 On unix systems:

  $ . setCP         
 

 On windows systems:

  > setCP.cmd  

Run the Application

     
  > java carrental.TestMain
  Rental declined Qun Under age: age is: 15
  >

Building the Rule Enabled Application


%ORACLE_HOME%/ant/bin

Ensure the %ORACLE_HOME% environment variable is defined, and from the %HOWTO_HOME% directory, type the command:

ant


  $ ant
                        

  Buildfile: build.xml
                        

                         

  init:
                        

                         

  prepare:
                        

                         

  classes:
                        

        [echo] Classpath is: /myinstall/OraRules/lib/rulesdk.jar:/myinstall/OraRules/lib/rl.jar:/oc4j/xdk/lib/xml.jar
                        

        [javac] Compiling 2 source files to /myinstall/OraDemosDoc/demo/car/build/classes
                        

        [jar] Building jar: /myinstall/OraDemosDoc/demo/car/build/lib/carRental.jar
                        

        [jar] Building jar: /myinstall/OraDemosDoc/demo/car/build/lib/carRental-objs.jar
                        

                         

  BUILD SUCCESSFUL
                        

  Total time: 6 second
                      

run the application


 

 

Summary


  • Built and ran a Java enabled rules application that runs the Oracle Business Rules Rules engine with rules defined using Java Bean based business objects (Java Facts).
Left Curve
Popular Downloads
Right Curve
Untitled Document