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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#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
{
internal static class StateMachineHelpers
{
internal static bool IsStateMachine(StateActivity state)
{
if (state == null)
throw new ArgumentNullException("state");
return (state is StateMachineWorkflowActivity);
}
internal static bool IsRootState(StateActivity state)
{
if (state == null)
throw new ArgumentNullException("state");
StateActivity parent = state.Parent as StateActivity;
return parent == null;
}
internal static bool IsLeafState(StateActivity state)
{
if (state == null)
throw new ArgumentNullException("state");
if (IsRootState(state))
return false;
foreach (Activity child in state.EnabledActivities)
{
if (child is StateActivity)
return false;
}
return true;
}
internal static bool IsRootExecutionContext(ActivityExecutionContext context)
{
return (context.Activity.Parent == null);
}
/// <summary>
/// Finds the enclosing state for this activity
/// </summary>
/// <param name="activity"></param>
/// <returns></returns>
internal static StateActivity FindEnclosingState(Activity activity)
{
Debug.Assert(activity != null);
Debug.Assert(activity.Parent != activity);
StateActivity state = activity as StateActivity;
if (state != null)
return state;
if (activity.Parent == null)
return null;
return FindEnclosingState(activity.Parent);
}
/// <summary>
/// Returns the root State activity
/// </summary>
/// <param name="state"></param>
/// <returns></returns>
internal static StateActivity GetRootState(StateActivity state)
{
Debug.Assert(state != null);
Debug.Assert(state.Parent != state);
if (state.Parent == null)
return state;
// this handles the case when the StateMachineWorkflow
// is called using an Invoke activity
if (!(state.Parent is StateActivity))
return state;
return GetRootState((StateActivity)state.Parent);
}
internal static bool IsInitialState(StateActivity state)
{
Debug.Assert(state != null);
string initialStateName = GetInitialStateName(state);
if (initialStateName == null)
return false;
return state.QualifiedName.Equals(initialStateName);
}
internal static bool IsCompletedState(StateActivity state)
{
Debug.Assert(state != null);
string completedStateName = GetCompletedStateName(state);
if (completedStateName == null)
return false;
return state.QualifiedName.Equals(completedStateName);
}
internal static string GetInitialStateName(StateActivity state)
{
StateActivity rootState = GetRootState(state);
return (string)rootState.GetValue(StateMachineWorkflowActivity.InitialStateNameProperty);
}
internal static string GetCompletedStateName(StateActivity state)
{
Debug.Assert(state != null);
StateActivity rootState = GetRootState(state);
return (string)rootState.GetValue(StateMachineWorkflowActivity.CompletedStateNameProperty);
}
/*
internal static bool IsInInitialStatePath(StateActivity state)
{
StateActivity rootState = GetRootState(state);
string initialStateName = GetInitialStateName(rootState);
StateActivity initialState = FindStateByName(rootState, initialStateName);
CompositeActivity current = initialState;
while (current != null)
{
if (current.QualifiedName == state.QualifiedName)
return true;
current = current.Parent;
}
return false;
}
*/
/// <summary>
/// Returns the State activity that is currently executing
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
static internal StateActivity GetCurrentState(ActivityExecutionContext context)
{
StateActivity state = context.Activity as StateActivity;
if (state == null)
state = FindEnclosingState(context.Activity);
Debug.Assert(state != null, "StateMachineHelpers.GetCurrentState: only valid to call this method from a State executor or a contained EventDriven");
StateActivity rootState = GetRootState(state);
StateMachineExecutionState executionState = StateMachineExecutionState.Get(rootState);
string currentStateName = executionState.CurrentStateName;
if (currentStateName == null)
return null;
StateActivity currentState = FindDynamicStateByName(rootState, currentStateName);
Debug.Assert(currentState == null || IsLeafState(currentState));
return currentState;
}
static internal StateActivity FindDynamicStateByName(StateActivity state, string stateQualifiedName)
{
while (!state.QualifiedName.Equals(stateQualifiedName) && ContainsState(state, stateQualifiedName))
{
foreach (Activity activity in state.EnabledActivities)
{
StateActivity childState = activity as StateActivity;
if (childState == null)
continue;
if (ContainsState(childState, stateQualifiedName))
{
StateActivity dynamicChildState = (StateActivity)state.GetDynamicActivity(childState);
if (dynamicChildState == null)
return null;
state = dynamicChildState;
break;
}
}
}
if (state.QualifiedName.Equals(stateQualifiedName))
return state;
else
return null;
}
static internal StateActivity FindStateByName(StateActivity state, string qualifiedName)
{
Debug.Assert(state != null);
Debug.Assert(qualifiedName != null);
StateActivity found = FindActivityByName(state, qualifiedName) as StateActivity;
return found;
}
static internal Activity FindActivityByName(CompositeActivity parentActivity, string qualifiedName)
{
return parentActivity.GetActivityByName(qualifiedName, true);
}
static internal bool ContainsEventActivity(CompositeActivity compositeActivity)
{
Debug.Assert(compositeActivity != null);
Queue<Activity> activities = new Queue<Activity>();
activities.Enqueue(compositeActivity);
while (activities.Count > 0)
{
Activity activity = activities.Dequeue();
if (activity is IEventActivity)
return true;
compositeActivity = activity as CompositeActivity;
if (compositeActivity != null)
{
foreach (Activity child in compositeActivity.Activities)
{
if (child.Enabled)
activities.Enqueue(child);
}
}
}
return false;
}
static internal IEventActivity GetEventActivity(EventDrivenActivity eventDriven)
{
CompositeActivity sequenceActivity = eventDriven as CompositeActivity;
Debug.Assert(eventDriven.EnabledActivities.Count > 0);
IEventActivity eventActivity = sequenceActivity.EnabledActivities[0] as IEventActivity;
Debug.Assert(eventActivity != null);
return eventActivity;
}
static internal EventDrivenActivity GetParentEventDriven(IEventActivity eventActivity)
{
Activity activity = ((Activity)eventActivity).Parent;
while (activity != null)
{
EventDrivenActivity eventDriven = activity as EventDrivenActivity;
if (eventDriven != null)
return eventDriven;
activity = activity.Parent;
}
return null;
}
static internal bool ContainsState(StateActivity state, string stateName)
{
if (state == null)
throw new ArgumentNullException("state");
if (String.IsNullOrEmpty(stateName))
throw new ArgumentNullException("stateName");
Queue<StateActivity> states = new Queue<StateActivity>();
states.Enqueue(state);
while (states.Count > 0)
{
state = states.Dequeue();
if (state.QualifiedName.Equals(stateName))
return true;
foreach (Activity childActivity in state.EnabledActivities)
{
StateActivity childState = childActivity as StateActivity;
if (childState != null)
{
states.Enqueue(childState);
}
}
}
return false;
}
}
#region StateMachineMessages
#if DEBUG
/*
* this is only used for testing the State Machine related resource messages
*
internal class StateMachineMessages
{
internal static void PrintMessages()
{
Console.WriteLine("GetInvalidUserDataInStateChangeTrackingRecord: {0}\n", SR.GetInvalidUserDataInStateChangeTrackingRecord());
Console.WriteLine("GetError_EventDrivenInvalidFirstActivity: {0}\n", SR.GetError_EventDrivenInvalidFirstActivity());
Console.WriteLine("GetError_InvalidLeafStateChild: {0}\n", SR.GetError_InvalidLeafStateChild());
Console.WriteLine("GetError_InvalidCompositeStateChild: {0}\n", SR.GetError_InvalidCompositeStateChild());
Console.WriteLine("GetError_SetStateOnlyWorksOnStateMachineWorkflow: {0}\n", SR.GetError_SetStateOnlyWorksOnStateMachineWorkflow());
Console.WriteLine("GetError_SetStateMustPointToAState: {0}\n", SR.GetError_SetStateMustPointToAState());
Console.WriteLine("GetError_InitialStateMustPointToAState: {0}\n", SR.GetError_InitialStateMustPointToAState());
Console.WriteLine("GetError_CompletedStateMustPointToAState: {0}\n", SR.GetError_CompletedStateMustPointToAState());
Console.WriteLine("GetError_SetStateMustPointToALeafNodeState: {0}\n", SR.GetError_SetStateMustPointToALeafNodeState());
Console.WriteLine("GetError_InitialStateMustPointToALeafNodeState: {0}\n", SR.GetError_InitialStateMustPointToALeafNodeState());
Console.WriteLine("GetError_CompletedStateMustPointToALeafNodeState: {0}\n", SR.GetError_CompletedStateMustPointToALeafNodeState());
Console.WriteLine("GetError_StateInitializationParentNotState: {0}\n", SR.GetError_StateInitializationParentNotState());
Console.WriteLine("GetError_StateFinalizationParentNotState: {0}\n", SR.GetError_StateFinalizationParentNotState());
Console.WriteLine("GetError_EventActivityNotValidInStateInitialization: {0}\n", SR.GetError_EventActivityNotValidInStateInitialization());
Console.WriteLine("GetError_EventActivityNotValidInStateFinalization: {0}\n", SR.GetError_EventActivityNotValidInStateFinalization());
Console.WriteLine("GetError_MultipleStateInitializationActivities: {0}\n", SR.GetError_MultipleStateInitializationActivities());
Console.WriteLine("GetError_MultipleStateFinalizationActivities: {0}\n", SR.GetError_MultipleStateFinalizationActivities());
Console.WriteLine("GetError_InvalidTargetStateInStateInitialization: {0}\n", SR.GetError_InvalidTargetStateInStateInitialization());
Console.WriteLine("GetError_CantRemoveState: {0}\n", SR.GetError_CantRemoveState());
Console.WriteLine("GetSqlTrackingServiceRequired: {0}\n", SR.GetSqlTrackingServiceRequired());
Console.WriteLine("GetStateMachineWorkflowMustHaveACurrentState: {0}\n", SR.GetStateMachineWorkflowMustHaveACurrentState());
Console.WriteLine("GetInvalidActivityStatus: {0}\n", SR.GetInvalidActivityStatus(new Activity("Hello")));
Console.WriteLine("GetStateMachineWorkflowRequired: {0}\n", SR.GetStateMachineWorkflowRequired());
Console.WriteLine("GetError_EventDrivenParentNotListen: {0}\n", SR.GetError_EventDrivenParentNotListen());
Console.WriteLine("GetGetUnableToTransitionToState: {0}\n", SR.GetUnableToTransitionToState("StateName"));
Console.WriteLine("GetInvalidStateTransitionPath: {0}\n", SR.GetInvalidStateTransitionPath());
Console.WriteLine("GetInvalidSetStateInStateInitialization: {0}\n", SR.GetInvalidSetStateInStateInitialization());
Console.WriteLine("GetStateAlreadySubscribesToThisEvent: {0}\n", SR.GetStateAlreadySubscribesToThisEvent("StateName", "QueueName"));
}
}
*/
#endif
#endregion
}
|