Developing Event Driven State Machine Workflows

advertisement
S1
S2
S3
Developing Event
Driven State
Machine Workflows
Adam Calderon
Principal Engineer - Interknowlogy
Microsoft MVP – C#
S4
Session Objectives And
Key Takeaways
Objectives
What is Windows Workflow Foundation
Why State Machine Workflows
State Machine Workflow Structure
The State Activity
The Event Driven Activity
The Set State Activity
Skip and Rework Patterns
Dynamic Updates to State Machines
Key Takeaways
State machine as a powerful modeling
paradigm
Agenda
State Machine Workflow Introduction
Building State Machine Workflows
Recursive Composition
Skip and Rework Pattern
Additional State Machine Patterns
Summary
Q&A
Types of Business Processes
Control flow processes
Are self driven once they are initiated
The set of events that may happen are highly
predictable
Mostly sequential in nature
Highly suitable for process automation
Event Driven processes
Are driven by external events
Must be able to respond to any high priority event even
if the some other work is in process
The sequence of events may not be predictable
The process can jump to any step in the process
Highly suitable for processes where human actions are
involved
What Do State Machines Workflows
Offer
Provide an effective modeling paradigm for Event driven
processes
Provide a natural paradigm for the business users to
express business intent
Business users always think in terms of logical states of the
process, possible events in those states and actions in response to
the events
Provide an easy way to query and visualize the process
The current state of the process is the current state of the state
machine
The set of possible transitions from the current state can be easily
obtained
Provide very powerful business exception handling
State Machines are a familiar paradigm for IT
professionals
When to Use State Machine
Workflow
State Machine workflow provides a very
powerful design paradigm
Not all processes can be modeled using State
Machines
Use it when there is a lot of human interactions
with the process
Use it when the workflow is driven by external
events
Use it when there is a lot of out of band events
that can be received in the workflow
Use it when it is hard to wire all the possible
paths within the process in a sequential
workflow
Basic Concepts of State Machine
Workflows
The basic elements of a state machine workflow
States
Events
Actions
Transitions
A state machine workflow is composed of a set
of states
In a given state a set of events can be received
Based on the event received an action is
performed at the end of which a state transition
may or may not be made
Example : A Simple PO
Process
On Order Completed
Waiting to
Create
Order
On Order Created
Order
Created
On Order Approved
Order
Approved
On Order
Completed
On Order
Shipped
On Order
Shipped
Order
Shipped
On Order
Completed
Order
Completed
A Purchase Order State
Machine Workflow
Developing Event Driven State Machine
Workflows
Agenda
State Machine Workflow Introduction
Building State Machine Workflows
Recursive Composition
Skip and Rework Pattern
Additional State Machine Patterns
Summary
Q&A
State Machine Implementation in
WinWF
State Machine Workflow
The state machine workflow
is a root activity like the
sequential workflow
There are 4 activities which
work in the state machine
workflow
State -- Represents the state of
the state machine
Event Driven – Used to handle
an event in a given state
Set State – Used to transition
from one state to another
State Initialization – Used for
default action on entering a
state
Event Driven
State
State Initialization
Event Driven
Set State
State
State Machine Workflow Activities
The State Activity
The state activity represents a
logical state of the state machine
It can contain a collection of
event driven activities
To be in a particular state in the
state machine means to execute
that state activity
The state activity can also contain
the ‘State Initialization’ activity
The state initialization activity is
optional and if present is executed by
default
State
State Initialization
Event Driven
Set State
State
How to Design a State
The “State” is the logical state of the
process
E.g. Order Shipped, Document Approved
The “State” is similar to a milestone in a
process
Is a natural fit as a tracking point for a process
The state machine workflow can only be in
one logical state at any point in time
In a given state the process may receive
only a valid set of events
E.g. publish document event in the document
approved state
Initial State and Completed State
A state machine workflow must have a start
state
This is the state in which the state machine will
be when the state machine instance is created
A state machine workflow can have a
completed state
When the workflow reaches the completed state
the workflow instance stops executing
The Start state and completed state can be
specified as properties on the root state
machine workflow activity
State Machine Workflow Activities:
The Event Driven Activity
The event driven activity is
used to handle an event
received in a particular state
The event driven activity is
common to State machine and
sequential workflows
An event driven activity is a
composite activity which
behaves like a sequence with
some additional attributes
Must have the first activity as an
activity that receives an event or a
delay activity
Cannot contain any other activity
that receives an event
Event Driven
Receive Event /
Delay
Action
Set State
Non Blocking Execution of the
Event Driven Activity
The event driven activity execution in a state
machine workflow must be non blocking
If the event driven execution is blocked other
event may happen which may invalidate the
current state and hence the execution of the
event handler
The state machine workflow enforces this pattern
by allowing only one event receive activity
State Machine Workflow Activities
The Set State Activity
The set state activity causes the actual transition
from one state to another
The set state activity must be used inside an
event driven activity
The set state activity must be the last executing
activity in the event handler
The pattern enforced here is that an event is handled
any needed actions performed and then a transition is
made
There can be multiple set state activities (For e.g. in an
if/else activity) but each one of them must be the last
one to execute in the event handler
Purchase Order State
Machine Workflow a
Deeper Look
Developing Event Driven State Machine
Workflows
Agenda
State Machine Workflow Introduction
Building State Machine Workflows
Recursive Composition
Skip and Rework Pattern
Additional State Machine Patterns
Summary
Q&A
Recursive Composition of States
in a State Machine
States activities can be recursively
composed
When a state activity contains other states
then that state cannot be transitioned to
Transition is only allowed to a leaf level state
Recursive composition provides a very
powerful design pattern
Help in business exception handling
Help in reuse of event handlers where the same
event needs to be handled in multiple states
PO Process Revisited
Order
Changed
On Order Changed
On Order Completed
Waiting to
Create
Order
On Order Created
Order
Created
On Order Approved
Order
Approved
On Order
Completed
On Order
Shipped
On Order
Shipped
Order
Shipped
On Order
Completed
Order
Completed
Recursive State Composition
Developing Event Driven State Machine
Workflows
Agenda
State Machine Workflow Introduction
Building State Machine Workflows
Recursive Composition
Skip and Rework Pattern
Additional State Machine Patterns
Summary
Q&A
Ability to query the workflow
model
The ability to query the model is a very
crucial is creating effective workflow
visualizations
State machine workflow offers a robust
infrastructure to query and interact with a
workflow instance
You can answer queries like
What is the current state of the workflow?
What transitions are possible from the current
state?
Enumerate all the states in the workflow?
Skip and Rework Patterns
Workflow involves ad hoc steps that may
not be modeled in advance
E.g. In the PO process the PO creator may
want to skip the approval action
E.g. The PO creator may want to send the PO
through an extra approval;
The State machine workflow has a built in
mechanism to reset the state of the state
machine to desired state
PO Process – Skip & Rework
Order
Changed
On Order Changed
On Order Completed
Set State Event
Waiting to
Create
Order
On Order Created
Order
Created
On Order Approved
Order
Approved
On Order
Completed
On Order
Shipped
On Order
Shipped
Order
Shipped
On Order
Completed
Order
Completed
Skip and Rework Pattern
Developing Event Driven State Machine
Workflows
Agenda
State Machine Workflow Introduction
Building State Machine Workflows
Recursive Composition
Skip and Rework Pattern
Additional State Machine Patterns
Summary
Q&A
Dynamic Change Patterns
Dynamic change is the ability to make structural
changes to the process
In the state machine dynamic change can be
introduced at several levels
Add a new state to the state machine
Handle a new event in a particular state
Rewire the state transitions
The dynamic update to the state machine
follows the same steps as for a sequential
workflow
Multiple State Machines in an
application
Order Fulfillment Application
An application is hardly
made up of one workflow
Multiple workflows work in
tandem within an
application
These workflows can be
autonomous but
cooperating with each
other
They synchronize their
states but not necessarily
control the lifetimes of other
workflows
S1
S2
S3
S4
S1
Order Approval
S2
S3
S4
S1
S2
S3
S4
Order Shipment
Warehouse
Processes
Summary
State Machine workflows are a key
innovation in WinWF
State machine workflows provide a very
powerful tool for modeling business
applications
Use it when the process to be implemented
has a lot of human interaction in it
The state machine is very easy to query
and visualize
The state machine model and sequential
model and complimentary
Adam Calderon
More info on InterKnowlogy
www.InterKnowlogy.com
Contact Information
E-mail: adamc@InterKnowlogy.com
Phone: 760-930-0075 x274
Blog: http://blogs.InterKnowlogy.com/AdamCalderon
About Adam Calderon
Microsoft® MVP – C#
Microsoft® ASP.NET (UI Server Frameworks) Advisory
Council
Developer / Author / Speaker
Download