10

State and finite state machines

A shipment lifecycle expressed four ways, including a deterministic stochastic machine.

Demo 10Source on GitHub โ†—

A shipment lifecycle (Created โ†’ Paid โ†’ InTransit โ†’ Delivered, with Cancelled and a stochastic-only Lost) expressed four independent ways that share only the status and event types, so cross-check tests prove they agree rather than assume it:

  • GoF State pattern: one type per status, default-reject base methods, unique_ptr-owned context.
  • Table-driven FSM: a (status, event) โ†’ status map.
  • std::variant FSM: tag types plus std::visit over an overload set.
  • Probability-driven FSM: deterministic events plus a probabilistic advance() using std::discrete_distribution over configured weights.

The stochastic machine takes its std::mt19937 by constructor injection, so tests seed it with a fixed value and assert an exact reproducible sequence, while a 20,000-trial test checks the empirical frequencies match the weights.

Build, run, observe

./scripts/build.sh cpp23
./scripts/verify.sh cpp23      # 31/31 tests pass
./examples/10-state-machines/demo.sh cpp23

The demo drives a shipment through legal transitions, rejects an illegal one, and runs a seeded stochastic sequence. Chapter 10 walks through when each representation fits. Builds standalone and under the cpp20 preset (29 tests, with the std::expected cases compiled out).