developing event driven state machine workflows s1 s2 s3 s4 adam calderon principal engineer -...

33
Developing Developing Event Driven Event Driven State Machine State Machine Workflows Workflows S1 S2 S3 S4 Adam Calderon Adam Calderon Principal Engineer - Principal Engineer - Interknowlogy Interknowlogy Microsoft MVP – C# Microsoft MVP – C#

Upload: jaron-robnett

Post on 29-Mar-2015

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Developing Event Developing Event Driven State Driven State Machine Machine WorkflowsWorkflows

S1S2

S3

S4

Adam CalderonAdam CalderonPrincipal Engineer - InterknowlogyPrincipal Engineer - InterknowlogyMicrosoft MVP – C#Microsoft MVP – C#

Page 2: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Session Objectives And Session Objectives And Key TakeawaysKey Takeaways

ObjectivesObjectivesWhat is Windows Workflow FoundationWhat is Windows Workflow FoundationWhy State Machine WorkflowsWhy State Machine WorkflowsState Machine Workflow StructureState Machine Workflow Structure

The State ActivityThe State ActivityThe Event Driven ActivityThe Event Driven ActivityThe Set State ActivityThe Set State Activity

Skip and Rework PatternsSkip and Rework PatternsDynamic Updates to State MachinesDynamic Updates to State Machines

Key TakeawaysKey TakeawaysState machine as a powerful modeling State machine as a powerful modeling paradigmparadigm

Page 3: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

AgendaAgenda

State Machine Workflow IntroductionState Machine Workflow IntroductionBuilding State Machine WorkflowsBuilding State Machine WorkflowsRecursive CompositionRecursive CompositionSkip and Rework PatternSkip and Rework PatternAdditional State Machine PatternsAdditional State Machine PatternsSummarySummaryQ&AQ&A

Page 4: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Types of Business ProcessesTypes of Business ProcessesControl flow processesControl flow processes

Are self driven once they are initiatedAre self driven once they are initiatedThe set of events that may happen are highly The set of events that may happen are highly predictablepredictableMostly sequential in natureMostly sequential in natureHighly suitable for process automationHighly suitable for process automation

Event Driven processesEvent Driven processesAre driven by external eventsAre driven by external eventsMust be able to respond to any high priority Must be able to respond to any high priority event even if the some other work is in processevent even if the some other work is in processThe sequence of events may not be predictable The sequence of events may not be predictable The process can jump to any step in the processThe process can jump to any step in the processHighly suitable for processes where human Highly suitable for processes where human actions are involvedactions are involved

Page 5: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

What Do State Machines What Do State Machines Workflows Workflows OfferOfferProvide an effective modeling paradigm for Event Provide an effective modeling paradigm for Event

driven processesdriven processesProvide a natural paradigm for the business users Provide a natural paradigm for the business users to express business intentto express business intent

Business users always think in terms of logical states of Business users always think in terms of logical states of the process, possible events in those states and actions in the process, possible events in those states and actions in response to the eventsresponse to the events

Provide an easy way to query and visualize the Provide an easy way to query and visualize the processprocess

The current state of the process is the current state of the The current state of the process is the current state of the state machinestate machineThe set of possible transitions from the current state can The set of possible transitions from the current state can be easily obtainedbe easily obtained

Provide very powerful business exception handlingProvide very powerful business exception handlingState Machines are a familiar paradigm for IT State Machines are a familiar paradigm for IT professionalsprofessionals

Page 6: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

When to Use State When to Use State Machine Machine WorkflowWorkflowState Machine workflow provides a very State Machine workflow provides a very

powerful design paradigmpowerful design paradigmNot all processes can be modeled using Not all processes can be modeled using State MachinesState MachinesUse it when there is a lot of human Use it when there is a lot of human interactions with the processinteractions with the processUse it when the workflow is driven by Use it when the workflow is driven by external eventsexternal eventsUse it when there is a lot of out of band Use it when there is a lot of out of band events that can be received in the events that can be received in the workflowworkflowUse it when it is hard to wire all the Use it when it is hard to wire all the possible paths within the process in a possible paths within the process in a sequential workflowsequential workflow

Page 7: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Basic Concepts of State Basic Concepts of State MachineMachineWorkflowsWorkflowsThe basic elements of a state machine The basic elements of a state machine

