The <variable> element defines a variable and contains information in a form that can be referenced by expressions. A variable can be defined at the level of the rule set, a rule block, or a rule.
Variables can have the same name at different levels in the rule hierarchy. When a variable is accessed, the most local definition of the variable is used. For example, if a variable is defined at the rule set level, rule block level, and rule level with the same name, the variable definition at the rule level is used by the expressions within that rule.
When variables are defined at the rule set or rule block level, multiple rules get and set these variables at different times. Therefore, to ensure that variable values are maintained correctly, be aware of how you code the interactions among variables in the rule set.
If the variable is defined at the rule set or rule block level, it is not reinitialized after the rule pattern is matched.
If a rule is defined with a grouping key, rule variables that are defined by the <variable> element are not valid within life cycle actions or within a <filteringPredicate> element that is contained within an <activateOnEvent> or <deactivateOnEvent> element that is contained within an <activationInterval> element. This is because in this case, the rule variables apply only to a rule instance, and rule instances do not exist at the time that these expressions are run.
<variable> has the following attributes:
Name | Description | Data type | Required? |
---|---|---|---|
name | Identifies a specific variable. A variable is referenced by its name. | xsd:NMTOKEN | Yes |
dataType | Identifies the type of information that a variable contains. This must be a fully qualified data type such as java.lang.String. | xsd:NMTOKEN | Yes |
<variable> contains the following elements.
The elements must be coded in the order that is shown. If an element is optional, it does not need to be coded, but all elements that are coded must follow the correct order.
Element | Required or optional? |
---|---|
<comment> | Optional. 0 or 1 occurrence is allowed. |
<varInitializer> | Required. 1 occurrence is allowed. |