Release 2 (9.2)
Part Number A96571-01
This chapter explains the concepts related to rules.
This chapter contains these topics:
A rule is a database object that enables a client to perform an action when an event occurs and a condition is satisfied. Rules are evaluated by a rules engine, which is a built-in part of Oracle. Both user-created applications and Oracle features, such as Streams, can be clients of the rules engine.
A rule consists of the following components:
Each rule is specified as a condition that is similar to the condition in the
WHERE clause of a SQL query. You can group related rules together into rule sets. A single rule can be in one rule set, multiple rule sets, or no rule sets.
A rule condition combines one or more expressions and operators and returns a Boolean value, which is a value of
NULL (unknown). An expression is a combination of one or more values and operators that evaluate to a value. A value can be data in a table, data in variables, or data returned by a SQL function or a PL/SQL function. For example, the following condition consists of two expressions (
30) and an operator (
This logical condition evaluates to
TRUE for a given row when the
department_id column is
30. Here, the value is data in the
department_id column of a table.
A single rule condition may include more than one condition combined with the
NOT conditional operators to form compound conditions. For example, consider the following compound condition:
This rule condition contains two conditions joined by the
OR conditional operator. If either condition evaluates to
TRUE, then the rule condition evaluates to
TRUE. If the conditional operator were
AND instead of
OR, then both conditions would have to evaluate to
TRUE for the entire rule condition to evaluate to
Rule conditions may contain variables. When you use variables in rule conditions, precede each variable with a colon (:). The following is an example of a variable used in a rule condition:
Variables enable you to refer to data that is not stored in a table. A variable may also improve performance by replacing a commonly occurring expression. Performance may improve because, instead of evaluating the same expression multiple times, the variable is evaluated once.
A rule condition may also contain an evaluation of a call to a subprogram. These conditions are evaluated in the same way as other conditions. That is, they evaluate to a value of
FALSE, or unknown. The following is an example of a condition that contains a call to a simple function named
is_manager that determines whether an employee is a manager:
Here, the value of
employee_id is determined by data in a table where
employee_id is a column.
Oracle9i SQL Reference for more information about conditions, expressions, and operators
A rule evaluation context is a database object that defines external data that can be referenced in rule conditions. The external data can either exist as variables, table data, or both.
A rule evaluation context provides the necessary information for interpreting and evaluating the rule conditions that reference external data. For example, if a rule refers to a variable, then the information in the rule evaluation context must contain the variable type. Or, if a rule refers to a table alias, then the information in the evaluation context must define the table alias.
The objects referenced by a rule are determined by the rule evaluation context associated with it. The rule owner must have the necessary privileges to access these objects, such as
SELECT privilege on tables,
EXECUTE privilege on types, and so on. The rule condition is resolved in the schema that owns the evaluation context.
For example, consider a rule evaluation context named
hr_evaluation_context that contains the following information:
depcorresponds to the
loc_id2are both of type
hr_evaluation_context rule evaluation context provides the necessary information for evaluating the following rule condition:
In this case, the rule condition evaluates to
TRUE for a row in the
hr.departments table if that row has a value in the
location_id column that corresponds to either of the values passed in by the
loc_id2 variables. The rule cannot be interpreted or evaluated properly without the information in the
hr_evaluation_context rule evaluation context.
The value of a variable referenced in a rule condition may be explicitly specified when the rule is evaluated, or the value of a variable may be implicitly available given the event.
Explicit variables are supplied by the caller at evaluation time. These values are specified by the
variable_values parameter when the
DBMS_RULE.EVALUATE procedure is run.
Implicit variables are not given a value at evaluation time. The value of an implicit variable is obtained by calling the variable value evaluation function. You define this function when you specify the
variable_types list during the creation of an evaluation context using the
DBMS_RULE_ADM.CREATE_EVALUATION_CONTEXT procedure. If the value for an implicit variable is specified during evaluation, then the specified value overrides the value returned by the variable value function.
variable_types list is of type
SYS.RE$VARIABLE_TYPE_LIST, which is a list of variables of type
SYS.RE$VARIABLE_TYPE. Within each instance of
SYS.RE$VARIABLE_TYPE in the list, the function used to determine the value of an implicit variable is specified as the
Whether variables are explicit or implicit is the choice of the designer of the application using the rules engine. The following are reasons for using an implicit variable:
DBMS_RULE.EVALUATEprocedure does not need to know anything about the variable, which may reduce the complexity of the application using the rules engine. For example, a variable may call a function that returns a value based on the data being evaluated.
DBMS_RULE.EVALUATEprocedure does not know the variable value based on the event, which may improve security if the variable value contains confidential information.
DBMS_RULE.EVALUATEprocedure does not need to specify many uncommon variables.
For example, in the following rule condition, the values of variable
x and variable
y could be specified explicitly, but the value of the variable
max could be returned by running the
y could be implicit variables, and variable
max could be an explicit variable. As you can see, there is no syntactic difference between explicit and implicit variables in the rule condition. You can determine whether a variable is explicit or implicit by querying the
DBA_EVALUATION_CONTEXT_VARS data dictionary view. For explicit variables, the
VARIABLE_VALUE_FUNCTION field is
NULL. For implicit variables, this field contains the name of the function called by the implicit variable.
A single rule evaluation context can be associated with multiple rules or rule sets. The following list describes which evaluation context is used when a rule is evaluated:
ADD_RULEprocedure in the
DBMS_RULE_ADMpackage, then the evaluation context specified in the
ADD_RULEprocedure is used for the rule when the rule set is evaluated.
ADD_RULEprocedure, then the evaluation context of the rule set is used for the rule when the rule set is evaluated.
You have the option of creating an evaluation function to be run with a rule evaluation context. You can then associate the function with the rule evaluation context by specifying the function name for the
evaluation_function parameter when you create the rule evaluation context with the
CREATE_EVALUATION_CONTEXT procedure in the
DBMS_RULE_ADM package. Then, this function evaluates rules using the evaluation context. The function must have the same parameter names and types as the
The evaluation function can be used to decide whether normal evaluation by the rules engine should continue. If evaluation by the rules engine should continue, then the evaluation function returns
DBMS_RULE_ADM.EVALUATION_CONTINUE. Otherwise, it returns one of the following values:
You should create an evaluation function only if you want to bypass the rules engine for evaluation. If you specify an evaluation function for an evaluation context, then the evaluation function is run during evaluation when the evaluation context is used by a rule set or rule, unless a particular rule in the rule set has its own evaluation context or an evaluation context was specified for a rule in the call to the
Oracle9i Supplied PL/SQL Packages and Types Reference for more information about the evaluation function specified in the
A rule action context contains optional information associated with a rule that is interpreted by the client of the rules engine when the rule is evaluated for an event. The client of the rules engine can be a user-created application or an internal feature of Oracle, such as Streams. Each rule has only one action context. The information in an action context is of type
SYS.RE$NV_LIST, which is a type that contains an array of name-value pairs.
The rule action context information provides a context for the action taken by a client of the rules engine when a rule evaluates to
TRUE. The rules engine does not interpret the action context. Instead, it returns the action context information when a rule evaluates to
TRUE. Then, a client of the rules engine can interpret the action context information.
For example, suppose an event is defined as the addition of a new employee to a company. If the employee information is stored in the
hr.employees table, then the event occurs whenever a row is inserted into this table. The company wants to specify that a number of actions are taken when a new employee is added, but the actions depend on which department the employee joins. One of these actions is that the employee is registered for a course relating to the department.
In this scenario, the company can create a rule for each department with an appropriate action context. Here, an action context returned when a rule evaluates to
TRUE specifies the number of a course that an employee should take. Here are the rule conditions and the action contexts for three departments:
|Rule Name||Rule Condition||Action Context Name-Value Pair|
These action contexts return the following instructions to the client application:
rule_dep_10rule instructs the client application to enroll the new employee in course number
rule_dep_20rule instructs the client application to enroll the new employee in course number
NULLaction context for the
rule_dep_30rule instructs the client application not to enroll the new employee any course.
Each action context can contain zero or more name-value pairs. If an action context contains more than one name-value pair, then each name in the list must be unique. In this example, the client application to which the rules engine returns the action context registers the new employee in the course with the returned course number. The client application does not register the employee for a course if a
NULL action context is returned or if the action context does not contain a course number.
If multiple clients use the same rule, or if you want an action context to return more than one name-value pair, then you can list more than one name-value pair in an action context. For example, suppose the company also adds a new employee to a department electronic mailing list. In this case, the action context for the
rule_dep_10 rule might contain two name-value pairs:
The following are considerations for names in name-value pairs:
Streams uses action contexts for rule-based transformations and, when subset rules are specified, for internal transformations that may be required on LCRs containing
You can add a name-value pair to an action context using the
ADD_PAIR member procedure of the
RE$NV_LIST type. You can remove a name-value pair from an action context using the
REMOVE_PAIR member procedure of the
RE$NV_LIST type. If you want to modify an existing name-value pair in an action context, then you should first remove it using the
REMOVE_PAIR member procedure and then add an appropriate name-value pair using the
ADD_PAIR member procedure.
The rules engine evaluates rule sets based on events. An event is an occurrence that is defined by the client of the rules engine. When an event occurs, the client sends the event to the
DBMS_RULE.EVALUATION procedure for evaluation in the form of an event context. An event context is a varray of type
SYS.RE$NV_LIST that contains name-value pairs that identify the event. This information is not directly used or interpreted by the rules engine. Instead, it is passed to client callbacks, such as a callback for an implicit variable. The client also sends the name of the rule set whose rules should be used to evaluate the event.
Along with the event context and rule set name, the client may also send table values and variable values, as well as other information. The table values contain rowids that refer to the data in table rows, and the variable values contain the data for explicit variables.
You specify an evaluation context when you run the
DBMS_RULE.EVALUATION procedure. Only rules that use the specified evaluation context are evaluated.
The rules engine uses the rules in the specified rule set to evaluate the event. Then, the rules engine returns the results to the client. The rules engine returns rules using the two
OUT parameters in the
maybe_rules. That is, the
true_rules parameter returns rules that evaluate to
TRUE, and, optionally, the
maybe_rules parameter returns rules that may evaluate to
TRUE given more information.
Figure 5-1 show the rule set evaluation process:
TRUEto the client. Each returned rule is returned with its entire action context, which may contain information or may be
Oracle9i Supplied PL/SQL Packages and Types Reference for more information about the
You can create the following types of database objects directly using the
You can create rules and rule sets indirectly using the
DBMS_STREAMS_ADM package. You control the privileges for these database objects using the following procedures in the
To allow a user to create rule sets, rules, and evaluation contexts in the user's own schema, grant the user the following system privileges:
These privileges, and the privileges discussed in the following sections, can be granted to the user directly or through a role.
When you grant a privilege on
If you want to grant access to an object in the
To create an evaluation context, rule, or rule set in a schema, a user must meet at least one of the following conditions:
To alter an evaluation context, rule, or rule set, a user must meet at least one of the following conditions:
ALTER_ON_RULE_SETobject privilege on the rule set.
To drop an evaluation context, rule, or rule set, a user must meet at least one of the following conditions:
This section describes the privileges required to place a rule in a rule set.
The user must meet at least one of the following conditions for the rule:
hrschema in a rule set, a user must be granted the
EXECUTE_ON_RULEprivilege for the
The user also must meet at least one of the following conditions for the rule set:
human_resourcesrule set in the
hrschema, a user must be granted the
ALTER_ON_RULE_SETprivilege for the
To evaluate a rule set, a user must meet at least one of the following conditions:
hrschema, a user must be granted the
EXECUTE_ON_RULE_SETprivilege for the
EXECUTE object privilege on a rule set requires that the grantor have the
EXECUTE privilege specified
OPTION on all rules currently in the rule set.
To use an evaluation context, a user must meet at least one of the following conditions for the evaluation context: