-
Notifications
You must be signed in to change notification settings - Fork 7
/
Stateless.linq
177 lines (143 loc) · 3.67 KB
/
Stateless.linq
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
<Query Kind="Program">
<NuGetReference>stateless</NuGetReference>
<Namespace>Stateless</Namespace>
<Namespace>Stateless.Graph</Namespace>
</Query>
void Main()
{
var phoneCall = new PhoneCall("Jaxel");
phoneCall.Print();
phoneCall.Dialed("Karla");
phoneCall.Print();
phoneCall.Connected();
phoneCall.Print();
phoneCall.SetVolume(2);
phoneCall.Print();
phoneCall.Hold();
phoneCall.Print();
phoneCall.Mute();
phoneCall.Print();
phoneCall.Unmute();
phoneCall.Print();
phoneCall.Resume();
phoneCall.Print();
phoneCall.SetVolume(11);
phoneCall.Print();
phoneCall.ToDotGraph().Dump();
}
// You can define other methods, fields, classes and namespaces here
public class PhoneCall
{
enum Trigger
{
CallDialed,
CallConnected,
LeftMessage,
PlacedOnHold,
TakenOffHold,
PhoneHurledAgainstWall,
MuteMicrophone,
UnmuteMicrophone,
SetVolume
}
enum State
{
OffHook,
Ringing,
Connected,
OnHold,
PhoneDestroyed
}
State _state = State.OffHook;
StateMachine<State, Trigger> _machine;
StateMachine<State, Trigger>.TriggerWithParameters<int> _setVolumeTrigger;
StateMachine<State, Trigger>.TriggerWithParameters<string> _setCalleeTrigger;
string _caller;
string _callee;
public PhoneCall(string caller)
{
_caller = caller;
_machine = new StateMachine<State, Trigger>(() => _state, s => _state = s);
_setVolumeTrigger = _machine.SetTriggerParameters<int>(Trigger.SetVolume);
_setCalleeTrigger = _machine.SetTriggerParameters<string>(Trigger.CallDialed);
_machine.Configure(State.OffHook)
.Permit(Trigger.CallDialed, State.Ringing);
_machine.Configure(State.Ringing)
.OnEntryFrom(_setCalleeTrigger, callee => OnDialed(callee), "Caller number to call")
.Permit(Trigger.CallConnected, State.Connected);
_machine.Configure(State.Connected)
.OnEntry(t => StartCallTimer())
.OnExit(t => StopCallTimer())
.InternalTransition(Trigger.MuteMicrophone, t => OnMute())
.InternalTransition(Trigger.UnmuteMicrophone, t => OnUnmute())
.InternalTransition<int>(_setVolumeTrigger, (volume, t) => OnSetVolume(volume))
.Permit(Trigger.LeftMessage, State.OffHook)
.Permit(Trigger.PlacedOnHold, State.OnHold);
_machine.Configure(State.OnHold)
.SubstateOf(State.Connected)
.Permit(Trigger.TakenOffHold, State.Connected)
.Permit(Trigger.PhoneHurledAgainstWall, State.PhoneDestroyed);
_machine.OnTransitioned(t => $"OnTransitioned: {t.Source} -> {t.Destination} via {t.Trigger}({string.Join(", ", t.Parameters)})".Dump("Transitioned"));
}
void OnSetVolume(int volume)
{
($"Volume set to {volume}!").Dump("SetVolume");
}
void OnUnmute()
{
Console.WriteLine("Microphone unmuted!");
}
void OnMute()
{
"Microphone muted!".Dump();
}
void OnDialed(string callee)
{
_callee = callee;
$"[Phone Call] placed for : [{_callee}]".Dump();
}
void StartCallTimer()
{
Console.WriteLine("[Timer:] Call started at {0}", DateTime.Now);
}
void StopCallTimer()
{
Console.WriteLine("[Timer:] Call ended at {0}", DateTime.Now);
}
public void Mute()
{
_machine.Fire(Trigger.MuteMicrophone);
}
public void Unmute()
{
_machine.Fire(Trigger.UnmuteMicrophone);
}
public void SetVolume(int volume)
{
_machine.Fire(_setVolumeTrigger, volume);
}
public void Print()
{
Console.WriteLine("[{1}] placed call and [Status:] {0}", _machine.State, _caller);
}
public void Dialed(string callee)
{
_machine.Fire(_setCalleeTrigger, callee);
}
public void Connected()
{
_machine.Fire(Trigger.CallConnected);
}
public void Hold()
{
_machine.Fire(Trigger.PlacedOnHold);
}
public void Resume()
{
_machine.Fire(Trigger.TakenOffHold);
}
public string ToDotGraph()
{
return UmlDotGraph.Format(_machine.GetInfo());
}
}