WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
Simple Drools Examples
1. Simple Drools Examples
A couple of simple examples for the JUG Milano meeting.
Matteo Mortari
http://linkedin.com/in/matteomortari
http://github.com/tarilabs
@tari_manga
2. Drools: effective use-cases
● Business logic changes often
● Rule definition as common language between
Developers, Analysts and Stakeholders
● Framework to support Data Analysis
● Data “cleaning”: filtering, augmentation, ...
● Inference: assert new data, FSM, …
● Time series: Complex Event Processing (CEP)
… and many others!
13. Filtering rules
rule "Filter01"
no-loop
salience 1000
when
$e : ZnetRxIoSampleResponse(
addressAsMacFormat(remoteAddress64) != "00:13:A2:00:40:68:E0:95"
)
then
retract($e);
end
rule "Filter02"
no-loop
salience 1000
when
$e : ZNetRxIoSampleResponse(
addressAsMacFormat(remoteAddress64) == "00:13:A2:00:40:68:E0:95" ,
containsAnalog == false
)
then
retract($e);
end
14. Inference rules
rule "Detect Docked"
no-loop
when
accumulate ( ZNetRxIoSampleResponse(
containsAnalog == true, $analog1 : analog1
) over window:length( 3 );
$avg : average( $analog1 ),
$count : count( $analog1 );
$avg > 950 , $count == 3
)
not ( DockedEvt() )
then
DockedEvt de = new DockedEvt();
de.setTs(drools.getWorkingMemory().getSessionClock().getCurrentTime());
insert(de);
end
15. CEP rules
rule "Toothbrush Session"
no-loop
when
$ude : UnDockedEvt()
$de : DockedEvt( this after $ude )
then
long millis = $de.getTs() - $ude.getTs() - 1000;
long mins = millis/1000/60;
long secs = (millis/1000) % 60;
long oscillations = (long) ( (7600.0/60/1000) * millis );
String sentence = "I just used my toothbrush! Total time: "
+( (mins>0)?mins+"m":"" )
+secs+"s "
+"Oscillations: "+oscillations;
LOG.debug("{}", sentence);
onCamel("direct:sentence", sentence);
retract($de);
retract($ude);
end