<a id="getting-started-label"></a>

# Getting started

This tutorial walks you through the initial setup of the req language tools and the writing of your first specification.

## Prerequisites

The language and tools are straightforward to use, but it is preferable that you are familiar with:

* The use of the terminal, PowerShell for Windows and sh for macOS and Linux
* The use of an IDE, VSCode preferentially

It is recommended that you have VSCode installed on your machine. You can download it [here](https://code.visualstudio.com/).

## Setup

This section will guide you through the installation of the required tools to start specifying with the req language.

#### NOTE
To perform an advanced setup, refer to the README of each tool in the [req-lang GitHub organization](https://github.com/req-lang).

### reqtool

reqtool is the only binary you will need to install. It is a general utility program that we will use to check the validity of a specification.

To install reqtool, open a terminal and copy the command below:

> ### Linux / macOS

> ```bash
> curl -fsSL https://raw.githubusercontent.com/req-lang/req/main/reqtool/install.sh | sh
> ```

> ### Windows

> ```powershell
> irm https://raw.githubusercontent.com/req-lang/req/main/reqtool/install.ps1 | iex
> ```

Authentication might be required to finalize the setup. Once it is done you should be able to run the following command:

```bash
reqtool help
```

A help message should be displayed indicating the available commands and the usage of the program.

### req-vscode

This is the VSCode extension that provides syntax coloring and other features for editing specifications in the IDE. It is recommended that you use it for this tutorial.

1. Go to the extension releases [https://github.com/req-lang/req-vscode/releases](https://github.com/req-lang/req-vscode/releases)
2. Download the req-vscode.vsix file
3. Follow the [official instructions](https://code.visualstudio.com/docs/configure/extensions/extension-marketplace#_install-from-a-vsix) to install a .vsix extension

The extension provides syntax coloring on its own. For diagnostics, hover and completion it uses the `reqlsp` language server (available from the [req-lang GitHub organization](https://github.com/req-lang)), which must be available in your `PATH`.

Open a new text file and save it with the .req extension. Copy the following code into the newly created file:

**Template code**

```req
package System
  part Model
  part

  package Requirements
  package
package
```

If everything works, you should see the keywords and labels highlighted.

#### HINT
This code represents the standard structure of most specifications in req. You will likely use it as a starting point very often.

## Your first specification

In this section we will set up a basic workflow so you understand how to work with req.

#### NOTE
Every specification and every report shown in this tutorial is a real file that was checked with
`reqtool check`. The reports are recorded, not typed: see the `getting-started` folder next to
this page in the documentation repository. On the web page, the lines that change from one step to the next are highlighted.

### Use the checker

Create a new file named intro.req. Copy-paste the above req code into this file. Then open a terminal at the directory where the file is located and execute the following command:

```bash
reqtool check intro.req
```

The `check` command performed a verification of the file `intro.req` and reports what it found:

```none
1:7          E04080 this entity is not used by any requirement
Model
1:7          E04070 this entity is not restricted by any requirement
Model
```

Each issue is a *diagnostic*. It gives the position (`line:column`, both counted from zero), the code
of the issue, what is wrong, and the text found at that position.

Here the checker tells us that the glossary already contains a part, `Model`, that no requirement
talks about. A specification is made of requirements: let’s write one.

#### NOTE
With the `reqlsp` language server installed, the VSCode extension shows the same diagnostics as you type. The command line interface is used here for clarity.

### Adding a requirement

Within the `Requirements` package, add the `Maximum_temperature` requirement:

```req
package System
  part Model
  part

  package Requirements
    requirement Maximum_temperature top bottom
      on System::Model
    is
      @@
        The [[System::Model]] temperature shall be less than 30C.
      @@
    requirement
  package
package
```

This looks like most requirements you have written in Excel or any ALM tool, with two additions:

* `on System::Model` says which entity the requirement is about, and `[[System::Model]]` in the text references it. If `Model` is renamed or deleted, the checker will tell you.
* `top bottom` says this requirement has no parent above it and no refinement below it. We will come back to this when we trace requirements.

Run `reqtool check intro.req` again. It prints nothing: the specification has no diagnostic.

While this is a perfectly valid way to use req, in the next section you will understand what the preferred approach is and why it matters.

### Adding an attribute

Consider the following question: what if several requirements deal with temperature and you want to identify exactly which ones?

This question comes up naturally when searching for contradictions or analyzing the impact of a change.
The notion of [Attribute](../reference/entity.md#reference-attribute-label) and [Part](../reference/entity.md#reference-part-label) was introduced precisely for this. Let’s add an attribute under the `Model` part, then split the requirement in two: rename the existing one to `Model_scope`, as a statement of scope, and add a new `Maximum_temperature` that is about the attribute rather than the whole part:

```req
package System
  part Model
    let temperature in real \ C \
  part

  package Requirements
    requirement Model_scope top bottom
      on System::Model
    is
      @@
        The [[System::Model]] shall keep its temperature under control.
      @@
    requirement

    requirement Maximum_temperature top bottom
      on System::Model::temperature
    is
      @@
        The [[System::Model::temperature]] shall be less than 30C.
      @@
    requirement
  package
package
```

`Maximum_temperature` is now explicitly linked to `temperature`. There is no need to search the specification manually to find requirements related to the concept of system temperature.

#### HINT
When extracting a part or an attribute reference, always prefer the narrowest scope. In the above requirement, we could have written `The [[System::Model]] temperature shall be less than 30C`, but that would lose precision: the requirement constrains a specific attribute, not the system as a whole.

### Your first diagnostic

For the sake of demonstration, delete the `temperature` attribute we just created. The requirements still reference it:

```req
package System
  part Model
  part

  package Requirements
    requirement Model_scope top bottom
      on System::Model
    is
      @@
        The [[System::Model]] shall keep its temperature under control.
      @@
    requirement

    requirement Maximum_temperature top bottom
      on System::Model::temperature
    is
      @@
        The [[System::Model::temperature]] shall be less than 30C.
      @@
    requirement
  package
package
```

Run `reqtool check intro.req`:

```none
17:14        E02010 reference target not found in scope
System::Model::temperature
14:9         E02010 reference target not found in scope
System::Model::temperature
13:16        E04010 requirement is missing a constraint
Maximum_temperature
```

The requirement references something that does not exist. Put the attribute back and the checker is clean again.

The `check` command acts like a compiler for your specifications. A wide variety of diagnostics can be reported; a description of each is available in the [Language Reference](../reference.md), and [req by example](examples.md) shows some of the most common.

### Switching to formal

The requirement we wrote is valid and makes the specification easy to browse. Two things can still be improved:

1. Writing the requirement in plain English adds boilerplate that carries no mathematical meaning.
2. We lose the precise semantics of what we expect from the attribute.

Formal notation addresses both: expressions are concise, and each formal requirement in req is implicitly a *shall* statement.

By preserving the mathematical meaning of the requirement, the compiler can also detect formal contradictions and catch an entirely different class of errors.

Let’s rewrite our requirement in formal notation:

```req
package System
  part Model
    let temperature in real \ C \
  part

  package Requirements
    requirement Model_scope top bottom
      on System::Model
    is
      @@
        The [[System::Model]] shall keep its temperature under control.
      @@
    requirement

    requirement Maximum_temperature top bottom
      on System::Model::temperature
    is
      System::Model::temperature < 30
    requirement
  package
package
```

If we had mistakenly written:

```req
package System
  part Model
    let temperature in real \ C \
  part

  package Requirements
    requirement Model_scope top bottom
      on System::Model
    is
      @@
        The [[System::Model]] shall keep its temperature under control.
      @@
    requirement

    requirement Maximum_temperature top bottom
      on System::Model::temperature
    is
      System::Model::temperature < false
    requirement
  package
package
```

The `check` command would report:

```none
17:6         E03010 expected type Number got Boolean
System::Model::temperature < false
```

This is first-class type checking applied to your specification.

### Make a refinement

So far we have written requirements that stand alone, which is what `top bottom` means. When working with multiple requirements, it is common to make the relationships between them explicit. Here is how to do that in req.

Replace `Model_scope` with a higher-level requirement about the whole part, `Bound_temperature`, and express the traceability link with `refines`. `Maximum_temperature` is no longer `top`, since it now has a parent, and it stays `bottom`:

```req
package System
  part Model
    let temperature in real \ C \
  part

  package Requirements
    requirement Bound_temperature top
      on System::Model
    is
      @@
        The [[System::Model]] shall keep its temperature bound.
      @@
    requirement

    requirement Maximum_temperature bottom
      on System::Model::temperature
      refines System::Requirements::Bound_temperature
    is
      System::Model::temperature < 30
    requirement
  package
package
```

The checker verifies that the link makes sense: `Maximum_temperature` restricts `temperature`, which belongs to `Model`, the part restricted by `Bound_temperature`. Refining a requirement about the battery with one about the wheel would be reported.

This is the complete `intro.req`. It should pass the `check` with no diagnostics: `reqtool check intro.req` prints nothing.

## Wrapping up

In this tutorial you learned how to set up the toolchain for the req language and make use of the compiler diagnostics.
You were introduced to the most common workflow:

1. Write in plain English
2. Extract attributes and parts from the requirements
3. Formalize and trace where relevant
4. Check and fix via `reqtool check`

You have built a specification that is:

* Formal, with mathematical and logical properties that are enforced by the compiler
* Easily browsable, traceable, and analyzable