workflowworkflowStatesStatesEventsEventsActionsActionsTransitionsTransitions

A state machine workflow is composed of A state machine workflow is composed of a set of a set of statesstatesIn a given state a set of In a given state a set of eventsevents can be can be receivedreceivedBased on the event received an Based on the event received an actionaction is is performed at the end of which a state performed at the end of which a state transitiontransition may or may not be made may or may not be made

Page 8: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Example : A Simple PO Example : A Simple PO ProcessProcess

On Order Created On Order ApprovedOrderCreated Order

Approved

OrderShipped

On Order Shipped

On Order Completed

On Order Completed

Waiting toCreate Order

On Order Completed

On Order Shipped

OrderCompleted

Page 9: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

A Purchase Order State A Purchase Order State Machine WorkflowMachine Workflow

Developing Event Driven State Developing Event Driven State Machine Workflows Machine Workflows

Page 10: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

AgendaAgenda

State Machine Workflow IntroductionState Machine Workflow IntroductionBuilding State Machine WorkflowsBuilding State Machine WorkflowsRecursive CompositionRecursive CompositionSkip and Rework PatternSkip and Rework PatternAdditional State Machine PatternsAdditional State Machine PatternsSummarySummaryQ&AQ&A

Page 11: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

State Machine Implementation State Machine Implementation in WinWFin WinWF

The state machine The state machine workflow is a root workflow is a root activity like the activity like the sequential workflowsequential workflowThere are 4 activities There are 4 activities which work in the state which work in the state machine workflowmachine workflow

StateState -- Represents the -- Represents the state of the state machinestate of the state machineEvent DrivenEvent Driven – Used to – Used to handle an event in a given handle an event in a given statestateSet StateSet State – Used to – Used to transition from one state to transition from one state to anotheranotherState InitializationState Initialization – Used – Used for default action on for default action on entering a stateentering a state

Event Driven

State Machine Workflow

Event Driven

State Initialization

State

Set State

State

Page 12: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

State Machine Workflow ActivitiesState Machine Workflow ActivitiesThe State ActivityThe State Activity

The state activity represents The state activity represents a logical state of the state a logical state of the state machinemachineIt can contain a collection of It can contain a collection of event driven activitiesevent driven activitiesTo be in a particular state in To be in a particular state in the state machine means to the state machine means to execute that state activityexecute that state activityThe state activity can also The state activity can also contain the ‘State contain the ‘State Initialization’ activityInitialization’ activity

The state initialization activity is The state initialization activity is optional and if present is optional and if present is executed by defaultexecuted by default

Event Driven

State Initialization

State

Set State

State

Page 13: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

How to Design a StateHow to Design a StateThe “State” is the logical state of the The “State” is the logical state of the processprocess

E.g. Order Shipped, Document ApprovedE.g. Order Shipped, Document Approved

The “State” is similar to a milestone The “State” is similar to a milestone in a processin a process

Is a natural fit as a tracking point for a Is a natural fit as a tracking point for a processprocess

The state machine workflow can only The state machine workflow can only be in one logical state at any point in be in one logical state at any point in timetimeIn a given state the process may In a given state the process may receive only a valid set of eventsreceive only a valid set of events

E.g. publish document event in the E.g. publish document event in the document approved statedocument approved state

Page 14: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Initial State and Completed Initial State and Completed StateState

A state machine workflow must have A state machine workflow must have a start statea start state

This is the state in which the state This is the state in which the state machine will be when the state machine machine will be when the state machine instance is createdinstance is created

A state machine workflow can have a A state machine workflow can have a completed statecompleted state

When the workflow reaches the When the workflow reaches the completed state the workflow instance completed state the workflow instance stops executingstops executing

The Start state and completed state The Start state and completed state can be specified as properties on the can be specified as properties on the root state machine workflow activityroot state machine workflow activity

Page 15: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

State Machine Workflow State Machine Workflow Activities:Activities:The Event Driven ActivityThe Event Driven Activity

The event driven activity is The event driven activity is used to handle an event used to handle an event received in a particular received in a particular statestateThe event driven activity is The event driven activity is common to State machine common to State machine and sequential workflowsand sequential workflowsAn event driven activity is An event driven activity is a composite activity which a composite activity which behaves like a sequence behaves like a sequence with some additional with some additional attributesattributes

