190 likes | 395 Views
Daniel van Wyk – 3fifteen – SA Developer.Net – Information Worker Group. Using the Workflow Foundation Rule Engine without using Workflow Foundation. My Story – Great Expectations. “... The rule engine will take care of that ...”
E N D
Daniel van Wyk – 3fifteen – SA Developer.Net – Information Worker Group Using the Workflow Foundation Rule Engine without using Workflow Foundation
My Story – Great Expectations • “... The rule engine will take care of that ...” • “... Certainly the rule engine is flexible enough to add new rules ...” • “... Ahhh ... That’s just a business rule we’ll add ...” • “... We’ll need a complex busines rule engine to cater for all the scenarios ...” • “ ... We’ll update the rule engine ...”
Typical Examples • Calculate tax, discount, etc • Medical claim assessment • Add message to account/order • Creating gift vouchers • Adding a free item based on your order • Fraud checking • Error and Message Logging
The essence of a rule • if (condition) then (action1) else (action2) • if person's age is greater than 55 then discount = discount + 10% • if discount is greater than 12% then discount = 12%
What should the “Rule Engine” do? • Ability to add rules on the fly • BONUS: easy editor • DOUBLE BONUS: a “busines person” can add rules • Save the rules (and load them) • Use .net objects used by application • Easy to hook into from application / domain layer • Let me code – no rule theory degree needed
What are the options? • Code the rules in your code (not too flexible) • Create your own scripting language • Dependency Injection • Rule engines • Open source • C# - Drools.Net, NxBRE, SRE • Java – Drools, OpenRules, Mandarax, SweetRules • Third party • Use the WF rule engine (thanks Google!)
Using the Rules Engine • Add References • System.Workflow.Activities • System.Workflow.ComponentModel • Create Instance of RuleSet • RuleSet rules = new RuleSet();
Using the Rules Engine (cont.) • Load Rules from file using (XmlTextReaderrulesReader = new XmlTextReader(filename)) { WorkflowMarkupSerializerserializer = new WorkflowMarkupSerializer(); rules = (RuleSet)serializer.Deserialize(rulesReader); }
Using the Rules Engine (cont.) • Apply RulesRuleValidation validation = new RuleValidation(typeof(Order), null); RuleExecution execution = new RuleExecution(validation, newOrder); rules.Execute(execution);
Demo – the basics • Loading rules from file • Editing rules • Applying rules • Saving rules
Chaining • Order of rules • Rules impacting each other • A rule might cause another rule to become valid • Controlling chaining • Implicit • Explicit
Rule Re-evalution options • "Always“ • Default behaviour for a rule. • The rules engine will always re-evaluate a rule with this setting, if the proper criteria are met. • This setting would not override a rule set chaining behaviour of "Sequential" • "Never“ • Turns off re-evaluation. • The rules engine only considers a rule "evaluated" if the rule executes a non-empty action. • Consider a rule that has Then actions, but no Else actions. If the rule is evaluated and its condition returns false, the rule is still a candidate for re-evaluation because the rule did not execute any actions.
Demo – down the rabbit hole ... • Rule Re-Evaluation • Rule Chaining
Where to next? • Instead of executing the rules from the UI, make the rule execution part of the class • Save rules in a database • Create a RuleRepository library
What can you NOT do? • Return a value from a rule • Use delegates in the rule action • this.OrderItems.ForEach(delegate(OrderItem i) { i.DiscountAmount = 10; })
References • http://odetocode.com/articles/458.aspx • http://blogs.microsoft.co.il/blogs/bursteg/archive/2006/10/11/RuleExecutionWithoutWorkflow.aspx • http://msdn.microsoft.com/en-us/library/aa480193.aspx • http://footheory.com/blogs/bennie/archive/2007/05/12/simple-application-extensibility-with-wf-rules.aspx • http://www.joshlane.net/blog/WindowsWorkflowRulesEngineWithoutWindowsWorkflow.aspx