Drools5 Community Training: Module 1.5 - Drools Expert First Example

24
Drools5 Community Training Sponsored by Plugtree

description

for more information visit: http://salaboy.wordpress.com

Transcript of Drools5 Community Training: Module 1.5 - Drools Expert First Example

Page 1: Drools5 Community Training: Module 1.5 - Drools Expert First Example

  

Drools5 Community Training Sponsored by Plugtree

Page 2: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Module 1.5: Drools Expert First Example

Drools5 Community Trainingversion: 1.0-SNAPSHOT

Release Date: 03/16/2011Under The Creative Common License

Page 3: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Module 1.5: Drools Expert First Example

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 1.5 - Drools Expert First Example

Agenda

IntroductionBusiness Rule StructureDrools Expert Example ScenarioHands on lab

Page 5: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Drools Expert in 2 slides

The core of Drools Lets us express our Knowledge -> Business RulesLets us create Sessions -> Our World It will be in charge of making inferences between them and firing the correspondent actions.Let's see how it works!

Page 6: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Drools Expert in 2 slides

Page 7: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Business Rule Structure

rule "My Rule" when <LHS> Person(name == "John") <CEs> then <RHS> System.out.println("Hi John!"); <Actions>end

Page 8: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Example Scenario

Page 9: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Example Model

We can define that a Pet will have a name, type and a position.

For this example a Person will have a Pet which he/she can call. And this Person can also call the fire department. The Firefighter will know how to retrieve a Cat from a Tree.

Page 10: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Example Rules

Rule "Call Cat when it is in a tree" When my Cat is on a limb in a tree Then I will call my Cat

Rule "Call the fire department" When my Cat is on a limb and it doesn't come down when I call Then call the Fire Department Rule "Firefighter gets the cat down" When the Firefighter can reach the Cat Then the Firefighter retrieves the Cat

Page 11: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Technical View of  the Model

Page 12: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Technical View of  the Rules

rule "Call Cat when it is in a tree" when $p: Person($pet: pet, petCallCount == 0) $cat: Pet(this == $pet, position == "on a limb", type == PetType.CAT) then //$cat.getName() + " come down!" $p.setPetCallCount($p.getPetCallCount()+1); update($p); end

Rule "Call Cat when it is in a tree" When my Cat is on a limb in a tree Then I will call my Cat

Page 13: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Technical View of  the Rules

rule "Call the fire department" when $p: Person($pet: pet, petCallCount > 0) $cat: Pet(this == $pet, position == "on a limb", type == PetType.CAT) then Firefighter firefighter = new Firefighter("Fred"); insert(firefighter);end

Rule "Call the fire department" When my Cat is on a limb and it doesn't come down when I call Then call the Fire Department

Page 14: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Technical View of  the Rules

rule "Firefighter gets the cat down" when $f: Firefighter() $p: Person($pet: pet, petCallCount > 0) $cat: Pet(this == $pet, position == "on a limb", type == PetType.CAT) then $cat.setPosition("on the street"); retract($f); update($cat);end

Rule "Firefighter gets the cat down" When the Firefighter can reach the Cat Then the Firefighter retrieves the Cat

Page 15: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Active Scenario in Java

For the previous rules to fire in our Java world we need to have:An instance of a Person that contains a relation to its PetAn instance of a Pet that is on a limb

Person person = new Person("Salaboy!"); Pet pet = new Pet("mittens", "on a limb", Pet.PetType.CAT); person.setPet(pet);

Page 16: Drools5 Community Training: Module 1.5 - Drools Expert First Example

API's Sneak Preview

Page 17: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Hands On

Project Name: drools5/00-Drools5-FirstExampleSources: https://github.com/Salaboy/Drools_jBPM5-Training-ExamplesCharacteristics:

Java Project (jar) Uses Maven (pom.xml)Drools Dependencies (core, api, compiler)DRL File (src/test/resources/rules.drl)

To do:Run the test (src/test/java/org/plugtree/example/FirstExampleTest.java)Get familiar with the output

Page 18: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Hands On

Exercise 1:Create a new rule that inserts a Dog (on the street) when there is Cat on the street too.Create a new rule for the situation when a Cat and a Dog are on the street, the Dog should chase it up to the tree and then the Cat should climb the limb.

Page 19: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Hands On - Solution

rule "Cat on the street" when Pet(position == "on the street", type == PetType.CAT) then Pet dog = new Pet("doggy", "on the street" , PetType.DOG); insert (dog);end rule "Cat on the street and a dog is around" when $cat: Pet(position == "on the street", type == PetType.CAT) $dog: Pet(position == "on the street", type == PetType.DOG) then modify($cat){ setPosition("on the limb"); } modify($dog){ setPosition("under the tree"); } end

Page 20: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Hands On - What Happen?

Exercise 1:What happens with the execution? Why?Try to find a solution

Page 21: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Briefing Up

Covered Topics:Quick review about Drools ExpertProject DependenciesA simple example that shows the key things that we need to have to use Drools

Page 22: Drools5 Community Training: Module 1.5 - Drools Expert First Example

  

Questions?

Page 23: Drools5 Community Training: Module 1.5 - Drools Expert First Example

Enjoy! Questions and Feedback are always appreciated!Stay Tuned!

Page 24: Drools5 Community Training: Module 1.5 - Drools Expert First Example

  

Contact us atwww.plugtree.com