Drools5 Community Training Module 3 Drools Expert DRL Syntax
-
Upload
salaboy-salaboy -
Category
Technology
-
view
5.906 -
download
2
description
Transcript of Drools5 Community Training Module 3 Drools Expert DRL Syntax
![Page 1: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/1.jpg)
Drools5 Community Training Sponsored by Plugtree
![Page 2: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/2.jpg)
Module 3: Drools Expert DRL Syntax
Drools5 Community Trainingversion: 1.0-SNAPSHOT
Release Date: 03/16/2011Under The Creative Common License
![Page 3: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/3.jpg)
Module 3: Drools Expert DRL Syntax
Drools5 Community Training Course by Mauricio "Salaboy" Salatino and Esteban Aliverti is licensed under a Creative Commons Attribution 3.0
Unported License.Based on a work at salaboy.wordpress.
com.Permissions beyond the scope of this
license may be available at http://salaboy.wordpress.com/.
![Page 4: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/4.jpg)
Agenda
● Introduction to Drools Expert● Drools DRL syntax introduction
○ LHS Conditional Elements ○ RHS○ Rules attributes○ Queries
● Drools 5.x APIs introduction
![Page 5: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/5.jpg)
Drools Expert Introduction
● Drools Expert is the rule engine core ● Lets us express Business Rules● It will be in charge of making inferences to get new
conclusions
![Page 6: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/6.jpg)
Components interaction
![Page 7: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/7.jpg)
Rules Execution Cycle
![Page 8: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/8.jpg)
Business Rule Structure
package ...import ...global ... rule "My Rule" <Attributes> when <LHS> Song(genre == "Jazz") <CEs> then <RHS> System.out.println("Hi John!");end
![Page 9: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/9.jpg)
LHS - Conditional Elements
● Pattern
● e.g. Song(genre == "Jazz") $song: Song(genre == "Jazz" || == "Avantgarde") $song: Song(duration > 360, $genre: genre == "Jazz")
![Page 10: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/10.jpg)
LHS - Conditional Elements
● Field Operators
● e.g.
Song( lyrics matches "AZ-az[drink]" ) Song( authors contains "John Zorn" ) Song( author memberof $greatMusicians )
![Page 11: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/11.jpg)
LHS - Conditional Elements
● AND
e.g. Person( name == "John" ) AND Pet(type == "cat")● OR
e.g. Policewoman (age > 30) OR Fireman(age > 31)
![Page 12: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/12.jpg)
LHS - Conditional Elements
● eval
e.g. eval( song.isJazz() )● not
e.g. not( Song( genre == "Pop") )
![Page 13: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/13.jpg)
LHS - Conditional Elements
● exists
e.g. exists ( Song(genre == "Jazz"))● forall
e.g. forall ( Song() Song(genre == "Jazz") )
![Page 14: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/14.jpg)
LHS - Conditional Elements
from CE
● e.g.
$playlist: Playlist() Song() from $playlist.songs $playlist: Playlist() Song(genre == "Jazz") from $playlist.songs
![Page 15: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/15.jpg)
LHS - Conditional Elements
● from
● e.g. global HibernateSession hbn;
$playlist: Playlist() Song() from hbn.namedQuery("SongByArtist") .setParameter("artist","John Zorn") .getResultList();
![Page 16: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/16.jpg)
LHS - Conditional Elements
● Collect
● e.g. $songs: ArrayList() from collect (Song(genre == "Jazz", year > 2000))
![Page 17: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/17.jpg)
LHS - Conditional Elements
● Accumulate
![Page 18: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/18.jpg)
LHS - Conditional Elements
● Accumulate CE: <result pattern> from accumulate( <source pattern>, init( <init code> ), action( <action code> ), reverse( <reverse code> ), result( <result expression>))
● e.g. $playlist: Playlist() $jazzSongs: Number( doubleValue > 100 ) from accumulate( Song(playlist == $playlist, genre == "Jazz"), init( double total = 0; ), action( total += 1; ), reverse( total -= 1; ), result( total ))
![Page 19: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/19.jpg)
LHS - Conditional Elements
● Accumulate Function Examples $playlist: Playlist() $total: Number( doubleValue > 100 ) from accumulate( $song: Song( playlist == $playlist, genre == "Jazz"), count($song))
![Page 20: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/20.jpg)
LHS - Conditional Elements
● Accumulate out-of-the-box Functions ○ average○ min○ max○ count○ sum
![Page 21: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/21.jpg)
LHS - Conditional Elements
● Accumulate custom function $playlist: Playlist() $total: Number( doubleValue > 100 ) from accumulate( $song: Song( playlist == $playlist), myCustomFc($song.duration))
● We can plug our custom function here. We just need to implement the AccumulateFunction interface.
![Page 22: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/22.jpg)
Nesting CEs
● Drools support nested CEs $petshop: PetShop() $total: Number( doubleValue > 10 ) from accumulate( $pet: Pet(petshop == $petshop, type == PetType.Cat) from $hbm.getNamedQuery("getPetsFromPetshopId") .setParamenter("petshopid",$petshop.getId()) .list(), count ($pet) )
![Page 23: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/23.jpg)
Right Hand Side
● Set of actions● MVEL or Java (http://mvel.codehaus.org/)● We will have a set of methods to modify the working
memory status○ insert()○ modify() / update ()○ retract()
rule "Fire ! ! !" when Fire() then insert(new Alarm()); end
![Page 24: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/24.jpg)
Rule Attributes
![Page 25: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/25.jpg)
Rule Attributes - no loop
● no-loop i.e. rule "infinite activation loop" no-loop true when $person: Person(age > 21) then update($person){ setName("John"); } end
![Page 26: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/26.jpg)
Rule Attributes - salience
● salience (higher priority first)rule "rule with priority" salience 10 when $song: Song() then System.out.println("Rule with higher priority Fired!");endrule "rule with less priority" salience 5 when $song: Song() then System.out.println("Rule with lower priority Fired!");end
![Page 27: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/27.jpg)
Rule Attributes - agenda-group
rule "Playlist ready" agenda-group "Create Playlist" when Playlist(size == 3) then //Set the focus to "Play playlist" agenda-group kcontext.getKnowledgeRuntime().getAgenda() .getAgendaGroup("Play playlist").setFocus(); end
rule "Play playlist" agenda-group "Play playlist" when $pl : Playlist() then $pl.play(); end
![Page 28: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/28.jpg)
Rule Attributes - lock-on-active
rule "Number Songs" salience 2 lock-on-active agenda-group "Create Playlist" when $song : Song() then modify($song){ setName( index.getAndIncrement()+ "_"+ $song.getName()); }end
rule "Fill playlist" salience 1 lock-on-active agenda-group "Create Playlist" when $pl : Playlist() $song : Song() then modify($pl){ addSong($song); }end
![Page 29: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/29.jpg)
Queries
Example
query "Get Persons by Name" (String name) Person(name = :name) end
![Page 30: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/30.jpg)
Drools Expert APIs
![Page 31: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/31.jpg)
KnowledgeBuilder
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); // Add our rules kbuilder.add(new ClassPathResource("rules.drl"), ResourceType.DRL); //Check for errors during the compilation of the rules KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { System.err.println(error); } throw new IllegalArgumentException("Could not parse knowledge."); }
![Page 32: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/32.jpg)
KnowledgeBase
// Create the Knowledge Base KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); // Add the binary packages (compiled rules) to the Knowledge Base kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
![Page 33: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/33.jpg)
KnowledgeSession
// Create a StatefulSession using the KnowledgeBase that // contains the compiled knowledge StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); // We can add a runtime logger to understand what is going on // inside the Engine KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession); // Insert a new Fact inside my world FactHandle myFactHandle = ksession.insert(new Person("Myself")); // Update a Fact using the FactHandle ksession.update(myFactHandle, new Person("Salaboy!")); // Retract/Remove from my world the Fact ksession.retract(myFactHandle);
![Page 34: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/34.jpg)
Full Picture
![Page 35: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/35.jpg)
Hands on Labs
Projects: ● 01 :: Drools Expert Introduction ● 02 :: Drools Expert Conditional Elements● 03 :: Drools Expert Rule Attributes
![Page 36: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/36.jpg)
Briefing Up
● Covered Topics:○ Rules Execution Lifecycle○ DRL Syntax
■ Conditional Elements in the LHS■ Actions in the RHS
![Page 37: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/37.jpg)
Questions?
![Page 38: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/38.jpg)
Enjoy! Questions and Feedback are always appreciated!Stay Tuned!
![Page 39: Drools5 Community Training Module 3 Drools Expert DRL Syntax](https://reader033.fdocuments.us/reader033/viewer/2022061206/54834183b07959330c8b49f3/html5/thumbnails/39.jpg)
Contact us atwww.plugtree.com