File: StateMachineExecutionState.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (268 lines) | stat: -rw-r--r-- 8,149 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;

#endregion Using directives

namespace System.Workflow.Activities
{
    [Serializable]
    internal class StateMachineExecutionState
    {
        #region Member Variables

        internal const string StateMachineExecutionStateKey = "StateMachineExecutionState";
        private StateMachineSubscriptionManager _subscriptionManager;
        private Queue<StateMachineAction> _actions;
        private string _currentStateName;
        private string _previousStateName;
        private string _nextStateName;
        private bool _completed = false;
        private bool _queueLocked = false;
        private bool _schedulerBusy = false;

        #endregion Member Variables

        #region Properties

        internal StateMachineSubscriptionManager SubscriptionManager
        {
            get
            {
                return _subscriptionManager;
            }
        }

        private Queue<StateMachineAction> Actions
        {
            get
            {
                if (_actions == null)
                    _actions = new Queue<StateMachineAction>();
                return _actions;
            }
        }

        internal bool SchedulerBusy
        {
            get
            {
                return _schedulerBusy;
            }
            set
            {
                _schedulerBusy = value;
            }
        }

        internal string CurrentStateName
        {
            get
            {
                return _currentStateName;
            }
            set
            {
                _currentStateName = value;
            }
        }

        internal string PreviousStateName
        {
            get
            {
                return _previousStateName;
            }
            set
            {
                _previousStateName = value;
            }
        }

        internal string NextStateName
        {
            get
            {
                return _nextStateName;
            }
            set
            {
                _nextStateName = value;
            }
        }

        internal bool Completed
        {
            get
            {
                return _completed;
            }
            set
            {
                _completed = value;
            }
        }

        internal bool HasEnqueuedActions
        {
            get
            {
                return this.Actions.Count > 0;
            }
        }

        #endregion Properties

        #region Constructors

        internal StateMachineExecutionState(Guid instanceId)
        {
            _subscriptionManager = new StateMachineSubscriptionManager(this, instanceId);
        }

        #endregion

        internal void LockQueue()
        {
            _queueLocked = true;
        }

        internal void EnqueueAction(StateMachineAction action)
        {
            Debug.Assert(!this._queueLocked);
            this.Actions.Enqueue(action);
        }

        internal StateMachineAction DequeueAction()
        {
            StateMachineAction action = this.Actions.Dequeue();
            if (this.Actions.Count == 0)
                _queueLocked = false;
            return action;
        }

        internal void ProcessActions(ActivityExecutionContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (this.SchedulerBusy)
                return;

            StateActivity state = (StateActivity)context.Activity;

            if (this.Actions.Count == 0)
            {
                this.SubscriptionManager.ProcessQueue(context);
                return;
            }

            StateMachineAction action = this.Actions.Peek();
            while (action.StateName.Equals(state.QualifiedName))
            {
                action = DequeueAction();
                action.Execute(context);

                // If the previous action just
                // requested something to the runtime
                // scheduler, then we quit, since
                // the scheduler takes precedence.
                // we'll pick up the processing of actions
                // after the scheduler return the control to us.
                if (this.SchedulerBusy)
                    return;

                if (this.Actions.Count == 0)
                    break;

                action = this.Actions.Peek();
            }

            if (this.Actions.Count > 0)
            {
                StateActivity rootState = StateMachineHelpers.GetRootState(state);
                StateActivity nextActionState = StateMachineHelpers.FindDynamicStateByName(rootState, action.StateName);
                if (nextActionState == null)
                    throw new InvalidOperationException(SR.GetInvalidStateMachineAction(action.StateName));

                nextActionState.RaiseProcessActionEvent(context);
            }
            else
            {
                this.SubscriptionManager.ProcessQueue(context);
            }
        }


        internal void ProcessTransitionRequest(ActivityExecutionContext context)
        {
            if (String.IsNullOrEmpty(this.NextStateName))
                return;

            StateActivity currentState = StateMachineHelpers.GetCurrentState(context);
            CalculateStateTransition(currentState, this.NextStateName);
            LockQueue();
            this.NextStateName = null;
        }

        internal void CalculateStateTransition(StateActivity currentState, string targetStateName)
        {
            if (currentState == null)
                throw new ArgumentNullException("currentState");
            if (String.IsNullOrEmpty(targetStateName))
                throw new ArgumentNullException("targetStateName");

            while (currentState != null && (currentState.QualifiedName.Equals(targetStateName) || !StateMachineHelpers.ContainsState(currentState, targetStateName)))
            {
                CloseStateAction action = new CloseStateAction(currentState.QualifiedName);
                this.Actions.Enqueue(action);
                currentState = currentState.Parent as StateActivity;
            }
            if (currentState == null)
                throw new InvalidOperationException(SR.GetUnableToTransitionToState(targetStateName));

            while (!currentState.QualifiedName.Equals(targetStateName))
            {
                foreach (Activity childActivity in currentState.EnabledActivities)
                {
                    StateActivity childState = childActivity as StateActivity;
                    if (childState != null)
                    {
                        // 
                        if (StateMachineHelpers.ContainsState(childState, targetStateName))
                        {
                            ExecuteChildStateAction action = new ExecuteChildStateAction(currentState.QualifiedName, childState.QualifiedName);
                            this.Actions.Enqueue(action);
                            currentState = childState;
                            break;
                        }
                    }
                }
            }
            if (!StateMachineHelpers.IsLeafState(currentState))
                throw new InvalidOperationException(SR.GetInvalidStateTransitionPath());
        }

        #region Static Methods

        internal static StateMachineExecutionState Get(StateActivity state)
        {
            Debug.Assert(StateMachineHelpers.IsRootState(state));
            StateMachineExecutionState executionState = (StateMachineExecutionState)state.GetValue(StateActivity.StateMachineExecutionStateProperty);
            Debug.Assert(executionState != null);
            return executionState;
        }

        #endregion
    }
}