Red Hat Training
A Red Hat training course is available for JBoss Enterprise SOA Platform
Chapter 13. Using Spreadsheet Decision Tables
13.1. Hard Keywords
Hard keywords are words which you cannot use when naming your domain objects, properties, methods, functions and other elements that are used in the rule text. These are words such as
true
, false
and other words which could easily be mistaken for a command.
13.2. Soft Keywords
Soft keywords can be used for naming domain objects, properties, methods, functions and other elements. The rules engine recognizes their context and processes them accordingly.
13.3. List of Soft Keywords
lock-on-active
date-effective
date-expires
no-loop
auto-focus
activation-group
agenda-group
ruleflow-group
entry-point
duration
package
import
dialect
salience
enabled
attributes
rule
extend
- when
- then
template
query
declare
function
global
eval
not
in
or
and
exists
forall
- accumulate
- collect
- from
action
reverse
result
end
- over
init
13.4. Comments
Comments are sections of text that are ignored by the rule engine. They are stripped out when they are encountered, except inside semantic code blocks (like a rule's RHS).
13.5. Single Line Comment Example
This is what a single line comment looks like. To create single line comments, you can use '//'. The parser will ignore anything in the line after the comment symbol:
rule "Testing Comments" when // this is a single line comment eval( true ) // this is a comment in the same line of a pattern then // this is a comment inside a semantic code block end
13.6. Multi-Line Comment Example
This is what a multi-line comment looks like. This configuration comments out blocks of text, both in and outside semantic code blocks:
rule "Test Multi-line Comments" when /* this is a multi-line comment in the left hand side of a rule */ eval( true ) then /* and this is a multi-line comment in the right hand side of a rule */ end
13.7. Error Messages
- 101: No viable alternative
- Indicates when the parser came to a decision point but couldn't identify an alternative.
- [ERR 101] Line 3:2 no viable alternative at input 'WHEN'
- This message means the parser has encountered the token
WHEN
(a hard keyword) which is in the wrong place, since the rule name is missing. - [ERR 101] Line 0:-1 no viable alternative at input '<eof>' in rule simple_rule in pattern [name]
- Indicates an open quote, apostrophe or parentheses.
- 102: Mismatched input
- Indicates that the parser was looking for a particular symbol that it didn't end at the current input position.
- [ERR 102] Line 0:-1 mismatched input '<eof>' expecting ')' in rule simple_rule in pattern [name]
- This error is the result of an incomplete rule statement. Usually when you get a 0:-1 position, it means that parser reached the end of source.
- 103: Failed predicate
- A validating semantic predicate evaluated to false. Usually these semantic predicates are used to identify soft keywords.
- 104: Trailing semi-colon not allowed
- This error is associated with the
eval
clause, where its expression may not be terminated with a semicolon. - 105: Early Exit
- The recognizer came to a subrule in the grammar that must match an alternative at least once, but the subrule did not match anything.
13.8. Package
A package is a collection of rules and other related constructs, such as imports and globals. The package members are typically related to each other, such as HR rules. A package represents a namespace, which ideally is kept unique for a given grouping of rules. The package name itself is the namespace, and is not related to files or folders in any way.
It is possible to assemble rules from multiple rule sources, and have one top level package configuration that all the rules are kept under (when the rules are assembled). It is not possible to merge into the same package resources declared under different names. A single Rulebase may, however, contain multiple packages built on it. A common structure is to have all the rules for a package in the same file as the package declaration (so that is it entirely self-contained).
13.9. Import Statements
Import statements work like import statements in Java. You need to specify the fully qualified paths and type names for any objects you want to use in the rules. JBoss Rules automatically imports classes from the Java package of the same name, and also from the package
java.lang
.
13.10. Using Globals
In order to use globals you must:
- Declare the global variable in the rules file and use it in rules. Example:
global java.util.List myGlobalList; rule "Using a global" when eval( true ) then myGlobalList.add( "Hello World" ); end
- Set the global value on the working memory. It is best practice to set all global values before asserting any fact to the working memory. Example:
List list = new ArrayList(); WorkingMemory wm = rulebase.newStatefulSession(); wm.setGlobal( "myGlobalList", list );
13.11. The From Element
The from element allows you to pass a Hibernate session as a global. It also lets you pull data from a named Hibernate query.
13.12. Using Globals with an e-Mail Service
Procedure 13.1. Task
- Open the integration code that is calling the rule engine.
- Obtain your emailService object and then set it in the working memory.
- In the DRL, declare that you have a global of type emailService and give it the name "email".
- In your rule consequences, you can use things like email.sendSMS(number, message).
Warning
Globals are not designed to share data between rules and they should never be used for that purpose. Rules always reason and react to the working memory state, so if you want to pass data from rule to rule, assert the data as facts into the working memory.Important
Do not set or change a global value from inside the rules. We recommend to you always set the value from your application using the working memory interface.