Écrire et vérifier un modèle › Use component frameworks

Écrire et vérifier un modèle

Cette page n’est pas encore traduite : voici la version anglaise.

Component frameworks

Component frameworks such as CORBA, DDS, AUTOSAR and ROS 2 say more about a part than its ports: what it runs when it is made and when it stops, the operations other parts call, what those may raise, and what a part assumes and guarantees. The components extension says these in arche-lang.

Extension — not covered by the 1.0 stability promise (components; the core)

It covers a part's life (on-init, on-start, on-stop, on-release), IDL operations and attributes (oneway, raises, exception, attribute, readonly), structs built on others (extends, abstract), derived values (let), private functions, and assume/guarantee contracts.

A tracker and its clients#

A sensor sends fixes to a tracker. The tracker keeps the latest as a Track, offers it through an interface, and works out its range in kilometres. A display calls the interface.

exception NotFound "Not found" {
  id: uint32
}

struct Reading {
  abstract
  id: uint32
}

struct Track extends Reading {
  range: float32 unit m
}

message FIX "Fix" {
  id:    uint32
  range: float32 unit m
}

interface Tracks "Tracks" {
  readonly attribute count: uint32
  fn latest() -> Track raises NotFound
  oneway fn forget(id: uint32)
}

component Sensor "Sensor" {
  out fixes: data sends FIX
  contract {
    guarantee fixes.range in 0..5000m
  }
  fn main "Measure" ```arche
    wait 1ms
    send fixes FIX { id: 1, range: 1500.0 }
  ```
}

component Tracker "Tracker" {
  in fixes: data receives FIX {
    handler FIX -> keep
  }
  in api:       provides Tracks
  var count:    uint32   = 0
  var ready:    bool     = false
  var last:     Track
  let range_km: float32 = last.range / 1000.0
  contract {
    assume fixes.range in 0..10000m
  }

  on-init {
    count = 0
  }
  on-start {
    ready = true
  }
  on-stop {
    ready = false
  }

  fn keep(id: uint32, range: float32) ```arche
    last = Track { id: id, range: clamped(range) }
    count += 1
  ```
  fn latest() -> Track ```arche
    return last
  ```
  fn forget(id: uint32) ```arche
    count -= 1
  ```
  private fn clamped(r: float32) -> float32 ```arche
    return clamp(r, 0.0, 10000.0)
  ```
}

component Display "Display" {
  out tracks: requires Tracks
  var shown:  uint32   = 0
  fn main "Show" ```arche
    wait 2ms
    let t = tracks.latest()
    shown = t.id
  ```
}

system Radar "Radar" {
  part sensor:  Sensor
  part tracker: Tracker
  part display: Display
  sensor.fixes -> tracker.fixes
  display.tracks -> tracker.api
}

test-plan TP-LIFE "The tracker's life" {
  case TC-READY "Started, then a fix" {
    at 0ms expect tracker.ready
    at 3ms expect tracker.count == 1 && tracker.range_km == 1.5
    at 3ms expect display.shown == 1
  }
}
Diagramme : Radar
Ce qu’archec dessine depuis ce code : Radar
Essayer
$ archec test main.arche
1/1 passed on the browser simulator, 0 failing, 0 known failure(s)

What each part of it does:

  • exception NotFound is a struct that an operation may raise instead of its result: raises NotFound.
  • struct Track extends Reading has the base's fields first: a Track has id and range. abstract says that Reading is only ever extended, so Reading { id: 1 } in a body is an error.
  • The interface Tracks has an attribute, an operation that gives a Track, and a oneway operation that the caller does not wait for.
  • The tracker's on-init and on-start run at 0, before any message. So tracker.ready is true from the first moment of the test.
  • let range_km is always worked out from last: the test reads it as 1.5 once the fix of 1500 m has come.
  • private fn clamped is the tracker's own. Only its bodies call it.
  • The two contracts meet: the sensor sends ranges up to 5000 m, and the tracker takes up to 10000 m.

A part's life#

on-init, on-start, on-stop and on-release are bodies on a component. archec sim and archec test run every part's on-init, then every part's on-start, at 0. That is before the state machines enter their first state and before the fn mains start. At the end of the run they run every on-stop, then every on-release. A component built on another (extends) has the base's when it says none.

Arche's simulator does not run them, so archec test runs the cases of such a system on the browser simulator, and says so.

Operations, attributes and exceptions#

An interface lists what a part provides (in api: provides Tracks) and what another requires (out tracks: requires Tracks). A body calls an operation through the port: tracks.latest(). The provider's function runs at once, and the caller gets its result (calling another part).

  • An argument is in by default. out and inout arguments say that the operation writes them.
  • A oneway operation has no result and no out argument (oneway-result).
  • raises names exceptions. A struct that is not one is raises-not-exception.
  • An attribute is a value the provider holds. readonly says that clients only read it.
  • A provider that lacks an operation is missing-operation. One with another number of arguments is operation-signature. One that makes it private is private-operation, since callers reach it over the link.

archec gen --target proto, ros2 and dds write the interfaces for those toolchains: a service per interface, a .srv per operation. The generator model gives each operation with its arguments' directions, oneway and raises, and each attribute with readonly.

Structs built on others#

struct Track extends Reading puts Reading's fields first. Bodies and every code generator see them: Track { id: 1, range: 80.0 }, t.id. In C the struct has them as fields of its own. An abstract struct cannot be made in a body.

Derived values#

let power: float32 unit W = voltage * current on a component is a value always worked out from the part's variables. A body reads it and never assigns it (derived-assign). Its expression reads variables, constants and other derived values, not functions or ports (derived-pure). Its unit is checked (derived-unit), and a value that reads itself is derived-cycle. In C it is a function of the part, float tracker_range_km(const tracker_t *self).

Contracts#

A contract says what a component assumes of what it receives and guarantees of what it sends. At each link, archec checks that the sender's guarantee implies the receiver's assumption: ranges, equalities and deadlines on the same field. A guarantee that does not meet the assumption is contract-unmet. Verification says how to write the clauses.

Limits#

  • The built-in code generators (c, cpp, embassy, go) do not call on-init, on-start, on-stop or on-release. Only the simulator runs them. The generator model carries them, with their C, C++, Rust and Go, for a generator of your own.
  • No body raises an exception, and a caller cannot catch one: raises is checked and exported, not run.
  • A body cannot read or write an attribute through a port: tracks.count is an error. Attributes are for the generated interfaces.
  • archec sim runs an out argument as an in one: its value does not come back.
  • in-states on a function, the states it may be called in, is documentation: a call in another state is not reported.
  • Contracts are checked on the model, at each link. They are not checked while the simulator runs, and a clause with or or a call is not read (contract-unknown).