-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathTransitionLogic.cs
More file actions
265 lines (238 loc) · 12.3 KB
/
TransitionLogic.cs
File metadata and controls
265 lines (238 loc) · 12.3 KB
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
//-------------------------------------------------------------------------------
// <copyright file="TransitionLogic.cs" company="Appccelerate">
// Copyright (c) 2008-2019 Appccelerate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-------------------------------------------------------------------------------
namespace Appccelerate.StateMachine.AsyncMachine.Transitions
{
using System;
using System.Threading.Tasks;
using States;
public class TransitionLogic<TState, TEvent>
: ITransitionLogic<TState, TEvent>
where TState : IComparable
where TEvent : IComparable
{
private readonly IExtensionHost<TState, TEvent> extensionHost;
private readonly IStateMachineInformation<TState, TEvent> stateMachineInformation;
private IStateLogic<TState, TEvent> stateLogic;
public TransitionLogic(
IExtensionHost<TState, TEvent> extensionHost,
IStateMachineInformation<TState, TEvent> stateMachineInformation)
{
this.extensionHost = extensionHost;
this.stateMachineInformation = stateMachineInformation;
}
public void SetStateLogic(IStateLogic<TState, TEvent> stateLogicToSet)
{
this.stateLogic = stateLogicToSet;
}
public async Task<ITransitionResult<TState>> Fire(
ITransitionDefinition<TState, TEvent> transitionDefinition,
ITransitionContext<TState, TEvent> context,
ILastActiveStateModifier<TState, TEvent> lastActiveStateModifier)
{
Guard.AgainstNullArgument("context", context);
var shouldFire = await this.ShouldFire(transitionDefinition, context).ConfigureAwait(false);
if (!shouldFire)
{
await this.extensionHost
.ForEach(extension => extension.SkippedTransition(
this.stateMachineInformation,
transitionDefinition,
context))
.ConfigureAwait(false);
return new NotFiredTransitionResult<TState>();
}
context.OnTransitionBegin();
await this.extensionHost
.ForEach(extension => extension.ExecutingTransition(
this.stateMachineInformation,
transitionDefinition,
context))
.ConfigureAwait(false);
var newState = context.StateDefinition.Id;
if (!transitionDefinition.IsInternalTransition)
{
await this.UnwindSubStates(transitionDefinition, context, lastActiveStateModifier).ConfigureAwait(false);
await this.Fire(transitionDefinition, transitionDefinition.Source, transitionDefinition.Target, context, lastActiveStateModifier)
.ConfigureAwait(false);
newState = await this.stateLogic.EnterByHistory(transitionDefinition.Target, context, lastActiveStateModifier)
.ConfigureAwait(false);
}
else
{
await this.PerformActions(transitionDefinition, context).ConfigureAwait(false);
}
await this.extensionHost
.ForEach(extension => extension.ExecutedTransition(
this.stateMachineInformation,
transitionDefinition,
context))
.ConfigureAwait(false);
return new FiredTransitionResult<TState>(newState);
}
private static void HandleException(Exception exception, ITransitionContext<TState, TEvent> context)
{
context.OnExceptionThrown(exception);
}
/// <summary>
/// Recursively traverses the state hierarchy, exiting states along
/// the way, performing the action, and entering states to the target.
/// </summary>
/// <remarks>
/// There exist the following transition scenarios:
/// 0. there is no target state (internal transition)
/// --> handled outside this method.
/// 1. The source and target state are the same (self transition)
/// --> perform the transition directly:
/// Exit source state, perform transition actions and enter target state
/// 2. The target state is a direct or indirect sub-state of the source state
/// --> perform the transition actions, then traverse the hierarchy
/// from the source state down to the target state,
/// entering each state along the way.
/// No state is exited.
/// 3. The source state is a sub-state of the target state
/// --> traverse the hierarchy from the source up to the target,
/// exiting each state along the way.
/// Then perform transition actions.
/// Finally enter the target state.
/// 4. The source and target state share the same super-state
/// 5. All other scenarios:
/// a. The source and target states reside at the same level in the hierarchy
/// but do not share the same direct super-state
/// --> exit the source state, move up the hierarchy on both sides and enter the target state
/// b. The source state is lower in the hierarchy than the target state
/// --> exit the source state and move up the hierarchy on the source state side
/// c. The target state is lower in the hierarchy than the source state
/// --> move up the hierarchy on the target state side, afterward enter target state.
/// </remarks>
/// <param name="transitionDefinition">The transition definition.</param>
/// <param name="source">The source state.</param>
/// <param name="target">The target state.</param>
/// <param name="context">The event context.</param>
/// <param name="lastActiveStateModifier">The last active state modifier.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
private async Task Fire(
ITransitionDefinition<TState, TEvent> transitionDefinition,
IStateDefinition<TState, TEvent> source,
IStateDefinition<TState, TEvent> target,
ITransitionContext<TState, TEvent> context,
ILastActiveStateModifier<TState, TEvent> lastActiveStateModifier)
{
if (source == transitionDefinition.Target)
{
// Handles 1.
// Handles 3. after traversing from the source to the target.
await this.stateLogic.Exit(source, context, lastActiveStateModifier).ConfigureAwait(false);
await this.PerformActions(transitionDefinition, context).ConfigureAwait(false);
await this.stateLogic.Entry(transitionDefinition.Target, context).ConfigureAwait(false);
}
else if (source == target)
{
// Handles 2. after traversing from the target to the source.
await this.PerformActions(transitionDefinition, context).ConfigureAwait(false);
}
else if (source.SuperState == target.SuperState)
{
//// Handles 4.
//// Handles 5a. after traversing the hierarchy until a common ancestor if found.
await this.stateLogic.Exit(source, context, lastActiveStateModifier).ConfigureAwait(false);
await this.PerformActions(transitionDefinition, context).ConfigureAwait(false);
await this.stateLogic.Entry(target, context).ConfigureAwait(false);
}
else
{
// traverses the hierarchy until one of the above scenarios is met.
// Handles 3.
// Handles 5b.
if (source.Level > target.Level)
{
await this.stateLogic.Exit(source, context, lastActiveStateModifier).ConfigureAwait(false);
await this.Fire(transitionDefinition, source.SuperState, target, context, lastActiveStateModifier).ConfigureAwait(false);
}
else if (source.Level < target.Level)
{
// Handles 2.
// Handles 5c.
await this.Fire(transitionDefinition, source, target.SuperState, context, lastActiveStateModifier).ConfigureAwait(false);
await this.stateLogic.Entry(target, context).ConfigureAwait(false);
}
else
{
// Handles 5a.
await this.stateLogic.Exit(source, context, lastActiveStateModifier).ConfigureAwait(false);
await this.Fire(transitionDefinition, source.SuperState, target.SuperState, context, lastActiveStateModifier).ConfigureAwait(false);
await this.stateLogic.Entry(target, context).ConfigureAwait(false);
}
}
}
private async Task<bool> ShouldFire(
ITransitionDefinition<TState, TEvent> transitionDefinition,
ITransitionContext<TState, TEvent> context)
{
try
{
return
transitionDefinition.Guard == null
|| await transitionDefinition.Guard.Execute(context.EventArgument)
.ConfigureAwait(false);
}
catch (Exception exception)
{
await this.extensionHost
.ForEach(extension => extension.HandlingGuardException(this.stateMachineInformation, transitionDefinition, context, ref exception))
.ConfigureAwait(false);
HandleException(exception, context);
await this.extensionHost
.ForEach(extension => extension.HandledGuardException(this.stateMachineInformation, transitionDefinition, context, exception))
.ConfigureAwait(false);
return false;
}
}
private async Task PerformActions(ITransitionDefinition<TState, TEvent> transitionDefinition, ITransitionContext<TState, TEvent> context)
{
foreach (var action in transitionDefinition.Actions)
{
try
{
await action.Execute(context.EventArgument).ConfigureAwait(false);
}
catch (Exception exception)
{
await this.extensionHost
.ForEach(extension => extension.HandlingTransitionException(this.stateMachineInformation, transitionDefinition, context, ref exception))
.ConfigureAwait(false);
HandleException(exception, context);
await this.extensionHost
.ForEach(extension => extension.HandledTransitionException(this.stateMachineInformation, transitionDefinition, context, exception))
.ConfigureAwait(false);
}
}
}
private async Task UnwindSubStates(
ITransitionDefinition<TState, TEvent> transitionDefinition,
ITransitionContext<TState, TEvent> context,
ILastActiveStateModifier<TState, TEvent> lastActiveStateModifier)
{
var o = context.StateDefinition;
while (o != transitionDefinition.Source)
{
await this.stateLogic.Exit(o, context, lastActiveStateModifier).ConfigureAwait(false);
o = o.SuperState;
}
}
}
}