Semantic Variants
The goal of this lesson is to change a rule of the statechart library, and to see that under the new rule the same models meet or break their requirements.
Running example
The running example is the junction of Semantic Libraries, with its crosswalk and its requirements: the two traffic lights are never both green, a light is green only while the other one is red, pedestrians walk only while the east light is red, a waiting car can get green and pedestrians can walk. We continue from where that lesson ended, its final state.
Why would anyone change a rule of the library? Because the languages built on statecharts do not agree on their rules, and the same diagram then behaves differently from tool to tool. One rule on which they differ is how the parts of a system step. In some, one part moves in a step while the others wait. In some, every part moves in every step, one after the other. In others, every part moves at once, and each reacts to what it saw before the step. Gamma, for one, has all three, as its asynchronous, cascade and synchronous composition. Whoever defines a language has to know what such a rule does to the models written in it. With the rule in a library, finding out takes one edit and a verification run.
The figure shows one step of the junction under each of the three rules, from both lights red with a car waiting at each.
Composition in the library
The junction still writes how it steps: its init starts its lights, and its main transition lets the environment set the sensors and the button and then steps one of the lights. Any other system written on the library would write the same lines, so that is a rule of the language as well. Let us move it into the library.
- The library has a class
System, which contains any number of sensors and any number of parts, the parts being statecharts. - A system starts every part, and in every step the environment sets every sensor and then one of the parts steps.
- The junction is a system and says only which sensors and which parts it has: its own
initand main transition go.
Verify all. The junction does what it did before, so every case keeps its verdict. The junction is data alone now, as in this state of the lesson.
Cascade composition
Let us change the rule so that every part steps in every step, one after the other, in the order the parts are declared, as a cascade composition does. Change only System, and before you verify, decide the verdict of each case.
Every case keeps its verdict again. Both lights may now move in the same step: why are they still never green together? Compare your rule with the cascade rule of this state.
Synchronous composition
In a synchronous composition, every part reacts to the same moment: each sees the state before the step, and all of them move at once. If a part fired its transition as soon as it chose it, the parts after it would already see its new state. So a step has two halves: first every part chooses the transition it fires, or none, and then all of them move. We get there in three changes, and verify after each.
First, split what a transition does into two transitions of its own: assumeEnabled, which can be taken only when the transition can fire, and fire, which activates the target. @Trace marks fire. The main transition of Transition calls both, so nothing changes yet: verify all, and every case keeps its verdict. Compare with this state.
Second, let a statechart choose before it moves. It remembers its choice in a variable that holds one of its transitions or nothing, as activeState holds one of its states. Give Statechart two transitions, choose, which chooses one of the transitions that can fire, or none, and move, which fires the chosen one. Nothing calls them yet, so every case keeps its verdict again. Compare with this state.
Third, switch the system: after the sensors, every part chooses, and then every part moves. Before you verify, decide the verdict of each case.
This time three of them fail: NeverBothGreenFailed, NorthGreenOnlyWhileEastRedFailed and WalkOnlyWhileEastRedFailed. The other two still pass, NorthCanTurnGreenPassed and PedestriansCanWalkPassed. In the trace of NeverBothGreen, both lights turn green in the same step:
Notice that we did not change any of the primary classes, only their behaviour. The lights, the junction and their cases are as they were. The library's classes kept their features too, except for the one new variable that holds a statechart's choice. In the first lesson, we fixed the guard so that a light turns green only while the other one is red. That guard is enough only as long as the two lights never decide at the same moment. Under the new rule they do: each sees the other one red, and both turn green. The final state of this lesson has the library with the synchronous rule.
Why do the two cases that ask whether something can happen still pass?
What a model means is decided by the library it is written on. This is how Semantifyr gives SysML v2 and Gamma their meaning, each with a library of its own. The next lesson, Extending the Library, goes back to the first rule, one part in a step, and gives the library a new concept.