[rules-users] Why this rule fires immediately ?

eskomk esko.hujanen at ebsolut.fi
Tue Oct 25 06:08:46 EDT 2011


Hi W et al.,

I came up with a following solution.
One reason why I came up with this kind of solution is that it is not firing
every time when there comes a temperature reading (ZZZBean) in the system
(working memory).

Would you comment if this solution is viable and if there are any pitfalls ?
It seems to work all right, though.

Rules involved:
TemperatureOver30_settracker
Update_tracker_count
TemperatureOver30_setalarm
TemperatureOver40_setalarm
TemprOver30_launch
TemprOver40_launch
Kill_tracker

The tracker is set when get a temperature reading > 30
(TemperatureOver30_settracker).
Every subsequent reading is added to tracker's internal list and retracted
from working memory (update_tracker_count).
Alarm30 is set if average reading from tracker's list is in range > 30 and <
40. Also tracker is retracted (TemperatureOver30_setalarm).
Alarm40 is set if average reading from tracker's list is over 40. Also
tracker is retracted (TemperatureOver40_setalarm).
Intelligent action is launched if Alarm30 is detected, alarm is rectracted
(TemprOver30_launch).
Another kind of intelligent action is launched if alarm40 is detected, alarm
is retracted (TemprOver40_launch).
Tracker is retracted if there are over a number of readings (here 10) in
tracker's internal list (Kill_tracker), which infers that the average
temperature is < 30.

Sensor readings and tracker are declared as events, alarms are facts.
The saliences are not final, I would like to get comments on that too.

rule "TemperatureOver30_settracker" dialect "mvel"
no-loop true
salience 150

when
    $tsb : ZZZBean(temperature > 30 && < 40)
    not TOver30Tracker(profileID == $tsb.profileID)
then
    TOver30Tracker $tracker = new TOver30Tracker();
    $tracker.profileID = $tsb.profileID;
    $tracker.level = 1;

    retract($tsb);

    $tracker.sensorBean = $tsb;
    $tracker.addSensorBean($tsb);

    insert($tracker);
end

rule "Update_tracker_count" dialect "mvel"
no-loop true
salience 100

when
    $tracker : TOver30Tracker()
    $tsb : ZZZBean(profileID == $tracker.profileID, this after $tracker)
then
    drools.retract($tsb);

    $tracker.addSensorBean($tsb);

    $tracker.count = $tracker.count + 1;
    drools.update($tracker);
end

rule "TemperatureOver30_setalarm" dialect "mvel"
no-loop true
salience 50

when
    $tracker : TOver30Tracker(count == 5, $prof : profileID)
    $avg : Number(floatValue > 30 && < 40) from accumulate(
        ZZZBean( $tempr : temperature, profileID == $prof) over
window:length( 5 ) from $tracker.beanList,
        average( $tempr ) )
then
    retract($tracker);

    TOver30Alarm $alarm = new TOver30Alarm();
    $alarm.profileID = $prof;
    $alarm.sensorBean = $tracker.sensorBean;
    $alarm.sensorBean.temperature = $avg;

    insert($alarm);
end

rule "TemperatureOver40_setalarm" dialect "mvel"
no-loop true
salience 50

when
    $tracker : TOver30Tracker(count == 5, $prof : profileID)
    $avg : Number(floatValue > 40) from accumulate(
        ZZZBean( $tempr : temperature, profileID == $prof) over
window:length( 5 ) from $tracker.beanList,
        average( $tempr ) )
then
    retract($tracker);

    TOver40Alarm $alarm = new TOver40Alarm();
    $alarm.profileID = $prof;
    $alarm.sensorBean = $tracker.sensorBean;

    insert($alarm);
end

rule "TemprOver30_launch" dialect "mvel"
no-loop true
when
    $alarm : TOver30Alarm($prof : profileID)
then
    retract($alarm);
    
    // Intelligent action goes here
end

rule "TemprOver40_launch" dialect "mvel"
no-loop true
when
    $alarm : TOver40Alarm($prof : profileID)
then
    retract($alarm);
    
    // Intelligent action goes here
end

rule "Kill_tracker" dialect "mvel"
no-loop true
salience 999

when
    $tracker : TOver30Tracker(count > 10)
then
    retract($tracker);
end


Best Regards,
Esko
-----
Esko Hujanen
www.ebsolut.fi

--
View this message in context: http://drools.46999.n3.nabble.com/Why-this-rule-fires-immediately-tp3430427p3451005.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list