Droolsand Rule Based Systems 2008 Srping

20
Drools and Rule Based Systems Srinath Perera

description

Presentation at IU, to the research group

Transcript of Droolsand Rule Based Systems 2008 Srping

Page 1: Droolsand Rule Based Systems 2008 Srping

Drools and Rule Based Systems

Srinath Perera

Page 2: Droolsand Rule Based Systems 2008 Srping

Rule Engine

• Terms Expert Systems / Business rules engine / Production Systems / Inference Engines are used to address rule engines based on their implementations.

Usually a Rule engine usually includes three parts. • Facts represented as working memory or another

set of rules e.g. Prolog  road(a,b) or Drools objects• Set of rules that declaratively define conditions or

situations e.g. Prolog route(X,Z) <- road(X,Z)• Actions executed or inference derived based on the

rules

Page 3: Droolsand Rule Based Systems 2008 Srping

RulesAllow users to specify the requirements declarative, using a

logic based languages. (Say what should happen, not how to do it). Rules may trigger other rules.

Four types of rules (from http://www.w3.org/2000/10/swap/doc/rule-systems)

• Derivation or Deduction Rules – Each rules express if some statements are true, another statement must be true. Called logical implication. E.g. Prolog

• Transformation Rules- transform between knowledge bases, e.g. therom proving

• Integrity Constraints – verification rules • Reaction or Event-Condition-Action (ECA) Rules – includes a

actions in addition to inference. e.g. Drools

Page 4: Droolsand Rule Based Systems 2008 Srping

Production Systems • Drools belongs to the category of rule engines called

production systems [1] (which execute actions based on conditions)

• Drools use forward chaining[2] (start with data and execute actions to infer more data )

• Priorities assigned to rules are used to decide the order of rule execution

• They remember all results and use that to optimize new derivations (dynamic programming like)

1. http://en.wikipedia.org/wiki/AI_production

2. http://en.wikipedia.org/wiki/Forward_chaining

Page 5: Droolsand Rule Based Systems 2008 Srping

Why rule engines? ~[1],[2][3]

• Simplify complicated requirements with declarative logic, raising the level of abstraction of the system

• Externalize the business logic (which are too dynamic) from comparatively static code base

• Intuitive and readable than code, easily understood by business people/ non technical users

• Create complex interactions which can have powerful results, even from simple facts and rules.

• Different approach to the problem, some problem are much easier using rules.

• Ability to specify explicit time and dates for rules to take effect

1. Real-World Rule Engines http://www.infoq.com/articles/Rule-Engines 2. Why are business rules better than traditional code?

http://www.edmblog.com/weblog/2005/11/why_are_busines.html 3. Rules-based Programming with JBoss Rules/Drools www.codeodor.com

Page 6: Droolsand Rule Based Systems 2008 Srping

When not to use rule engines?

• It is slower then usual code most of the time, so unless one of the following is true is should not be used– Complexity of logic is hard to tackle– Logic changes too often– Required to use by non technical users

• Interactions between rules could be quite complex, and one mistake could change the results drastically and unexpected way e.g recursive rules

• Due to above testing and debugging is required, so if results are hard to verified it should not be used.

Page 7: Droolsand Rule Based Systems 2008 Srping

Drools

• Facts as a Object repository of java objects• New objects can be added, removed or updated• support if <query> then <action> type rules• Queries use OOP format • Support not, or, and, forall and exists completing

first order logic

Page 8: Droolsand Rule Based Systems 2008 Srping

PatternsHave a OOP based intuitive rule format. We presents examples using a insurance quota example.

• Following rule reject all customers whose age less than 17. rule "MinimumAge"when    c : Customer(age < 17)then    c.reject();end

Conditions support <, >, ==, <=, >=, matches / not matches, contains / not contains. And following rules provide a discount if customer is married or older than 25.

rule "Discount"when    c : Customer( married == true || age > 25)then    c.addDiscount(10);end

Page 9: Droolsand Rule Based Systems 2008 Srping

OR, AND, eval()• OR – true if either of the statements true

– E.g. Customer(age > 50) or Vehicle( year > 2000)

• AND – provide logical, if no connectivity is define between two statements, “and” is assumed by default. For an example.

c : Customer( timeSinceJoin > 2); not (Accident(customerid == c.name))

andc : Customer( timeSinceJoin > 2) and     not (Accident(customerid == c.name))

are the same.

• eval(boolean expressions) – with eval(..) any Boolean expression can be used. – E.g. C:Customer(age > 20)

eval(C.calacuatePremium() > 1000)

Page 10: Droolsand Rule Based Systems 2008 Srping

Not

• Not – negation or none can be found. E.g.

not Plan( type = “home”)

is true if no plan of type home is found. Following is true if customer has take part in no accidents.

rule "NoAccident"when    c : Customer( timeSinceJoin > 2);     not (Accident(customerid == c.name))then    c.addDiscount(10);end

Page 11: Droolsand Rule Based Systems 2008 Srping

For all

• True if all objects selected by first part of the query satisfies rest of the conditions. For an example following rule give 25 discount to customers who has brought every type of plans offered.

rule "OtherPlans"when    forall ($plan : PlanCategory() c : Customer(plans contains $plan))then    c.addDiscount(25);end

Page 12: Droolsand Rule Based Systems 2008 Srping

Exists• True if at least one matches the query, • This is Different for just having Customer(), which

is like for each which get invoked for each matching set.

• Following rule give a discount for each family where two members having plans

rule “FamilyMembers"when $c : Customer()    exists (Customer( name contains $c.family))then    c.addDiscount(5);end

Page 13: Droolsand Rule Based Systems 2008 Srping

Conflict resolution• Each rule may define attributes There are other

parameters you can found from [1]. E.g. rule "MinimumAge"salience = 10

when    c : Customer(age < 17)then    c.reject();end

• salience define priority of the rule and decide their activation order.

1. http://labs.jboss.com/drools/documentation.html

Page 14: Droolsand Rule Based Systems 2008 Srping

Drools Performance• Measuring Rule engine performance is tricky. • Main factors are number of objects and number of rules. But

results depends on nature of rules. • A user feedback [1] claims Drools about 4 times faster than

JRules [4].

• [2] shows a comparison between Drools, Jess [5] and Microsoft

rule engine. Overall they are comparable in performance. 1. http://blog.athico.com/2007/08/drools-vs-jrules-performance-and-future.html

2. http://geekswithblogs.net/cyoung/articles/54022.aspx

3. Jess - http://herzberg.ca.sandia.gov/jess/

4. JRules http://www.ilog.com/products/jrules/

(Sequential\Rete)

rules Objects Drools JRules

1219 100 4ms/4ms 16ms/15ms

Page 15: Droolsand Rule Based Systems 2008 Srping

Drools Performance Contd.• I have ran the well known rule engine bench mark [1]

implementation provided with Drools. (On linbox3 - 1GB memory, 4 CPU 3.20GHz )

1. http://www.cs.utexas.edu/ftp/pub/ops5-benchmark-suite/HOW.TO.USE

Bench Marks Rule Count Object

Count

Time (ms)

Waltz 31 958 1582

31 3873 9030

Waltz DB 34 393 420

34 697 956

34 1001 1661

34 1305 2642

Page 16: Droolsand Rule Based Systems 2008 Srping

Data Mining Use Case

Page 17: Droolsand Rule Based Systems 2008 Srping

Rule based Solution

• We represent Queries as Objects that include bounds and list of selected data products

• We represent Data products as Objects that include location and time it was collected.

• Then following two rules will solve the problem– Rule 1. For each data item, if it match spatial and

temporal boundaries, add it to data collected for query– Rule 2. When temporal end time is passed, invoke the

data mining workflow with collected data

Page 18: Droolsand Rule Based Systems 2008 Srping
Page 19: Droolsand Rule Based Systems 2008 Srping

Concrete Rules• RULE 1. For each data item, if it match spatial and temporal boundaries

of a Query, add it to data collected for query

when      q: Query(completed = false);      d: Data( x > q.minX && x < q.maxX

&& y > q.minY && y < q.maxY && timeStamp > q.start && timestamp < q.end)

then      q.addDataProduct(d); end

RULE 2. When temporal end time is passed, invoke the data mining workflow with collected data

when      system:System()      q: Query(completed = false, end < system.currentTime); then      q.completed = true;      q.runDataMiningAndInvokeWorkflow(); end

Page 20: Droolsand Rule Based Systems 2008 Srping

Conclusion

• Drools provide a OOP based intuitive rule language based on Rete (which is state of art public algorithm)

• It has good performance, comparable with Jess (which I not free).

• It is Open source, has a healthy and active community and JBoss cooperation backing it

• Extensively used in business rule community