--- title: Traffic Light categories: behavioral ... This example demonstrates modeling of a simple Traffic Light state machine. Based on slide no. 3 from [lecture presentation](http://www.cs.mcgill.ca/~adenau/teaching/cs304/lecture16.pdf) ```{.clafer .links} ``` # The most simple example * no concurrency, * no forks, * deterministic, * no events, * no time. ## Traffic light model with transitions only ```clafer abstract xor TrafficLight_1 initial xor on -->> off initial xor normal initial green --> yellow yellow --> red red --> green xor flashing -->> normal initial yellow_on --> yellow_off yellow_off --> yellow_on off --> on ``` The constructs mean the following: * `on -->> off`: `on` lasts any number of steps, but must eventually become `off`. * `off --> on`: `off` lasts exactly one step. * `initial xor normal`: every time `on` starts, it starts in `normal`, and `initial green` makes it start in `green`. Re-entering `on` starts it again. * `normal` has no transition, because we do not want to force flashing: `normal` may stay or switch to `flashing` at any step. * `flashing -->> normal`: `flashing` lasts at least one step and must eventually end. * `green --> yellow` and the others: the strict cycles inside a state. ## Properties An assertion must hold in every trace of every traffic light, and the command `check` looks for a counterexample within the scopes and the maximum trace length. The quantifier needs the parentheses: a transition binds more loosely than `|`. The light never goes from green directly to red, and it is green right after it was off: ```clafer assert [ all t : TrafficLight_1 | (t.on.normal.green --> no t.on.normal.red) ] assert [ all t : TrafficLight_1 | (t.off --> t.on.normal.green) ] ``` The transition `red --> green` lasts as long as the parent of `red`, `normal`. When `normal` switches to `flashing`, or `on` to `off`, the transition lapses, so this assertion has a counterexample, e.g., red followed by `yellow_on`: ```clafer assert [ all t : TrafficLight_1 | (t.on.normal.red --> t.on.normal.green) ] ``` This one has none: ```clafer assert [ all t : TrafficLight_1 | (t.on.normal.red --> (t.on.normal.green || t.on.flashing || t.off)) ] ``` ## Scenarios A scenario is a concrete traffic light with a constraint on its trace. A chain of transitions is a sequence, read from left to right, and `initially` anchors it to the start of the trace. Without `initially`, the chain would have to hold in every state of the trace. The scenario below is active on this page: the normal cycle, then flashing, then off. ```clafer tl : TrafficLight_1 [ initially (green --> yellow --> red --> green --> yellow_on --> yellow_off --> off) ] ``` It desugars to ``` [ initially (green && X (yellow && X (red && X (green && X (yellow_on && X (yellow_off && X off)))))) ] ``` All the Clafer code on a page forms one model, and all its concrete clafers exist at the same time. Therefore, the other scenarios are not part of the model of this page. To try one, open the model in the IDE and use it to replace the scenario above. On, eventually flashing, and the flashing cycle: ``` t1 : TrafficLight_1 [ initially (on -->> flashing -->> yellow_on -->> yellow_off -->> yellow_on) ] ``` On, then flashing at the next step: ``` t2 : TrafficLight_1 [ initially (on --> yellow_on --> yellow_off --> yellow_on) ] ``` It desugars to ``` [ initially (on => X (yellow_on && X (yellow_off && X yellow_on))) ] ``` Observe, that the state after `on` is `yellow_on`, and not `flashing` followed by `yellow_on`. `flashing` starts in `yellow_on`, so `on --> flashing --> yellow_on` would require `yellow_on` twice in a row, which `yellow_on --> yellow_off` forbids. On until `yellow_off`, then the flashing cycle step by step: ``` t3 : TrafficLight_1 [ initially (on -->> yellow_off --> yellow_on --> yellow_off) ] ```