Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
                                Drools5 Community TrainingHands On Playing with Rules      Sponsored by Plugtree
Hands On 1: Drools Expert   Playing with Rules   Drools5 Community Training      version: 1.0-SNAPSHOT     Release Date: 0...
Hands On 1: Drools Expert   Playing with Rules Drools5 Community Training Course  by Mauricio "Salaboy" Salatino and  Este...
Overview● Drools Expert Examples   ○ Introduction examples   ○ Conditional elements examples   ○ Rules Attributes examples
Introduction Examples● Project: drools5/01-DroolsExpert-Introduction● Test: SimpleRulesExampleTest.java● Rules: src/main/r...
Introduction Examples● Exercise   ○ Find a way to get "Warn when we have a POP songs and     Playlist" Rule activated but ...
Conditional Elements examples● Project: drools5/02-DroolsExpert-ConditionalElements● Simple Rules Example:   ○ Test Class:...
Conditional Elements Examples● Exercise 1   ○ Replace "Add Playlists that contains songs with letter a     in their titles...
Conditional Elements examplesSolution:rule "Playlists that contains songs with letter a in their titles."when        $play...
Conditional Elements examples● Exercise 2   ○ Create a custom accumulate function to collect random     songs from a playl...
Conditional Elements Examples● eval Example:    ○ Test Class: EvalRulesExampleTest.java    ○ Rules: /rules/EvalRules.drl  ...
Conditional Elements Examples● Exercise   ○ Replace "Warn when we have a Playlist longer than 9000     seconds" with an eq...
Conditional Elements ExamplesSolutionrule "Playlist longer than 9000 seconds without eval"when$playlist: Playlist($songs: ...
Rule Attributes examples● Project: drools/03-DroolsExpert-RulesAttributes● Test Class: RuleAttributesExampleTest.java● Att...
               Questions?
Enjoy! Questions and Feedback arealways appreciated!
                          Contact us atwww.plugtree.com
Upcoming SlideShare
Loading in …5
×

Drools5 Community Training HandsOn 1 Drools DRL Syntax

6,180 views

Published on

for more information visit:
http://salaboy.wordpress.com/2011/02/23/drools-5-community-training-announced-roadmap/

  • Be the first to comment

Drools5 Community Training HandsOn 1 Drools DRL Syntax

  1. 1.    Drools5 Community TrainingHands On Playing with Rules Sponsored by Plugtree
  2. 2. Hands On 1: Drools Expert Playing with Rules Drools5 Community Training version: 1.0-SNAPSHOT Release Date: 09/05/2011Under The Creative Common License
  3. 3. Hands On 1: Drools Expert Playing with Rules 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/.
  4. 4. Overview● Drools Expert Examples ○ Introduction examples ○ Conditional elements examples ○ Rules Attributes examples
  5. 5. Introduction Examples● Project: drools5/01-DroolsExpert-Introduction● Test: SimpleRulesExampleTest.java● Rules: src/main/resources/rules/rules.drl● Kbuilder, Kbase and Ksession (createKSession() method) ○ Take a look at the addEventListener() method● DRL syntax in the rule file: ○ Take a look at the LHS ○ Take a look at the RHS● Rules activation and execution (you can activate the logger)
  6. 6. Introduction Examples● Exercise ○ Find a way to get "Warn when we have a POP songs and Playlist" Rule activated but not fired. ○ Find a way to get "Warn when we have a POP songs and Playlist" Rule activated and fired.
  7. 7. Conditional Elements examples● Project: drools5/02-DroolsExpert-ConditionalElements● Simple Rules Example: ○ Test Class: SimpleRulesExampleTest.java ○ Rules: SimpleRules.drl ○ Conditional Elements on LHS ○ from Conditional Element● Advanced from Examples: ○ Test Class: AdvancedFromRulesExampleTest.java ○ Rules: AdvancedFromRules.drl ○ collect, accumulate, accumulate custom function, Using Hibernate session in from CE
  8. 8. Conditional Elements Examples● Exercise 1 ○ Replace "Add Playlists that contains songs with letter a in their titles to the list using the accumulate conditional elements defining init, action, reverse and result functions. i.e: from accumulate( $s: Song() from $songs, init(), action(), reverse(), result() )
  9. 9. Conditional Elements examplesSolution:rule "Playlists that contains songs with letter a in their titles."when $playlist: Playlist($songs: songs) $match: java.util.List(empty == false) from accumulate( $s: Song() from $songs, init( java.util.List result = new java.util.ArrayList();), action( if ($s.getTitle().contains("a")) result.add($s); ), result( result ) )then System.out.println("Playlist found: "+$playlist); for (int i=0; i<$match.size(); i++){ System.out.println("t--"+$match.get(i)); }end
  10. 10. Conditional Elements examples● Exercise 2 ○ Create a custom accumulate function to collect random songs from a playlist. ○ Copy the SongsWithALetterOnTheirTitlesFunction.java implementation
  11. 11. Conditional Elements Examples● eval Example: ○ Test Class: EvalRulesExampleTest.java ○ Rules: /rules/EvalRules.drl ○ use of eval in LHS● exists Example: ○ Test Class: ExistsRulesExampleTest.java ○ Rules: ExistsRules.drl ○ use of exists and not in the LHS
  12. 12. Conditional Elements Examples● Exercise ○ Replace "Warn when we have a Playlist longer than 9000 seconds" with an equivalent Rule without using eval
  13. 13. Conditional Elements ExamplesSolutionrule "Playlist longer than 9000 seconds without eval"when$playlist: Playlist($songs: songs) AND $n : Number(intValue > 9000) from accumulate( $s : Song() from $songs, sum($s.getDuration()))thenSystem.out.println("We have found a Playlist longer than 9000 seconds");end
  14. 14. Rule Attributes examples● Project: drools/03-DroolsExpert-RulesAttributes● Test Class: RuleAttributesExampleTest.java● Attributes: ○ salience ○ no-loop: ■ Test: noLoop() Rules: /rules/NoLoopRules.drl ○ lock-on-active: ■ Test: lockOnActive() Rules: /rules/LockOnActiveRules.drl ○ agenda-group: ■ Test: agendaGroup() Rules: /rules/AgendaGroupRules.drl
  15. 15.    Questions?
  16. 16. Enjoy! Questions and Feedback arealways appreciated!
  17. 17.     Contact us atwww.plugtree.com

×