Skip to content

Commit 09bef6d

Browse files
committed
update commented example in readme
1 parent 77a7c80 commit 09bef6d

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

Diff for: README.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,44 @@ type AwaitEvent = enum
9393
- [References](#references)
9494

9595
## Commented example: Water phases
96+
97+
The example below gives you a short overview of how to build your state machine.
98+
99+
Recipe:
100+
- A state enum (called `Phase` in the example)
101+
- An event/trigger/condition enum (called `Event`)
102+
- Declaring a state machine
103+
- Declaring prologue, epilogue, initial state, terminal state. SOme are optional
104+
- Implement your events. Those are boolean tests.
105+
Events have visibility on variables declared
106+
- in the prologue
107+
- in `onEntry`
108+
- and the synthesized function parameters (here `tempFeed`).
109+
```Nim
110+
synthesize(waterMachine):
111+
proc observeWater(tempFeed: var seq[float])
112+
```
113+
- Implement common setup and teardown on state entry and exit if needed
114+
- Describe behaviours (i.e. state to state transition):
115+
- Transition without condition
116+
- Conditional transition due to an event
117+
- "Interrupt" which is a conditional transition that shortcuts regular control flow
118+
and allow handling exceptional cases, for example reaching the end of the `tempFeed` sequence.
119+
- Synthesize the state machine
120+
- Run it
121+
- ...
122+
- Profit!
123+
96124
```Nim
97125
type Phase = enum
98126
## States of your automaton.
99127
## The terminal state does not need to be defined
100128
Solid
101129
Liquid
102130
Gas
103-
Plasma # Plasma is almost unused
131+
# Plasma is unused. On the graph display, it will not be reachable from the InitialState.
132+
# The graph will also show that transitions out of the Plasma state are undefined via an `unreachable` transition.
133+
Plasma
104134
105135
type Event = enum
106136
## Named events. They will be associated with a boolean expression.
@@ -283,7 +313,8 @@ Output of the waterMachine
283313

284314
![water state transitions](examples/water_phase_transitions.png)
285315

286-
Alternative to Graphviz can be used on the `.dot` files if the output is unsatisfactory. Remember that graph node placement is usually an NP-complete task and requires heuristics to be solved that may not be optimal for your specific graph. In that case consider splitting your finite state machine hence building hierarchical state machines.
316+
Alternatives to Graphviz can be used on the `.dot` files if the output is unsatisfactory. Remember that graph node placement is usually an NP-complete task and requires heuristics to be solved that may not be optimal for your specific graph.
317+
In that case consider splitting your finite state machine hence building hierarchical state machines.
287318

288319
## Technical constraints
289320

Diff for: examples/water_phase_transitions.nim

+5-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ setEpilogue(waterMachine):
6363
# to trigger state transitions.
6464
#
6565
# Events have visibility over the following variables:
66-
# - The parameters of the synthesized state machine
66+
# - The parameters of the synthesized state machine in our case it is `tempFeed`
67+
# ```
68+
# synthesize(waterMachine):
69+
# proc observeWater(tempFeed: var seq[float])
70+
# ```
6771
# - The variables created in `setPrologue`
6872
# - The variables created in `onEntry`. Keep in mind
6973
# that `onEntry` in state-specific and that

0 commit comments

Comments
 (0)