Drools - Artezio

advertisement
www.artezio.com
The art of technology
Drools
and
Drools 5 Fusion
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531
Fax: +7 (495) 232-2683 Email:
info@artezio.com
Drools is an open source rules engine,
which lets you express business logic rules in a
declarative way using non-XML native
language
2
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Agenda
• Drools Basics
-
Advantages of Drools
DRL file
Executing rules
Conflicts resolution
Updating knowledge in rules engine
• Drools Fusion (CEP)
- What is Complex Event Processing
- Drools Fusion vision
- Features and examples
3
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Advantages of Drools
• Separates application from conditions controlling the flow
- Rules can be modified by different groups
- No need to recompile or redeploy
- All rules are in one place
• Readable native non-XML language
- Easy to learn
- Eclipse plug-in helps to visualize rules
• Native BRMS implementation
• Complex event processing engine
• Rete algorithm
4
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
DRL file
rule "Extract and classify followers“ dialect=“java”
when
twitter : Twitter()
then
for (User user : twitter.getFollowers()) {
Follower follower = new Follower(); follower.setUser(user);
follower.setFollows(twitter);
follower.setClassification(TwitterUserType.getType(TwitterUtils.getTwitterI
nfluenceRatio(user)));
follower.setHasPicture(TwitterUtils.hasSetProfileImage(user));
insert(follower);
}
end
rule "User has no picture“ dialect=“java”
when
follower : Follower(hasPicture == false)
then
follower.setRanking(follower.getRanking() - 30.0);
end
5
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Executing rules
• First, we need to create rules knowledge base
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
• It parses and compiles DRL files
knowledgeBuilder.add(drlFileAsResource, ResourceType.DRL);
• It extracts named packages of rules for KnowledgeBaseFactory
Collection pkgs = knowledgeBuilder.getKnowledgePackages();
knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(pkgs);
6
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Executing rules
• KnowledgeSession provides the way of exposing objects to
be ruled
StatefulKnowledgeSession knowledgeSession =
knowledgeBase.newStatefulKnowledgeSession();
Twitter twitter = new Twitter(twitterUser, twitterPassword);
knowledgeSession.insert(twitter);
• Fire rules
knowledgeSession.fireAllRules();
7
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
www.artezio.com
The art of technology
Questions…(to be continued)
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531
Fax: +7 (495) 232-2683 Email:
info@artezio.com
Conflicts resolution
• Often we need non-trivial flow of rules
- Validate data in knowledge base
- Prepare base for future rules flow
- Cover rules from execution
It is solved by using salience parameter…
9
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Conflicts resolution (example)
• DRL file first block
package demo;
import demo.Machine;
import demo.Test;
import demo.TestDAO;
import java.util.Calendar;
import java.sql.Timestamp;
global TestDAO testDAO;
• Defining functions in DRL file
function void setTestsDueTime(Machine machine, int numberOfDays) { setDueTime(machine,
Calendar.DATE, numberOfDays);
}
function void setDueTime(Machine machine, int field, int amount) {
Calendar calendar = Calendar.getInstance();
…
machine.setTestsDueTime(new Timestamp(calendar.getTimeInMillis()));
}
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
10
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Conflicts resolution (example)
rule "Tests for type2, DNS server machine"
salience 100
when
machine : Machine( type == "Type2", functions contains "DNS Server")
then
Test test5 = testDAO.findByKey(Test.TEST5);
machine.getTests().add(test5);
insert( test5 );
end
rule "Due date for Test 5"
salience 50
when
machine : Machine()
Test( id == Test.TEST5 )
then
setTestsDueTime(machine, 14);
end
11
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Conflicts resolution
• Rules with higher salience will be fired first
- preparing objects for future rules execution
- filtering some instances from ruling
• Dynamic salience
rule "high value fires first“
salience (person.bonus * item.price)
when
person : Person()
item : Item()
then
...
end
12
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Updating knowledge in rules engine
• Knowledge base can be updated inside rule’s body
- insert()
–
Inserted object will be used by rules engines inside current
session
- update()
–
Updates existing in working memory object for the rest of rules
- delete()
–
Removed object will not be ruled on current execution
13
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Updating knowledge in rules engine
rule "Tests for type2, DNS server machine"
salience 100
when
machine : Machine( type == "Type2", functions contains "DNS Server")
then
Test test5 = testDAO.findByKey(Test.TEST5);
machine.getTests().add(test5);
update( machine );
end
rule "Due date for Test 5"
salience 50
when
machine : Machine( tests contains (testDAO.findByKey(Test.TEST5)) )
then
setTestsDueTime(machine, 14);
end
14
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Once again…
• Separates application from conditions controlling
the flow
• Significantly reduce the complexity of components
that implement the business-rules logic
• Rules engine expresses rules using a declarative
approach that is maintainable and extensible
• Easy to learn by Java developers
15
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
www.artezio.com
The art of technology
Questions… (to be continued)
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531
Fax: +7 (495) 232-2683 Email:
info@artezio.com
www.artezio.com
The art of technology
Drools 5 Fusion
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531
Fax: +7 (495) 232-2683 Email:
info@artezio.com
Agenda
• What is Complex Event Processing
• Drools Fusion scenario
• Features
-
Events
Session Clock
Streams support
Temporal reasoning
Event processing modes
Sliding windows
KnowledgeBase partitioning
Memory management
18
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
What is Complex Event Processing
Complex Event Processing is an event processing
concept for identifying the meaningful events
within an event cloud
19
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
What is Complex Event Processing
• CEP employs techniques like …
- detection of complex patterns of many events
- event correlation and abstraction
- event hierarchies
- relationships between events (causality, membership,
timing)
- event-driven processes
20
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Drools Fusion scenario
• Understand and handle events as first class
citizens of the platform
• Select a set of interesting events in a cloud or
stream of events
• Detect the relevant relationships (patterns) among
these events
• Take appropriate actions based on the patterns
detected
21
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features
•
•
•
•
•
•
•
•
Events
Session Clock
Streams support
Temporal reasoning
Event processing modes
Sliding windows
KnowledgeBase partinioning
Memory management
22
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Events
• Events, from a Drools perspective, are just a
special type of fact
-
usually immutable
strong temporal constraints
managed lifecycle
use of sliding windows
23
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Events
• Event declaration
public class VoiceCall {
private String originNumber;
private String destinationNumber;
private Date callDateTime;
private long callDuration;
// constructors, getters and setters
}
• DRL file
declare VoiceCall
@role( event )
@timestamp( callDateTime )
@duration( callDuration )
@expires( 1h35m )
end
Artezio LLC
24
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Session Clock
• Session clock provide availability of concept “now”
• Implements strategy pattern
- Rules testing
- Regular execution
- Special environments
–
–
clock synchronization
appServer provided clocks
- Rules replay or simulation
• Two default implementations are provided
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
25
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Session Clock
• Real time clock
KnowledgeSessionConfiguration config =
KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
config.setOption( ClockTypeOption.get("realtime") );
• Pseudo clock
KnowledgeSessionConfiguration conf =
KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption( ClockTypeOption.get( "pseudo" ) );
StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession( conf, null );
SessionPseudoClock clock = session.getSessionClock();
FactHandle handle1 = session.insert( tick1 );
clock.advanceTime( 10, TimeUnit.SECONDS );
FactHandle handle2 = session.insert( tick2 );
clock.advanceTime( 30, TimeUnit.SECONDS );
FactHandle handle3 = session.insert( tick3 );
26
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Streams support
• Different forms of streams
-
JMS queues
database tables
raw sockets
Web Service calls
flat text files
etc.
• Events are orders by timestamp
• Meanings are usually extracted from correlation of
events and even streams
• Homogeneous and heterogeneous streams
27
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Streams support
StatefulKnowledgeSession session = ...
WorkingMemoryEntryPoint atmStream = session.getWorkingMemoryEntryPoint( "ATM
Stream" );
atmStream.insert( aWithdrawRequest );
rule "apply fee on withdraws on branches"
when
WithdrawRequest( $ai : accountId, processed == true )
from entry-point "Branch Stream"
CheckingAccount( accountId == $ai )
then
// apply a $2 fee on the account
end
28
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Temporal reasoning
$eventA : EventA( this after[ 3m30s, 4m ] $eventB )
3m30s <= $eventA.startTimestamp - $eventB.endTimeStamp <= 4m
$eventA : EventA( this before[ 3m30s, 4m ] $eventB )
$eventA : EventA( this coincides[15s, 10s] $eventB )
abs( $eventA.startTimestamp - $eventB.startTimestamp ) <= 15s &&
abs( $eventA.endTimestamp - $eventB.endTimestamp ) <= 10s
$eventA : EventA( this during[ 2s, 6s, 4s, 10s ] $eventB )
2s <= $eventA.startTimestamp - $eventB.startTimestamp <= 6s &&
4s <= $eventB.endTimestamp - $eventA.endTimestamp <= 10s
$eventA : EventA( this finishes[ 5s ] $eventB )
$eventB.startTimestamp < $eventA.startTimestamp &&
abs( $eventA.endTimestamp - $eventB.endTimestamp ) <= 5s
$eventA : EventA( this meets[ 5s ] $eventB )
abs( $eventB.startTimestamp - $eventA.endTimestamp) <= 5s
29
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Event Processing modes
• Cloud mode
- no notion of time
- no clock synchronization
- no events ordering
• Stream mode
-
time-ordered events
synchronization between stream by session clocks
automatic event lifecycle management
rules delaying
sliding window support
30
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Event Processing modes
• Enabling Stream mode
KnowledgeBaseConfiguration config =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption( EventProcessingOption.STREAM );
or
drools.eventProcessingMode = stream
• Rules delaying using negative pattern
rule "Sound the alarm"
when
$f : FireDetected( )
not( SprinklerActivated( this after[0s,10s] $f ) )
then
// sound the alarm
end
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
31
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Sliding windows
• Sliding time windows
• Sliding length windows
rule "Sound the alarm in case temperature rises above threshold"
when
TemperatureThreshold( $max : max )
Number( doubleValue > $max ) from accumulate(
SensorReading( $temp : temperature ) over window:length( 100 ),
average( $temp ) )
then
// sound the alarm
end
32
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: KnowledgeBase partitioning
•
•
•
•
Does your hardware contain multiple processors?
Does your knowledge session process a high volume of facts?
Are the LHS of your rules expensive to evaluate? (ex: use expensive
"from" expressions)
Does your knowledge base contain hundreds or more rules?
KnowledgeBaseConfiguration config =
knowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption( MultithreadEvaluationOption.YES );
config.setOption( MaxThreadsOption.get(5) );
or
drools.multithreadEvaluation = <true|false>
drools.maxThreads = <-1|1..n>
33
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Features: Memory management
• explicitly, using the expiration policy
declare StockTick
@expires( 30m )
end
• implicitly, analyzing the temporal constraints on events
rule "correlate orders"
when
$bo : BuyOrderEvent( $id : id )
$ae : AckEvent( id == $id, this after[0,10s] $bo )
then
// do something
end
34
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
Finally…
• Session clocks provide an important concept of
“now”
• Events are usually immutable and have strong
relationships
• A list of operators allows to detect correlation of
events
- after
- before
- includes
• Sliding windows allow to accumulate data during
period of time
35
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531 Fax: +7 (495) 232-2683 Email: info@artezio.com
www.artezio.com
The art of technology
Questions…
Artezio LLC
Address: 3G Gubkina Str., suite 504, Moscow, Russia, 119333
Phone: +7 (495) 981-0531
Fax: +7 (495) 232-2683 Email:
info@artezio.com
Download