Introduction to Drools

7,461 views

Published on

Published in: Education, Technology
  • Good content, to help you whether you need drools or not, have added a high level overview of drools at http://technologistics.blogspot.com/2010/10/drools-overview.html
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Introduction to Drools

  1. 1. eBusiness Technologies (ebTech) Introduction to Drools Adrian Giurca, eBusiness Technologies, Craiova, March 2009 Dr. Adrian Giurca Brandenburg University of Technology Cottbus, Germany
  2. 2. Repetition <ul><ul><li>Embedding rules in large applications is a difficult task </li></ul></ul><ul><ul><li>Many controversies and open questions when we want to use libraries to connect AI Languages to Java Applications </li></ul></ul><ul><ul><li>Hard difficulties to share data between libraries and the main application ( Prolog facts versus Java classes ) </li></ul></ul><ul><ul><li>Logic can be difficult to be employed in rule modeling </li></ul></ul><ul><ul><li>Fortunately we have Drools an open source Java-based rule engine </li></ul></ul><ul><ul><li>In addition, Drools employs a Java-based syntax for rules </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  3. 3. Developing Rule-based applications <ul><ul><li>Vocabulary constraints: </li></ul></ul><ul><ul><ul><li>The rule-based part of the application is based on the existent domain vocabulary of your application </li></ul></ul></ul><ul><ul><ul><li>Therefore you have to design rules considering this vocabulary </li></ul></ul></ul><ul><ul><li>Is a standalone rule-based application? </li></ul></ul><ul><ul><ul><li>Then is just a server-side component </li></ul></ul></ul><ul><ul><ul><li>You can add a basic JSP layer or a Java client </li></ul></ul></ul><ul><ul><li>Is an Enterprise application (JEE5)? </li></ul></ul><ul><ul><ul><li>Then the rule engine should be integrated at the business layer </li></ul></ul></ul><ul><ul><li>Is a Web Service application? </li></ul></ul><ul><ul><ul><li>Then the rule engine should be controlled by the WS EJB </li></ul></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  4. 4. Drools Vocabulary is basically POJO package org.btu.it.userv.vocabulary; class YoungDriver extends Driver { // properties //default constructor public YoungDriver(){ } //setters and getters for the properties } Adrian Giurca, eBusiness Technologies, Craiova, March 2009 <ul><ul><li>POJO – Plain Old Java Object (search on Google) </li></ul></ul>
  5. 5. Rules are grouped in rulesets <ul><ul><li>Rulesets in Drools are captured by means of packages. </li></ul></ul><ul><ul><li>A package is a placeholder for rules , imports , globals , functions : </li></ul></ul><ul><ul><ul><li>Imports make vocabulary classes accessible to rules </li></ul></ul></ul><ul><ul><ul><li>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. </li></ul></ul></ul><ul><ul><ul><li>Functions are static Java functions usually designed to be available in rules action part. </li></ul></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  6. 6. 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 here global java.io.BufferedWriter out; global org.btu.it.userv.utils.Output writer; // Rules #write any global function here function void writeLog(String text, BufferedWriter out)throws IOException{ // function code comes here } # or imports such as import function org.btu.it.userv.utils.Logger.writeLog Adrian Giurca, eBusiness Technologies, Craiova, March 2009 Ruleset name Vocabulary Global variables Global functions
  7. 7. Drools Rule structure <ul><ul><li>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). </li></ul></ul><ul><ul><li>A rule must have a unique name , in the scope of the rule package. </li></ul></ul><ul><ul><li>Attributes are optional. </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>Rules cannot be nested. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  8. 8. Rules condition part (LHS) <ul><ul><li>Rule conditions part is a logical formula built by using conjunction ( , ), disjunction ( || ), and negation ( not ) on conditional elements </li></ul></ul><ul><ul><li>There are two main conditional elements i.e. pattern and eval </li></ul></ul><ul><ul><li>The pattern condition is the most important one. Basic and advanced pattern conditions are provided </li></ul></ul><ul><ul><li>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. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  9. 9. Actions (the RHS) <ul><ul><li>The action part part should contain a list of actions to be executed. </li></ul></ul><ul><ul><li>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 - &quot;when this, then do this&quot;, not &quot;when this, maybe do this&quot;. </li></ul></ul><ul><ul><li>The RHS part of a rule should also be kept small , thus keeping it declarative and readable . </li></ul></ul><ul><ul><li>The main purpose of the RHS is to insert, retract or modify facts from the working memory. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  10. 10. Standard Actions in RHS <ul><ul><li>update(object, handle); will tell the engine that an object has changed and rules may need to be reconsidered. </li></ul></ul><ul><ul><li>insert(new Something()); will place a new object of your creation in working memory. </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>retract(handle); removes an object from working memory. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  11. 11. Slide No. 11 <ul><ul><li>This is Slide No. 11 (half time) and we don't know yet how to use rules in a large application?! </li></ul></ul><ul><li>To do : </li></ul><ul><ul><li>Download Drools </li></ul></ul><ul><ul><li>Use it. Make running an </li></ul></ul><ul><li>example from their library </li></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  12. 12. The basic pattern <ul><ul><li>patternBinding is a variable bound to a Java object instance of patternType bean. </li></ul></ul><ul><ul><li>patternBinding is bound sequentially to each instance of the class patternType for which the constraints holds. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009 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
  13. 13. Constraints (1) <ul><ul><li>Are used in a pattern definition </li></ul></ul><ul><ul><li>Logically they are conditions referring to different properties of the pattern class type </li></ul></ul><ul><ul><li>Can be combined in complex constraints by using logical connectors </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009 For example: $driver:Driver(age < 25) age < 25 is a (field) constraint
  14. 14. Constraints (2) <ul><ul><li>Constraints can be more complex: </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009 <ul><ul><li>constraintGroup corresponds to a conjunction of constraints </li></ul></ul><ul><ul><li>fieldConstraint represents constraints on the class properties. There are three types of restrictions: Single Value Restriction, Compound Value Restriction and Multi Restriction. </li></ul></ul><ul><ul><li>inlineEvalConstraint can use any valid dialect expression as long as it is evaluated to a primitive Boolean </li></ul></ul>
  15. 15. Field constraints (1) <ul><ul><li>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() . </li></ul></ul><ul><ul><li>You can access fields by using the bean-name convention (so getType() can be accessed as type ) </li></ul></ul><ul><ul><li>For example, </li></ul></ul><ul><li>YoungDriver(maritalStatus == MaritalStatus.MARRIED) </li></ul><ul><li>uses the getMaritalStatus() method on the YoungDriver instance. </li></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  16. 16. Field constraints (2) <ul><ul><li>You can restrict a field by using a large number of constructs based on various operators. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009 <ul><ul><li>fieldBinding is a variable bound to the value of the fieldName property i.e. </li></ul></ul><ul><li>YoungDriver($age:age < 25) </li></ul><ul><li>where $age is the fieldBinding , age is the fieldName and <25 is the restriction </li></ul>
  17. 17. The eval pattern and inlineEvalConstraint <ul><ul><li>eval and inlineEvalConstraint have similar model. </li></ul></ul><ul><ul><li>Evaluates a specific expression to a Boolean </li></ul></ul><ul><ul><li>Over use of eval reduces the declaratives of your rules and can result in a poor performing engine. </li></ul></ul><ul><ul><li>While 'evals' can be used anywhere the best practice is to add it as the last conditional element in the LHS of a rule. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009 For example: eval(isApprovedByManager($client, $vip)) supposing isApprovedByManager() be a Boolean function.
  18. 18. Advanced patterns - from <ul><ul><li>allows users to specify a source for patterns to reason over. </li></ul></ul><ul><ul><li>This allows the engine to reason over data not in the Working Memory. </li></ul></ul><ul><ul><li>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. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009 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
  19. 19. More complex from : using collect <ul><ul><li>allows rules to reason over collection of objects collected from the given source or from the working memory. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009 $drivers : ArrayList() from collect( Driver( gender == 'F', noOfAccidents > 0 ) from $town.getDrivers() )
  20. 20. More complex from: using accumulate <ul><ul><li>Is a form of collect </li></ul></ul><ul><ul><li>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. </li></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  21. 21. 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 ) ) Adrian Giurca, eBusiness Technologies, Craiova, March 2009
  22. 22. Do you like rule-based applications? <ul><ul><li>Attend the next lecture </li></ul></ul><ul><ul><ul><li>Tomorrow at 4PM in the same room </li></ul></ul></ul>Adrian Giurca, eBusiness Technologies, Craiova, March 2009

×