A Tech Log

November 13, 2008

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

ruleSetDialog.ShowDialog();

// 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);

rulesWriter.Close();

// Deserialize from a .rules file.

XmlTextReader rulesReader = new XmlTextReader(fileName);

WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

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

rulesReader.Close();

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

ruleSet.Execute(execution);




An Introduction to WWF and rules engine is available here:

http://msdn2.microsoft.com/en-gb/library/aa480193.aspx


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

http://wf.netfx3.com/files/folders/rules_samples/entry309.aspx



Advertisements

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 )

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

Create a free website or blog at WordPress.com.

%d bloggers like this: