Вы находитесь на странице: 1из 34

Implement business logic with the Drools rules engine

Use a declarative programming approach to write your program's business logic

Jhih-Jhong Hong 2006/8/16

References
z

R. Olivier, Implement business logic with the Drools rules engine, IBM developerWorks Website, http://www-128.ibm.com/ developerworks/java/library/j-drools/ , May 2006 M. Proctor, M. Neale, P. Lin, M. Frandsen, JBoss Rules User Guide, JBoss Rules Documentation Website, http://labs.jboss.com/portal/jbossrules/docs/i ndex.html , July 2006

Outline
1. 2. 3. 4. 5.

Introduction Overview of Drools Rule Specification Scenario Conclusion

Outline
1. 2. 3. 4. 5.

Introduction Overview of Drools Rule Specification Scenario Conclusion

Introduction

Business logic is the solution of enterprise software for financial planning and managed accounts It is hard to maintain or update an applications business logic with the dynamic changes of market economics

Rule Engine

Rules Engines are the pluggable software components that separate the business rules from the application code
Inference Engine

Production Memory (Rules)

Pattern Matcher Agenda

Working Memory (Facts)

Advantages of using Rules Engine

There are three advantages of using rule engine to implement business logic

Declarative programming Logic and data separation Centralization knowledge

Drools is a rule engine which has advantages above

Outline
1. 2. 3. 4. 5.

Introduction Overview of Drools Rule Specification Scenario Conclusion

Overview of Drools

Drools 3.0 is an open source rule engine written in the java language

Understandable rules Tool integration (For Eclipse 3.2 or greater) Fast execution speed JSR 94-compliant (Java rule engine API standard) Free

Process Flow

The process flow of Drools can be split into tow main parts :

Authoring Runtime

Authoring
Parser drl xml

Runtime
Working Memory
Working Memory Event Support Agenda

(describe rules) Intermediate AST

Truth Maintenance Agenda Event Support System 1..n 1 package 1..n 1 Rule base

Package Builder
Compiler Code Generator

Outline
1. 2. 3. 4. 5.

Introduction Overview of Drools Rule Specification Scenario Conclusion

Rule Specification (Part 1)


package com.sample Package Name import com.sample.ObjectModel.*; Import Statement rule NTUT Discount no-loop true salience 20 when c:Customer(store==NTUT) then c.setDiscount(0.1); modify(c); end Rule Name Attribute Condition Consequence

Rule Specification (Part 2)

rule

name attribute

Left Hand Side (LHS): a common name for the conditional part of the rule Right Hand Side (RHS): a common name for the consequence or action part of the rule

When LHS then RHS end

Rule Attributes

no-loop salience agenda-group auto-focus activation-group duration

Left Hand Side (LHS)

Customer(store==NTUT)

True if there is a object which type is Customer and its field constraint store is equal to NTUT c and d are instances True if there is a object which type is Goods and its field constraints name is equal to Cola and paid is false

c:Customer(d:Discount)

Good(name==Cola, paid==false)

Field Constraint Specification

Follow JavaBeans convention

Classes used with field constrain rely on methods of getters and setters such as getObject(), isObject(), setObjectetc If primitive type is used for a field, Drools will autobox them to corresponding object type, and no auto-unbox even using Java 5

Autobox primitive type for field

Right Hand Side (RHS)

Java code which can use working memory operations as following :


assert assertLogical modify retract

Outline
1. 2. 3. 4. 5.

Introduction Overview of Drools Rule Specification Scenario Conclusion

Scenario

FamilyMart have three business rules as following :


The price of the second cola drink is $10 2. The customer will receive a Pokemon magnet when total cost is more than $75 3. If convenience store is located in NTUT, then goods will be discounted by 10%
1.

Suppose there is a rich man who will buy all goods in FamilyMart, and we want to know if the rules are executed correctly

Step by Step
1. 2. 3. 4.

5.

Create a new rule project Create a class for object model Modify Sample.drl for normal rule Create a new rule resource (Sample2.drl) for special rules Modify DroolsTest.java

Outline
1. 2. 3. 4. 5.

Introduction Overview of Drools Rule Specification Scenario Conclusion

Conclusion

Drools can reduce complexity of components that implement the business rules logic in Java application, and it is easy to maintain or extend the business logic by declarative programming

Q&A?

Conclusion

Drools can reduce complexity of components that implement the business rules logic in Java application, and it is easy to maintain or extend the business logic by declarative programming

Q&A?

Conclusion

Drools can reduce complexity of components that implement the business rules logic in Java application, and it is easy to maintain or extend the business logic by declarative programming

Q&A?

The Rete View

The Rete view


Generate the Rete Tree visualization Help developer to understand the structure of rules in drl file

Rete Tree
ReteNode ObjectTypeNode AlphaNode LeftInputAdapterNode JoinNode TerminalNode

Goods

Customer

paid==false

Sample.drl

The Rete Algorithm (Part 1)


x? y? x? y? z? "Pattern network"

"Join network" p

x & y => p x & y & z => q

8 nodes q

The Rete Algorithm (Part 2)


Optimization 1: Sharing in pattern network x? y? z?

x & y => p x & y & z => q

6 nodes q

The Rete Algorithm (Part 3)


Optimization 2: Sharing in join network x? y? z?

x & y => p x & y & z => q

5 nodes q

Query
query Cola" Rule g:Goods(name==Cola) end LHS

QueryResults results Source code that uses the rule =workingMemory.getQueryResults(Cola"); for (Iterator it = results.iterator; it.hasNext(); ) { QueryResult result = (QueryResult) it.next(); Goods g = (Goods) result.get(g"); }

Additional Feature (Part 1)

Domain Specific Language (DSL)

It is a way of extending the rule language to your problem domain Support spreadsheet format such as elx and csv

Decision tables

Additional Feature (Part 2)

XML

Using for tools that generate rules from some input or from another rule language It defines a simple API to access a rule engine, but doesnt standardize the semantics of rule execution

JSR 94 API

Additional Information

Part 1 The Rete View Part 2 Query Part 3 Additional Feature

Вам также может понравиться