.. _examples-label: 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 `__ 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. .. literalinclude:: examples/01-first-requirement.req :language: req `Open the "A first requirement" example in the playground `__ 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. .. literalinclude:: examples/02-attributes-and-units.req :language: req `Open the "Attributes and units" example in the playground `__ .. 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. .. literalinclude:: examples/03-refinement.req :language: req `Open the "Refinement" example in the playground `__ 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. .. literalinclude:: examples/04-temporal.req :language: req `Open the "Temporal properties" example in the playground `__ .. 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. .. literalinclude:: examples/05-conditions.req :language: req `Open the "Conditions" example in the playground `__ Domains and sets *********************** A domain restricts the values an attribute may take. Braces write a finite set and square brackets write an interval. .. literalinclude:: examples/06-domains-and-sets.req :language: req `Open the "Domains and sets" example in the playground `__ 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. literalinclude:: examples/diagnostics/reference-not-found.req :language: req .. literalinclude:: examples/diagnostics/reference-not-found.out :language: none :caption: reqtool check Add the missing ``radius`` attribute to ``Wheel``, or fix the reference. `Open the "Reference not found" example in the playground `__ A formal expression with the wrong type ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. literalinclude:: examples/diagnostics/type-mismatch.req :language: req .. literalinclude:: examples/diagnostics/type-mismatch.out :language: none :caption: reqtool check A duration is a number: compare it with a number. `Open the "Type mismatch" example in the playground `__ A refinement between unrelated parts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. literalinclude:: examples/diagnostics/unrelated-refinement.req :language: req .. literalinclude:: examples/diagnostics/unrelated-refinement.out :language: none :caption: reqtool check ``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 `__