1

I recently started programming with Drools Fusion and I have a smart wearable that sends pedometer and heart rate data to my laptop. I then process this data with using the drools rule language. But suppose I have multiple smart wearables with each an unique MAC address. I use time windows and my question is how can I change my rule file so that the rules only fire for events with the same macaddress and take appropiate action based on this MAC address. My current rule file is the following:

import hellodrools.Steps
import hellodrools.HeartRate
import hellodrools.AppInfo

declare AppInfo
    @role(event)
end

declare Steps
    @role(event)
end

declare HeartRate
    @role(event)    
end


rule "ACC STEPS RULE"
when
    accumulate( Steps( $s : steps )
                over window:time( 1h ) from entry-point "entrySteps"; 
        $fst: min( $s ), $lst: max( $s );
        $lst - $fst < 50 )
then
    System.out.println("STEPS RULE: get moving!");
    System.out.println($lst + "   " + $fst);

end

rule "HEARTRATE RULE 1"
when
    $heartrate : HeartRate(heartRate >= 150) from entry-point "entryHeartRate"
then
    System.out.println("Heartrate is to high!");
end

rule "HEARTRATE RULE 2"
when
    $heartrate : HeartRate(heartRate <= 50 && heartRate >= 35) from entry-            point "entryHeartRate"
then
    System.out.println("Heartrate is to low!");
end

rule "HEARTRATE RULE 3"
when
    $heartrate : HeartRate(heartRate < 35 && heartRate >= 25) from entry-point "entryHeartRate"
then
    System.out.println("Heartrate is critical low!");
end

rule "HEARTRATE RULE 4"
when
    $max : Double() from accumulate(
        HeartRate( $heartrates : heartRate ) over window:time( 10s ) from entry-point "entryHeartRate",
        max( $heartrates ) )&&
    $min : Double() from accumulate(
        HeartRate( $heartrates : heartRate ) over window:time( 10s ) from entry-point "entryHeartRate",
        min( $heartrates ) )&&
    eval( ($max - $min) >= 50 )
then
    System.out.println("Heartrate to much difference in to little time!");
end

My HeartRate events have the following fields:

int heartRate;
Date timeStamp;
String macAddress;

My Steps events have the following fields:

double steps;
Date timeStamp;
String macAddress;
Tim
  • 445
  • 5
  • 19

1 Answers1

1

This is simple: you need to define a fact, call it Walker with String macAddress, create it with the MAC address the rules should handle, and then

rule "ACC STEPS RULE"
when
  Walker( $mac: macAddress )
  accumulate( Steps( $s : steps, macAddress == $mac )
              over window:time( 1h ) from entry-point "entrySteps"; 
      $fst: min( $s ), $lst: max( $s );
      $lst - $fst < 50 )
  then ... end

and similarly with the other rules. - You may simplify this (a little) by defining a basic rule

rule "MAC"
when
  Walker( $mac: macAddress )
then end

and write the other rules as extensions:

rule "ACC STEPS RULE" extends "MAC" ...

so you don't need to repeat the Walker pattern for each rule.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thank you for answering. Like I said, I just started with drools and I don't understand it complety. I created a new class Walker with a field macAddress. Then declared it as an fact in my rule file : `declare Walker @role(fact) end` and updated my rules. I then did the following: `Walker walker = new Walker(macAddress); entryPointSteps.insert(walker);` But nothing happened. Could you explain it a bit more. Thank you. – Tim Mar 13 '16 at 11:42
  • 1
    If you insert using an `entryPointSteps` you'll have to use `from entrySteps' in the rules. Did you? – laune Mar 13 '16 at 12:51
  • I have one last question. Now if I insert a new stepevent with a certain macAddress, the rule will be fired for all macAddress in the system. But I would like that it only fires for the macAddress of the inserted event. How can I change this? – Tim Mar 14 '16 at 20:20
  • Then some constraint (like the one in the Steps pattern) comparing the macAddress of some Walker to the one in the Steps event is missing. – laune Mar 14 '16 at 21:11
  • Could you elaborate this more? – Tim Mar 14 '16 at 21:25
  • 1
    Perhaps *you* elaborate your situation some more. More blind guessing isn't likely to help. - Ask another question, stating the rule(s) you have and the facts you insert. – laune Mar 15 '16 at 07:16
  • I've made a new question. http://stackoverflow.com/questions/36008719/drools-rule-fire-only-for-inserted-event – Tim Mar 15 '16 at 10:34