Must have the first activity as Must have the first activity as an activity that receives an an activity that receives an event or a delay activityevent or a delay activityCannot contain any other Cannot contain any other activity that receives an event activity that receives an event

Event Driven

Set State

Receive Event /Delay

Action

Page 16: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Non Blocking Execution of the Non Blocking Execution of the Event Driven ActivityEvent Driven Activity

The event driven activity execution in a The event driven activity execution in a state machine workflow must be non state machine workflow must be non blockingblockingIf the event driven execution is blocked If the event driven execution is blocked other event may happen which may other event may happen which may invalidate the current state and hence the invalidate the current state and hence the execution of the event handlerexecution of the event handlerThe state machine workflow enforces this The state machine workflow enforces this pattern by allowing only one event receive pattern by allowing only one event receive activity activity

Page 17: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

State Machine Workflow State Machine Workflow Activities Activities The Set State ActivityThe Set State Activity

The set state activity causes the actual The set state activity causes the actual transition from one state to anothertransition from one state to anotherThe set state activity must be used inside The set state activity must be used inside an event driven activityan event driven activityThe set state activity must be the last The set state activity must be the last executing activity in the event handlerexecuting activity in the event handler

The pattern enforced here is that an event is The pattern enforced here is that an event is handled any needed actions performed and then handled any needed actions performed and then a transition is madea transition is madeThere can be multiple set state activities (For There can be multiple set state activities (For e.g. in an if/else activity) but each one of them e.g. in an if/else activity) but each one of them must be the last one to execute in the event must be the last one to execute in the event handlerhandler

Page 18: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Purchase Order State Purchase Order State Machine Workflow a Machine Workflow a Deeper LookDeeper Look

Developing Event Driven State Developing Event Driven State Machine Workflows Machine Workflows

Page 19: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

AgendaAgenda

State Machine Workflow IntroductionState Machine Workflow IntroductionBuilding State Machine WorkflowsBuilding State Machine WorkflowsRecursive CompositionRecursive CompositionSkip and Rework PatternSkip and Rework PatternAdditional State Machine PatternsAdditional State Machine PatternsSummarySummaryQ&AQ&A

Page 20: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Recursive Composition of Recursive Composition of States States in a State Machinein a State Machine

States activities can be recursively States activities can be recursively composedcomposedWhen a state activity contains other When a state activity contains other states then that state cannot be states then that state cannot be transitioned totransitioned to

Transition is only allowed to a leaf level Transition is only allowed to a leaf level statestate

Recursive composition provides a Recursive composition provides a very powerful design patternvery powerful design pattern

Help in business exception handlingHelp in business exception handlingHelp in reuse of event handlers where the Help in reuse of event handlers where the same event needs to be handled in same event needs to be handled in multiple statesmultiple states

Page 21: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

PO Process RevisitedPO Process Revisited

On Order ChangedOrder

Changed

On Order Created On Order ApprovedOrderCreated Order

Approved

OrderShipped

On Order Shipped

On Order Completed

On Order Completed

Waiting toCreate Order

On Order Completed

On Order Shipped

OrderCompleted

Page 22: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Recursive State Recursive State CompositionComposition

Developing Event Driven State Developing Event Driven State Machine Workflows Machine Workflows

Page 23: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

AgendaAgenda

State Machine Workflow IntroductionState Machine Workflow IntroductionBuilding State Machine WorkflowsBuilding State Machine WorkflowsRecursive CompositionRecursive CompositionSkip and Rework PatternSkip and Rework PatternAdditional State Machine PatternsAdditional State Machine PatternsSummarySummaryQ&AQ&A

Page 24: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Ability to query the Ability to query the workflow modelworkflow model

The ability to query the model is a The ability to query the model is a very crucial is creating effective very crucial is creating effective workflow visualizationsworkflow visualizationsState machine workflow offers a State machine workflow offers a robust infrastructure to query and robust infrastructure to query and interact with a workflow instanceinteract with a workflow instanceYou can answer queries likeYou can answer queries like

What is the current state of the workflow?What is the current state of the workflow?What transitions are possible from the What transitions are possible from the current state?current state?Enumerate all the states in the workflow? Enumerate all the states in the workflow?

