/
finitomata.ex
1175 lines (1001 loc) · 38.1 KB
/
finitomata.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# credo:disable-for-this-file Credo.Check.Refactor.LongQuoteBlocks
# credo:disable-for-this-file Credo.Check.Refactor.CyclomaticComplexity
# credo:disable-for-this-file Credo.Check.Refactor.Nesting
defmodule Finitomata do
@doc false
def behaviour(value, funs \\ [])
def behaviour(value, behaviour) when is_atom(behaviour) do
with {:module, ^behaviour} <- Code.ensure_compiled(behaviour),
true <- function_exported?(behaviour, :behaviour_info, 1),
funs when is_list(funs) <- behaviour.behaviour_info(:callbacks) do
behaviour(value, funs)
else
_ ->
{:error, "The behavoiur specified is invalid ‹" <> inspect(value) <> "›"}
end
end
def behaviour(value, funs) when is_atom(value) do
case Code.ensure_compiled(value) do
{:module, ^value} ->
if Enum.all?(funs, fn {fun, arity} -> function_exported?(value, fun, arity) end) do
{:ok, value}
else
{:error,
"The module specified ‹" <>
inspect(value) <>
"› does not implement requested callbacks ‹" <> inspect(funs) <> "›"}
end
{:error, error} ->
{:error, "Cannot find the requested module ‹" <> inspect(value) <> "› (#{error})"}
end
end
using_schema = [
fsm: [
required: true,
type: :string,
doc: "The FSM declaration with the syntax defined by `syntax` option."
],
syntax: [
required: false,
type:
{:or,
[
{:in, [:flowchart, :state_diagram]},
{:custom, Finitomata, :behaviour, [Finitomata.Parser]}
]},
default: :flowchart,
doc: "The FSM dialect parser to convert the declaration to internal FSM representation."
],
impl_for: [
required: false,
type: {:or, [{:in, [:all, :none]}, :atom, {:list, :atom}]},
default: :all,
doc: "The list of transitions to inject default implementation for."
],
timer: [
required: false,
type: :pos_integer,
default: 5_000,
doc: "The interval to call `on_timer/2` recurrent event."
],
auto_terminate: [
required: false,
type: {:or, [:boolean, :atom, {:list, :atom}]},
default: false,
doc: "When `true`, the transition to the end state is initiated automatically."
],
ensure_entry: [
required: false,
type: {:or, [{:list, :atom}, :boolean]},
default: [],
doc: "The list of states to retry transition to until succeeded."
],
shutdown: [
required: false,
type: :pos_integer,
default: 5_000,
doc: "The shutdown interval for the `GenServer` behind the FSM."
],
persistency: [
required: false,
type: {:or, [{:in, [nil]}, {:custom, Finitomata, :behaviour, [Finitomata.Persistency]}]},
default: nil,
doc:
"The implementation of `Finitomata.Persistency` behaviour to backup FSM with a persistent storage."
],
listener: [
required: false,
type:
{:or,
[
{:in, [nil]},
{:custom, Finitomata, :behaviour, [[handle_info: 2]]},
{:custom, Finitomata, :behaviour, [Finitomata.Listener]}
]},
default: nil,
doc:
"The implementation of `Finitomata.Listener` behaviour _or_ a `GenServer.name()` to receive notification after transitions."
]
]
@using_schema NimbleOptions.new!(using_schema)
doc_options = """
## Options to `use Finitomata`
#{NimbleOptions.docs(@using_schema)}
"""
doc_readme = "README.md" |> File.read!() |> String.split("\n---") |> Enum.at(1)
@moduledoc doc_readme <> "\n" <> doc_options
require Logger
alias Finitomata.Transition
@typedoc """
The ID of the `Finitomata` supervision tree, useful for the concurrent
using of different `Finitomata` supervision trees.
"""
@type id :: any()
@typedoc "The name of the FSM (might be any term, but it must be unique)"
@type fsm_name :: any()
@typedoc "The payload that can be passed to each call to `transition/3`"
@type event_payload :: any()
@typedoc "The resolution of transition, when `{:error, _}` tuple, the transition is aborted"
@type transition_resolution ::
{:ok, Transition.state(), Finitomata.State.payload()} | {:error, any()}
defmodule State do
@moduledoc """
Carries the state of the FSM.
"""
alias Finitomata.Transition
@typedoc "The payload that has been passed to the FSM instance on startup"
@type payload :: any()
@typedoc "The map that holds last error which happened on transition (at given state and event)."
@type last_error ::
%{state: Transition.state(), event: Transition.event(), error: any()} | nil
@typedoc "The internal representation of the FSM state"
@type t :: %{
__struct__: State,
name: Finitomata.fsm_name(),
lifecycle: :loaded | :created | :unknown,
persistency: nil | module(),
listener: nil | module(),
current: Transition.state(),
payload: payload(),
timer: non_neg_integer(),
history: [Transition.state()],
last_error: last_error()
}
defstruct name: nil,
lifecycle: :unknown,
persistency: nil,
listener: nil,
current: :*,
payload: %{},
timer: false,
history: [],
last_error: nil
defimpl Inspect do
@moduledoc false
import Inspect.Algebra
def inspect(%Finitomata.State{} = state, %Inspect.Opts{} = opts) do
doc =
if true == get_in(opts.custom_options, [:full]) do
state |> Map.from_struct() |> Map.to_list()
else
default_registry = Finitomata.Supervisor.registry_name(nil)
name =
case state.name do
{:via, Registry, {^default_registry, name}} -> name
{:via, Registry, {registry, name}} -> {registry, name}
other -> other
end
persisted? =
case state.lifecycle do
:unknown -> false
:loaded -> true
:created -> true
end
errored? =
case state.last_error do
nil -> false
%{error: {:error, kind}} = error -> [{kind, Map.delete(error, :error)}]
%{error: error} -> error
error -> error
end
previous =
case state.history do
[{last, _} | _] -> last
[last | _] -> last
[] -> nil
end
[
name: name,
state: [
current: state.current,
previous: previous,
payload: state.payload
],
internals: [
errored?: errored?,
persisted?: persisted?,
timer: state.timer
]
]
end
concat(["#Finitomata<", to_doc(doc, opts), ">"])
end
end
end
@doc """
This callback will be called from each transition processor.
"""
@callback on_transition(
Transition.state(),
Transition.event(),
event_payload(),
State.payload()
) :: transition_resolution()
@doc """
This callback will be called from the underlying `c:GenServer.init/1`.
Unlike other callbacks, this one might raise preventing the whole FSM from start.
When `:ignore`, or `{:continues, new_payload}` tuple is returned from the callback,
the normal initalization continues through continuing to the next state.
`{:ok, new_payload}` prevents the _FSM_ from automatically getting into start state,
and the respective transition must be called manually.
"""
@callback on_start(State.payload()) ::
{:continue, State.payload()} | {:ok, State.payload()} | :ignore
@doc """
This callback will be called if the transition failed to complete to allow
the consumer to take an action upon failure.
"""
@callback on_failure(Transition.event(), event_payload(), State.t()) :: :ok
@doc """
This callback will be called on entering the state.
"""
@callback on_enter(Transition.state(), State.t()) :: :ok
@doc """
This callback will be called on exiting the state.
"""
@callback on_exit(Transition.state(), State.t()) :: :ok
@doc """
This callback will be called on transition to the final state to allow
the consumer to perform some cleanup, or like.
"""
@callback on_terminate(State.t()) :: :ok
@doc """
This callback will be called recurrently if `timer: pos_integer()`
option has been given to `use Finitomata`.
"""
@callback on_timer(Transition.state(), State.t()) ::
:ok
| {:ok, State.payload()}
| {:transition, {Transition.event(), event_payload()}, State.payload()}
| {:transition, Transition.event(), State.payload()}
| {:reschedule, non_neg_integer()}
@optional_callbacks on_start: 1,
on_failure: 3,
on_enter: 2,
on_exit: 2,
on_terminate: 1,
on_timer: 2
@doc """
Starts the FSM instance.
The arguments are
- the global name of `Finitomata` instance (optional, defaults to `Finitomata`)
- the implementation of FSM (the module, having `use Finitomata`)
- the name of the FSM (might be any term, but it must be unique)
- the payload to be carried in the FSM state during the lifecycle
The FSM is started supervised. If the global name/id is given, it should be passed
to all calls like `transition/4`
"""
@spec start_fsm(id(), module(), any(), any()) :: DynamicSupervisor.on_start_child()
def start_fsm(id \\ nil, impl, name, payload) do
DynamicSupervisor.start_child(
Finitomata.Supervisor.manager_name(id),
{impl, name: fqn(id, name), payload: payload}
)
end
@doc """
Initiates the transition.
The arguments are
- the id of the FSM (optional)
- the name of the FSM
- `event` atom or `{event, event_payload}` tuple; the payload will be passed to the respective
`on_transition/4` call, payload is `nil` by default
- `delay` (optional) the interval in milliseconds to apply transition after
"""
@spec transition(
id(),
fsm_name(),
Transition.event() | {Transition.event(), State.payload()},
non_neg_integer()
) ::
:ok
def transition(id \\ nil, target, event_payload, delay \\ 0)
def transition(target, {event, payload}, delay, 0) when is_integer(delay),
do: transition(nil, target, {event, payload}, delay)
def transition(id, target, event, delay) when is_atom(event) and is_integer(delay),
do: transition(id, target, {event, nil}, delay)
def transition(id, target, {event, payload}, 0),
do: id |> fqn(target) |> GenServer.cast({event, payload})
def transition(id, target, {event, payload}, delay) when is_integer(delay) and delay > 0 do
fn ->
Process.sleep(delay)
id |> fqn(target) |> GenServer.cast({event, payload})
end
|> Task.start()
|> elem(0)
end
@doc """
The state of the FSM.
"""
@spec state(id(), fsm_name(), reload? :: :cached | :payload | :full) ::
nil | State.t() | State.payload()
def state(id \\ nil, target, reload? \\ :full)
def state(target, reload?, :full) when reload? in ~w|cached payload full|a,
do: state(nil, target, reload?)
def state(id, target, reload?), do: id |> fqn(target) |> do_state(reload?)
@spec do_state(fqn :: GenServer.name(), reload? :: :cached | :payload | :full) ::
nil | State.t() | State.payload()
defp do_state(fqn, :cached), do: :persistent_term.get({Finitomata, fqn}, nil)
defp do_state(fqn, :payload), do: do_state(fqn, :cached) || do_state(fqn, :full).payload
defp do_state(fqn, :full) do
fqn
|> GenServer.whereis()
|> case do
nil ->
nil
pid when is_pid(pid) ->
{:ok, state} = :gen.call(pid, :"$gen_call", :state, 1_000)
:persistent_term.put({Finitomata, fqn}, state.payload)
state
end
catch
:exit, :normal -> nil
end
@doc """
Returns `true` if the transition to the state `state` is possible, `false` otherwise.
"""
@spec allowed?(id(), fsm_name(), Transition.state()) :: boolean()
def allowed?(id \\ nil, target, state),
do: id |> fqn(target) |> GenServer.call({:allowed?, state})
@doc """
Returns `true` if the transition by the event `event` is possible, `false` otherwise.
"""
@spec responds?(id(), fsm_name(), Transition.event()) :: boolean()
def responds?(id \\ nil, target, event),
do: id |> fqn(target) |> GenServer.call({:responds?, event})
@doc """
Returns supervision tree of `Finitomata`. The healthy tree has all three `pid`s.
"""
@spec sup_tree(id()) :: [
{:supervisor, nil | pid()},
{:manager, nil | pid()},
{:registry, nil | pid()}
]
def sup_tree(id \\ nil) do
[
supervisor: Process.whereis(Finitomata.Supervisor.supervisor_name(id)),
manager: Process.whereis(Finitomata.Supervisor.manager_name(id)),
registry: Process.whereis(Finitomata.Supervisor.registry_name(id))
]
end
@doc """
Returns `true` if the supervision tree is alive, `false` otherwise.
"""
@spec sup_alive?(id()) :: boolean()
def sup_alive?(id \\ nil),
do: id |> sup_tree() |> Keyword.values() |> Enum.all?(&(not is_nil(&1)))
@doc """
Returns `true` if the FSM specified is alive, `false` otherwise.
"""
@spec alive?(any(), fsm_name()) :: boolean()
def alive?(id \\ nil, target), do: id |> fqn(target) |> GenServer.whereis() |> is_pid()
@doc false
@spec child_spec(any()) :: Supervisor.child_spec()
def child_spec(id \\ nil)
def child_spec(nil),
do: Supervisor.child_spec(Finitomata.Supervisor, [])
def child_spec(id),
do: Supervisor.child_spec({Finitomata.Supervisor, id}, id: {Finitomata, id})
@doc false
@spec start_link(any()) ::
{:ok, pid} | {:error, {:already_started, pid()} | {:shutdown, term()} | term()}
def start_link(id \\ nil) do
Supervisor.start_link([Finitomata.child_spec(id)], strategy: :one_for_one)
end
@doc false
defmacro __using__(opts) when is_list(opts) do
raise_opts = fn description ->
[
file: Path.relative_to_cwd(__CALLER__.file),
line: __CALLER__.line,
description: description
]
end
if not Keyword.keyword?(opts) do
raise CompileError, raise_opts.("options to `use Finitomata` must be a keyword list")
end
ast(opts, @using_schema)
end
@doc false
@doc deprecated: "Use `use fsm: …, syntax: …` instead"
defmacro __using__({fsm, syntax}), do: ast([fsm: fsm, syntax: syntax], @using_schema)
@doc false
@doc deprecated: "Use `use fsm: …, syntax: …` instead"
defmacro __using__(fsm), do: ast([fsm: fsm], @using_schema)
@doc false
defp ast(options, schema) do
quote location: :keep, generated: true do
NimbleOptions.validate!(unquote(options), unquote(Macro.escape(schema)))
require Logger
alias Finitomata.Transition, as: Transition
import Finitomata.Defstate, only: [defstate: 1]
@on_definition Finitomata.Hook
@before_compile Finitomata.Hook
syntax =
Keyword.get(
unquote(options),
:syntax,
Application.compile_env(:finitomata, :syntax, :flowchart)
)
if syntax in [Finitomata.Mermaid, Finitomata.PlantUML] do
reporter = if Code.ensure_loaded?(Mix), do: Mix.shell(), else: Logger
reporter.info([
[:yellow, "deprecated: ", :reset],
"using built-in modules as syntax names is deprecated, please use ",
[:blue, ":flowchart", :reset],
" and/or ",
[:blue, ":state_diagram", :reset],
" instead"
])
end
syntax =
case syntax do
:flowchart -> Finitomata.Mermaid
:state_diagram -> Finitomata.PlantUML
module when is_atom(module) -> module
end
shutdown =
Keyword.get(
unquote(options),
:shutdown,
Application.compile_env(:finitomata, :shutdown, 5_000)
)
auto_terminate =
Keyword.get(
unquote(options),
:auto_terminate,
Application.compile_env(:finitomata, :auto_terminate, false)
)
persistency =
Keyword.get(
unquote(options),
:persistency,
Application.compile_env(:finitomata, :persistency, nil)
)
listener =
Keyword.get(
unquote(options),
:listener,
Application.compile_env(:finitomata, :listener, nil)
)
use GenServer, restart: :transient, shutdown: shutdown
impls = ~w|on_transition on_failure on_enter on_exit on_terminate on_timer|a
impl_for =
case Keyword.get(unquote(options), :impl_for, :all) do
:all -> impls
:none -> []
transition when is_atom(transition) -> [transition]
list when is_list(list) -> list
end
if impl_for -- impls != [] do
raise CompileError,
description:
"allowed `impl_for:` values are: `:all`, `:none`, or any combination of `#{inspect(impls)}`"
end
fsm =
case syntax.parse(unquote(options[:fsm])) do
{:ok, result} ->
result
{:error, description, snippet, _, {line, column}, _} ->
raise SyntaxError,
file: "lib/finitomata.ex",
line: line,
column: column,
description: description,
snippet: %{content: snippet, offset: 0}
{:error, error} ->
raise TokenMissingError,
description: "description is incomplete, error: #{error}"
end
hard =
fsm
|> Transition.determined()
|> Enum.filter(fn
{state, :__end__} ->
case auto_terminate do
^state -> true
true -> true
list when is_list(list) -> state in list
_ -> false
end
{state, event} ->
event
|> to_string()
|> String.ends_with?("!")
end)
[Transition.hard(fsm), hard]
|> Enum.map(fn h -> h |> Enum.map(&elem(&1, 1)) |> Enum.uniq() end)
|> Enum.reduce(&Kernel.--/2)
|> unless do
raise CompileError,
description:
"transitions marked as `:hard` must be determined, non-determined found: #{inspect(Transition.hard(fsm) -- hard)}"
end
hard =
Enum.map(hard, fn {from, event} ->
{from, Enum.find(fsm, &match?(%Transition{from: ^from, event: ^event}, &1))}
end)
soft =
Enum.filter(fsm, fn
%Transition{event: event} ->
event
|> to_string()
|> String.ends_with?("?")
end)
ensure_entry =
unquote(options)
|> Keyword.get(
:ensure_entry,
Application.compile_env(:finitomata, :ensure_entry, [])
)
|> case do
list when is_list(list) -> list
true -> [Transition.entry(fsm)]
_ -> []
end
timer =
unquote(options)
|> Keyword.get(:timer)
|> case do
value when is_integer(value) and value >= 0 -> value
true -> Application.compile_env(:finitomata, :timer, 5_000)
_ -> false
end
@__config__ %{
syntax: syntax,
fsm: fsm,
impl_for: impl_for,
persistency: persistency,
listener: listener,
auto_terminate: auto_terminate,
ensure_entry: ensure_entry,
states: Transition.states(fsm),
events: Transition.events(fsm),
paths: Transition.paths(fsm),
loops: Transition.loops(fsm),
hard: hard,
soft: soft,
timer: timer
}
@__config_keys__ Map.keys(@__config__)
@__config_soft_events__ Enum.map(soft, & &1.event)
@__config_hard_states__ Keyword.keys(hard)
@doc """
The convenient macro to allow using states in guards, returns a compile-time
list of states for `#{inspect(__MODULE__)}`.
"""
defmacro config(key) when key in @__config_keys__ do
value = @__config__ |> Map.get(key) |> Macro.escape()
quote do: unquote(value)
end
@doc """
Getter for the internal compiled in _FSM_ information.
"""
@spec __config__(atom()) :: any()
def __config__(key) when key in @__config_keys__,
do: Map.get(@__config__, key)
@doc false
@doc deprecated: "Use `__config__(:fsm)` instead"
def fsm, do: Map.get(@__config__, :fsm)
@doc false
@doc deprecated: "Use `__config__(:entry)` instead"
def entry, do: Transition.entry(fsm())
@doc false
@doc deprecated: "Use `__config__(:states)` instead"
def states, do: Map.get(@__config__, :states)
@doc false
@doc deprecated: "Use `__config__(:events)` instead"
def events, do: Map.get(@__config__, :events)
@doc false
def start_link(payload: payload, name: name),
do: start_link(name: name, payload: payload)
@doc ~s"""
Starts an _FSM_ alone with `name` and `payload` given.
Usually one does not want to call this directly, the most common way would be
to start a `Finitomata` supervision tree with `Finitomata.Supervisor.start_link/1`
or even better embed it into the existing supervision tree _and_
start _FSM_ with `Finitomata.start_fsm/3` passing `#{inspect(__MODULE__)}` as the first
parameter.
FSM representation
```#{@__config__[:syntax] |> Module.split() |> List.last() |> Macro.underscore()}
#{@__config__[:syntax].lint(unquote(options[:fsm]))}
```
"""
case @__config__[:persistency] do
nil ->
def start_link(name: name, payload: payload) do
GenServer.start_link(__MODULE__, %{name: name, payload: payload}, name: name)
end
@doc false
def start_link(payload),
do: GenServer.start_link(__MODULE__, %{name: nil, payload: payload})
module when is_atom(module) ->
def start_link(name: name, payload: payload) do
GenServer.start_link(
__MODULE__,
%{name: name, payload: payload, with_persistency: @__config__[:persistency]},
name: name
)
end
end
defmacrop report_error(err, from \\ "Finitomata") do
quote generated: true, location: :keep, bind_quoted: [err: err, from: from] do
case err do
%{__exception__: true} ->
{ex, st} = Exception.blame(:error, err, __STACKTRACE__)
Logger.debug(Exception.format(:error, ex, st))
{:error, Exception.message(err)}
_ ->
Logger.warning(
"[⚑ ↹] #{from} raised: " <> inspect(err) <> "\n" <> inspect(__STACKTRACE__)
)
{:error, :on_transition_raised}
end
end
end
@doc false
@impl GenServer
def init(%{name: name, payload: payload, with_persistency: persistency} = state)
when not is_nil(name) and not is_nil(persistency) do
{lifecycle, payload} =
case payload do
module when is_atom(module) ->
persistency.load({payload, id: name})
%struct{} = payload ->
persistency.load({struct, payload |> Map.from_struct() |> Map.put_new(:id, name)})
%{type: type, id: id} ->
persistency.load({type, %{id => name}})
end
init(%{name: name, payload: payload, lifecycle: lifecycle, persistency: persistency})
end
def init(%{name: name, payload: payload} = init_arg) do
lifecycle = Map.get(init_arg, :lifecycle, :unknown)
{lifecycle, payload} =
case function_exported?(__MODULE__, :on_start, 1) and
apply(__MODULE__, :on_start, [payload]) do
false -> {lifecycle, payload}
{:ok, payload} -> {:loaded, payload}
{:continue, payload} -> {lifecycle, payload}
:ignore -> {lifecycle, payload}
end
state = %State{
name: name,
lifecycle: lifecycle,
persistency: Map.get(init_arg, :persistency, nil),
timer: @__config__[:timer],
payload: payload
}
:persistent_term.put({Finitomata, state.name}, state.payload)
if is_integer(@__config__[:timer]) and @__config__[:timer] > 0,
do: Process.send_after(self(), :on_timer, @__config__[:timer])
if lifecycle == :loaded,
do: {:ok, state},
else: {:ok, state, {:continue, {:transition, event_payload({:__start__, nil})}}}
end
@doc false
@impl GenServer
def handle_call(:state, _from, state), do: {:reply, state, state}
@doc false
@impl GenServer
def handle_call({:allowed?, to}, _from, state),
do: {:reply, Transition.allowed?(@__config__[:fsm], state.current, to), state}
@doc false
@impl GenServer
def handle_call({:responds?, event}, _from, state),
do: {:reply, Transition.responds?(@__config__[:fsm], state.current, event), state}
@doc false
@impl GenServer
def handle_cast({event, payload}, state),
do: {:noreply, state, {:continue, {:transition, {event, payload}}}}
@doc false
@impl GenServer
def handle_continue({:transition, {event, payload}}, state),
do: transit({event, payload}, state)
@doc false
@impl GenServer
def terminate(reason, state) do
safe_on_terminate(state)
end
@spec history(Transition.state(), [Transition.state()]) :: [Transition.state()]
defp history(current, history) do
case history do
[^current | rest] -> [{current, 2} | rest]
[{^current, count} | rest] -> [{current, count + 1} | rest]
_ -> [current | history]
end
end
@spec event_payload(Transition.event() | {Transition.event(), Finitomata.event_payload()}) ::
{Transition.event(), Finitomata.event_payload()}
defp event_payload({event, %{} = payload}),
do: {event, Map.update(payload, :__retries__, 1, &(&1 + 1))}
defp event_payload({event, payload}),
do: event_payload({event, %{payload: payload}})
defp event_payload(event),
do: event_payload({event, %{}})
@spec transit({Transition.event(), Finitomata.event_payload()}, State.t()) ::
{:noreply, State.t()} | {:stop, :normal, State.t()}
defp transit({event, payload}, state) do
with {:responds, true} <-
{:responds, Transition.responds?(@__config__[:fsm], state.current, event)},
{:on_exit, :ok} <- {:on_exit, safe_on_exit(state.current, state)},
{:ok, new_current, new_payload} <-
safe_on_transition(state.name, state.current, event, payload, state.payload),
{:allowed, true} <-
{:allowed, Transition.allowed?(@__config__[:fsm], state.current, new_current)},
new_history = history(state.current, state.history),
state = %State{
state
| payload: new_payload,
current: new_current,
history: new_history
},
{:on_enter, :ok} <- {:on_enter, safe_on_enter(new_current, state)} do
:persistent_term.put({Finitomata, state.name}, state.payload)
case new_current do
:* ->
{:stop, :normal, state}
hard when hard in @__config_hard_states__ ->
{:noreply, state,
{:continue, {:transition, event_payload(@__config__[:hard][hard].event)}}}
_ ->
{:noreply, state}
end
else
{err, false} ->
Logger.warning("[⚐ ↹] transition not exists or not allowed (:#{err})")
safe_on_failure(event, payload, state)
{:noreply, state}
{err, :ok} ->
Logger.warning("[⚐ ↹] callback failed to return `:ok` (:#{err})")
safe_on_failure(event, payload, state)
{:noreply, state}
err ->
state = %State{state | last_error: %{state: state.current, event: event, error: err}}
cond do
event in @__config_soft_events__ ->
Logger.debug("[⚐ ↹] transition softly failed " <> inspect(err))
{:noreply, state}
@__config__[:fsm]
|> Transition.allowed(state.current, event)
|> Enum.all?(&(&1 in @__config__[:ensure_entry])) ->
{:noreply, state, {:continue, {:transition, event_payload({event, payload})}}}
true ->
Logger.warning("[⚐ ↹] transition failed " <> inspect(err))
safe_on_failure(event, payload, state)
{:noreply, state}
end
end
end
if @__config__[:timer] do
@impl GenServer
@doc false
def handle_info(:on_timer, state) do
state.current
|> safe_on_timer(state)
|> case do
:ok ->
{:noreply, state}
{:ok, state_payload} ->
:persistent_term.put({Finitomata, state.name}, state_payload)
{:noreply, %State{state | payload: state_payload}}
{:transition, {event, event_payload}, state_payload} ->
transit({event, event_payload}, %State{state | payload: state_payload})
{:transition, event, state_payload} ->
transit({event, nil}, %State{state | payload: state_payload})
{:reschedule, value} when is_integer(value) and value >= 0 ->
{:noreply, %State{state | timer: value}}
weird ->
Logger.warning("[⚑ ↹] on_timer returned a garbage " <> inspect(weird))
{:noreply, state}
end
|> tap(fn
{:noreply, %State{timer: timer}} when is_integer(timer) and timer > 0 ->
Process.send_after(self(), :on_timer, timer)
_ ->
:ok
end)
end
end
@spec safe_on_transition(
Finitomata.fsm_name(),
Transition.state(),
Transition.event(),
Finitomata.event_payload(),
State.payload()
) ::
{:ok, Transition.state(), State.payload()}
| {:error, any()}
| {:error, :on_transition_raised}
defp safe_on_transition(name, current, event, event_payload, state_payload) do
current
|> on_transition(event, event_payload, state_payload)
|> maybe_store(name, current, event, event_payload, state_payload)
|> tap(&maybe_pubsub(&1, name))
rescue
err -> report_error(err, "on_transition/4")
end
@spec safe_on_failure(Transition.event(), Finitomata.event_payload(), State.t()) :: :ok
defp safe_on_failure(event, event_payload, state_payload) do
if function_exported?(__MODULE__, :on_failure, 3) do
with other when other != :ok <-
apply(__MODULE__, :on_failure, [event, event_payload, state_payload]) do
Logger.info("Unexpected return from a callback [#{inspect(other)}], must be :ok")
:ok
end
else
:ok
end
rescue
err -> report_error(err, "on_failure/3")
end
@spec safe_on_enter(Transition.state(), State.t()) :: :ok
defp safe_on_enter(state, state_payload) do
if function_exported?(__MODULE__, :on_enter, 2) do
with other when other != :ok <- apply(__MODULE__, :on_enter, [state, state_payload]) do
Logger.info("Unexpected return from a callback [#{inspect(other)}], must be :ok")
:ok
end
else
:ok
end
rescue
err -> report_error(err, "on_enter/2")
end
@spec safe_on_exit(Transition.state(), State.t()) :: :ok
defp safe_on_exit(state, state_payload) do
if function_exported?(__MODULE__, :on_exit, 2) do
with other when other != :ok <- apply(__MODULE__, :on_exit, [state, state_payload]) do