Finitomata :: FSM boilerplate based on callbacks

Finitomata provides a boilerplate for FSM implementation, allowing to concentrate on the business logic rather than on the process management and transitions/events consistency tweaking.

It reads a description of the FSM from a string in PlantUML format.

It validates the FSM is consistent, namely it has a single initial state, one or more final states, and no orphan states. If everything is OK, it generates a GenServer that could be used both alone, and with provided supervision tree. This GenServer requires to implement three callbacks

  • on_transition/4 — mandatory
  • on_failure/3 — optional
  • on_terminate/1 — optional

All the callbacks do have a default implementation, that would perfectly handle transitions having a single to state and not requiring any additional business logic attached.


defmodule MyFSM do
  @fsm """
  [*] --> s1 : to_s1
  s1 --> s2 : to_s2
  s1 --> s3 : to_s3
  s2 --> [*] : ok
  s3 --> [*] : ok

  use Finitomata, @fsm

  def on_transition(:s1, :to_s2, event_payload, state_payload),
    do: {:ok, :s2, state_payload}

@mudasobwa thanks for sharing this library, I’ve used gen_statem a few times in the past and I think your library makes these kinds of use-cases a lot more ergonomic. Have you ever thought making a compile-time dependency, rather than a runtime dependency?

My thought here is that I could use something like finitomata to help me develop a FSM module, but I could generate an elixir/erlang file with all of the expansion done so that I don’t have to specify finitomata as a dependency of my library. This also means that other libraries could use different versions of the tool without need to resolve to a shared version.

Just curious if you’ve considered a use-case like that, or maybe I should give it a try myself to see how it goes?

Well, there is no runtime dependency already, besides provided supervision subtree, registry, and helpers to deal with dynamic children.

If you don’t need all the above and are ready to manage the processes yourselves, you don’t depend on Finitomata in runtime. use Finitomata injects the code into your module in compile time. Generating this boilerplate code instead of injecting is not a big deal, a simple call to Macro.to_string/1 would do (e. g. from __before_compile__ callback.)

mix task to generate such a module is also trivial. I personally don’t like generators because the code they produce is hard to support, code injection works better. But I’d love to see a PR with a mix task generating the module.

FWIW, this thread contains links to other similar libraries and usage examples.

Thanks @mudasobwa your breakdown definitely improved my thinking. I can see how a generator might be a pain to deal with in source control etc. And the distinction between run-time, compile-time and deploy-time is helpful as well. Thanks again for the library, I’ll give it a run

1 Like

After some hesitation, two new callbacks have been added.

  • on_enter/2 — optional
  • on_exit/2 — optional