Lesson: A Switch and a Lamp
This lesson models a room with a switch and a lamp in SysML v2, verifies three requirements, and reads the trace of a requirement that does not hold.
The Model
The room has two parts. The switch reacts to presses of its button and tells the lamp to toggle; the lamp turns on and off whenever it is told to. The parts communicate through items sent over ports. Declare the items and the port definitions:
Press and Toggle are item definitions without features: only their kind matters here. A ButtonPort receives presses, a TogglePort sends toggles. The in and out keywords give the direction of each item.
The switch is a part with a button, an output and a state machine:
The state machine starts in Off. The transition turnOn fires when a Press arrives on the button while the switch is Off; it sends a new Toggle through the output and moves to On. The transition turnOff does the same in the other direction. Naming the transitions is optional, but the names show up in the witness later.
The lamp has an input port that is the conjugate (~) of TogglePort, so its toggle item flows in:
Finally, the room puts the two parts together:
The room's own button port is bound to the switch's button, and a flow connects the switch's output to the lamp's input.
The model runs in steps. In each step someone may press the button, every state machine fires one enabled transition, and the items sent move along the flows, arriving in the next step. Semantic Decisions states the full semantics.
Requirements and Verification Cases
Requirements talk to the verifier through three calculation definitions, declared at the top of the file:
Semantifyr recognizes them by their names, as Writing a Verification Case says.
A verification case is a verification def whose subject is the system under verification and whose objective is a requirement over it. The first case asks whether the lamp can ever be lit:
The second claims that the lamp is never lit, which is false:
The third claims that the lamp is only ever lit while the switch is on. Before you run it, think about whether it holds:
Verifying
Press Verify all in any of the editors above; each verifies the whole model. Each case is verified in a few seconds.
To verify it on your machine, save the model as room.sysml and run semantifyr-sysml verify room.sysml.
Reading the Trace
A case has a trace when a single execution proves its verdict: a failed mustAlways or a passed eventually requirement. Open the trace of LampLitOnlyWhileSwitchOnViolated; it has two steps:
- The button is pressed:
root.system.switch.Behaviour.turnOnfires and the switch's output sends aToggle. Afterwards the switch isOn, the lamp is stillDark, and the toggle waits onroot.system.lamp.input. - The button is pressed again:
turnOfffires and sends a secondToggle, while the lamp accepts the first one androot.system.lamp.Behaviour.lightUpfires. Afterwards the switch isOffand the lamp isLit.
The requirement fails because the parts communicate asynchronously: the lamp follows the switch one step later. Whether that is a defect of the design or of the requirement is for the engineer to decide.
The SysML trace view shows the same run as a SysML v2 model that refines Room (SysML v2 traces); on your machine it is trace.sysml, next to the case's report.json.