2. Content
Rules Engine ? What is this ?
When to use ?
RETE algorithm
In Java - JBoss Drools
features
Syntax
In ruby ?
3. Motor de Reglas ¿what a #@!$ ?
Application
Rule engine
Rule def.
knowledge base
Rule def
4. When to use?
Infrastructure decoupled from the source
System to allow the final user to define their rules
and change them dynamically
Developers are not the primary responsibility for changing
rules
Moderate or high complexity of business rules
Performance !
Conclusion: down the rules of your system, so that
they can be reused, decouple the code and allow the
user to know the rules ... to modify !
5. Tipical Code
01 if ((user.isMemberOf(AdministratorGroup) && user.isMemberOf(teleworkerGroup)) ||
02 user.isSuperUser() {
03 // more checks for specific cases
04 if ((expenseRequest.code().equals("B203") ||
05 (expenseRequest.code().equals("A903") && (totalExpenses < 200) &&
06 (bossSignOff > totalExpenses)) && (deptBudget.notExceeded)) {
07 // issue payments
08 }
09 else if {
10 // check lots of other conditions
11 }
12 }
Change this rules ! ... %&”@ !
13 else {
14 // even more business logic
15 }
16 }
6. Rete Algorithm – the
Solution !
Pattern matching algorithm for implementing
production rule systems (wikipedia)
http://en.wikipedia.org/wiki/Rete_algorithm
replaces if ... Then with an optimized logic network
7. Rete 2
RETE – characteristics:
It reduces or eliminates certain types of redundancy through the use of
node sharing.
It stores partial matches when performing joins between different fact
types.
It allows for efficient removal of memory elements when facts are
retracted from working memory.
Widely used to implement matching functionality within pattern-
matching engines that exploit a match-resolve-act cycle to
support forward and backward chaining and inferencing.
Forward-chaining (facts -> goals)
Backguard chaining (goals -> facts)
8. Drools –
http://jboss.org/drools
Introduction video -
http://blog.athico.com/2010/03/fosdem-50-minute-introduction-into.h
Introduction
Rules engine framework for java
It is used in other jboss projects like JBPM, SEAM, Jboss ESB and
others
The rules can be written in Java, Python, Groovy Ruby and your own
rule language
Jboss tools – plugin Eclipse to use this framework
10. Syntax or rules definition
rule “<name>”
<attribute> <value>
when
<LHS>
then
<RHS>
end
11. Example – rule definition
rule.drl
package com.mooveit.rule
import com.mooveit.rule.Global
global Global global;
rule "r1 - first rule"
when
instance : Entity( field1 > 2010)
then
...
end
12. Example
Business Action
1
StatefulSession session = getRulesBase().newStatefulSession();
session.setGlobal("rulesImpl", this);
session.insert(instance);
session.fireAllRules();
2
3 knowledge base
+instance
Execute the “then” part for the instance that match the facts
17. Rules Engine In ruby
Ruleby - https://github.com/codeaspects/ruleby
Rule engine for Ruby
Active project but with few people dedicated
October 11 of 2010 last push in the master branch
Install: gem install ruleby
Others
Ruby Rools - http://rools.rubyforge.org/
Last activity (ago 2009 in rools users)
Status: Beta
2 members
A few projects without activity: rein, ruby-rules
Treetop
language for describing languages
It's not a pure rule engine ... but can serve
18. Ruleby Description
Provide a pure Ruby Domain Specific Language (DSL)
rule [m.status == :HELLO] do |v|
puts v[:m].message
end
(LHS) Left hand side of the production (the ‘when’) is
represented by the Array parameter
(RHS) The right hand side (the ‘then’) is represented by the
block.
engine :hello_engine do |e|
HelloWorldRulebook.new(e).rules
assert e, Message.new(:HELLO, ‘Hello World’)
e.match
end
Result:Hello World
19. Ruleby links
The Ruleby DSL for more information.
https://github.com/codeaspects/ruleby/wiki/The-Ruleby
Download
http://gemcutter.org/gems/ruleby
User Group
http://groups-beta.google.com/group/ruleby