A Tech Log

November 13, 2008

Processing Collections Using .Net Rules Engine

Filed under: Development — adallow @ 11:18 pm
Tags: ,

Using the rules engines’ chaining behaviour it’s possible to loop through a collection of items and execute a rule for each item in the collection.

E.g. I created a spam filter that uses the rules engine to check for keywords in an email. Instead of creating a separate rule for each keyword you can just store all the keywords in a collection and use the chaining behaviour of the rules engine to loop through the collection executing a rule for each keyword.

To do this i followed a pattern taken from MSDN: Processing Collections In Rules

Rule 1 (Priority = 2) //Always execute this rule once to create the enumerator.
IF 1==1
THEN this.enumerator = this.myCollection.GetEnumerator()
Rule 2 (Priority = 1)
IF this.enumerator.MoveNext()
THEN this.currentInstance = this.enumerator.Current
Rules 3-N (Priority = 0)
…. //Additional rules written against this.currentInstance
Rule N+1 (Priority = -1)
// can be any condition as long as it is evaluated every time;
// this.currentInstance will be evaluated each time this.currentInstance changes, whereas
// “1==1” would only be evaluated once.
IF this.currentInstance == this.currentInstance


Update(“this/enumerator”) //this will cause Rule 2 to be reevaluated

In my spam filter example I had 4 rules which did the following:

  1. Get the enumerator for my collection (List<string>) of spam words
  2. Move the enumerator to the next spam word, and set current spam word variable.
  3. Check if the email contains the current spam word
  4. Update enumerator, so that rule 2 will be re-evaluated (due to chaining behaviour of the rules engine).

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: