Upload
jaron-robnett
View
233
Download
0
Tags:
Embed Size (px)
Citation preview
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#
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
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
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
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
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
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
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
A Purchase Order State A Purchase Order State Machine WorkflowMachine Workflow
Developing Event Driven State Developing Event Driven State Machine Workflows Machine Workflows
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
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
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
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
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
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
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
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
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
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
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
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
Recursive State Recursive State CompositionComposition
Developing Event Driven State Developing Event Driven State Machine Workflows Machine Workflows
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
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?
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
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
Skip and Rework Skip and Rework PatternPattern
Developing Event Driven State Developing Event Driven State Machine Workflows Machine Workflows
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
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
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
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
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