Execute Windows Workflow Rules without Workflow

11 באוקטובר 2006

24 comments

Embedding business rules is very powerful and useful. If you are using Windows Workflow Foundation you can model you business process and use a PolicyActivity. But If you don't want to model your business process with a workflow, but still want to execute rules in it?


The Windows Workflow Foundation Rules Engine has the API for it. I'll take this post to explore it a little bit and give some examples.


 


You can run a RuleSet against any object…


Typically you would execute your RuleSets against you workflow, to change the workflow state according to some properties. But, you can also execute rules againt any other .net object.


 


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


 


Download a sample project that demonstrates creation, editing, serialization and execution of RuleSet against a custom object.


 


Enjoy!

Add comment
facebook linkedin twitter email

24 comments

  1. rotu10 בינואר 2007 ב 8:04

    Good day.
    Is there a way to determine which of the rules inside the ruleset have been satisfied during ruleSet.Execute?

    ex.
    Rule1 – satisfied
    Rule2 – not satisfied
    etc.

    Thanks

  2. Guy Burstein10 בינואר 2007 ב 11:07

    You can use the RuleAnalysis class and the RuleExpressionWalker classes to determine that. They both work at the level of a single rule and not the whil ruleset, but a simple iteration should do it.
    Good luck!
    Guy

  3. Chakshu Shah22 באוגוסט 2007 ב 7:51

    How can I use this Rules Engine in my ASP.Net 2.0 application? The only option I have to expose it as a web-service and then use? Can you think of so other way of accomplishing this?

  4. Guy Burstein22 באוגוסט 2007 ב 9:32

    You can use it in ASP.Net exactly in the same way. You can handle any postback event and call the Rules Engine API.

  5. Chakshu Shah23 באוגוסט 2007 ב 8:52

    Please note that I want to use it in a ASP.Net 2.0 application. I dont think I shall be able to use it in the same way. The option I was thinking was to wrap up the Rules Engine with a Web-Service and use it through ASP.Net 2.0. If I follow this approach,
    1) Will I be able to modify the rules from my front-end GUI?
    2) Is the WF Rules Engine advanced enough to support complex rules?

    This is kind of urgent requirement. Your prompt help would be highly appreciated.

  6. Guy Burstein23 באוגוסט 2007 ב 17:58

    Hi

    I see no reason for not being able to use the Rules Engine in an ASP.Net Application just as you would do in any other application.

    You have no GUI for editing rules for web applications. Only a windows forms dialog ships with WF and it is targeted mostly for developers.

    The WF Rules Enging support complex conditions and rules, and you can read all about its capabilities here:
    http://msdn2.microsoft.com/en-us/library/aa480193.aspx

    Guy

  7. Chakshu Shah24 באוגוסט 2007 ב 13:03

    Thanks for all the prompt replies but I still have doubts :)
    As I understand, WF is part of .Net 3.0 and so is the Rules Engine that is part of the Workflow Framework. I have a requirement of using this Rules Engine from ASP.Net 2.0. Since Rules Engine is a part of .Net 3.0, I dont think I will be able to use the Rules API from 3.0 directly in 2.0. So, I was thinking of using a web-service.

    Do you think the solution is workable? or are you saying that I will be able to use the APIs directly in 2.0?

  8. Guy Burstein24 באוגוסט 2007 ב 17:22

    Of course! The .net framework 3.0 contains only extensions for the .net framework 2.0, but the CLR remains the same. This means that you can easily reference the .net 3.0 assemblies and work directly with them.

    Guy

  9. zxevil16316 במרץ 2008 ב 11:15

    qdejCE Hi from Russia!

  10. poonamb16 במאי 2008 ב 15:35

    hi,
    in my application, i require to save this .rules file in SQL server 2005. i should be able to retrieve this file from database whenever rule applies, make changes if needed and save it back to database.
    please help.

  11. Josh8 ביולי 2008 ב 11:17

    Hi Guy, first of all thanks for great articale.
    its gives me lots of (new) ideas of using wf rules instead of biztalk.
    the problem with biztalk bre composer is that users need to install or use it from trminal place. but with wf (as your example) we can launch the rule set dialog from any client application.
    my question is what the best way to call the wf rule from biztalk orch(we use biztalk2006) , of course the simple option is to call to webservice from BTS that host the wf rules.
    Is there any option to call the wf rule directly from the BTS orch shape? is any problem to call from .net2.0(bts 2006) to .net3.5?

    Thanks,
    Josh

  12. Guy Burstein8 ביולי 2008 ב 14:43

    I guess you could do this (never tried though).
    Keep in mind that you cannot reference a .Net 3.5 assembly from a .Net 2.0 project, so you may want to do this via reflection.

  13. Marc7 באוגוסט 2008 ב 21:56

    I would like to be able to audit the execution of my rules. I prefer not to use Trace because I want more control over the output. I found RuleActionTrackingEvent, but it appears to be only available for rules executed within a workflow. Are there any other alternatives? In an earlier post, you mentioned RuleAnalysis and RuleExpressionWalker. However, it's not clear from the MSDN documentation how to really use them. Any suggestions? Thanks.

  14. steve28 באוקטובר 2008 ב 20:32

    Is there a reference for the rules scripting language?

    Good stuff!

  15. Guy10 במרץ 2009 ב 20:28

    If any one know about rule engine working. please contact me. I need to create a rule engine using php and sql. contact me at stylishboy_18@hotmail.com

  16. SK18 במרץ 2009 ב 22:56

    Great post.!

    I have a doubt.
    How can I edit the rules once I created?
    Or how do I display the "Select Rule Set" dialog?

  17. CSR14 באפריל 2009 ב 13:42

    I need check some fields in a form for trigger some events, and I need to do so without entering code in the form.
    Could I use Rule Set Toolkit in a winform for this one?

  18. Saman5 במאי 2009 ב 14:01

    Thnx Guy. Your solution was a great help and prevent me to use other unknown rules engines.

  19. Barry19 במאי 2009 ב 10:57

    Is it possible to invoke the rules engine within a CLR procedure in SQL Server?

  20. Vibhuti16 ביוני 2009 ב 8:46

    Hello,
    Thanks for Good Article !!
    I have been go through it but i have one question in my mind that can we give Parenthesis in Rule to give high precedence to operator .
    Like i have a rule : rating=10 AND Channel="A" OR time=8 AM
    In this rule first expression with AND operator will execute and then it will execute with OR.like : (rating=10 AND Channel="A") OR time=8 AM because of high precedence of AND.
    But suppose i want to execute rule like: rating=10 AND (Channel="A" OR time=8 AM ) ,so can we give Parenthesis to make high precedence of OR operator ,so first expression with OR will execute and then it will execute with AND.

  21. brutal2 ביולי 2009 ב 17:40

    Can this be used for C++?

  22. brutal2 ביולי 2009 ב 17:56

    Can this be used in C++ CRL project?

  23. Noel22 ביולי 2009 ב 11:46

    Hi can we have rules applied on list on objects which will use linq to give some conditions. In short can we apply rules on Linq to SQL results.

Comments are closed.