Revision 9b0d959aea55e085083652864435023cc39f0785 (click the page title to view the current version)

Behavioral Modeling - Power Window

This example demonstrates modeling both structure and behaviour with variability.

Module Downloads: | [.cfr] | [.html] |

Feature modeling

We model features as final, that is, we do not allow dynamic reconfiguration. Feature configuration from the first snapshot remains unchanged for the entire trace.

Metamodel

abstract final Feature
abstract final FeatureModel

Model

The optional feature express indicates the functionality of automatically closing the window. The feature pinchProtection indicates the functionality of detecting obstacles and stopping the window.

WinFeatures : FeatureModel
manualUpDown : Feature
express : Feature ?
pinchProtection : Feature ?

Component and statemachine modeling

Metamodel

Simple component modeling. Nesting the Port under Component ensures that all ports are always children of components. The components are final because we do not allow dynamic reconfiguration; however, the ports are not final: a port instance can appear to point to a value, disappear, and a new instance can appear to point to a different value.

abstract final Component
abstract Port
abstract final Command

Inside the components, we will embed statemachines modeling their behavior. The states are not final because we want to allow state changes.

abstract State
abstract final StateMachine

Model

Commands received by the window motor.

motorUp : Command
motorStop : Command
motorDown : Command

Window motor. The port cmd is mandatory, meaning that the motor must always be given a command.

abstract WinMotor : Component
cmd : Port -> Command

Requests from the user. Observe, that the request expressUp is optional.

abstract final UserRequest
expressUp : UserRequest ?
stop : UserRequest
down : UserRequest

The user request expressUp is only available when the feature express is present (alphabet variability).

The component pinchDetector of each WinController is only required if the feature pinchProtection is present.

Instances of the ports endOfTravel and objectDetected appear when the corresponding binary signal is received. On the other hand, instances of the port req point to a specific user request. No instance of the port req indicates no user request. Finally, we allow at most one of req, endOfTravel, and objectDetected in order to remove non-determinism. After an expressUp, the user does not need to hold the button: the window keeps moving up on its own.

abstract WinController : Component
motor : WinMotor
pinchDetector : Component ?
req : Port -> UserRequest ?
endOfTravel : Port ?
objectDetected : Port ?
[ always req = expressUp => next no req ]
final xor WinStates : StateMachine
xor movingUp : State
[ req = down --> movingDown ]
initial basic : State
[ ! req = expressUp --> basic ]
movingUpX : State
[ this --> movingUpX ]
initial xor stopped : State
[ closed && req = down --> movingDown ]
[ partlyOpen && req = up --> movingUp ]
[ open && req = up --> movingUp ]
[ ! (closed && req = down || partlyOpen && (req = up || req = down) || open && req = up) --> stopped ]
closed : State
[ this --> closed ]
partlyOpen : State
[ this --> partlyOpen ]
open : State
[ this --> open ]
movingDown : State
[ req = up --> movingUp ]
[ ! (req = up || endOfTravel || req = stop) --> movingDown ]

We model state actions by fixing the motor command. E.g., in state movingDown, motor command is fixed to motorDown.

State transitions are modeled using “next transitions”: -->. In the state movingUp we demonstrate “guarded next transition”: movingUpX -[objectDetected]-> stopped.partlyOpen.

A transition lasts as long as the parent of the state it is in. A transition that leaves its parent state is therefore a constraint of that parent: e.g., closed && req=down --> movingDown is a constraint of stopped, not of closed. Written in closed, it would lapse together with stopped, and it would only force the window to leave stopped, to any state.

A transition only says what happens when its trigger occurs; it does not keep the state from changing otherwise. Therefore, each state also says that it lasts when none of its triggers occurs, e.g., !(req=up || endOfTravel || req=stop) --> movingDown. The substates last as long as their parent: this --> closed lapses when the window leaves stopped.

Disabling objectDetected port

Without the feature pinchProtection, the port objectDetected should be inactive. Having the feature will allow receiving the signal objectDetected.

Variability in behavior using inheritance

Two kinds of window controllers. They are abstract: a concrete controller is one of them, e.g., wc : WinCtrWithChime instead of wc : WinController below. A model with several concrete controllers is solved for all of them together, which takes much longer.

