-
Notifications
You must be signed in to change notification settings - Fork 0
/
Elevator.cs
147 lines (121 loc) · 4.01 KB
/
Elevator.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
using Appccelerate.StateMachine.Machine;
using Appccelerate.StateMachine;
using Appccelerate.StateMachine.Machine.Reports;
using System;
using System.IO;
namespace appccelerate_statemachine_plantuml_rapport
{
public class Elevator
{
private readonly PassiveStateMachine<States, Events> elevator;
private enum States
{
Healthy,
OnFloor,
Moving,
MovingUp,
MovingDown,
DoorOpen,
DoorClosed,
Error
}
private enum Events
{
GoUp,
GoDown,
OpenDoor,
CloseDoor,
Stop,
Error,
Reset
}
public Elevator()
{
var builder = new StateMachineDefinitionBuilder<States, Events>();
builder.DefineHierarchyOn(States.Healthy)
.WithHistoryType(HistoryType.Deep)
.WithInitialSubState(States.OnFloor)
.WithSubState(States.Moving);
builder.DefineHierarchyOn(States.Moving)
.WithHistoryType(HistoryType.Shallow)
.WithInitialSubState(States.MovingUp)
.WithSubState(States.MovingDown);
builder.DefineHierarchyOn(States.OnFloor)
.WithHistoryType(HistoryType.None)
.WithInitialSubState(States.DoorClosed)
.WithSubState(States.DoorOpen);
builder.In(States.Healthy)
.On(Events.Error).Goto(States.Error);
builder.In(States.Error)
.On(Events.Reset).Goto(States.Healthy)
.On(Events.Error);
builder.In(States.OnFloor)
.ExecuteOnEntry(this.AnnounceFloor)
.ExecuteOnExit(Beep)
.ExecuteOnExit(Beep) // just beep a second time
.On(Events.CloseDoor).Goto(States.DoorClosed)
.On(Events.OpenDoor).Goto(States.DoorOpen)
.On(Events.GoUp)
.If(CheckOverload).Goto(States.MovingUp)
.Otherwise().Execute(this.AnnounceOverload)
.On(Events.GoDown)
.If(CheckOverload).Goto(States.MovingDown)
.Otherwise().Execute(this.AnnounceOverload);
builder.In(States.Moving)
.On(Events.Stop).Goto(States.OnFloor);
builder.WithInitialState(States.OnFloor);
var definition = builder
.Build();
elevator = definition
.CreatePassiveStateMachine("Elevator");
elevator.Start();
}
public void WritePlantumlRapport()
{
Console.WriteLine("Writing plantuml rapport...");
var plant = new PlantumlStateMachineReport<States, Events>(File.CreateText(elevator.ToString() + ".plantuml"));
elevator.Report(plant);
}
public void GoToUpperLevel()
{
this.elevator.Fire(Events.CloseDoor);
this.elevator.Fire(Events.GoUp);
this.elevator.Fire(Events.OpenDoor);
}
public void GoToLowerLevel()
{
this.elevator.Fire(Events.CloseDoor);
this.elevator.Fire(Events.GoDown);
this.elevator.Fire(Events.OpenDoor);
}
public void Error()
{
this.elevator.Fire(Events.Error);
}
public void Stop()
{
this.elevator.Fire(Events.Stop);
}
public void Reset()
{
this.elevator.Fire(Events.Reset);
}
private void AnnounceFloor()
{
/* announce floor number */
Console.WriteLine("Announcement: floor number");
}
private void AnnounceOverload()
{
Console.WriteLine("Announcement: overload");
}
private void Beep()
{
Console.WriteLine("Beep!");
}
private bool CheckOverload()
{
return false;
}
}
}