A Tech Log

November 18, 2008

Drools.NET is a Business Rules Engine (BRE)

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

Drools.NET is a Business Rules Engine (BRE) based on Charles Forgy’s Rete algorithm. Developers can now exploit a powerful Rule Engine through a completely managed .NET code base. Drools.NET is based on Jboss Rules, and comes with all the features of that Rules Engine.

Drools can be used in place of the WWF rules engine if required.

see http://droolsdotnet.codehaus.org/

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).

Windows Workflow Foundation Rules Engine

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

With .Net 3.0 or later there is the ‘Windows Workflow Foundation’ which includes the Rules Engine.

Using the rules engine you can create an external set of rules and execute them against any .Net object.

from http://blogs.microsoft.co.il/blogs/bursteg/archive/2006/10/11/RuleExecutionWithoutWorkflow.aspx

Create a RuleSet

The RuleSet Editor Dialog is exposed as part of the API, so you can use it to create RuleSets as part of your program:

// Create a RuleSet that waorks with Orders (just another .net Object)

RuleSetDialog ruleSetDialog = new RuleSetDialog(typeof(Order), null, null);

// Show the RuleSet Editor


// Get the RuleSet after editing

RuleSet ruleSet = ruleSetDialog.RuleSet;

Serialize and Deserialize rules

Windows Workflow Foundation uses the WorkflowMarkupSerializer to Serialize and Deserialize rules.

// Serialize to a .rules file

WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

XmlWriter rulesWriter = XmlWriter.Create(fileName);

serializer.Serialize(rulesWriter, ruleSet);


// Deserialize from a .rules file.

XmlTextReader rulesReader = new XmlTextReader(fileName);

WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

ruleSet = (RuleSet)serializer.Deserialize(rulesReader);


Execute RuleSet againt an object

Execute your RuleSet after editing, serializing and deserialising it, but first validate that the rules can be executed against the input object.

// Execute the rules and print the entity’s properties

Order myOrder = new Order(…);

RuleValidation validation = new RuleValidation(typeof(Order), null);

RuleExecution execution = new RuleExecution(validation, myOrder);


An Introduction to WWF and rules engine is available here:


includes useful information about chainging:

Each rule in a RuleSet has a priority value with a default of 0. The rules in a RuleSet can be considered a sorted collection, ordered by the priority values. The WF Rules evaluator evaluates rules individually and executes the rule’s actions based on the results of the rule’s condition evaluation

A selection of examples are available from


Blog at WordPress.com.