Top Banner
Drools Allan Huang @ Delta DRC
23

Drools

Aug 16, 2015

Download

Software

Allan Huang
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Drools

Drools

Allan Huang @ Delta DRC

Page 2: Drools

Rule Engine

Page 3: Drools

Rule

Page 4: Drools

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.

Page 5: Drools

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.

Page 6: Drools

Rule Engine

Page 7: Drools

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.

Page 8: Drools

Forward Chaining

Page 9: Drools

Backward Chaining

Page 10: Drools

Drools Rule Language

Drools Rule Language SQL Clause

Page 11: Drools

Drools Concept Analogy

Drools Terms SQL Terms

Session Entry Point (Fusion -

CEP) When Event (time

== 1) Then

Default Database Database Select, From, Where Trigger

Page 12: Drools

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"

Page 13: Drools

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.

Page 14: Drools

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" )

Page 15: Drools

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.

Page 16: Drools

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!

Page 17: Drools

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.

Page 18: Drools

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.

Page 19: Drools

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

Page 20: Drools

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.

Page 21: Drools

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

Page 23: Drools

Q&A