Drools Allan Huang @ Delta DRC
Why Rules Engine? Rules are independent and each rule doesn’t
know if another rule even exists. A complex rule can be divided into simpler
rules that infer parts of information of a complex scenario and feed it to the working memory, and other rules might depend on that data to get activated.
Drools Rule Engine A rule engine is a piece of software, which having
some knowledge is able to perform conclusions. Knowledge and inferences are stored in rules,
which are called production rules. Production rules consist of conditions and
actions, which are executed when their conditions are true.
A working memory, which stores all objects we may want to use while making a decision.
Then conflict resolution is provided by the agenda. It arranges the order of actions, which has been selected to be run.
Hybrid Reasoning System Forward Chaining Engine – Data-driven
Facts are asserted into working memory, which results in one or more rules being concurrently true and scheduled for execution by the Agenda.
Backward Chaining Engine – Goal-driven We start with a conclusion which the engine tries
to satisfy. If it can't, then it searches for conclusions that it can satisfy.
It continues this process until either the initial conclusion is proven or there are no more sub goals.
Prolog is an example of a Backward Chaining engine.
Drools Concept Analogy
Drools Terms SQL Terms
Session Entry Point (Fusion -
CEP) When Event (time
== 1) Then
Default Database Database Select, From, Where Trigger
DRL Terms Comment
//dialect "mvel“ /*dialect "mvel"*/
Package package com.delta.rca.rules;
Import import com.delta.rca.context.StaffPosition;
Global global org.slf4j.Logger logger;
Dialect dialect "java" or dialect "mvel"
DRL Attributes no-loop
Setting no-loop to true will skip the creation of another Activation for the rule with the current set of facts.
salience Salience is a form of priority where rules with higher salience values
are given higher priority when ordered in the Activation queue. Drools also supports dynamic salience.
date-effective A rule can only activate after the date and time of the date-effective
attribute. date-expires
A rule cannot activate if the current date and time is after the date-expires attribute.
duration The duration dictates that the rule will fire after a specified duration
if it is still true.
When (LHS) Syntax Pattern
It can potentially match on each fact that is inserted in the working memory.
Pattern1( ) Pattern2( ) = Pattern1( ) and Pattern2( ) Object( )
matches all objects in the working memory
Pattern Binding Person( $firstAge : age ) The prefixed dollar symbol ($) is a convention.
Property Access Person( age > 30 && < 40 ) Person( name== "mark", address.( city == "london", country
== "uk") ) Person( $streetName : address!.street ) Cheese( type matches "(Buffalo)?\\S*Mozarella" )
Advanced Conditional Elements Timers and Calendars
Rules now support both interval and cron based timers, which replace the now deprecated duration attribute.
forall Evaluates to true when all facts that match the first pattern match all the
remaining patterns. from
Enables users to specify an arbitrary source for data to be matched by LHS patterns.
collect Allows rules to reason over a collection of objects obtained from the given
source or from the working memory. accumulate
accumulate is a more flexible and powerful form of collect. built-in accumulate functions
average, min, max, count, sum, collectList, collectSet
eval allows any semantic code (that returns a primitive boolean) to be executed.
Then (RHS) Syntax update(fact, facthandle)
Informs the engine that an event or fact has changed and rules need to be reconsidered.
update(object) Is similar to above update method, the engine will look up the facthandle for
you, via an identity check. modify(facthandle) {propertyX=1, propertyY=2…}
Is similar to above update method, not only update object itself, also its properties be updated.
insert(new Something()) Inserts a new object into the Working Memory.
insertLogical(new Something()) Is similar to above insert method, but the object will be automatically retracted
when there are no more facts to support the truth of the currently firing rule. delete(facthandle)
Removes an object from the Working Memory. retract(facthandle)
Is similar to above delete method, but it deprecated!
Complex Event Processing Event Processing
It is a method of tracking and analyzing (processing) streams of information (data) about things that happen (events),[1] and deriving a conclusion from them.
Complex Event Processing (CEP) Combines data from multiple sources to infer
events or patterns that suggest more complicated circumstances.
Its goal is to identifying the Meaningful Events and respond to them as quickly as possible.
Event Stream Processing Event Stream Processing (ESP)
Deals with the task of processing streams of event data with the goal of identifying the meaningful pattern within those streams
Employs techniques such as detection of relationships between multiple events, event correlation, event hierarchies, and other aspects such as causality, membership and timing.
Semantic Events = Meaningful Events.
Drools Fusion It is the module responsible for adding Event
Processing Capabilities into the Drools platform for CEP / ESP. Support Events, with their proper semantics. Allow detection, correlation, aggregation and composition
of events. Support Temporal Constraints in order to model the
temporal relationships between events. Support Sliding Windows of interesting events.
Sliding Time Windows Sliding Length Windows
Support a session scoped unified clock. Real Time clock Pseudo clock
…
Temporal Reasoning $eventA : EventA( this after[ 3m30s, 4m ] $eventB )
3m30s <= $eventA.start - $eventB.end <= 4m $eventA : EventA( this during[ 5s, 10s ] $eventB )
5s <= $eventA.start - $eventB.start <= 10s && 5s <= $eventB.end - $eventA.end <= 10s
$eventA : EventA( this meets[ 5s ] $eventB ) abs( $eventB.start - $eventA.end) <= 5s
$eventA : EventA( this overlaps[ 5s, 10s ] $eventB ) $eventA.start < $eventB.start < $eventA.end < $eventB.end && 5s
<= $eventA.end - $eventB.start <= 10s $eventA : EventA( this starts[ 5s ] $eventB )
abs( $eventA.start - $eventB.start ) <= 5s && $eventA.end < $eventB.end
Other Temporal Operators Before, Coincides, Finishes, Finished By, Includes, Met By,
Overlapped, Started By.
Java Notes Model Classes
Add serialVersionUID Override the equals, hashCode, toString methods
Use EqualsBuilder, HashCodeBuilder, ToStringBuilder classes in Apache Commons Lang to override above methods
Service Singleton or Static method AOP, e.g. Spring-based application
Reference Why Rules? Drools ~ Basic concepts Introduction to Drools: Rules fall from your eyes (PROCESSES%20&%20RULES)%20OR%20(RULES
%20&%20PROCESSES)%203/X Complex event processing Event stream processing Drools Documentation
Chapter 5. Hybrid Reasoning Chapter 6: User Guide Chapter 7: Rule Language Reference Chapter 8. Complex Event Processing Chapter 20: Examples