平庸与杰出=加法与减法

思考其乐无穷 IT剩者为王

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
To define a state machine workflow, you start with the StateMachineWorkflowActivity class (found in the System.Workflow.Activities namespace). This is the base class for your workflow when you select State Machine Workflow as the Add Item template or when you select one of the State Machine Workflow Project templates. You can add a state machine workflow to any project that supports workflows. This means you can mix and match sequential and state machine workflows in the same project.
定义一个状态机工作流,你必须从StateMachineWorkflowActivity开始,在将状态机工作流作为添加项目模版或者创建一个状态机项目模版时,StateMachineWorkflowActivity是基础类.你可以添加一个状态机工作流到任何项目中,这意味着你可以混合和匹配状态机和顺序级工作流到同一个项目中.

Individual states are defined in the workflow using the StateActivity (also in the System.Workflow.Activities namespace). Each event that you wish to handle is represented by an EventDrivenActivity. This is a composite activity that triggers execution when an event is received.
The first child of an EventDrivenActivity must support the IEventActivity interface in order to trigger execution of the other child activities when the event is received. When used in a state machine workflow, this typically means that the first event is a HandleExternalEventActivity. Following the HandleExternalEventActivity, you include the other activities that you wish to execute when the event is received.
在工作流中,个别的状态实例是通过StateActivity声明定义的,每个事件都是需要通过EventDrivenActivity来调用和声明的. 当事件(event)接收到通知,这个复合的activity就会被触发执行.为了其他的子activities在事件接受到通知时被触发执行,第一个EventDrivenActivity必须继承 IEventActivity 接口.当使用状态机工作流时,这个特色意味着第一个事件必须是HandleExternalEventActivity 跟随着HandleExternalEventActivity实体,你可以将其他希望这个事件接受到通知后执行的实体加入进来.

The only direct child activities that are allowed for the StateMachineWorkflowActivity are the StateActivity and the EventDrivenActivity. This makes sense based on what you already know about state machine workflows. They contain states and can also directly handle events.
StateMachineWorkflowActivity直系的子实体只有StateActivity和EventDrivenActivity,他们构成了状态机工作流的基础(或者说状态机工作流基于他们两个),他们包含状态并可以直接调用事件

Each StateActivity has a select list of direct child activities that it supports. As I already mentioned,it accepts multiple instances of EventDrivenActivity, with each instance associated with one, and only one, event. A StateActivity also accepts a single StateInitializationActivity and a single StateFinalizationActivity. Both of these activities are composites and are optional.
每个状态活动(StateActivity)都拥有一列它所支持的直系子实体可供使用,就像我以前所提到的,他可以支持若干个EventDrivenActivity实例,并且每个实例有切只能关联一个事件.一个StateActivity活动可以有一个StateInitializationActivity和一个StateFinalizationActivity

If you include a StateInitializationActivity, the activities that it contains are executed as soon as the StateActivity begins execution. This is done before it begins listening for any events that you have defined. If you include a StateFinalizationActivity, it is executed immediately prior to transitioning to another state.
如果你引入了一个StateInitializationActivity活动,那么这个StateActivity一旦开始执行,它所包含的活动都将被执行.在做这些之前,它会首先监听你所定义的所有事件,如果有StateFinalizationActivity活动,它会被立即执行切换到其他状态.

To transition to another state, you use the SetStateActivity. You include the SetStateActivity as the last child of an EventDrivenActivity instance. Since this activity causes a transition to another state, it doesn’t make sense to include other activities following it.
为了切换到其他状态,你可以使用SetStateActivity活动,你可以将SetStateActivity活动作为EventDrivenActivity实例的最后一个子活动,一旦这个活动(SetStateActivity)导致切换到其他状态,它就不在执行紧随其后的其他活动.


As shown in Figure 9-2, a state machine workflow (StateMachineWorkflowActivity) contains one or more instances of StateActivity. Each StateActivity contains one or more EventDrivenActivity instances. Each EventDrivenActivity begins with an activity that is capable of receiving an event (HandleExternalEventActivity). Following this activity, you include other activities that you wish to execute when the event is received. Finally, the last child activity of the EventDrivenActivity can be a SetStateActivity which transitions to another state. A similar stack of EventDrivenActivity instances can also be added directly as children of the state machine workflow. These activities enable handling of events regardless of the current state. The StateInitializationActivity and StateFinalizationActivity are optional activities that are children of the StateActivity.
正如Figure 9-2所展示的,一个状态机工作流(StateMachineWorkflowActivity)可以包括一个或多个StateActivity实例,每 个StateActivity活动又可以包括一个或多个EventDrivenActivity实例,每个EventDrivenActivity活动从 一个可以接收事件的活动(HandleExternalEventActivity)开始.跟随着这个活动 HandleExternalEventActivity),就是这个事件被调用时你希望执行的其他活动.最后, EventDrivenActivity最后一个子活动是可以切换到其他状态的SetStateActivity活动.
类似堆栈的EventDrivenActivity实例可以作为子活动直接添加到状态机工作流中.这些活动(EventDrivenActivity)可 以不管当前的状态去处理事件.StateInitializationActivity和StateFinalizationActivity是 StateActivity可选择的子活动,不是必须的.
图:Figure 9-2


注意:You may notice that the StateMachineWorkflowActivity class derives from StateActivity. This is what enables StateMachineWorkflowActivity to exhibit the behavior of a state, such as handling events and acting as the parent for other states.
一定要注意到状态机工作流活动从StateActivity活动开始

