Skip to main content

Semantic Libraries

The goal of this lesson is to write models on a library of classes that says once how a statechart runs, so that each light of the running example is only data: its states, its transitions and the state it starts in.

Running example​

In Modeling and Verification the light was drawn as a state diagram, but the model was not written that way. It was a choice of assumptions over a color, written out again for each light of the junction.

Why would we want to use a library? For reusability. What a state machine does when it runs is the same for every light: a transition fires while the state it leaves is active and its guards hold. If we write it once, as classes, the two lights of the junction, a pedestrian light and any later light can all reuse it. Each of them then only says what it is.

This lesson adds a crosswalk to the junction. It crosses the east road in front of the east light. The pedestrians have a light of their own, which shows stop or walk, and a button they press when they want to cross.

northsensoreastpedestriansbutton

The junction keeps the requirements of the first lesson and gets two more:

  • Pedestrians walk only while the east light is red.
  • Pedestrians can walk.

A statechart as objects​

A library is a handful of ordinary classes. In ours, a state is an object, and a statechart is an object that contains its states and holds the active one:

class State {
container parent: Statechart opposite states

prop isActive(): bool {
return parent.activeState == self
}

tran activate() {
parent.activeState := self
}
}

class Statechart {
contains states: State[0..*] opposite parent
contains transitions: Transition[0..*]
refers initial: State

var activeState: states[0..1] := nothing

redefine init {
inline initial.activate()
}

redefine tran {
choice {
inline for choice (transition in transitions) {
inline transition.main()
}
} or {
}
}
}

A Statechart contains any number of states, [0..*]. Each State knows the statechart that contains it through its container feature, and opposite ties the two directions together. The type of activeState is a feature, states, so the variable holds one of this statechart's states, or nothing (feature-typed variables). A state is active when activeState holds it, and activating a state assigns it to activeState. The statechart's init activates its initial state once, before the first step. In every step, inline for choice fires one of the transitions that can fire, or the statechart stays as it is.

A transition is an object too:

class Transition {
refers from: State
refers to: State

@Trace
redefine tran {
assume(from.isActive())
inline to.activate()
}
}

It refers to the state it leaves and the state it enters, without owning them. Its main transition can be taken only while its source is active, and it activates its target. inline puts the body of the called transition in its place. @Trace makes the Calls view of a trace list each transition that fires.

With these three classes, a traffic light is data:

class TrafficLight : Statechart {
contains red: State subsets states
contains yellow: State subsets states
contains green: State subsets states

redefine refers initial: State = red

contains redToGreen: Transition subsets transitions {
redefine refers from: State = red
redefine refers to: State = green
}
contains greenToYellow: Transition subsets transitions {
redefine refers from: State = green
redefine refers to: State = yellow
}
contains yellowToRed: Transition subsets transitions {
redefine refers from: State = yellow
redefine refers to: State = red
}
}

@VerificationCase
class LightCanTurnGreen : TrafficLight {
redefine prop {
return EF green.isActive()
}
}

The light extends Statechart. Its three states subset the inherited states, so they are states of the light, and its three transitions subset transitions. The nested block of a transition redefines the references it inherits: redefine refers from: State = red binds the source of this transition to the light's red state at compile time. The light has no sensor yet, so it turns green whenever it likes.

Press Open on the block and verify all: LightCanTurnGreen passes, LightCanTurnGreenPassed, and the Calls view of its trace shows redToGreen firing.

The pedestrian light is the first light to reuse the library.

  • Write a class PedestrianLight on the library. It shows stop or walk, starts at stop, and can switch between the two.
  • Add the case PedestriansCanWalk, which asks whether it can show walk.

The case passes, PedestriansCanWalkPassed, and your pedestrian light says nothing about how it runs. Compare it with one solution.

Guards​

A traffic light of the running example turns green only for a waiting car, and only while the crossing traffic is stopped. Such conditions are the guards of a transition. A guard can be a sensor, which the environment sets, as it set the car sensor in the first lesson. A guard can also be a state of another statechart, and then it holds while that state is active:

abstract class Condition {
abstract prop isActive(): bool
}

class Sensor : Condition {
var detects: bool := false

redefine prop isActive(): bool {
return detects
}
}

class State : Condition {
container parent: Statechart opposite states

redefine prop isActive(): bool {
return parent.activeState == self
}

tran activate() {
parent.activeState := self
}
}

