+ All Categories
Home > Education > Introduction to Drools

Introduction to Drools

Date post: 19-May-2015
Category:
Upload: giurca
View: 6,552 times
Download: 10 times
Share this document with a friend
Popular Tags:
22
Adrian Giurca, eBusiness Technologies, Craiova, March 2009 eBusiness Technologies (ebTech) Introduction to Drools Dr. Adrian Giurca Brandenburg University of Technology Cottbus, Germany
Transcript
Page 1: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

eBusiness Technologies (ebTech)

Introduction to Drools

Dr. Adrian GiurcaBrandenburg University of Technology

Cottbus, Germany

Page 2: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Repetition

• Embedding rules in large applications is a difficult task• Many controversies and open questions when we want

to use libraries to connect AI Languages to Java Applications

• Hard difficulties to share data between libraries and the main application (Prolog facts versus Java classes)

• Logic can be difficult to be employed in rule modeling• Fortunately we have Drools an open source Java-based

rule engine• In addition, Drools employs a Java-based syntax for rules

Page 3: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Developing Rule-based applications

• Vocabulary constraints:o The rule-based part of the application is based on the

existent domain vocabulary of your applicationo Therefore you have to design rules considering this

vocabulary• Is a standalone rule-based application?

o Then is just a server-side componento You can add a basic JSP layer or a Java client

• Is an Enterprise application (JEE5)?o Then the rule engine should be integrated at the

business layer• Is a Web Service application?

o Then the rule engine should be controlled by the WS EJB

Page 4: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Drools Vocabulary is basically POJO

package org.btu.it.userv.vocabulary;

