<a id="examples-label"></a>

# By Examples

Short, complete specifications, one idea at a time. Each one is a real `.req` file, kept in
`source/tutorials/examples` of the documentation repository (under the [req-lang](https://github.com/req-lang)
organization) and checked with `reqtool check` there. When a file has diagnostics, the report shown under it
is the recorded, unedited output. `reqtool check` prints nothing when there is none, which is the case of the
first six examples: they show no report.

The first six examples describe a *sensor node*: a battery-powered device that measures and
reports over a radio.

#### NOTE
Reading the examples, three words appear in requirement headers:

* `on` names the entity the requirement restricts. A formal requirement must use it in its expression.
* `top` marks a requirement that needs no parent requirement above it.
* `bottom` marks a requirement that needs no refinement below it.

A requirement with both `top` and `bottom` stands alone. Without them, the checker expects the
requirement to be linked to its parent and children with `refines`.

## A first requirement

A requirement in plain English, attached to the part it is about. Writing `[[Radio]]` inside the
text references the part, so the glossary and the sentence cannot drift apart.

```req
package Sensor_node
  part Radio
  part

  requirement Report_interval top bottom
    on Radio
  is
    @@
      The [[Radio]] shall transmit a measurement report every 10 minutes.
    @@
  requirement
package
```

[Open the “A first requirement” example in the playground](https://req-lang.org/playground.html#example=examples/01-first-requirement)

## Attributes and units

Attributes are the typed quantities requirements talk about. They live in a part, have a domain
such as `real` or `integer`, and may carry a unit between backslashes. Formal requirements
constrain one attribute each.

```req
package Sensor_node
  part Battery
    let voltage in real \ V \
    let temperature in real \ C \
  part

  requirement Battery_supply top bottom
    on Battery
  is
    @@
      The [[Battery]] shall power the node for five years without replacement.
    @@
  requirement

  requirement Voltage_floor top bottom
    on Battery::voltage
  is
    Battery::voltage >= 2.0
  requirement

  requirement Temperature_ceiling top bottom
    on Battery::temperature
  is
    Battery::temperature < 60
  requirement
package
```

[Open the “Attributes and units” example in the playground](https://req-lang.org/playground.html#example=examples/02-attributes-and-units)

#### NOTE
Units are free-form labels for now: the checker does not validate them.

## Refinement

A requirement can be *refined* by more precise ones. `refines` links a requirement to its
parent, and the checker verifies the link makes sense: a refining requirement must restrict an
entity inside what its parent restricts.

```req
package Sensor_node
  part Radio
    let report_period in real \ min \
    let payload_size in integer \ B \
  part

  requirement Regular_reporting top
    on Radio
  is
    @@
      The [[Radio]] shall report often enough to detect a sensor
      failure within 30 minutes, using small payloads.
    @@
  requirement

  requirement Report_period bottom
    on Radio::report_period
    refines Regular_reporting
  is
    Radio::report_period <= 10
  requirement

  requirement Payload_size bottom
    on Radio::payload_size
    refines Regular_reporting
  is
    Radio::payload_size <= 51
  requirement
package
```

[Open the “Refinement” example in the playground](https://req-lang.org/playground.html#example=examples/03-refinement)

## Temporal properties

Behavior over time is written with temporal operators such as `always`, `eventually`,
`previously` and `rising`. The tags between `#` records which verification technique the
property is meant for.

```req
package Sensor_node
  part Cooling
    let overheating in boolean
    let fan_on in boolean
  part

  requirement Fan_reacts top
    on Cooling
  is
    @@
      The [[Cooling]] shall start the fan whenever the node overheats.
    @@
  requirement

  # verification model-checking #
  requirement Fan_follows_overheat bottom
    on Cooling::overheating
    refines Fan_reacts
  is
    always (Cooling::overheating implies eventually Cooling::fan_on)
  requirement

  requirement Fan_stays_off_when_cool bottom
    on Cooling::fan_on
    refines Fan_reacts
  is
    rising Cooling::fan_on implies previously Cooling::overheating
  requirement
package
```

[Open the “Temporal properties” example in the playground](https://req-lang.org/playground.html#example=examples/04-temporal)

#### NOTE
Today the checker type-checks temporal properties. Verifying them with a model checker is
on the roadmap and is not available yet.

## Conditions

`when` lists cases, with `otherwise` as the fallback. Every case whose condition holds applies, so the
conditions below do not overlap. Conditions can reference attributes of other parts, so requirements can
cross the glossary.

```req
package Sensor_node
  part Battery
    let voltage in real \ V \
  part

  part Radio
    let tx_power in real \ dBm \
  part

  requirement Battery_supply top bottom
    on Battery
  is
    @@
      The [[Battery]] shall remain within its safe voltage range.
    @@
  requirement

  requirement Voltage_range top bottom
    on Battery::voltage
  is
    Battery::voltage >= 2.0 and Battery::voltage <= 3.6
  requirement

  requirement Power_saving top
    on Radio
  is
    @@
      The [[Radio]] shall lower its transmit power as the battery drains.
    @@
  requirement

  requirement Adaptive_power bottom
    on Radio::tx_power
    refines Power_saving
  is
    when
      Battery::voltage > 3.0 then Radio::tx_power = 14,
      Battery::voltage > 2.4 and Battery::voltage <= 3.0 then Radio::tx_power = 8,
      otherwise Radio::tx_power = 0
    end
  requirement
package
```

[Open the “Conditions” example in the playground](https://req-lang.org/playground.html#example=examples/05-conditions)

## Domains and sets

A domain restricts the values an attribute may take. Braces write a finite set and square
brackets write an interval.

```req
package Sensor_node
  part Radio
    let channel in { 868, 915 }
    let duty_cycle in [ 0, 1 ]
  part

  requirement Regional_compliance top
    on Radio
  is
    @@
      The [[Radio]] shall comply with the European 868 MHz band rules.
    @@
  requirement

  requirement Band bottom
    on Radio::channel
    refines Regional_compliance
  is
    Radio::channel = 868
  requirement

  requirement Duty_cycle_limit bottom
    on Radio::duty_cycle
    refines Regional_compliance
  is
    Radio::duty_cycle <= 0.01
  requirement
package
```

[Open the “Domains and sets” example in the playground](https://req-lang.org/playground.html#example=examples/06-domains-and-sets)

## What the checker reports

A diagnostic gives the position, the code, what is wrong and the text it found there. These
three are among the first you will meet.

### A reference to something that does not exist

```req
package System
  part Wheel
  part

  requirement Wheel_size top bottom
    on Wheel
  is
    @@
      The [[Wheel]] radius, [[Wheel::radius]], shall be below 30 cm.
    @@
  requirement
package
```

**reqtool check**

```none
8:30         E02010 reference target not found in scope
Wheel::radius
```

Add the missing `radius` attribute to `Wheel`, or fix the reference.

[Open the “Reference not found” example in the playground](https://req-lang.org/playground.html#example=examples/diagnostics/reference-not-found)

### A formal expression with the wrong type

```req
package System
  part Wheel
    let deceleration_duration in real \ s \
  part

  requirement Wheel_deceleration top
    on Wheel
  is
    @@
      The [[Wheel]] shall decelerate within 3s.
    @@
  requirement

  requirement Deceleration_bound bottom
    on Wheel::deceleration_duration
    refines Wheel_deceleration
  is
    Wheel::deceleration_duration < false
  requirement
package
```

**reqtool check**

```none
17:4         E03010 expected type Number got Boolean
Wheel::deceleration_duration < false
```

A duration is a number: compare it with a number.

[Open the “Type mismatch” example in the playground](https://req-lang.org/playground.html#example=examples/diagnostics/type-mismatch)

### A refinement between unrelated parts

```req
package System
  part Wheel
    let deceleration_duration in real \ s \
  part

  part Battery
    let voltage in real \ V \
  part

  requirement Battery_supply top bottom
    on Battery
  is
    @@
      The [[Battery]] shall supply the drive.
    @@
  requirement

  requirement Wheel_deceleration top
    on Wheel
  is
    @@
      The [[Wheel]] shall decelerate within 3s.
    @@
  requirement

  requirement Battery_voltage bottom
    on Battery::voltage
    refines Wheel_deceleration
  is
    Battery::voltage > 20
  requirement

  requirement Deceleration_bound bottom
    on Wheel::deceleration_duration
    refines Wheel_deceleration
  is
    Wheel::deceleration_duration <= 3
  requirement
package
```

**reqtool check**

```none
27:12        E04190 refinement does not restrict a child of the refined restriction
Wheel_deceleration
```

`Battery_voltage` claims to refine a requirement about the wheel, but it restricts a battery
attribute. Either the link is wrong, or the glossary is missing the relationship between the two.

[Open the “Unrelated refinement” example in the playground](https://req-lang.org/playground.html#example=examples/diagnostics/unrelated-refinement)