Condition is an abstract class: it says that a condition can be active, and leaves it to its subclasses to say when. A transition refers to any number of guards, and inline for assumes each of them before the transition fires:

class Transition {
refers from: State
refers to: State
refers guards: Condition[0..*]

@Trace
redefine tran {
assume(from.isActive())
inline for (guard in guards) {
assume(guard.isActive())
}
inline to.activate()
}
}
statestransitionsfrom, toguardsConditionisActive()StateisActive()activate()Sensorvar detectsStatechartvar activeStateinitialmain: fires oneTransitionmain: fires

The traffic light refers to its sensor and to the states it waits for, in which the crossing traffic is stopped. A light may wait for any number of states, so waitsFor is [0..*]. Its transition to green declares the sensor and every state in waitsFor as guards. The light leaves open which sensor and which states those are:

class TrafficLight : Statechart {
refers sensor: Sensor
refers waitsFor: State[0..*]

contains red: State subsets states
contains yellow: State subsets states
contains green: State subsets states

redefine refers initial: State = red

contains redToGreen: Transition subsets transitions {
redefine refers from: State = red
redefine refers to: State = green
refers carWaiting: Condition subsets guards = sensor
refers crossingClear: Condition[0..*] subsets guards = waitsFor
}
contains greenToYellow: Transition subsets transitions {
redefine refers from: State = green
redefine refers to: State = yellow
}
contains yellowToRed: Transition subsets transitions {
redefine refers from: State = yellow
redefine refers to: State = red
}
}
sensorwaitsForredgreenyellowredToGreengreenToYellowyellowToRedfromtoguards

The junction holds two traffic lights and their sensors, and fills in what each light left open. A light has one sensor, so the junction redefines sensor. A light waits for any number of states, so the junction names each of them in a reference of its own that subsets waitsFor, as the transition named each of its guards. Here the north light waits for the east light's red state, and the east light for the north light's:

class Junction {
contains northSensor: Sensor
contains eastSensor: Sensor

contains north: TrafficLight {
redefine refers sensor: Sensor = northSensor
refers eastStopped: State subsets waitsFor = east.red
}
contains east: TrafficLight {
redefine refers sensor: Sensor = eastSensor
refers northStopped: State subsets waitsFor = north.red
}

redefine init {
inline north.init()
inline east.init()
}

redefine tran {
havoc(northSensor.detects)
havoc(eastSensor.detects)
choice {
inline north.main()
} or {
inline east.main()
}
}
}

The north and the east light are two instances of TrafficLight, each with its own states, transitions and active state. A light contained in the junction does nothing unless the junction calls it: the junction's init starts both lights, and its main transition lets the environment set both sensors and then steps one of the lights.

Open the junction on the library and verify all. The requirements of the first lesson hold as they did on its model: NorthCanTurnGreenPassed, NeverBothGreenPassed and NorthGreenOnlyWhileEastRedPassed. In the Calls view of NorthCanTurnGreen, a transition is named by its path from the root instance, such as root.north.redToGreen, which tells the two lights apart.

The crosswalk​

Now let us add the crosswalk of the running example to the junction, without changing the library.

  • The pedestrian light turns to walk only while its button is pressed and the east light is red.
  • The junction contains the pedestrian light and its button, which the environment presses or releases in every step, as it sets the sensors.
  • PedestriansCanWalk becomes a case of the junction, and the case WalkOnlyWhileEastRed requires that pedestrians walk only while the east light is red.

Before you verify, decide whether WalkOnlyWhileEastRed holds. The pedestrians can walk, PedestriansCanWalkPassed, but with the crosswalk of this state of the junction the requirement fails: WalkOnlyWhileEastRedFailed. Read its trace in Live. When do the pedestrians start to walk, and what does the east light do after that?

The problem is in the model, and the library and the traffic light can stay as they are. Find and fix it!

When it is fixed, every case of the junction passes, WalkOnlyWhileEastRedPassed among them. Compare your repair with the final state of this lesson.

A guard is checked only at the moment its transition fires, and not afterwards. The pedestrians started to walk while the east light was red. After that, nothing kept the east light red, since it waited only for the north light:

startstep 1step 2northeastpedestriansbuttonnoyesnoeast carnonoyesbreaks WalkOnlyWhileEastRed

Now the east light waits for two states, and the junction names both of them in its data.

The lights are data now, and one rule is still written in the model: how the junction steps its lights. The next lesson, Semantic Variants, moves that rule into the library too, and changes it.