Change State Over Time
Write a state machine as tasks: three packing stations that move between running, slow and stopped, each state lasting a set time, and each record telling which state a station was in.
Before You Start
-
The commands run from the root of a clone of the repository, where the files are in
docs/snippets/generate-data/. The Full Files block of this page holds the same files, for a reader who saves them by hand. -
First Simulation explains arrivals, tasks and the records a run writes.
- Task Flow explains
nextand itsweights.
Steps
Start the Stations
initial: 3 creates three stations when the run is built, and spawn: running starts each one in the task running. The key generator names them station-1 to station-3.
Write a Task for Each State
Each state is a task. service_time is how long a station stays in the state, the payload names the station and the state, and next chooses the state that follows. After running or slow the choice is by weights, and after stopped it is always slow, so a station never goes from stopped to running. A task writes its record when its time ends.
| docs/snippets/generate-data/change_state_over_time_low_level.py:states | |
|---|---|
The Low-level file keeps the dwell times and the rules in two dictionaries. A Router draws the next state from the rule of the current one, from its own random sequence, route.<state>.
Check the Result
Run the model for 2 simulated hours and print its first 8 records. Each station publishes a started record when it enters a state, and a record with its station and state when the state ends. The three stations start running at 0 seconds, and the first state ends at 300 seconds.
This prints every record of the run as an [EVT] line with the same fields and values.
This prints every record of the run as an [EVT] line with the same fields and values.
The time between two records of a station is the dwell time of the later state. The docs test follows every station through the run: each move is one of the allowed ones, and each state lasts its set time.