Revision 7d8c1c9871e342728b75f58d5de71e0c3237803e (click the page title to view the current version)

Behavioral/PowerWindow

Changes from 7d8c1c9871e342728b75f58d5de71e0c3237803e to 9b0d959aea55e085083652864435023cc39f0785
---
title: Behavioral Modeling - Power Window
categories: behavioral
...

This example demonstrates modeling both structure and behaviour with variability. 
This example demonstrates modeling both structure and behaviour with variability.

```{.clafer .links}
```

# 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

```clafer
abstract final Feature
abstract final FeatureModel
```

## Model 
## 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.

```clafer
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: 
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.

```clafer
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.

```clafer
abstract State
abstract final StateMachine
```

## Model 
## Model

Commands received by the window motor.

```clafer
motorUp : Command
motorStop : Command
motorDown : Command
```

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

```clafer
abstract WinMotor : Component
    cmd : Port -> Command
```

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

```clafer
abstract final UserRequest
up : UserRequest
expressUp : UserRequest ?
stop : UserRequest
down : UserRequest
```

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

```clafer
[ express <=> expressUp ]
```

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

```clafer
[ all c : WinController | pinchProtection <=> c.pinchDetector ]
```

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. 
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.

```clafer
abstract WinController : Component
    motor : WinMotor
    pinchDetector : Component ?

    req : Port -> UserRequest ?
    endOfTravel : Port ?
    objectDetected : Port ?

    [ lone (req, endOfTravel, objectDetected )]
    [ always req=expressUp => next no req]

    final xor WinStates : StateMachine
        xor movingUp : State
            [ motor.cmd = motorUp ]
            [ req=down     --> movingDown ]
            [ endOfTravel  --> stopped.closed ]
            [ basic && req=stop --> stopped.partlyOpen ]
            [ movingUpX -[objectDetected]-> stopped.partlyOpen ]
            [ !(req=down || endOfTravel || basic && req=stop || movingUpX && objectDetected) --> movingUp ]
            initial basic : State
                [ req=expressUp --> movingUpX ]
                [ !(req=expressUp) --> basic ]
            movingUpX : State
                [ this --> movingUpX ]

        initial xor stopped : State
            [ motor.cmd = motorStop ]
            [ closed && req=down --> movingDown ]
            [ partlyOpen && req=up --> movingUp ]
            [ partlyOpen && req=down --> movingDown ]
            [ 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
            [ motor.cmd = motorDown ]
            [ req=up       --> movingUp ]
            [ endOfTravel  --> stopped.open ]
            [ req=stop     --> stopped.partlyOpen ]
            [ !(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. 
Without the feature `pinchProtection`, the port `objectDetected` should be inactive.
Having the feature will allow receiving the signal `objectDetected`.

```clafer
[ all c : WinController | no pinchProtection => no c.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 

## With continuous chime

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

## With intermittent chime

```clafer
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).

```clafer
abstract ChimingStrategy
    active : State ?
        audible : State ?
```

Concrete strategies:

```clafer
NoChiming : ChimingStrategy
    [ not audible ]
```

```clafer
Continuous : ChimingStrategy
    [ active => audible ]
```

```clafer
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.

```clafer
abstract WinControllerStrategies : WinController
    chime : Port ?
    strategy -> ChimingStrategy
    [ strategy.active <=> movingUpX ]
    [ strategy.active.audible <=> chime ]
```

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.

```clafer
wc : WinController
```

## 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.

```clafer
assert [ wc.req = down && wc.WinStates.stopped.closed --> no wc.WinStates.stopped.closed ]
```

## 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.

```clafer
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.

```clafer
assert [ wc.objectDetected --> no wc.WinStates.movingUp ]
```

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.

```clafer
assert [ wc.objectDetected && wc.WinStates.movingUp.movingUpX --> no wc.WinStates.movingUp ]
```

## 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) ]
```

```{.clafer .ide}
```