[id='cep-sliding-windows-proc_{context}']

= Declaring sliding windows for rule data

You can declare a sliding window of time (flow of time) or length (number of occurrences) for events so that the {RULE_ENGINE} uses data from only that window to evaluate the rules.

.Procedure
In the DRL rule file, specify `over window:<time_or_length>(<value>)` for the inserted fact.

For example, the following two DRL rules activate a fire alarm based on an average temperature. However, the first rule uses a sliding time window to calculate the average over the last 10 minutes while the second rule uses a sliding length window to calculate the average over the last one hundred temperature readings.

.Average temperature over sliding time window
[source]
----
rule "Sound the alarm if temperature rises above threshold"
when
  TemperatureThreshold($max : max)
  Number(doubleValue > $max) from accumulate(
    SensorReading($temp : temperature) over window:time(10m),
    average($temp))
then
  // Sound the alarm.
end
----

.Average temperature over sliding length window
[source]
----
rule "Sound the alarm if 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
----

The {RULE_ENGINE} discards any `SensorReading` events that are more than 10 minutes old or that are not part of the last one hundred readings, and continues recalculating the average as the minutes or readings "slide" forward in real time.

The {RULE_ENGINE} does not automatically remove outdated events from the KIE session because other rules without sliding window declarations might depend on those events. The {RULE_ENGINE} stores events in the KIE session until the events expire either by explicit rule declarations or by implicit reasoning within the {RULE_ENGINE} based on inferred data in the KIE base.
