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
}
}$ archec test main.arche
1/1 passed on the browser simulator, 0 failing, 0 known failure(s)
What each part of it does:
exception NotFoundis a struct that an operation may raise instead of its result:raises NotFound.struct Track extends Readinghas the base's fields first: aTrackhasidandrange.abstractsays thatReadingis only ever extended, soReading { id: 1 }in a body is an error.- The interface
Trackshas an attribute, an operation that gives aTrack, and aonewayoperation that the caller does not wait for. - The tracker's
on-initandon-startrun at 0, before any message. Sotracker.readyis true from the first moment of the test. let range_kmis always worked out fromlast: the test reads it as 1.5 once the fix of 1500 m has come.private fn clampedis 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
inby default.outandinoutarguments say that the operation writes them. - A
onewayoperation has no result and nooutargument (oneway-result). raisesnames exceptions. A struct that is not one israises-not-exception.- An
attributeis a value the provider holds.readonlysays that clients only read it. - A provider that lacks an operation is
missing-operation. One with another number of arguments isoperation-signature. One that makes itprivateisprivate-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 callon-init,on-start,on-stoporon-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:
raisesis checked and exported, not run. - A body cannot read or write an attribute through a port:
tracks.countis an error. Attributes are for the generated interfaces. archec simruns anoutargument as aninone: its value does not come back.in-stateson 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
oror a call is not read (contract-unknown).