![Page 1: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/1.jpg)
![Page 2: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/2.jpg)
Rules Engine Use and Extensibility in Microsoft Windows Workflow FoundationMatt Winkler – Technical Evangelist WFMicrosoft Corporationhttp://blogs.msdn.com/mwinkle
CON312
![Page 3: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/3.jpg)
Agenda
IntroductionRule executionRule extensibility
![Page 4: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/4.jpg)
Activity Library
Workflow Runtime Engine
Runtime Services
Visual Studio Designer
Long Running Logic
Flexibility
What is Workflow?
Workflow is the software implementation of business process or business logic
What’s the value of Workflow?
Released Nov 2006 as a part of Windows Vista
Transparency
A Workflow
Host Process
Activity
Windows Workflow Foundation
![Page 5: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/5.jpg)
WF Rules Terminology
Conditions Boolean expressions
RuleIf Condition THEN <ThenActions> ELSE <ElseActions>
RuleSetCollection of rulesExecution semantics
![Page 6: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/6.jpg)
Rules BenefitsSimple semantics for defining discrete, atomic rulesAggregation of rules into rule sets that represent rich application behavior
Modeling Power
Declarative nature allows domain experts to concentrate on the business requirements
Model-driven design allows for rich authoring and management toolsExternalization of rule logic from application code
Rapid deployment model
Supporting impact analysis and simulation capabilities
Approachability
Ease of Change
![Page 7: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/7.jpg)
Why Rules?
Externalize decision details from processCreate tooling for rule customization by non-developersExecute a set of rules against a set of factsTake advantage of complex execution semantics that are nearly impossible to model in code
![Page 8: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/8.jpg)
Where Do We Use Rules?Activity Conditions
If-ElseWhileReplicator Conditioned Activity Group (CAG)Conditions on Custom Activities
Policy ActivityEncapsulates the definition and execution of a RuleSet
Custom Activities.NET Applications
![Page 9: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/9.jpg)
Rule Execution
![Page 10: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/10.jpg)
Activity Conditions
Code conditionsFast, fast, fastCompiled into workflow
Declarative conditionsExternalized in .rules fileCompiled into workflow, but easy to change
![Page 11: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/11.jpg)
Externalizing Rules
Versioning and ManagementIndependent from the applicationSpecialized management tools for rules
Deployment and CompilationEase of changeNo application recompilationRules only deployment / No application redeployment
![Page 12: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/12.jpg)
Externalizing Rules Approach 1Use declarative, XAML-only workflows
public WorkflowInstance CreateWorkflow(
XmlReader workflowDefinitionReader, XmlReader rulesReader, Dictionary<string, object>
namedArgumentValues)
![Page 13: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/13.jpg)
Externalizing Rules Approach 2Use DynamicUpdate
static void UpdateRules(WorkflowInstance instance){
WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();XmlTextReader reader = new XmlTextReader("..\\RealRules.rules");RuleDefinitions rules = serializer.Deserialize(reader) as RuleDefinitions;WorkflowChanges changes = new WorkflowChanges( instance.GetWorkflowDefinition());changes.TransientWorkflow.SetValue(
RuleDefinitions.RuleDefinitionsProperty, rules);instance.ApplyWorkflowChanges(changes);
}
![Page 14: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/14.jpg)
Externalizing Rules Approach 3Write a custom loader
workflowRuntime.AddService(new CustomLoader());class CustomLoader : DefaultWorkflowLoaderService{ protected override Activity CreateInstance(Type workflowType) { Activity workflow = base.CreateInstance(workflowType); WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer(); XmlTextReader reader = new XmlTextReader("..\\RealRules.rules"); RuleDefinitions rules = serializer.Deserialize(reader) as RuleDefinitions; workflow.SetValue(RuleDefinitions.RuleDefinitionsProperty, rules); return workflow; }}
![Page 15: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/15.jpg)
Externalizing Rules Approach 4Create rules service
RuleSet Designer
RuleSet Repository
Host
RuleSet Service
Workflow1 Workflow2 Workflow3CustomPolicy
Activity
CustomPolicy
Activity
CustomPolicy
Activity
![Page 16: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/16.jpg)
Externalizing Rules
Option 1: Explicit Pattern
Option2: Custom Loader
Option 3: WorkflowChanges
Option 4:CreateWF overload
Outside WF
YES NO NO NO
When are the rules loaded?
When policy activity executes
When WF is loaded (initial, re-hydrated)
When DynamicUpdates are made
When instance is created
Workflows with code-beside
YES YES YES NO
![Page 17: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/17.jpg)
Key APIs
RuleSetDialog ruleSetDialog = new RuleSetDialog(targetType, ruleSet);DialogResult result = ruleSetDialog.ShowDialog();RuleSet newRuleSet = ruleSetDialog.RuleSet;
Host RuleSet Editor
WorkflowMarkupSerializer.Serialize(XmlWriter writer, object obj);
Serialize Workflow
Add RuleSetService to WorkflowRuntime
WorkflowRuntime.AddService(new RuleSetService());
![Page 18: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/18.jpg)
Key APIs
RuleValidation validation = new RuleValidation(targetObject.GetType(), null);
RuleExecution execution = new RuleExecution(validation, targetObject, null);
ruleSet.Execute(execution);
Execute RuleSet
ValidationErrorsCollection errors = validation.Errors;
Validation Errors
![Page 19: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/19.jpg)
External Rule Execution
demo
![Page 20: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/20.jpg)
Rule Chaining
Rulesets can get very big, (10,000+ rules)The rules engine tracks changes (side effects) of rule execution to enable rule re-executionWe have fine grained control over execution
PriorityChaining mechanisms
ImplicitExplicit“Partially Explicit”
![Page 21: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/21.jpg)
Amount = $25 Total = $80Discount = 10%
Forward Chaining Example
Rule 1 (P0)If Total > $50 & < $100 Then Discount = 10%Rule 2 (P0)If Total >= $100Then Discount = 15%Rule 3 (P0)If Amount > $0Then Total = Total + Amount
Rule 1Rule 3 Rule 2
Execution Sequence
$105
RuleSet
15%
![Page 22: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/22.jpg)
Rule Attributes
Declaratively decorate methods in a class to allow rule engine to infer chaining behavior[RuleRead(path)]
Indicate decorated method relies on variable in path
[RuleWrite(path)]Indicate decorated method updates variable in path
[RuleInvoke(methodName)]Indicate decorated method calls another method which updates a variable
![Page 23: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/23.jpg)
Collection Processing
Rule 1 (Priority =2)If 1==1Then
this.enumerator = this.collection.GetEnumerator()
Rule 2 (Priority = 1)If this.enumerator.MoveNext()Then
this.currentInstance = this.enumerator.Current
Rules 3 – n (Priority = 0)// write these against currentInstance
Rule n+1 (Priority = -1)If this.currentInstance == this.currentInstanceThen
Update(this/enumerator)
![Page 24: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/24.jpg)
Collection Processing
demo
![Page 25: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/25.jpg)
RULES EXTENSIBILITY
![Page 26: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/26.jpg)
Custom Rules Authoring UI ExampleBusiness Language Rules
InRuleTM for Windows® Workflow Foundation
![Page 27: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/27.jpg)
InRuleTM for Windows® Workflow Foundation
Custom Rules Authoring UI ExampleDecision Table
![Page 28: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/28.jpg)
Using the Rules Object ModelRule Conditions Object Model
RuleCondition
1
*
Activity
ActivityCondition
RuleConditionReference
-ConditionName
*
-Name *11
RuleDefinitions
![Page 29: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/29.jpg)
RuleDefinitionsRuleSet 1
*
Rule
1*RuleSetReference - RuleSetName
*
-Name
1
Activity
11
RuleCondition
RuleExpressionCondition
CodeExpression
RuleAction
RuleStatementAction RuleHaltAction RuleUpdateAction
CodeStatement
Using the Rules Object ModelRules Object Model
![Page 30: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/30.jpg)
Supported CodeDom TypesCodeArrayIndexerExpressionCodeAssignStatementCodeBinaryOperatorExpressionCodeCastExpressionCodeDirectionExpressionCodeExpressionStatementCodeFieldReferenceExpressionCodeIndexerExpression
CodeMethodInvokeExpressionCodeMethodReferenceExpressionCodePrimitiveExpressionCodePropertyReferenceExpressionCodeThisReferenceExpressionCodeTypeReferenceCodeTypeReferenceExpressionCodeObjectCreateExpression*
CodeArrayCreateExpression*
* New in NETFX 3.5
![Page 31: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/31.jpg)
Using the Rules Object Model
demo
![Page 32: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/32.jpg)
Custom Expression and Actions
Re-use/share commonly used expressions and actionsChoose text representation in the Rules editorCan participate in RuleSet validation and raise errorsCan participate in the forward chaining analysisAccess to ActivityExecutionContext (access to the running activities, host services, etc.)Raises the authoring semantic
![Page 33: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/33.jpg)
Rule Expression Key APIs
Derives from CodeExpressionImplements IRuleExpression
public interface IRuleExpression
{
void AnalyzeUsage(RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier);
CodeExpression Clone();
void Decompile(StringBuilder stringBuilder, CodeExpression parentExpression);
RuleExpressionResult Evaluate(RuleExecution execution);
bool Match(CodeExpression expression);
RuleExpressionInfo Validate(RuleValidation validation, bool isWritten);
}
![Page 34: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/34.jpg)
Rule Actions Key APIs
Derives from RuleAction
[Serializable]
public abstract class RuleAction
{
public abstract RuleAction Clone();
public abstract void Execute(RuleExecution context);
public abstract ICollection<string> GetSideEffects(RuleValidation validation);
public abstract bool Validate(RuleValidation validator);
}
![Page 35: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/35.jpg)
Custom Rule Expressions and Actions
demo
![Page 36: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/36.jpg)
Execute Activity Custom Rule Action
Given a rule action has access to ActivityExecutionInstance, we could schedule an activity to executeGain the ability to model rule actions as activities (or a sequence of activities)Lose the ability to react to side effects from activity execution (chaining), but could re-run ruleset
![Page 37: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/37.jpg)
Using Rules to Drive Workflow
demo
![Page 38: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/38.jpg)
Summary
Rules are an easy way to add flexible logic to your processRules can be used stand-alone by any .NET applicationManagement and tooling can be built on top of the rule API’sCustom actions and expressions make it easier to expose functionality to ruleset designers
![Page 39: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/39.jpg)
Evaluation Forms
![Page 40: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/40.jpg)
Questions?
![Page 41: Matt Winkler – Technical Evangelist WF Microsoft Corporation CON312](https://reader038.vdocuments.us/reader038/viewer/2022110400/56649db35503460f94aa2b0f/html5/thumbnails/41.jpg)
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.