-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandmade_fsm.cpp
More file actions
129 lines (109 loc) · 2.88 KB
/
handmade_fsm.cpp
File metadata and controls
129 lines (109 loc) · 2.88 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
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
// states
enum class State { OffHook, Connecting, Connected, OnHold, OnHook };
// events to triger transitions
enum class Trigger {
CallDialed,
HungUp,
CallConnected,
PlacedOnHold,
TakenOffHold,
LeftMessage,
StopUsingPhone
};
inline ostream &operator<<(ostream &os, const State &s) {
switch (s) {
case State::OffHook:
os << "off the hook";
break;
case State::Connecting:
os << "connecting";
break;
case State::Connected:
os << "connected";
break;
case State::OnHold:
os << "on hold";
break;
case State::OnHook:
os << "on the hook";
break;
}
return os;
}
inline ostream &operator<<(ostream &os, const Trigger &t) {
switch (t) {
case Trigger::CallDialed:
os << "call dialed";
break;
case Trigger::HungUp:
os << "hung up";
break;
case Trigger::CallConnected:
os << "call connected";
break;
case Trigger::PlacedOnHold:
os << "placed on hold";
break;
case Trigger::TakenOffHold:
os << "taken off hold";
break;
case Trigger::LeftMessage:
os << "left message";
break;
case Trigger::StopUsingPhone:
os << "putting phone on hook";
break;
default:
break;
}
return os;
}
typedef pair<Trigger, State> Transition;
typedef vector<Transition> Transitions;
int main() {
// FSM: Each State is connected to some transitions
map<State, Transitions> rules;
rules[State::OffHook] = {{Trigger::CallDialed, State::Connecting},
{Trigger::StopUsingPhone, State::OnHook}};
rules[State::Connecting] = {{Trigger::HungUp, State::OffHook},
{Trigger::CallConnected, State::Connected}};
rules[State::Connected] = {{Trigger::LeftMessage, State::OffHook},
{Trigger::HungUp, State::OffHook},
{Trigger::PlacedOnHold, State::OnHold}};
rules[State::OnHold] = {{Trigger::TakenOffHold, State::Connected},
{Trigger::HungUp, State::OffHook}};
State currentState{State::OffHook};
State exitState{State::OnHook};
while (true) {
cout << "The phone is currently " << currentState << endl;
select_trigger:
// Display available states:
cout << "Select a trigger:"
<< "\n";
int i = 0;
for (auto item : rules[currentState]) {
cout << i++ << ". " << item.first << "\n";
}
// Read selected action
size_t input;
cin >> input;
// handle errors
if ((input + 1) > rules[currentState].size()) {
cout << "Incorrect option. Please try again."
<< "\n";
goto select_trigger;
}
// Update state based on the transition.
currentState = rules[currentState][input].second;
if (currentState == exitState)
break;
}
cout << "We are done using the phone"
<< "\n";
return 0;
}