forked from microsoft/coyote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigator.cs
297 lines (251 loc) · 10.7 KB
/
Navigator.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Coyote.Actors;
using Microsoft.Coyote.Samples.Common;
using Microsoft.Coyote.Specifications;
namespace Microsoft.Coyote.Samples.DrinksServingRobot
{
/// <summary>
/// The Navigator state machine manages the handling of a long running request to serve a drink
/// to someone in a camera picture. The idea is the robot sees someone who wants a drink, and
/// goes about making this happen. The Navigator uses MockStorage to persist this operation
/// so that it can be fault tolerant in the light of "failover" of the Navigator.
/// </summary>
internal class Navigator : StateMachine
{
private ActorId CreatorId;
private ActorId RobotId;
private ActorId StorageId;
private ActorId CognitiveServiceId;
private ActorId RoutePlannerServiceId;
private bool Terminating;
private readonly LogWriter Log = LogWriter.Instance;
private const string DrinkOrderStorageKey = "DrinkOrderStorageKey";
internal class NavigatorConfigEvent : Event
{
public ActorId CreatorId;
public ActorId StorageId;
public ActorId CognitiveServiceId;
public ActorId RoutePlannerId;
public NavigatorConfigEvent(ActorId creatorId, ActorId storageId, ActorId cognitiveServiceId, ActorId routePlannerId)
{
this.CreatorId = creatorId;
this.StorageId = storageId;
this.CognitiveServiceId = cognitiveServiceId;
this.RoutePlannerId = routePlannerId;
}
}
internal class TerminateEvent : Event { }
internal class GetDrinkOrderEvent : Event
{
public RoomPicture Picture;
public GetDrinkOrderEvent(RoomPicture picture)
{
this.Picture = picture;
}
}
internal class DrinkOrderConfirmedEvent : Event
{
}
internal class DrinkOrderProducedEvent : Event
{
public DrinkOrder DrinkOrder;
public DrinkOrderProducedEvent(DrinkOrder drinkOrder)
{
this.DrinkOrder = drinkOrder;
}
}
internal class GetDrivingInstructionsEvent : Event
{
public readonly Location StartPoint;
public readonly Location EndPoint;
public GetDrivingInstructionsEvent(Location startPoint, Location endPoint)
{
this.StartPoint = startPoint;
this.EndPoint = endPoint;
}
}
internal class HaltedEvent : Event { }
[Start]
[OnEntry(nameof(OnInit))]
[OnEventDoAction(typeof(TerminateEvent), nameof(OnTerminate))]
[DeferEvents(typeof(WakeUpEvent), typeof(GetDrinkOrderEvent), typeof(GetDrivingInstructionsEvent))]
internal class Init : State { }
internal void OnInit(Event e)
{
if (e is NavigatorConfigEvent configEvent)
{
this.CreatorId = configEvent.CreatorId;
this.StorageId = configEvent.StorageId;
this.CognitiveServiceId = configEvent.CognitiveServiceId;
this.RoutePlannerServiceId = configEvent.RoutePlannerId;
}
this.RaisePushStateEvent<Paused>();
}
private void SaveGetDrinkOrderEvent(GetDrinkOrderEvent e)
{
this.SendEvent(this.StorageId, new KeyValueEvent(this.Id, DrinkOrderStorageKey, e));
}
internal class WakeUpEvent : Event
{
internal readonly ActorId ClientId;
public WakeUpEvent(ActorId clientId)
{
this.ClientId = clientId;
}
}
internal class RegisterNavigatorEvent : Event
{
internal ActorId NewNavigatorId;
public RegisterNavigatorEvent(ActorId newNavigatorId)
{
this.NewNavigatorId = newNavigatorId;
}
}
[OnEventDoAction(typeof(WakeUpEvent), nameof(OnWakeUp))]
[OnEventDoAction(typeof(KeyValueEvent), nameof(RestartPendingJob))]
[DeferEvents(typeof(TerminateEvent), typeof(GetDrinkOrderEvent), typeof(GetDrivingInstructionsEvent))]
internal class Paused : State { }
private void OnWakeUp(Event e)
{
this.Log.WriteLine("<Navigator> starting");
if (e is WakeUpEvent wpe)
{
this.Log.WriteLine("<Navigator> Got RobotId");
this.RobotId = wpe.ClientId;
// tell this client robot about this new navigator. During failover testing
// of the Navigator, this can be swapping out the Navigator that the robot is using.
this.SendEvent(this.RobotId, new RegisterNavigatorEvent(this.Id));
}
// Check storage to see if we have a pending request already.
this.SendEvent(this.StorageId, new ReadKeyEvent(this.Id, DrinkOrderStorageKey));
}
internal void RestartPendingJob(Event e)
{
if (e is KeyValueEvent kve)
{
var key = kve.Key;
object value = kve.Value;
Specification.Assert(key != null, $"Error: KeyValueEvent contains a null key");
if (key == DrinkOrderStorageKey)
{
this.RestartPendingGetDrinkOrderRequest(value as GetDrinkOrderEvent);
}
this.RaiseGotoStateEvent<Active>();
}
}
private void RestartPendingGetDrinkOrderRequest(GetDrinkOrderEvent e)
{
if (e != null)
{
this.ProcessDrinkOrder(e);
this.Log.WriteLine("<Navigator> Restarting the pending Robot's request to find drink clients ...");
}
else
{
this.Log.WriteLine("<Navigator> There was no prior pending request to find drink clients ...");
}
}
[OnEntry(nameof(InitActive))]
[OnEventDoAction(typeof(GetDrinkOrderEvent), nameof(GetDrinkOrder))]
[OnEventDoAction(typeof(ConfirmedEvent), nameof(OnStorageConfirmed))]
[OnEventDoAction(typeof(GetDrivingInstructionsEvent), nameof(GetDrivingInstructions))]
[OnEventDoAction(typeof(DrinksClientDetailsEvent), nameof(SendClientDetailsToRobot))]
[OnEventDoAction(typeof(DrivingInstructionsEvent), nameof(SendDrivingInstructionsToRobot))]
[IgnoreEvents(typeof(KeyValueEvent))]
internal class Active : State { }
private void InitActive()
{
this.Log.WriteLine("<Navigator> initialized.");
}
private void GetDrinkOrder(Event e)
{
if (e is GetDrinkOrderEvent getDrinkOrderEvent)
{
this.SaveGetDrinkOrderEvent(getDrinkOrderEvent);
}
}
private void OnStorageConfirmed(Event e)
{
if (e is ConfirmedEvent ce && ce.Key == DrinkOrderStorageKey)
{
Specification.Assert(
!ce.Existing,
$"Error: The storage `{DrinkOrderStorageKey}` was already set which means we lost a GetDrinkOrderEvent");
this.SendEvent(this.RobotId, new DrinkOrderConfirmedEvent());
this.ProcessDrinkOrder(ce.Value as GetDrinkOrderEvent);
}
}
private void ProcessDrinkOrder(GetDrinkOrderEvent e)
{
// continue on...
var picture = e.Picture;
this.SendEvent(this.CognitiveServiceId, new RecognizeDrinksClientEvent(this.Id, picture));
}
private void SendClientDetailsToRobot(Event e)
{
// When the cognitive service recognizes someone in the picture it sends us a
// DrinksClientDetailsEvent containing information about who is in the picture and where
// they are located.
if (e is DrinksClientDetailsEvent drinksClientDetailsEvent)
{
var details = drinksClientDetailsEvent.Details;
this.SendEvent(this.RobotId, new DrinkOrderProducedEvent(new DrinkOrder(details)));
}
}
private void GetDrivingInstructions(Event e)
{
// When the DrinkOrderProducedEvent is received by the Robot it calls back with
// this event to request driving instructions. This operation is not restartable. Instead,
// during failover of the navigator the robot will re-request any driving instructions.
if (e is GetDrivingInstructionsEvent getDrivingInstructionsEvent)
{
this.ProcessDrivingInstructions(getDrivingInstructionsEvent);
}
}
private void SendDrivingInstructionsToRobot(Event e)
{
if (e is DrivingInstructionsEvent drivingInstructionsEvent)
{
this.SendEvent(this.RobotId, drivingInstructionsEvent);
// The drink order is now completed, so we can delete the persistent job.
this.Log.WriteLine("<Navigator> drink order is complete, deleting the job record.");
this.SendEvent(this.StorageId, new DeleteKeyEvent(this.Id, DrinkOrderStorageKey));
}
}
private void ProcessDrivingInstructions(GetDrivingInstructionsEvent e)
{
this.SendEvent(this.RoutePlannerServiceId, new GetRouteEvent(this.Id, e.StartPoint, e.EndPoint));
}
private void OnTerminate(Event e)
{
if (e is TerminateEvent)
{
this.TerminateMyself();
}
}
private void TerminateMyself()
{
if (!this.Terminating)
{
this.Terminating = true;
this.Log.WriteLine("<Navigator> Terminating as previously ordered ...");
this.SendEvent(this.CognitiveServiceId, HaltEvent.Instance);
this.SendEvent(this.RoutePlannerServiceId, HaltEvent.Instance);
this.CognitiveServiceId = this.RoutePlannerServiceId = null;
this.Log.WriteLine("<Navigator> Sent Termination Confirmation to my Creator ...");
this.SendEvent(this.CreatorId, new HaltedEvent());
this.Log.WriteLine("<Navigator> Halting now ...");
this.RaiseHaltEvent();
}
}
protected override Task OnEventUnhandledAsync(Event e, string state)
{
// this can be handy for debugging.
return base.OnEventUnhandledAsync(e, state);
}
}
}