Using the BasicRules Engine
This is a very basic rules engine that implements com.liferay.portal.kernel.bi.rules.RulesEngine interface. A rules engine evaluates whether a user is qualified for something, or not.
This engine evaluates a rule for "a user" to decide whether the user is qualified or not. The rule can be constructed based on the user's attributes, time of day, date and day of the week.
Although, this implementation is currently based only on user's attributes, it can be enhanced to consider other aspects like user's membership etc.
Unlike the regular business rules engine, this is a simplest implementation.
- It does NOT use the popular RETE algorithm.
- It is always stateless.
- It does not support Queries.
- It does not persist the rules, which is the responsibility of the rules engine extending from this basic module or the module utilizing this rules engine. Thus it does not support administrative methods like add/remove/update.
In other words, this works as an ad-hoc evaluator of rules for an user.
The Engine #
|Grouping||( )||Used for grouping the rules to override precedence|
|Comparison||= != > >= < <= =||The = operator performs a match when wildcard is used.|
Also remember the equality operator is == and not a single equals
|AND and OR respectively|
- The user attributes are prefixed by "user." string. e.g. user.screenName.
- If the specified attribute is not a regular attribute, then it looks for a custom attribute.
- User attributes can use splatters () only with the "=" operator.
For example, user.firstName=Jo will evaluate to true for all users with first name starting with Jo.
- To specify date with user attributes, such as birthday, use the full format mm/dd/yyyy. This means if the date is 4/5/2009, then specify it as user.birthday==04/05/2009.
- Attributes listed below are available for any user, provided the values have been defined; otherwise they are treated as empty.
- The name for the date attribute is "date". e.g. date==04/14/2009
- The format is mm/dd/yyyy. Although the format is specified in Locale.US, it is evaluated in the user's timezone at runtime.
- Date can accept splatters () as wildcards. e.g. date>=/15/ will evaluate to true on 15th of every month.
date==mm// - matches the month
date==/dd/ - matches the date
date==//yyyy - matches the year
- The name for the time attribute is "time" and the format is hh:mm. e.g. time==13:41.
- The time is specified in 24 hours format.
- Time can accept splatters () as a wildcard.
For example: time==:00 will evaluate to true on the hour.
time==hh: - matches the hour
time==:mm - matches the minute
Day of the week#
- The name for the day attribute is "day". e.g. day==2.
- Days are numbered 1..7 for Sunday..Saturday.
day== will evaluate to true every day, while day>1&&day<7 will evaluate to true for weekdays.
Note: Only day, date and time take wildcards with any operator.
How to use it#
You need a BasicFact to be passed on to the engine for execution.
See com.liferay.portal.bi.rules.basicrulesengine.BasicFact in portal-service.
Construct the BasicFact and set the userId for the user.
Then call execute method of the BasicRulesEngine with the fact and the rule string or the ResourceRetriever for the rule.
The engine will evaluate the rule for the user and set the "qualified" field of the fact depending on the outcome.
After the execution, you can call the "isQualified" method to find out if the user passed the rule.
BasicRulesEngine basicRulesEngine = new BasicRulesEngine();BasicFact basicFact = new BasicFact(10145); String rule = "user.screenName = Jo"; basicRulesEngine.execute(rule, Arrays.asList(basicFact)); boolean userCanDoXYZ = basicFact.isQualified();}}}
Examples of Rules #
|date >= /1/ && date <= /7/ && (day == 1 |
||day == 7)
|Evaluates to true during first 7 days of every month but only on weekends|
|user.loginIP = 192.168.. && user.male != true||True for female users who login from IP address that matches 192.168..|
|user.age >= 17.6||users who are 17 years and 6 months or older.|