Introducing: by Mario Fusco Red Hat - Senior Software Engineer Twitter: @mariofusco
Introducing: by Mario Fusco Red Hat - Senior Software Engineer Twitter: @mariofusco
by Mario Fusco Red Hat Senior Software Engineer [email protected] twitter: @mariofusco
Drools Vision
Drools was born as a rule engine, but following the vision of becoming a single platform for business modelling, it realized it could achieve this goal only by leveraging 3 complementary business modelling techniques: Business Rule Management (Drools Expert) Business Process Management (Drools Flow) Complex Event Processing (Drools Fusion)
Drools Expert
Drools Flow
Drools Fusion
Declarative
Imperative
Rule's anatomy
Quotes on Rule names are optional if the rule name has no spaces.
rule <name> <attribute> <value> when Pattern-matching salience <int> against objects in the agenda-group <string> <LHS> Working Memory no-loop <boolean> auto-focus <boolean> then duration <long> .... <RHS> end
Code executed when a match is found
Imperative vs Declarative
A method must be called directly Specific passing of arguments
rule Hello Mark when Person( name == mark ) then System.out.println( Hello Mark ); end
What is a pattern
Rule's definition
// Java public class Applicant { private String name; private int age; private boolean valid; // getter and setter here } // DRL declare Applicant name : String age : int valid : boolean end
rule "Is of valid age" when $a : Applicant( age >= 18 ) then modify( $a ) { valid = true }; end
Building
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource("Rules.drl"), ResourceType.DRL); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (kbuilder.hasErrors()) { System.err.println(kbuilder.getErrors().toString()); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
Executing
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession(); Applicant applicant = new Applicant( "Mr John Smith", 21 ); assertFalse( applicant.isValid() ); ksession.execute( applicant ); assertTrue( applicant.isValid() );
Conditional Elements
not Bus( color = red ) exists Bus( color = red ) forall ( $bus : Bus( color == red ) ) $owner : Person( name == mark ) Pet( name == rover ) from $owner.pets $zipCode : ZipCode()
Hibernate session
.list()
rule "Issue Child Bus Pass" when $p : Person( ) IsChild( person =$p ) then logicalInsert(new ChildBusPass( $p ) ); end
Complex Event
An abstraction of other events called its members
Drools Fusion
Drools modules for Complex Event Processing Understand and handle events as a first class platform citizen (actually special type of Fact) Select a set of interesting events in a cloud or stream of events Detect the relevant relationship (patterns) among these events Take appropriate actions based on the patterns detected
Drools Guvnor
Centralised repository for Drools Knowledge Bases Web based GUIs ACL for rules and other artifacts Version management Integrated testing
Drools Planner
Works on all kinds of planning problems
NP-complete Hard & soft constraints Huge search space
Planning constraints can be weighted and are written as declarative score rules The planner algorithm is configurable. It searches through the solutions within a given amount of time and returns the best solution found
Q
Mario Fusco Red Hat Senior Software Engineer
A
[email protected] twitter: @mariofusco