Nicozan-Blog

Knowledge Seeking

Drools Fusion – Sliding Windows November 9, 2010

Filed under: Drools — nicozan @ 11:52 am

Drools Fusion offers the Sliding Window operation, It gives us the posibilty to catch events of interest as the ones belonging to a window that is constantly moving. Drools Fusion has two Sliding Window implementations:

º Time Based Sliding Window: allows the user to write rules that will only match events occurring in the last X time units. The following code snippet shows the syntax of the operator:


rule “Sales that occurred in the last 10 minutes”
when
Sale() over window:time( 10m )
then
System.out.println(“A Sale occurred in the last 10 minutes”);
end

In the rule example above , the Sale() event will match IF AND ONLY IF it happens in the last 10 minutes.This 10 minutes are considered since the last event that is inserted into the working memory. So the Time Window is MOVED everytime we insert a new event.

º Length Based Sliding Window: works the same way as Time Based Window, but discard events based on the arrival of new events instead of flow of time.The following code snippet shows the syntax of the operator:


rule “Sales that had the article CoffeeCup from the last 10 Sales”
when
Sale(article == “CoffeCup”) over window:length( 10 )
then
System.out.println(“CoffeeCup Sold!”);
end

In the rule example above, the Sale() event will match IF AND ONLY IF the sale is one of the last 10 inserted sales into the working memory and its attribute article equal “CoffeCup”.

Some Considerations:
1. The Sliding Window is only available when engine is running in STREAM MODE.
2. Don`t forget to declare your EVENTS in the declaration part of your DRL File.

Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s