qmuntal / stateless

Go library for creating finite state machines
BSD 2-Clause "Simplified" License
898 stars 47 forks source link

What are internal transitions? #48

Closed lexuzieel closed 1 year ago

lexuzieel commented 1 year ago

In the code there are mentions of "internal" transitions as well as of an "initial" transition which supposedly adds an internal transition to the machine. What is the purpose of this and how and when to use them? The README just tells how to define them in code without any examples on how to actually use them.

qmuntal commented 1 year ago

Hi @avixFF, internal transitions are standard state machine concepts, nothing specific to this library. From wikipedia:

Very commonly, an event causes only some internal actions to execute but does not lead to a change of state (state transition). In this case, all actions executed comprise the internal transition. For example, when one types on a keyboard, it responds by generating different character codes. However, unless the Caps Lock key is pressed, the state of the keyboard does not change (no state transition occurs). -- https://en.wikipedia.org/wiki/UML_state_machine#Internal_transitions

You have some examples in example_test.go:

https://github.com/qmuntal/stateless/blob/491b43867de630ac46cfdd5701276f06a51dd60f/example_test.go#L52-L63

lexuzieel commented 1 year ago

Ah yes, I see. So in the context of this library does this mean that internal transitions is basically a way to define multiple independent events with different logic for the same state?

qmuntal commented 1 year ago

internal transitions is basically a way to define multiple independent events with different logic for the same state?

Yep, and those events don't trigger any state transition.