/
transition.ex
248 lines (212 loc) · 7.94 KB
/
transition.ex
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
defmodule Finitomata.Transition do
@moduledoc """
The internal representation of `Transition`.
It includes `from` and `to` states, and the `event`, all represented as atoms.
"""
alias Finitomata.Transition
@typedoc "The state of FSM"
@type state :: atom()
@typedoc "The event in FSM"
@type event :: atom()
@typedoc """
The transition is represented by `from` and `to` states _and_ the `event`.
"""
@type t :: %{
__struct__: Transition,
from: state(),
to: state(),
event: event()
}
defstruct [:from, :to, :event]
@doc false
@spec from_parsed([binary()]) :: t()
def from_parsed([from, to, event])
when is_binary(from) and is_binary(to) and is_binary(event) do
[from, to, event] =
Enum.map(
[from, to, event],
&(&1 |> String.trim_leading("[") |> String.trim_trailing("]") |> String.to_atom())
)
%Transition{from: from, to: to, event: event}
end
@doc ~S"""
Returns the state _after_ starting one, so-called `entry` state.
iex> {:ok, transitions} =
...> Finitomata.PlantUML.parse("[*] --> s1 : foo\ns1 --> s2 : ok\ns2 --> [*] : ko")
...> Finitomata.Transition.entry(transitions)
:s1
"""
@spec entry([t()]) :: state()
def entry(transitions) do
transition = Enum.find(transitions, &match?(%Transition{from: :*}, &1))
transition.to
end
@doc ~S"""
Returns `true` if the transition `from` → `to` is allowed, `false` otherwise.
iex> {:ok, transitions} =
...> Finitomata.PlantUML.parse("[*] --> s1 : foo\ns1 --> s2 : ok\ns2 --> [*] : ko")
...> Finitomata.Transition.allowed?(transitions, :s1, :s2)
true
...> Finitomata.Transition.allowed?(transitions, :s1, :*)
false
"""
@spec allowed?([t()], state(), state()) :: boolean()
def allowed?(transitions, from, to) do
Enum.any?(transitions, &match?(%Transition{from: ^from, to: ^to}, &1))
end
@doc ~S"""
Returns `true` if the state `from` hsa an outgoing transition with `event`, false otherwise.
iex> {:ok, transitions} =
...> Finitomata.PlantUML.parse("[*] --> s1 : foo\ns1 --> s2 : ok\ns2 --> [*] : ko")
...> Finitomata.Transition.responds?(transitions, :s1, :ok)
true
...> Finitomata.Transition.responds?(transitions, :s1, :ko)
false
"""
@spec responds?([t()], state(), event()) :: boolean()
def responds?(transitions, from, event) do
Enum.any?(transitions, &match?(%Transition{from: ^from, event: ^event}, &1))
end
@doc ~S"""
Returns the list of all the transitions, matching the options.
Used internally for the validations.
iex> {:ok, transitions} =
...> Finitomata.Mermaid.parse(
...> "idle --> |to_s1| s1\n" <>
...> "s1 --> |to_s2| s2\n" <>
...> "s1 --> |to_s3| s3\n" <>
...> "s2 --> |to_s3| s3")
...> Finitomata.Transition.allowed(transitions, to: [:idle, :*])
[{:*, :idle, :__start__}, {:s3, :*, :__end__}]
iex> Finitomata.Transition.allowed(transitions, from: :s1)
[{:s1, :s2, :to_s2}, {:s1, :s3, :to_s3}]
iex> Finitomata.Transition.allowed(transitions, from: :s1, to: :s3)
[{:s1, :s3, :to_s3}]
iex> Finitomata.Transition.allowed(transitions, from: :s1, with: :to_s3)
[{:s1, :s3, :to_s3}]
iex> Finitomata.Transition.allowed(transitions, from: :s2, with: :to_s2)
[]
"""
@spec allowed([t()], [{:from, state()} | {:to, state()} | {:with, event()}]) :: [
{state(), state(), event()}
]
def allowed(transitions, options \\ []) do
from = List.wrap(options[:from])
to = List.wrap(options[:to])
event = List.wrap(options[:with])
case {from, to, event} do
{[], [], []} ->
for %Transition{from: from, to: to, event: event} <- transitions, do: {from, to, event}
{fa, [], []} ->
for %Transition{from: from, to: to, event: event} <- transitions,
from in fa,
do: {from, to, event}
{[], ta, []} ->
for %Transition{from: from, to: to, event: event} <- transitions,
to in ta,
do: {from, to, event}
{[], [], ea} ->
for %Transition{from: from, to: to, event: event} <- transitions,
event in ea,
do: {from, to, event}
{fa, ta, []} ->
for %Transition{from: from, to: to, event: event} <- transitions,
from in fa and to in ta,
do: {from, to, event}
{fa, [], ea} ->
for %Transition{from: from, to: to, event: event} <- transitions,
from in fa and event in ea,
do: {from, to, event}
{[], ta, ea} ->
for %Transition{from: from, to: to, event: event} <- transitions,
to in ta and event in ea,
do: {from, to, event}
{fa, ta, ea} ->
for %Transition{from: from, to: to, event: event} <- transitions,
from in fa and to in ta and event in ea,
do: {from, to, event}
end
end
@doc ~S"""
Returns the list of all the transitions, matching the `from` state and the `event`.
Used internally for the validations.
iex> {:ok, transitions} =
...> Finitomata.PlantUML.parse("[*] --> s1 : foo\ns1 --> s2 : ok\ns2 --> [*] : ko")
...> Finitomata.Transition.allowed(transitions, :s1, :foo)
[:s2]
...> Finitomata.Transition.allowed(transitions, :s1, :*)
[]
"""
@spec allowed([t()], state(), event()) :: [state()]
def allowed(transitions, from, event) do
for %Transition{from: ^from, to: to, event: ^event} <- transitions, do: to
end
@doc ~S"""
Returns `Finitomata.Transition.event()` if there is a determined transition
from the current state.
The transition is determined, if it is the only transition allowed from the state.
Used internally for the validations.
iex> {:ok, transitions} =
...> Finitomata.Mermaid.parse(
...> "idle --> |to_s1| s1\n" <>
...> "s1 --> |to_s2| s2\n" <>
...> "s1 --> |to_s3| s3\n" <>
...> "s2 --> |to_s3| s3")
...> Finitomata.Transition.determined(transitions)
[s3: {:__end__, :*}, s2: {:to_s3, :s3}, idle: {:to_s1, :s1}]
"""
@spec determined([t()]) :: [{state(), {event(), state()}}]
def determined(transitions) do
transitions
|> states()
|> Enum.reduce([], fn state, acc ->
transitions
|> allowed(from: state)
|> case do
[{^state, target, event}] -> [{state, {event, target}} | acc]
_ -> acc
end
end)
end
@doc ~S"""
Returns `{:ok, {event(), state()}}` tuple if there is a determined transition
from the current state, `:error` otherwise.
The transition is determined, if it is the only transition allowed from the state.
Used internally for the validations.
iex> {:ok, transitions} =
...> Finitomata.Mermaid.parse(
...> "idle --> |to_s1| s1\n" <>
...> "s1 --> |to_s2| s2\n" <>
...> "s1 --> |to_s3| s3\n" <>
...> "s2 --> |to_s3| s3")
...> Finitomata.Transition.determined(transitions, :s1)
:error
iex> Finitomata.Transition.determined(transitions, :s2)
{:ok, {:to_s3, :s3}}
iex> Finitomata.Transition.determined(transitions, :s3)
{:ok, {:__end__, :*}}
"""
@spec determined([t()], state()) :: {:ok, {event(), state()}} | :error
def determined(transitions, state) do
transitions
|> allowed(from: state)
|> case do
[{^state, target, event}] -> {:ok, {event, target}}
_ -> :error
end
end
@doc ~S"""
Returns the not ordered list of states, excluding the starting and ending states `:*`.
iex> {:ok, transitions} =
...> Finitomata.PlantUML.parse("[*] --> s1 : foo\ns1 --> s2 : ok\ns2 --> [*] : ko")
...> Finitomata.Transition.states(transitions)
[:s1, :s2]
"""
@spec states([t()]) :: [state()]
def states(transitions) do
transitions
|> Enum.flat_map(fn %Transition{from: from, to: to} -> [from, to] end)
|> Enum.uniq()
|> Enum.reject(&(&1 == :*))
end
end