forked from dotnet-state-machine/stateless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (104 loc) · 3.91 KB
/
Copy pathProgram.cs
File metadata and controls
115 lines (104 loc) · 3.91 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
namespace AlarmExample
{
/// <summary>
/// A simple Console Application that allows for interactive input
/// to test the <see cref="Alarm"/> implemented as a Stateless state
/// machine.
/// </summary>
internal class Program
{
static Alarm? _alarm;
static void Main(string[] args)
{
_alarm = new Alarm(10, 10, 10, 10);
string input = "";
WriteHeader();
while (input != "q")
{
Console.Write("> ");
input = Console.ReadLine()!;
if (!string.IsNullOrWhiteSpace(input))
switch (input.Split(" ")[0])
{
case "q":
Console.WriteLine("Exiting...");
break;
case "fire":
WriteFire(input);
break;
case "canfire":
WriteCanFire();
break;
case "state":
WriteState();
break;
case "h":
case "help":
WriteHelp();
break;
case "c":
case "clear":
Console.Clear();
WriteHeader();
break;
default:
Console.WriteLine("Invalid command. Type 'h' or 'help' for valid commands.");
break;
}
}
}
static void WriteHelp()
{
Console.WriteLine("Valid commands:");
Console.WriteLine("q - Exit");
Console.WriteLine("fire <state> - Tries to fire the provided commands");
Console.WriteLine("canfire <state> - Returns a list of fireable commands");
Console.WriteLine("state - Returns the current state");
Console.WriteLine("c / clear - Clear the window");
Console.WriteLine("h / help - Show this again");
}
static void WriteHeader()
{
Console.WriteLine("Stateless-based alarm test application:");
Console.WriteLine("---------------------------------------");
Console.WriteLine("");
}
static void WriteCanFire()
{
foreach (AlarmCommand command in (AlarmCommand[])Enum.GetValues(typeof(AlarmCommand)))
if (_alarm != null && _alarm.CanFireCommand(command))
Console.WriteLine($"{Enum.GetName(typeof(AlarmCommand), command)}");
}
static void WriteState()
{
if(_alarm != null )
Console.WriteLine($"The current state is {Enum.GetName(typeof(AlarmState), _alarm.CurrentState())}");
}
static void WriteFire(string input)
{
if (input.Split(" ").Length == 2)
{
try
{
if (Enum.TryParse(input.Split(" ")[1], out AlarmCommand command))
{
if (_alarm != null)
_alarm.ExecuteTransition(command);
}
else
{
Console.WriteLine($"{input.Split(" ")[1]} is not a valid AlarmCommand.");
}
}
catch (InvalidOperationException)
{
Console.WriteLine($"{input.Split(" ")[1]} is not a valid AlarmCommand to the current state.");
}
}
else
{
Console.WriteLine("fire requires you to specify the command you want to fire.");
}
}
}
}