Page 25: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Skip and Rework PatternsSkip and Rework Patterns

Workflow involves ad hoc steps that Workflow involves ad hoc steps that may not be modeled in advancemay not be modeled in advance

E.g. In the PO process the PO creator may E.g. In the PO process the PO creator may want to skip the approval actionwant to skip the approval actionE.g. The PO creator may want to send the E.g. The PO creator may want to send the PO through an extra approval;PO through an extra approval;

The State machine workflow has a The State machine workflow has a built in mechanism to reset the state built in mechanism to reset the state of the state machine to desired stateof the state machine to desired state

Page 26: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

On Order ChangedOrder

Changed

On Order Created On Order ApprovedOrderCreated Order

Approved

OrderShipped

On Order Shipped

On Order Completed

On Order Completed

Waiting toCreate Order

On Order Completed

On Order Shipped

OrderCompleted

PO Process – Skip & PO Process – Skip & ReworkRework

Set State Event

Page 27: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Skip and Rework Skip and Rework PatternPattern

Developing Event Driven State Developing Event Driven State Machine Workflows Machine Workflows

Page 28: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

AgendaAgenda

State Machine Workflow IntroductionState Machine Workflow IntroductionBuilding State Machine WorkflowsBuilding State Machine WorkflowsRecursive CompositionRecursive CompositionSkip and Rework PatternSkip and Rework PatternAdditional State Machine PatternsAdditional State Machine PatternsSummarySummaryQ&AQ&A

Page 29: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Dynamic Change PatternsDynamic Change Patterns

Dynamic change is the ability to make Dynamic change is the ability to make structural changes to the processstructural changes to the processIn the state machine dynamic change can In the state machine dynamic change can be introduced at several levelsbe introduced at several levels

Add a new state to the state machineAdd a new state to the state machineHandle a new event in a particular stateHandle a new event in a particular stateRewire the state transitionsRewire the state transitions

The dynamic update to the state machine The dynamic update to the state machine follows the same steps as for a sequential follows the same steps as for a sequential workflowworkflow

Page 30: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Multiple State Machines in Multiple State Machines in an applicationan application

An application is An application is hardly made up of one hardly made up of one workflowworkflowMultiple workflows Multiple workflows work in tandem within work in tandem within an applicationan applicationThese workflows can These workflows can be autonomous but be autonomous but cooperating with each cooperating with each otherother

They synchronize their They synchronize their states but not states but not necessarily control the necessarily control the lifetimes of other lifetimes of other workflowsworkflows

Order Fulfillment ApplicationOrder Fulfillment Application

S1S2

S3S4

S1S2

S3S4

S1S2

S3S4

Order ApprovalOrder Approval

Order ShipmentOrder Shipment

Warehouse Warehouse ProcessesProcesses

Page 31: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

SummarySummaryState Machine workflows are a key State Machine workflows are a key innovation in WinWFinnovation in WinWFState machine workflows provide a State machine workflows provide a very powerful tool for modeling very powerful tool for modeling business applicationsbusiness applicationsUse it when the process to be Use it when the process to be implemented has a lot of human implemented has a lot of human interaction in itinteraction in itThe state machine is very easy to The state machine is very easy to query and visualizequery and visualizeThe state machine model and The state machine model and sequential model and complimentarysequential model and complimentary

Page 32: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Adam CalderonAdam Calderon

More info on InterKnowlogyMore info on InterKnowlogywww.InterKnowlogy.comwww.InterKnowlogy.com

Contact InformationContact InformationE-mail: E-mail: [email protected]@InterKnowlogy.comPhone: Phone: 760-930-0075 x274760-930-0075 x274Blog: Blog:

http://blogs.InterKnowlogy.com/AdamCalderonhttp://blogs.InterKnowlogy.com/AdamCalderon

About Adam CalderonAbout Adam CalderonMicrosoftMicrosoft®® MVP – C# MVP – C#MicrosoftMicrosoft®® ASP.NET (UI Server Frameworks) ASP.NET (UI Server Frameworks) Advisory CouncilAdvisory CouncilDeveloper / Author / SpeakerDeveloper / Author / Speaker

Page 33: Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#