Oracle Business Rules

First Created: 09/09/2005
Last Updated:  01/27/2006
Author:  Lance Zaklan

How-To: Building a Rules Enabled Java Application - Car Rental with XML 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 XML data  (XML Facts).
  • Build, deploy, and run a sample rules enabled Java application that use XML 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, XML.
  • 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%/lib provides access to xml.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-xml/src/ - contains all Java source code for the example.
    • carrental/ 
      • TestXML.java - Test application for the carrental example.
  • how-to-rules-xml/dict
    •  CarxmlRepository file repository containing the dictionary.
  • how-to-rules-xml/lib
    • carxml.jar -   jar containing TestXML.class for running the sample.
    • generated.jar - jar containing JAXB classes for carrental.xsd schema.
  • how-to-rules-xml/data
    • carrental.xml - Test data.
    • carrental.xsd - schema describing the incoming xml document.
  • how-to-rules-xml/docs
    • Readme.html - This document.
  • how-to-rules-xml/build.xml- An Ant build file to compile TestXML.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-xml/setCP - unix shell script to set classpath.
  • how-to-rules-xml/setCP .cmd - windows shell script to set classpath.

Examining the Rule Enabled Application Code






Importing the Rules SDK and Rules RL Classes



import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.util.Properties;

import javax.xml.bind.*;

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;


Creating a JAXB Context and Unmarshalling the XML Document



JAXBContext jc = JAXBContext.newInstance("generated");
Unmarshaller um = jc.createUnmarshaller();
String fs = System.getProperty("file.separator");
String xmlpath = "data" + fs + "carrental.xml" ;
Object root = um.unmarshal(new File(xmlpath));

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 + "CarxmlRepository";


        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( "CarRentalxml", "HowToxml" );
     }
 
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 XML Data from Within a Rule Session






  1. The first argument for assertXPath is the JAXB generated package name, for this example, generated.
  2. The second argument for assertXPath is the root object for the unmarshalled XML document. For this example the unmarshalled object reference is the root object.
  3. The second argument for assertXPath is the root object for the unmarshalled XML document. For this example the unmarshalled object reference is the root object.
List argList = new ArrayList(3);
argList.add( "generated" );
argList.add( root );
argList.add( "//*" );
session.callFunctionWithArgumentList( "assertXPath", argList );

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

%HOWTO_HOME%



  $ . setCP         

 On windows systems:

  > setCP.cmd         


Run the Application

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

Modifying the Source and 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:



To build the application, type the following command from the %HOWTO_HOME% directory:

ant


 $ ant
                        

Buildfile: build.xml
                        

                         

init:
                        

                         

prepare:
                        

                         

generate-jaxb:
                        

     [echo] Classpath is: /myoraclehome/rules/lib/rulesdk.jar:/myoraclehome/rules/lib/rl.jar:/myoraclehome/lib/xml.jar:/myoraclehome/lib/xmlparserv2.jar
                        

     [java] generated/Repository.java
                        

     [java] generated/RepositoryType.java
                        

     [java] generated/DriverType.java
                        

                         

     [java] generated/RepositoryImpl.java
                        

     [java] generated/RepositoryTypeImpl.java
                        

     [java] generated/DriverTypeImpl.java
                        

     [java] generated/ObjectFactory.java
                        

                         

                         

     [java] XML-32111: (Warning) following generated java source files overwrote existing files "
                        

     [java] /mydemo_dir/build/jaxbgen/generated/Repository.java
                        

     [java] /mydemo_dir/build/jaxbgen/generated/RepositoryType.java
                        

     [java] /mydemo_dir/build/jaxbgen/generated/DriverType.java
                        

     [java] /mydemo_dir/build/jaxbgen/generated/RepositoryImpl.java
                        

     [java] /mydemo_dir/build/jaxbgen/generated/RepositoryTypeImpl.java
                        

     [java] /mydemo_dir/build/jaxbgen/generated/DriverTypeImpl.java
                        

     [java] /mydemo_dir/build/jaxbgen/generated/ObjectFactory.java
                        

     [java] /mydemo_dir/build/jaxbgen/generated/jaxb.properties
                        

     [java] "
                        

     [java] Java Result: -1
                        

    [javac] Compiling 7 source files to /mydemo_dir/build/jaxbgen
                        

      [jar] Building jar: /mydemo_dir/build/lib/generated.jar
                        

     [copy] Copying 1 file to /mydemo_dir/lib
                        

                         

classes:
                        

     [echo] Classpath is: /myinstall/lib/rulesdk.jar:/myinstall/lib/rl.jar:/myoc4jinstall/lib/xml.jar:/myoc4jinstall/lib/xmlparserv2.jar:/mydemo_dir/lib/generated.jar
                        

    [javac] Compiling 1 source file to /mydemo_dir/build/classes
                        

      [jar] Building jar: /mydemo_dir/build/lib/carxml.jar
                        

     [copy] Copying 1 file to /mydemo_dir/lib
                        

                         

BUILD SUCCESSFUL
                        

Total time: 3 seconds
                        

                         

   $ant classes
                        

Buildfile: build.xml
                        

                         

init:
                        

                         

prepare:
                        

                         

classes:
                        

     [echo] Classpath is: myinstall/lib/rulesdk.jar:myinstall/lib/rl.jar:/myoraclehome/lib/xml.jar:/myoraclehome/lib/xmlparserv2.jar:/mydemo_dir/lib/generated.jar
                        

    [javac] Compiling 1 source file to /mydemo_dir/build/classes
                        

      [jar] Building jar: /mydemo_dir/build/lib/carxml.jar
                        

     [copy] Copying 1 file to /mydemo_dir/lib
                        

                         

BUILD SUCCESSFUL
                        

Total time: 2 seconds
                      

run the application

 

 

Summary


  • Built and ran a Java enabled rules application that runs the Oracle Business Rules Rules engine with rules defined using XML schema based business objects (XML Facts).

                         

                      

 

 


Left Curve
Popular Downloads
Right Curve
Untitled Document