With continuous chime

abstract WinCtrWithContChime : WinController
chime : Port ?
[ movingUpX <=> chime ]

With intermittent chime

abstract WinCtrWithChime : WinController
chime : Port ?
[ no movingUpX => no chime ]
[ chime --> no chime ]
[ no chime && movingUpX --> movingUpX => chime ]

Variability in behaviour using strategy pattern

We first define an abstract chiming strategy: chiming can be audible but only if active (expressed by nesting).

abstract ChimingStrategy
active : State ?
audible : State ?

Concrete strategies:

NoChiming : ChimingStrategy
[ not audible ]
Continuous : ChimingStrategy
[ active => audible ]
Intermittent : ChimingStrategy
[ audible --> not audible ]
[ not audible --> active => audible ]

Now we can parametrize the controller with a chiming strategy. Chiming should be active when moving up in express mode. The chime should be emitted only when the chiming is audible.

abstract WinControllerStrategies : WinController
chime : Port ?
strategy -> ChimingStrategy

A concrete controller chooses its chiming strategy with a constraint, e.g.:

wc : WinControllerStrategies
    [ strategy = Intermittent ]

Scenarios and properties

We define a concrete window controller wc. A property is an assertion: it must hold in every trace of wc, and the command check looks for a counterexample within the scopes and the maximum trace length. A scenario is a constraint on the trace of wc, anchored to the start of the trace with initially: it is possible when the model has a trace with it.

Checking the properties

A counterexample is found quickly, but a property that holds must be checked on every trace up to the maximum trace length, which takes long. The window reaches each of its states within two steps from the start, and each property below is about a state and the next one. Therefore, check them with a shorter trace: the command tl 4 and then check. The times of check in claferIG for each property, one after another:

Property tl 3 tl 4 tl 5 tl 10 (default)
The request down leaves closed holds, 19 s holds, 42 s holds, 81 s holds, 7 min
No motorUp when closed holds, 4 s holds, 8 s holds, 13 s holds, 68 s
Stops moving up after an object counterexample, 2 s counterexample, 1 s counterexample, 1 s counterexample, 1 s
Stops moving up in express mode after an object holds, 6 s holds, 14 s holds, 28 s holds, 141 s
All four (check) 31 s 65 s 123 s 10.5 min

The results are the same for all the trace lengths. With tl 10, the counterexample still comes in a second, but proving the three properties that hold takes ten minutes, most of it for the first one. A property that holds is proven only for the traces up to the maximum trace length and within the scopes.

It must be impossible for the user to request opening and the window remains closed

A transition holds trivially in a state where its source does not, so never (wc.req = down --> wc.WinStates.stopped.closed) would not say this. Instead, the request in the state closed leads to another state.

It must be impossible for the window to be closed and continue sending motorUp command

It is a safety property to prevent burning the motor.

assert [ never wc.WinStates.stopped.closed && wc.motor.cmd = motorUp ]

The window must never continue movingUp after an object was detected

It is a safety property to prevent injury.

This property has a counterexample: the window keeps moving up in basic, while the user holds the button, with an object detected. Only the express mode movingUpX is protected, which the following property states. It has no counterexample.

An example positive scenario

All the Clafer code on a page forms one model. Therefore, the scenarios are not part of the model of this page. To try one, open the model in the IDE and add it under wc.

The window opens, the user reverses it, moves it up in express mode, and an object stops it. The scenario states its first state, since a chain is an implication from its first state, which holds trivially when the trace starts elsewhere.

    [ initially (WinStates.stopped.closed && req=down) ]
    [ initially (WinStates.stopped.closed && req=down --> WinStates.movingDown -->> req=up
        --> WinStates.movingUp.basic && req=expressUp --> WinStates.movingUp.movingUpX -->> objectDetected
        --> WinStates.stopped.partlyOpen) ]

It must be possible to go from closed to open via partiallyOpen state

The window moves down between the states in which it is stopped.

    [ initially WinStates.stopped.closed ]
    [ initially (WinStates.stopped.closed -->> WinStates.movingDown -->> WinStates.stopped.partlyOpen
        -->> WinStates.movingDown -->> WinStates.stopped.open) ]