Initial and Completed States
初始化和完成状态
Since a state machine workflow is composed of multiple states, you may be wondering which state
the workflow is in when it first begins execution. The StateMachineWorkflowActivity class includes
an InitialStateName property. This property identifies the initial state and must be set by you when
implementing the workflow. Failure to set this property results in an error when you build the project
containing the workflow.
因为一个状态机工作流是由多个状态组成的,you may be wondering which state the workflow is in when it first begins execution.
状态机工作流类包括一个InitialStateName属性,这个属性确定了初始状态并且在工作流实例化的时候必须设置它(理解的可能不正确),

The StateMachineWorkflowActivity class also includes another important property named
CompletedStateName. This property identifies the state that causes the completion of the workflow.
When you transition to this completion state, this marks the end of the workflow and it stops
execution and completes. A completion state is an ordinary StateActivity, but if you select it in
the CompletedStateName property, it takes on this special meaning. One notable restriction is that a
completion state cannot contain any children. As soon as you transition to this state, the workflow
completes so there is no need to define any children. If any children were defined, they wouldn’t be
executed.
状态机工作流类还包括另外一个重要的属性:CompletedStateName,这个属性确定了工作流完成的原因.
当你执行到这个completion state,他会引起工作流完成.completion state是一个普通的状态活动,但是如果你设置了
CompletedStateName属性,他就拥有了特殊含义.值得注意的是,完成状态不能包含任何子活动.为了你可以切换到这个状态,
并且结束工作流,在完成状态中不能添加任何子活动,如果已经添加了子活动,他们也不会被执行.

The CompletedStateName property is optional. At first, you might think this is a mistake, since
without it the workflow will never complete. This is not a mistake and that is exactly the behavior you
will see if you don’t include a completion state. The workflow will never complete. Instead, it will
continue to transition from state to state and handle the events defined for those states. For some
applications, this is exactly the type of behavior that you want. Obviously, for such long-running
(never ending) workflows, you would want to make sure that you use a persistence service. Otherwise,
the workflow would end prematurely if the host process was ever shut down.
完成状态属性是可选的,开始你可能认为这是一个误解或者错误,因为会造成工作永远不会完成.
这不是一个错误,在工作流中不包括完成状态是一个严密(合法)的行为.工作流将不会完成,但是,他将继续的从一个状态切换
到另一个状态,并调用这些状态所定义的事件.显然,作为一个一直在运行(永远不停止)的工作流,你可以把他作为一个持续的
服务来使用.另外一旦主机被关闭,这个工作流也将被过早结束(译者主:这个应该是有问题的,流程实例可以被序列化到数据库或者
文件系统中,当服务重新启用时,这些流程实例可以被重新加载到工作流中)

Recursive Composition of States
递归组合状态
The StateActivity can also include other instances of StateActivity. This is called recursive composition
of states. The primary use of this technique is to allow handling of selected events from more
than one state. For example, assume that you have two states: state A and state B. You may have an
event that you wish to handle while you are in either state. You have two ways to handle this. Your
first option is to define the event within each state, duplicating the stack of activities you’ve defined
to handle the event. This approach is shown in Figure 9-3.
状态活动(StateActivity)也可以包含其他的StateActivity实例,这被称之为递归组合状态( recursive composition
of states)这里使用的主要技术是允许从一个或多个状态处理所选择的事件(啥意思,可以跨state?).
举个例子,假象你有两个状态:状态A和状态B,你希望有一个方法可以在任何一个状态下都可以处理.你有两个方法来解决这个问题.
一个选择是:在每个状态中都定义这个方法,

Your second option is to enclose both of these states in a new parent state and handle the event
in the parent state. Using this second approach, you could create a new state C, and move states A
and B into it as children. You can then add the EventDrivenActivity to state C to handle the event.
This approach is illustrated in Figure 9-4.
你的第二个选择是:将这两个状态需要处理的事件封装到一个新的父状态中.实现第二个途径,你需要创建一个状态C
并且将状态A和状态B作为子状态添加到C中,并且添加一个EventDrivenActivity活动到状态C来处理事件.

Since it is defined in a common parent state, the event can now be handled when the workflow
is currently in state A or B. Although not shown in Figure 9-4, each of the child states (A and B) can
still contain their own set of events that they alone handle. You will see an example of this type of
state composition in one of the upcoming example workflows.
因为状态C并定义为通用的父状态,工作流中的状态A和B都可以处理这些事件.尽管没有在在Figure 9-4展示,每个子状态(A和B)
都可以包含自己的事件来实现特有的处理.不久就将看到关于这方面的例子.

When you transition to another state using SetStateActivity, you can only transition to leaf
states. Leaf states are those that do not contain other states. Using the previous example, you could
transition to state A or B but not to the parent state C since it contains other states. The parent state
(C in this example) is also not a valid candidate state for the InitialStateName and CompletedStateName
properties of the state machine workflow. Since you can’t transition to a composite parent state, you are
not allowed to designate it as the initial or completed state.
当你需要切换到其他状态时可以使用SetStateActivity活动.你可以只切换到leaf states(leaf states 啥意思????)
Leaf states 不能包含其他的状态(应该是子叶状态,就是没有任何外延的状态,nnd说的还是不太清晰).
使用前面的例子,你可以切换到A或者B但是不能切换到C,因为C包含了其他状态.这个父状态(例子中的状态C)不能设置
InitialStateName and CompletedStateName属性,因为你不能切换到组合父状态,也就不能把它制定为initial 或者 completed状态


posted on 2007-11-10 18:25  我是蚂蚁  阅读(1961)  评论(2编辑  收藏  举报