10 Working with Oracle Business Rules and ADF Business Components
Oracle Business Rules enables you to use Oracle ADF Business Components view objects as facts. As with all fact types, trees of facts, representing master-detail data, can be asserted as a unit. Oracle Business Rules has built-in tests (RLcontains) and optional "tree mode" syntax (for example, master-detail) to support navigating one-to-many relationships.
Introduction to Using Business Rules with ADF Business Components
The ADF Business Components rule development process can be summarized as below.
Using ADF Business Components rule, you can:
-
Create view object definitions.
-
Create action types.
-
Create rule dictionary.
-
Register view object fact types.
-
Register Java fact types for actions.
-
If you are invoking from Java:
-
If the view object is already instantiated at the Decision Point, code the Decision Point invocation passing the view object instance.
-
If the view object is not instantiated at the Decision Point, code the Decision Point invocation passing the view object key values.
-
Understanding Oracle Business Rules ADF Business Components Fact Types
When an ADF Business Components view object is imported into an Oracle Business Rules data model, an ADF Business Components fact type is created which has a property corresponding to each attribute of the view object.
Additionally, the ADF Business Components fact type contains the following:
-
A property named ViewRowImpl which points directly to the
oracle.jbo.Row
instance that each fact instance represents. -
A property named key_values which points to an
oracle.rules.sdk2.decisionpoint.KeyChain
object. You can use this property to retrieve the set of key-values for this row and its parent rows.
Note the following:
-
Relationships between view object definitions are determined by introspection of attributes on the View Definition, specifically, those attributes which are View Link Accessors.
The ADF Business Components fact type importer correctly determines which relationships are 1-to-1 and which are 1-to-many and generates definitions in the dictionary accordingly. For 1-to-many relationships the type of the property generated is a
List
which contains facts of the indicated type at runtime. -
ADF Business Components fact types are not Java fact types and do not allow invoking methods on any explicitly created implementation classes for the view object.
If you need to call such methods then add the view object implementation to the dictionary as a Java fact type instead of as an ADF Business Components fact type. In this case, all getters and setters and other methods become available but the trade-off is that related view objects become inaccessible and, should related view object access be required, these relationships must be explicitly managed.
-
Internally in Oracle Business Rules, when you use ADF Business Components fact types these fact types are created as instances of RL fact types. Thus, you cannot assert ADF Business Components view object instances directly to a Rule Session, but must instead use the helper methods provided in the
MetadataHelper
andADFBCFactTypeHelper
classes. For more information, see Java API Reference for Oracle Business Rules.
Understanding Oracle Business Rules Decision Point Action Type
With Rules SDK, the primary way to update a view object within a Decision Point is with an action type. An action type is a Java class that you import into the rule dictionary data model in the same way you import a rule pattern fact type Java class. A new instance of this action type is then asserted in the action of a rule and then processed by the Postprocessing Ruleset in the DecisionPointDictionary
.
A Java class to be used as an action type must conform to the following requirements:
-
The Java fact type class must subclass
oracle.rules.sdk2.decisionpoint.ActionType
ororacle.rules.sdk2.decisionpoint.KeyedActionType
.By subclassing
KeyedActionType
the Java class inherits a standardoracle.rules.sdk2.decisionpoint.KeyChain
attribute, which may be used to communicate the rule fact's primary keys and parent-keys to theActionType
instance. -
The class has a default constructor.
-
The class implements abstract
exec
method for theActionType
. Theexec
method should contain the main action which you want to perform. -
The Java class must have properties which conform to the
JavaBean
interface (that is, each property must have a getter and setter method).
See Example 10-1 for a sample ActionType
implementation.
Table 10-1 shows the methods in DecisionPointInstance
that an application developer might need when implementing the ActionType
exec
.
Table 10-1 DecisionPointInstance Methods
Method | Description |
---|---|
|
Supplies a If you intend to use the decision function from a Decision service, use only String values. |
|
Gives access to the Oracle Business Rules RuleSession object from which static configuration variables in the Rule Dictionary may be accessed. |
|
If populated by the caller, supplies a String value to be used for Set Control indirection. |
|
Provides a transaction object so that action types may make persistent changes in the back end. |
|
Adds a named result to the list of output values in the form of a String key and Object value. Output is assembled as a |
Sample ActionType Implementation
Example 10-1 shows a sample ActionType
implementation and an oracle.rules.sdk2.decisionpoint.DecisionPointInstance
as a parameter to the exec
method.
Example 10-1 Implementing an ActionType
package com.example; import oracle.jbo.domain.Number; import oracle.rules.sdk2.decisionpoint.ActionType; import oracle.rules.sdk2.decisionpoint.DecisionPointInstance; public class RaiseAction extends ActionType { private double raisePercent; public void exec(DecisionPointInstance dpi) { Number salary = (Number)getViewRowImpl().getAttribute("Salary"); salary = (Number)salary.multiply(1.0d + getRaisePercent()).scale(100,2, new boolean[]{false}); dpi.addResult("raise for " + this.getViewRowImpl().getAttribute("EmployeeId"), getRaisePercent() + "=>" + salary ); getViewRowImpl().setAttribute("Salary", salary); } public void setRaisePercent(double raisePercent) { this.raisePercent = raisePercent; } public double getRaisePercent() { return raisePercent; } }
Using Rules Designer you can select parameters appropriate for the ActionType
you are configuring.
Using Decision Points with ADF Business Components Facts
You can use a Decision Point to execute a decision function. There are certain Decision Point methods that only apply when working with ADF Business Components Fact types.
For more information on decision functions, see Working with Decision Functions .
How to Call a Decision Point with ADF Business Components Facts
When you use ADF Business Components fact types you invoke a decision function using the Rules SDK Decision Point interface.
To call a decision function using the Rules SDK Decision Point interface:
Setting the Decision Point Transaction
The Oracle Business Rules SDK framework requires an oracle.jbo.server.DBTransactionImpl2
instance to load a ViewObject
and to provide ActionType
instances within a transactional context. The class oracle.jbo.server.DBTransactionImpl2
is the default JBO transaction object returned by calling the ApplicationModule
method getTransaction
. Setting the transaction requires calling the DecisionPointInstance
method setTransaction
with the Transaction
object as a parameter.
Should a DBTransaction
instance not be available for some reason, the Oracle Business Rules SDK framework can bootstrap one using any of the three provided overrides of the setTransaction
method.
These require one of:
-
A JDBC URL, user name, and password.
-
A JDBC connection object.
-
A
javax.sql.DataSource
object and a flag to specify whether theDataSource
represents a JTA transaction or a local transaction.
Setting Runtime Properties
Runtime properties may be provided with the setProperty
method. These can then be retrieved by ActionType
instances during their execution. If no runtime properties are needed, you may safely omit these calls.
Calling the Invoke Method for an ADF Business Components Rule
The ViewObject
to be used in a Decision Point invocation can be specified in one of two ways, as shown in Table 10-2.
Table 10-2 Setting the View Object for a Decision Point Invocation
ViewObject Set Method | Description |
---|---|
|
The decision function is invoked once for each How to Add Retract Employees Ruleset shows this usage. To use |
|
The decision function is invoked once with all of the |
What You Need to Know About Decision Point Invocation
Care must be taken when invoking Decision Points using a view object that loads large amounts of data, since the default behavior of the JBO classes is to load all data eagerly. If a view object with many rows and potentially very many child rows is loaded into memory, not only is there risk of memory-exhaustion, but DML actions taken based on such large data risk using all rollback segments.
Sample to Invoke a Decision Point Using setinputs Method
Example 10-2 shows how to invoke a Decision Point with a ViewObject
instance using the setInputs
method. For the complete example, see the example shown in How to Add the Outside Manager Finder Class.
Example 10-2 Invoking a Decision Point Using setInputs Method
public class OutsideManagerFinder { private static final String AM_DEF = "com.example.AppModule"; private static final String CONFIG = "AppModuleLocal"; private static final String VO_NAME = "EmployeesView1"; private static final DictionaryFQN DICT_FQN = new DictionaryFQN("com.example", "Chapter10Rules"); private static final String DF_NAME = "FindOutsideManagers"; private DecisionPoint dp = null; public OutsideManagerFinder() { try { dp = new DecisionPointBuilder() .with(DICT_FQN) .with(DF_NAME) .build(); } catch (SDKException e) { System.err.println(e); } }
public void run() { final ApplicationModule am = Configuration.createRootApplicationModule(AM_DEF, CONFIG); final ViewObject vo = am.findViewObject(VO_NAME); final DecisionPointInstance point = dp.getInstance(); point.setTransaction((DBTransactionImpl2)am.getTransaction()); point.setAutoCommit(true); point.setInputs(new ArrayList<Object>(){{ add(vo); }}); try { List<Object> invokeList = point.invoke();
List<DecisionPoint.NamedValue> results = point.getResults();
} catch (RLException e) { System.err.println(e); } catch (SDKException e) { System.err.println(e); } }
Sample to Invoke a Decision Point Using setViewObject Method
Example 10-3 shows how to invoke a DecisionPoint
using the setViewObject
method to set the ViewObject
.
Example 10-3 Invoking a Decision Point Using setViewObject Method
public void run() { final ApplicationModule am = Configuration.createRootApplicationModule(AM_DEF, CONFIG); final ViewObject vo = am.findViewObject(VO_NAME); final DecisionPointInstance point = dp.getInstance(); point.setTransaction((DBTransactionImpl2)am.getTransaction()); point.setAutoCommit(true); point.setViewObject(vo); try { List<Object> invokeList = point.invoke();
List<DecisionPoint.NamedValue> results = point.getResults();
} catch (RLException e) { System.err.println(e); } catch (SDKException e) { System.err.println(e); } }
How to Call a Decision Function with Java Decision Point Interface
To call a decision function with a ruleset using ADF Business Components fact types with the Oracle Business Rules SDK Decision Point interface you must configure the decision function with certain options. For more information on using decision functions, see Working with Decision Functions.
To define a decision function using the Java Decision Point interface:
What You Need to Know About Decision Function Configuration with ADF Business Components
Both rulesets and decision functions may be included in the definition of a decision function. It is common for an application to require some rules or decision functions which act as "plumbing code". This plumbing code is only needed if you are using ActionType.
Such applications include components that perform transformations on the input data, assert auxiliary facts, or process output facts. The plumbing code may need to run before or after the rules that contain the core business rules of the application.
You can separate these application concerns and their associated rules from the application functional concerns using nested decision functions. Using nested decision functions, the inner decision function does not contain the administrative, plumbing-oriented concerns, and thus only presents those rules which define the core logic of the application. This design eliminates the need for the user to understand the administrative rules and prevents a user from inappropriately modifying these rules (and possibly rendering the system inoperable due to these changes).
To create a configuration using multiple rulesets and nested decision functions, create two decision functions and add one to the other. A good naming scheme is to suffix the nested inner decision function with the name Core
. The user specified rulesets can be added to the inner Core
decision function. For example, DecisionFunction_1 can be defined to run the DecisionPointDictionary.Preprocessing decision function, the DecisionFunction_1Core decision function, and the DecisionPointDictionary.Postprocessing decision function. For this example, DecisionFunction_1Core contains the core business logic rulesets.
It is also common for the input of a Decision Point to be an ADF Business Components fact type that is the root of a tree of ADF Business Components objects. However, the user might only write business rules that match on a subset of the types found in the tree. In this case, it is a good practice to define the inputs of the nested decision functions to be only the types which are actually matched in the contained rulesets. For example, consider a Decision Point calling a decision function whose input is an Employee
fact type with the Tree option selected; if this decision function includes a nested decision function with rulesets that only matched on the Department
fact type. In this case, the nested decision function could either have as its input specified as an Employee
fact type with the Tree option selected, or a Department
fact type with the List option selected. For this example, the Tree option causes the children of the Employee
instances, including the Department
instances to be asserted (due to the one-to-many relationship between these types). If Employee
is an input to the outer decision function and the Tree option is selected, the then Department
fact type instances are asserted, and you can identify the signature on the inner decision function as a list of Department
instances (these are the exact types which are being matched on for this decision function).
Creating a Business Rules Application with ADF Business Components Facts
The ADF Business Components sample application shows the use of ADF Business Component fact types.
How to Create an Application That Uses ADF Business Components Facts
To work with Oracle Business Rules with ADF Business Components facts, you first need to create an application and a project in Oracle JDeveloper.
To create an application that uses ADF Business Components facts:
-
Start Oracle JDeveloper. This displays the Oracle JDeveloper start page.
-
In the Application Navigator, in the application menu click New Application....
-
In the Name your application page enter the name and location for the new application:
-
In the Application Name field, enter an application name. For example, enter
Chapter10
. -
In the Directory field, enter or browse for a directory name or accept the default.
-
In the Application Package Prefix field, enter an application package prefix. For example, enter
com.example
.This should be a globally unique prefix and is commonly a domain name owned by your company. The prefix, followed by a period, applies to objects created in the initial project of an application.
In this sample, use the prefix
com.example
, as shown in Figure 10-1Click Next.
-
-
On the Name your project page:
-
In the Project Name field, enter
Chapter10
. -
In the Directory field, enter or browse for a directory name or accept the default.
-
In the Project Features area, select ADF Business Components as shown in Figure 10-2.
Click Next.
-
-
On the Configure Java settings page:
-
In the Default Package field, enter
com.example
. -
In the Java Source Path field, enter or browse for a directory name or accept the default.
-
In the Output Directory field, enter or browse for a directory name or accept the default as shown in Figure 10-3:
Click Next.
-
-
For Project 2, on the Name your project page:
-
In the Project Name field, enter
ViewController
. -
In the Directory field, enter or browse for a directory name or accept the default.
-
In the Project Features area, select ADF Faces as shown in Figure 10-4:
Click Next.
-
-
For Project 2, on the Configure Java settings page:
-
In the Default Package field, enter
com.example.view
. -
In the Java Source Path field, enter or browse for a directory name or accept the default.
-
In the Output Directory field, enter or browse for a directory name or accept the default, as shown in Figure 10-5:
-
-
Click Finish when done.
How to Create ADF Business Components Application for Business Rules
You need to add ADF Business Components from a database table. For this example we use the standard HR database tables.
To add ADF Business Components:
How to Update View Object Tuning for Business Rules Sample Application
You should tune the ViewObject
to meet the performance requirements of your application.
To set tuning options for EmployeesView or DepartmentsView:
- In the Application Navigator, double-click EmployeesView to set tuning options for employees or DepartmentsView to set tuning options for departments.
- In the General navigation tab, expand Tuning.
- In the Tuning area, select All Rows.
- In the Tuning area, in the Batches of: field, enter 128.
- In the Tuning area, select All at Once.
How to Create a Dictionary for Oracle Business Rules
You use Oracle JDeveloper to create an Oracle Business Rules dictionary.
To create a dictionary:
How to Add Decision Point Dictionary Links
You need to add a dictionary links to the Oracle Business Rules supplied Decision Point Dictionary. This dictionary supports features for working with the Decision Point interface with ADF Business Components objects.
Add decision point dictionary links:
- In the Rules Designer, click the Links navigation tab.
- From the menu next to the Create button, select Decision Point Dictionary. This operation can take a while to complete. After waiting, Rules Designer adds a link to the Decision Point Dictionary.
How to Import the ADF Business Components Facts
You import ADF Business Components facts with Rules Designer to make these objects available when you create rules.
Import the ADF Business Components facts:
- In Rules Designer, select the Facts navigation tab.
- Select the ADF-BC Facts tab.
- Click the Create... button. This displays the ADF Business Components Fact page.
- In the Connection field, from the list select the connection which your ADF Business Components objects use. The Search Classpath area shows a list of classpaths.
- In the View Definition field, select the name of the view object to import. For example, select com.example.EmployeesView.
- Click OK. This displays the Facts navigation tab.
ADF Business Components Facts can include a circular reference, as indicated with the validation warning:
RUL-05037: A circular definition exists in the data model
When this warning is shown in the Business Rule validation log, you need to manually resolve the circular reference. To do this you clear the Visible check box for one of the properties that is involved in the circular reference.
How to Mark a Property as Non-visible
To mark a property as non-visible:
- Select the Facts navigation tab and select the ADF Business Components Facts tab.Â
- Double-click the icon in the DepartmentsView row.
- In the Properties table, in the EmployeesView row clear the Visible check box.
- Click OK.
How to Set Alias for DepartmentsView and EmployeesView
To set alias for DepartmentsView and EmployeesView:
- Select the Facts navigation tab and select the ADF Business Components Facts tab.
- In the Alias column, replace EmployeesView with Employee.
- In the Alias column, replace DepartmentsView with Department.
How to Add and Run the Outside Manager Ruleset
The sample code that runs the outside manager ruleset invokes the Decision Point with the view object set using the setInputs
method. This invokes the decision function once, with all of the view object rows loaded in a List
. Note that invoking the Decision Point this way is not scalable, because all of the view object rows must be loaded into memory at the same time, which can lead to OutOfMemory exceptions. Only use this invocation style when there are a small and known number of view object rows. You can also use a Decision Point with setViewObject
. For more information, see How to Call a Decision Point with ADF Business Components Facts.
How to Add the Outside Manager Ruleset and Add a Decision Function
After the view objects are imported as facts, you can rename the ruleset and create the decision function for the application.
To rename the ruleset, select the Ruleset_1 navigation tab in Rules Designer and then select the ruleset name and enter Outside Manager Ruleset
to rename the ruleset.
To add a decision function:
Several warnings appear. These warnings are removed in later steps when you add rules to the ruleset.
How to Create the ActionType Java Implementation Class
To create the sample application and to modify the view object in a rule, you need to create a Java implementation class for abstract class oracle.rules.sdk2.decisionpoint.ActionType
. All subclasses of ActionType
must implement the abstract exec
method.
To create the ActionType Java implementation class:
How to Import the Message Action Java Fact
You just created a new Java class and you need to add this class as a Java fact type in Rules Designer to use later when you create rules.
To create the Java fact type:
- In Rules Designer, click the Facts navigation tab.
- Select the Java Facts tab.
- Click Create....
- In the Create Java Fact dialog, in the Classes area navigate in the tree and expand
com
andexample
to display the MessageAction check box. - Select the MessageAction check box.
- Click OK. This adds the fact to the table.
How to Add the Find Managers Rule
You add the rule to find the managers that are in a different departments than their employees.
To add the find managers in different departments rule:
How to Add the Outside Manager Finder Class
Add the outside manager finder class. This uses the Decision Point to execute a decision function.
To add the Outside Manager Finder Class:
- Select the Chapter10 project.
- Right-click and select New....
- In the New Gallery, in the Categories area select General.
- In the New Gallery, in the Items area select Java Class. Click OK.
- In the Name field, enter
OutsideManagerFinder
. Click OK. - Replace the contents of this class with the code shown in example below showing Outside Manager Finder java class.
package com.example; import java.util.ArrayList; import oracle.jbo.ApplicationModule; import oracle.jbo.ViewObject; import oracle.jbo.client.Configuration; import oracle.rules.rl.exceptions.RLException; import oracle.rules.sdk2.decisionpoint.DecisionPoint; import oracle.rules.sdk2.decisionpoint.DecisionPointBuilder; import oracle.rules.sdk2.decisionpoint.DecisionPointInstance; import oracle.rules.sdk2.exception.SDKException; import oracle.rules.sdk2.repository.DictionaryFQN; public class OutsideManagerFinder { private static final String AM_DEF = "com.example.AppModule"; private static final String CONFIG = "AppModuleLocal"; private static final String VO_NAME = "EmployeesView1"; private static final DictionaryFQN DICT_FQN = new DictionaryFQN("com.example", "Chapter10Rules"); private static final String DF_NAME = "FindOutsideManagers"; private DecisionPoint dp = null; public OutsideManagerFinder() { try { dp = new DecisionPointBuilder() .with(DICT_FQN) .with(DF_NAME) .build(); } catch (SDKException e) { System.err.println(e); } } public void run() { final ApplicationModule am = Configuration.createRootApplicationModule(AM_DEF, CONFIG); final ViewObject vo = am.findViewObject(VO_NAME); final DecisionPointInstance point = dp.getInstance(); point.setInputs(new ArrayList<Object>(){{ add(vo); }}); try { point.invoke(); } catch (RLException e) { System.err.println(e); } catch (SDKException e) { System.err.println(e); } } public static void main(String[] args) { OutsideManagerFinder omf = new OutsideManagerFinder(); omf.run(); } }
How to Update ADF META INF for Local Dictionary Access
You need to update the ADF-META-INF
file with MDS information for accessing the dictionary. You can use a local file with MDS to access the Oracle Business Rules dictionary. However, this procedure is not the usual dictionary access method with Oracle Business Rules in a production environment. For information on using a Decision Point to access a dictionary with MDS in a production environment, see What You Need to Know About Using Decision Point in a Production Environment.
To update ADF-META-INF:
How to Copy Definitions/Dictionary to MDS Accessible Location
To copy definitions/dictionary to MDS accessible location:
- In a file system navigator, outside of Oracle JDeveloper navigate to the Chapter10 application, and in the Chapter10 project, in the src folder select and copy the com folder. Or if you want to copy dictionary to MDS accessible location, copy the oracle directory that contains the Oracle Business Rules dictionary.
- In the application directory for Chapter10, above the Chapter10 project, navigate to the .adf directory.
- Copy the com folder to this directory. or copy the oracle folder to this directory.
How to Build and Run the Project to Check the Outside Manager Finder
You can build and test the project by running the find managers with employees in different departments rule.
Build the OutsideManagerFinder configuration:
- From the dropdown menu next to Run button, select Manage Run Configurations....
- In the Project Properties dialog, click New....
- In the Create Run Configuration dialog, enter a name. For example, enter
OutsideManagerFinder
. - Click OK.
- With OutsideManagerFinder selected, click Edit....
- In the Default Run Target field, click Browse....
- Select OutsideManagerFinder.java from the
src\com\example
folder. - Click Open.
- In the Edit Run Configuration dialog, click OK.
- In the Project Properties dialog, click OK.
To run the project, select OutsideManagerFinder in the dropdown menu next to the Run project button. Running this configuration generates output, as shown in example below:
Emp Shelley Higgins(205) in dept 110 manager outside of department, Neena Kochhar(101) in dept 90 Emp Hermann Baer(204) in dept 70 manager outside of department, Neena Kochhar(101) in dept 90 Emp Susan Mavris(203) in dept 40 manager outside of department, Neena Kochhar(101) in dept 90 Emp Michael Hartstein(201) in dept 20 manager outside of department, Steven King(100) in dept 90 Emp Jennifer Whalen(200) in dept 10 manager outside of department, Neena Kochhar(101) in dept 90 Emp Kimberely Grant(178) in dept null manager outside of department, Eleni Zlotkey(149) in dept 80 Emp Eleni Zlotkey(149) in dept 80 manager outside of department, Steven King(100) in dept 90 Emp Gerald Cambrault(148) in dept 80 manager outside of department, Steven King(100) in dept 90 Emp Alberto Errazuriz(147) in dept 80 manager outside of department, Steven King(100) in dept 90 Emp Karen Partners(146) in dept 80 manager outside of department, Steven King(100) in dept 90 Emp John Russell(145) in dept 80 manager outside of department, Steven King(100) in dept 90 Emp Kevin Mourgos(124) in dept 50 manager outside of department, Steven King(100) in dept 90 Emp Shanta Vollman(123) in dept 50 manager outside of department, Steven King(100) in dept 90 Emp Payam Kaufling(122) in dept 50 manager outside of department, Steven King(100) in dept 90 Emp Adam Fripp(121) in dept 50 manager outside of department, Steven King(100) in dept 90 Emp Matthew Weiss(120) in dept 50 manager outside of department, Steven King(100) in dept 90 Emp Den Raphaely(114) in dept 30 manager outside of department, Steven King(100) in dept 90 Emp Nancy Greenberg(108) in dept 100 manager outside of department, Neena Kochhar(101) in dept 90 Emp Alexander Hunold(103) in dept 60 manager outside of department, Lex De Haan(102) in dept 90
How to Add and Run the Department Manager Ruleset
The sample code that runs the department manager ruleset invokes the Decision Point with the view object set using the setViewObject
method. This invokes the decision function once for each row in the view object. All decision function calls occur in the same RuleSession. Between decision function calls, the RuleSession preserves all state from the previous decision function call. Thus, any objects asserted during the previous call remain in working memory for the next call unless they are explicitly retracted by rulesets that you supply. When the state is maintained, you can retract all facts or selectively retract facts between calls by running a ruleset with rules that use the retract action. This ruleset is run as part of the same decision function that you use with the Decision Point. The retract all employees ruleset demonstrates retracting these facts. For more information, see How to Call a Decision Point with ADF Business Components Facts.
How to Add the Department Manager Finder Ruleset
You now add the department manager finder ruleset.
To add the department manager finder ruleset:
- In Rules Designer, click Create Ruleset....
- In the Create Ruleset dialog, in the Name field enter
Department Manager Finder Ruleset
. - Click OK.
How to Add the Find Rule in the Department Manager Finder Ruleset
Next you add the Find rule to find department managers. This rule demonstrates the use of Tree Mode rules with Oracle ADF Business Components fact types.
Add department manager finder rule:
How to Add Retract Employees Ruleset
You add a ruleset to retract the employee fact type instances. This ensures that the Employee fact type is removed between invocations of the decision function.
To add the retract employee ruleset:
- Add the Retract Employees Ruleset.
- In the Retract Employees Ruleset, add a rule and name it Retract all employees.
How to Add the Find Department Managers Decision Function
Now you create the decision function for the department manager finder ruleset. You use this decision function to execute the ruleset from a Decision Point.
To add a decision function for department manager finder ruleset:
How to Add the Department Manager Finder Java Class
Add the department manager finder class. This class include the code with the Decision Point that executes the decision function.
Add the department manager finder class:
- In the Application Navigator, select the Chapter10 project.
- Right-click and select New....
- In the New Gallery, in the Categories area select General.
- In the New Gallery, in the Items area, select Java Class. Click OK.
- In the Name field, enter
DeptManagerFinder
. Click OK. - Replace the contents of this class with the code example shown below:
package com.example; import oracle.jbo.ApplicationModule; import oracle.jbo.ViewObject; import oracle.jbo.client.Configuration; import oracle.jbo.server.DBTransactionImpl2; import oracle.rules.rl.exceptions.RLException; import oracle.rules.sdk2.decisionpoint.DecisionPoint; import oracle.rules.sdk2.decisionpoint.DecisionPointBuilder; import oracle.rules.sdk2.decisionpoint.DecisionPointInstance; import oracle.rules.sdk2.exception.SDKException; import oracle.rules.sdk2.repository.DictionaryFQN; public class DeptManagerFinder { private static final String AM_DEF = "com.example.AppModule"; private static final String CONFIG = "AppModuleLocal"; private static final String VO_NAME = "EmployeesView1"; private static final String DF_NAME = "FindDepartmentManagers"; private static final DictionaryFQN DICT_FQN = new DictionaryFQN("com.example", "Chapter10Rules"); private DecisionPoint dp = null; public DeptManagerFinder() { try { dp = new DecisionPointBuilder() .with(DICT_FQN) .with(DF_NAME) .build(); } catch (SDKException e) { System.err.println(e); } } public void run() { final ApplicationModule am = Configuration.createRootApplicationModule(AM_DEF, CONFIG); final ViewObject vo = am.findViewObject(VO_NAME); final DecisionPointInstance point = dp.getInstance(); point.setTransaction((DBTransactionImpl2)am.getTransaction()); point.setAutoCommit(true); point.setViewObject(vo); try { point.invoke(); } catch (RLException e) { System.err.println(e); } catch (SDKException e) { System.err.println(e); } } public static void main(String[] args) { new DeptManagerFinder().run(); } }
How to Copy the Dictionary to an MDS Accessible Location
Copy the updated dictionary to an MDS accessible location.
Copy dictionary to MDS accessible location:
- In a file system navigator, outside of Oracle JDeveloper, navigate to the Chapter10 application, and project and copy the oracle directory that contains the dictionary.
- In the application directory for Chapter10, above the Chapter10 project, navigate to the
.adf
directory. - Copy the oracle folder to this directory.
How to Build and Run the Project to Check the Find Managers Rule
You can build and test the project to execute the department manager finder ruleset.
Build the project:
- From the dropdown menu next to Run button, select Manage Run Configurations....
- In the Project Properties dialog, click New....
- In the Create Run Configuration dialog, enter the name. For example, enter
DeptManagerFinder
. - In the Copy Settings From field, enter Default.
- Click OK.
- With DeptManagerFinder selected, click Edit....
- In the Default Run Target field, click Browse....
- Select DeptManagerFinder.java from the
src\com\example
directory and click Open. - In the Edit Run Configuration dialog, click OK.
- In the Project Properties dialog, click OK.
To run the project, select DeptManager Finder in the menu, next to the Run project button. Running the decision point generates output, as shown in code example below:
Michael Hartstein is the manager of dept Marketing John Russell is the manager of dept Sales Adam Fripp is the manager of dept Shipping Den Raphaely is the manager of dept Purchasing Alexander Hunold is the manager of dept IT Shelley Higgins is the manager of dept Accounting Hermann Baer is the manager of dept Public Relations Susan Mavris is the manager of dept Human Resources Jennifer Whalen is the manager of dept Administration Nancy Greenberg is the manager of dept Finance Steven King is the manager of dept Executive Shelley Higgins is the manager of dept Accounting Hermann Baer is the manager of dept Public Relations Susan Mavris is the manager of dept Human Resources Jennifer Whalen is the manager of dept Administration Nancy Greenberg is the manager of dept Finance Alexander Hunold is the manager of dept IT Alexander Hunold is the manager of dept IT Nancy Greenberg is the manager of dept Finance Den Raphaely is the manager of dept Purchasing Adam Fripp is the manager of dept Shipping John Russell is the manager of dept Sales Jennifer Whalen is the manager of dept Administration Michael Hartstein is the manager of dept Marketing Susan Mavris is the manager of dept Human Resources Hermann Baer is the manager of dept Public Relations Shelley Higgins is the manager of dept Accounting
When you see duplicate entries in the output, when working with tree mode rules in this example, the duplicate entries are due to multiple rule firings on the same data in a different part of the view object graph.
How to Add and Run the Raises and Retract Employees Rulesets
The sample code that runs the raises ruleset invokes the Decision Point by specifying the view object using the setViewObject
method. This invokes the decision function once for each row in the view object. The retract employees ruleset retracts all instances of Employee
asserted for each call, so that they do not remain in working memory between calls to the decision function. The action type shown in How to Create the Raise ActionType Java Implementation Class shows how to change the ViewRowImpl
attribute values with a ActionType
.
For more information, see How to Call a Decision Point with ADF Business Components Facts.
How to Add the Raises Ruleset
You now add the raises ruleset.
To add the raises ruleset:
- In Rules Designer, click Create Ruleset....
- In the Create Ruleset dialog, in the Name field enter
Raises Ruleset
. - Click OK.
How to Create the Raise ActionType Java Implementation Class
To create this part of the sample application and to modify the view object in the raises rule, you need to create a Java implementation class for the abstract class oracle.rules.sdk2.decisionpoint.ActionType
. All subclasses of ActionType
must implement the abstract exec
method.
To create the raise ActionType Java implementation class:
How to Import the Raise Action Java Fact
You just created a new Java class. You import this class as a Java fact type in Rules Designer to use later when you create rules.
To create the Java fact type:
How to Add the 12 Year Raise Rule
This rule shows how to use action types to update database entries.
To add 12 year raise rule:
How to Add the Employee Raises Decision Function
Now create the decision function for the employee raises and the retract all employees rulesets.
To add a decision function:
How to Add the Employee Raises Java Class
Add the employee raises class. This executes the decision function.
To add the employee raises class:
- Select the Chapter10 project.
- Right-click and select New....
- In the New Gallery, in the Categories area select General.
- In the New Gallery, in the Items area, select Java Class. Click OK.
- In the Name field, enter
EmployeeRaises
. Click OK. - Replace the contents of this class with the
deptmanagerfinder
class code shown below:
package com.example; import oracle.jbo.ApplicationModule; import oracle.jbo.ViewObject; import oracle.jbo.client.Configuration; import oracle.jbo.server.DBTransactionImpl2; import oracle.rules.rl.exceptions.RLException; import oracle.rules.sdk2.decisionpoint.DecisionPoint; import oracle.rules.sdk2.decisionpoint.DecisionPointBuilder; import oracle.rules.sdk2.decisionpoint.DecisionPointInstance; import oracle.rules.sdk2.exception.SDKException; import oracle.rules.sdk2.repository.DictionaryFQN; public class EmployeeRaises { private static final String AM_DEF = "com.example.AppModule"; private static final String CONFIG = "AppModuleLocal"; private static final String VO_NAME = "EmployeesView1"; private static final String DF_NAME = "EmployeeRaises"; private static final DictionaryFQN DICT_FQN = new DictionaryFQN("com.example", "Chapter10Rules"); private DecisionPoint dp = null; public EmployeeRaises() { try { dp = new DecisionPointBuilder() .with(DICT_FQN) .with(DF_NAME) .build(); } catch (SDKException e) { System.err.println(e); } } public void run() { final ApplicationModule am = Configuration.createRootApplicationModule(AM_DEF, CONFIG); final ViewObject vo = am.findViewObject(VO_NAME); final DecisionPointInstance point = dp.getInstance(); point.setTransaction((DBTransactionImpl2)am.getTransaction()); point.setAutoCommit(true); point.setViewObject(vo); try { point.invoke(); } catch (RLException e) { System.err.println(e); } catch (SDKException e) { System.err.println(e); } for (DecisionPoint.NamedValue result : point.getResults()){ System.out.println(result.getName() + " " + result.getValue()); } } public static void main(String[] args) { new EmployeeRaises().run(); } }
How to Copy Dictionary to MDS Accessible Location
Copy the updated dictionary to the MDS accessible location.
To copy dictionary to MDS accessible location:
- In a file system navigator, outside of Oracle JDeveloper, navigate to the Chapter10 folder and the Chapter10 project and copy the oracle directory that contains the dictionary.
- In the application directory for Chapter10, above the Chapter10 project, navigate to the
.adf
directory. - Copy the oracle folder to this directory.
How to Build and Run the Project to Check the Raises Rule
You can build and test the project by running employee raises ruleset.
To build the project:
- From the dropdown menu next to Run button, select Manage Run Configurations....
- In the Project Properties dialog, click New....
- In the Create Run Configuration dialog, enter the name. For example, enter
EmployeeRaises
. - In the Copy Settings From field, enter Default. Click OK.
- With EmployeeRaises selected, click Edit....
- In the Default Run Target field, click Browse....
- Select EmployeeRaises.java from the
src\com\example
folder. Click Open. - In the Edit Run Configuration dialog, click OK.
- In the Project Properties dialog, click OK.
To run the project, select EmployeeRaises in the menu, next to the Run project button. Oracle JDeveloper displays the output as shown in example below:
raise for 100 0.03=>81.7 raise for 101 0.03=>1872.46 raise for 102 0.03=>60596.78 raise for 103 0.03=>31146.26 raise for 104 0.03=>20159.43 raise for 108 0.03=>35822.68 raise for 109 0.03=>26084.5 raise for 114 0.03=>27500.92 raise for 115 0.03=>7524.5 raise for 120 0.03=>16262.34 raise for 121 0.03=>16183.41 raise for 122 0.03=>15591.35 raise for 131 0.03=>3671.33 raise for 133 0.03=>4567.98 raise for 137 0.03=>4838.1 raise for 141 0.03=>4703.71 raise for 142 0.03=>4044.79 raise for 145 0.03=>17734.79 raise for 146 0.03=>17101.39 raise for 147 0.03=>15201.23 raise for 150 0.03=>12667.7 raise for 151 0.03=>12034.32 raise for 156 0.03=>13047.73 raise for 157 0.03=>12395.35 raise for 158 0.03=>11400.93 raise for 159 0.03=>10134.16 raise for 168 0.03=>14567.86 raise for 174 0.03=>13934.48 raise for 175 0.03=>11147.58 raise for 184 0.03=>5480.03 raise for 185 0.03=>5193.76 raise for 192 0.03=>5219.1 raise for 193 0.03=>4940.41 raise for 200 0.03=>5740.99 raise for 201 0.03=>16962.05 raise for 203 0.03=>8481.03 raise for 204 0.03=>13047.73 raise for 205 0.03=>15657.27 raise for 206 0.03=>10829.62