class YoungDriver extends Driver {

// properties

//default constructorpublic YoungDriver(){}//setters and getters for the properties}

• POJO – Plain Old Java Object (search on Google)

Page 5: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Rules are grouped in rulesets

• Rulesets in Drools are captured by means of packages.• A package is a placeholder for rules, imports, globals,

functions:o Imports make vocabulary classes accessible to ruleso Globals are global variables. They are used to make

application objects available to the rules. They must be understand as constants in the reasoning process.

o Functions are static Java functions usually designed to be available in rules action part.

Page 6: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Sample ruleset (package)

package org.btu.it.userv.driverPremium

#list any import classes here.import org.btu.it.userv.vocabulary.YoungDriver;import java.io.IOException;// ...

#declare any global variables hereglobal java.io.BufferedWriter out;global org.btu.it.userv.utils.Output writer;

// Rules

#write any global function herefunction void writeLog(String text, BufferedWriter out)throws IOException{// function code comes here}# or imports such asimport function org.btu.it.userv.utils.Logger.writeLog

Ruleset name

Vocabulary

Global variables

Global functions

Page 7: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Drools Rule structure

• A rule specifies that when a particular set of conditions occur - specified in the Left Hand Side (LHS), then do this - specified as a list of actions in the Right Hand Side (RHS).

• A rule must have a unique name, in the scope of the rule package.

• Attributes are optional.

• The rule LHS follows the when keyword. The RHS follows the then keyword (ideally on a newline). The rule is terminated by the end keyword.

• Rules cannot be nested.

Page 8: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Rules condition part (LHS)

• Rule conditions part is a logical formula built by using conjunction (,), disjunction (||), and negation (not) on conditional elements

• There are two main conditional elements i.e. pattern and eval

• The pattern condition is the most important one. Basic and advanced pattern conditions are provided

• Essentially eval is a Boolean expression evaluator. This can refer to variables that were bound in the rule LHS, and functions in the rule package.

Page 9: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Actions (the RHS)

• The action part part should contain a list of actions to be executed.

• Any valid Java code is allowed. However, is bad practice to use imperative or conditional code in the RHS of a rule; as a rule should be atomic in nature - "when this, then do this", not "when this, maybe do this".

• The RHS part of a rule should also be kept small, thus keeping it declarative and readable.

• The main purpose of the RHS is to insert, retract or modify facts from the working memory.

Page 10: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Standard Actions in RHS

• update(object, handle); will tell the engine that an object has changed and rules may need to be reconsidered.

• insert(new Something()); will place a new object of your creation in working memory.

• insertLogical(new Something()); similar to insert, but the object will be automatically retracted when there are no more facts to support the truth of the currently firing rule.

• retract(handle); removes an object from working memory.

Page 11: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Slide No. 11

• This is Slide No. 11 (half time) and we don't know yet how to use rules in a large application?!

To do:– Download Drools– Use it. Make running an example from their library

Page 12: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

The basic pattern

• patternBinding is a variable bound to a Java object instance of patternType bean.

• patternBinding is bound sequentially to each instance of the class patternType for which the constraints holds.

For example:

$driver:Driver(age < 25)

The $driver variable is bound sequentially to all Driver objects from the working memory for which the age value is less than 25, something like:

X driver(X) && age(X) < 25

Page 13: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Constraints (1)

• Are used in a pattern definition• Logically they are conditions referring to different

properties of the pattern class type• Can be combined in complex constraints by using logical

connectors

For example:$driver:Driver(age < 25)

age < 25 is a (field) constraint

Page 14: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Constraints (2)

• Constraints can be more complex:

• constraintGroup corresponds to a conjunction of constraints• fieldConstraint represents constraints on the class properties.

There are three types of restrictions: Single Value Restriction, Compound Value Restriction and Multi Restriction.

• inlineEvalConstraint can use any valid dialect expression as long as it is evaluated to a primitive Boolean

Page 15: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Field constraints (1)

• A field corresponds to a getter for a property on the bean object. If your model objects follow the java bean pattern, then fields are exposed using getXXX() or isXXX().

• You can access fields by using the bean-name convention (so getType() can be accessed as type)

• For example,

YoungDriver(maritalStatus == MaritalStatus.MARRIED)uses the getMaritalStatus() method on the YoungDriver instance.

Page 16: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Field constraints (2)

• You can restrict a field by using a large number of constructs based on various operators.

• fieldBinding is a variable bound to the value of the fieldName property i.e.

YoungDriver($age:age < 25) where $age is the fieldBinding, age is the fieldName and <25 is the restriction

Page 17: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

The eval pattern and inlineEvalConstraint

• eval and inlineEvalConstraint have similar model.• Evaluates a specific expression to a Boolean• Over use of eval reduces the declaratives of your rules and

can result in a poor performing engine. • While 'evals' can be used anywhere the best practice is to

add it as the last conditional element in the LHS of a rule.

For example:eval(isApprovedByManager($client, $vip))supposingisApprovedByManager() be a Boolean function.

Page 18: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Advanced patterns - from

• allows users to specify a source for patterns to reason over. • This allows the engine to reason over data not in the Working

Memory. • This allows integration with other application components and

frameworks. One common example is the integration with data retrieved on-demand from databases using hibernate named queries.

For example:$item : OrderItem( value > 100 ) from $order.items $item is bounded sequentially to all OrderItem objects with value greater than 100 from the list $order.items

Page 19: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

More complex from: using collect

• allows rules to reason over collection of objects collected from the given source or from the working memory.

$drivers : ArrayList() from collect( Driver( gender == 'F', noOfAccidents > 0 ) from $town.getDrivers() )

Page 20: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

More complex from: using accumulate

• Is a form of collect• Allows a rule to iterate over a collection of objects, executing

custom actions for each of the elements, and at the end return a result object.

Page 21: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Accumulate example

$total : Number( doubleValue > 100 ) from accumulate( OrderItem( order == $order, $value : value ), init( double total = 0; ), action( total += $value; ), reverse( total -= $value; ), result( total ) )

Page 22: Introduction to Drools

Adrian Giurca, eBusiness Technologies, Craiova, March 2009

Do you like rule-based applications?

• Attend the next lecture o Tomorrow at 4PM in the same room


Recommended