19
Using the Workflow Foundation Rule Engine without using Workflow Foundation Daniel van Wyk – 3fifteen – SA Developer.Net – Information Worker Group

Using the Workflow Foundation Rule Engine without using Workflow Foundation

  • Upload
    alena

  • View
    98

  • Download
    1

Embed Size (px)

DESCRIPTION

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 ...” - PowerPoint PPT Presentation

Citation preview

Page 1: Using the Workflow Foundation Rule Engine without using Workflow Foundation

Using the Workflow Foundation Rule Engine without using Workflow Foundation

Daniel van Wyk – 3fifteen – SA Developer.Net – Information Worker Group

Page 2: 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 ...”

Page 3: Using the Workflow Foundation Rule Engine without using Workflow Foundation

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

Page 4: Using the Workflow Foundation Rule Engine without using Workflow Foundation

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%

Page 5: Using the Workflow Foundation Rule Engine without using Workflow Foundation

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

Page 6: Using the Workflow Foundation Rule Engine without using Workflow Foundation

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

Page 7: Using the Workflow Foundation Rule Engine without using Workflow Foundation

WF – Workflow Foundation

Page 8: Using the Workflow Foundation Rule Engine without using Workflow Foundation

Using the Rules Engine

Add References System.Workflow.Activities System.Workflow.ComponentModel

Create Instance of RuleSet RuleSet rules = new RuleSet();

Page 9: Using the Workflow Foundation Rule Engine without using Workflow Foundation

Using the Rules Engine (cont.) Load Rules from file

using (XmlTextReader rulesReader = new XmlTextReader(filename))

{ WorkflowMarkupSerializer serializer = new

WorkflowMarkupSerializer(); rules = (RuleSet)serializer.Deserialize(rulesReader); }

Page 10: Using the Workflow Foundation Rule Engine without using Workflow Foundation

Using the Rules Engine (cont.) Apply Rules

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

RuleExecution execution = new RuleExecution(validation, newOrder);

rules.Execute(execution);

Page 11: Using the Workflow Foundation Rule Engine without using Workflow Foundation

Demo – the basics

Loading rules from file Editing rules Applying rules Saving rules

Page 12: Using the Workflow Foundation Rule Engine without using Workflow Foundation

Chaining

Order of rules Rules impacting each other

A rule might cause another rule to become valid

Controlling chaining Implicit Explicit

Page 13: Using the Workflow Foundation Rule Engine without using Workflow Foundation

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.

Page 14: Using the Workflow Foundation Rule Engine without using Workflow Foundation

Demo – down the rabbit hole ... Rule Re-Evaluation Rule Chaining

Page 15: Using the Workflow Foundation Rule Engine without using Workflow Foundation

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

Page 16: Using the Workflow Foundation Rule Engine without using Workflow Foundation

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

Page 18: Using the Workflow Foundation Rule Engine without using Workflow Foundation

Questions?

Page 19: Using the Workflow Foundation Rule Engine without using Workflow Foundation