# Concepts, intents and relationships

> Describe the things in your domain, the actions you can take, and how things connect, without tying them to one system's data structures

Source: https://taxilang.org/docs/language/ontology

Semantic types give meaning to single values, like a `FirstName` or an `OrderId`.
Models describe the data structures that real systems return.

Taxi has three more declarations for describing your domain at a higher level:

| Declaration | What it names | Example |
|---|---|---|
| `concept` | A kind of thing, described by the values it has | A `Person` has a `FirstName` and a `LastName` |
| `intent` | An action, described by what goes in and what comes out | `Purchase` takes a product request and returns a confirmation |
| `relationship` | A named link from one kind of thing to another | A `Person` `worksFor` a `LegalEntity` |

A fourth keyword, `satisfies`, connects your models and operations to them.

## A quick example

```taxi
type PersonId inherits String
type FirstName inherits String
type LastName inherits String
type LegalEntityId inherits String
type LegalEntityName inherits String
type HireDate inherits Date

// The things in the domain
concept Person      { PersonId, FirstName, LastName }
concept LegalEntity { LegalEntityId, LegalEntityName }

// How they connect
relationship worksFor: Person -> LegalEntity?

// An action
write intent Hire(Person, LegalEntityId, HireDate): PersonId

// One system's data, declared against the concepts
model HrEmployer satisfies LegalEntity {
   id: LegalEntityId
   name: LegalEntityName
}

model HrEmployee satisfies Person, worksFor {
   id: PersonId
   first: FirstName
   last: LastName
   employer: HrEmployer?
}

parameter model NewHire {
   personId: PersonId
   first: FirstName
   last: LastName
   employerId: LegalEntityId
   startDate: HireDate
}

service HrApi {
   operation employee(PersonId): HrEmployee
   write operation hire(NewHire): PersonId satisfies Hire
}
```

Here's what each part does:

- `concept Person` says that a person has a `PersonId`, a `FirstName` and a `LastName`. It doesn't say how that data is laid out.
- `model HrEmployee satisfies Person` says that the HR system's employee record is a `Person`. The compiler checks that it has the values `Person` needs.
- `relationship worksFor` names the link from a person to their employer. `HrEmployee` declares that it holds that link, in its `employer` field.
- `write intent Hire` names the action of hiring someone. `HrApi::hire` declares that it performs it, and the compiler checks that its inputs and output fit.

## Why use them?

Models describe the shape of one system's data. Different systems describe the same thing in different shapes.
A person might be a nested JSON object in one API, and a flat CSV row in another.

Concepts, intents and relationships let you describe your domain once, then declare how each system fits it.
The compiler checks each declaration, so mismatches are caught when you compile, not when you run a query.

In queries, you can then ask for things by what they mean. Using the schema above:

```taxi
find { Person } as {
   name: FirstName
   employer: LegalEntityName? = worksFor::LegalEntityName
}
```

This query doesn't name a system, a model or a field path. The compiler works out where `worksFor` is held.

> **Note: For readers who know OWL**
>
> If you've used OWL or other ontology languages: a `concept` is close to a class, and a `relationship` is close to an object property. Taxi's version is checked by the compiler against the models you already have, rather than being a separate description of them.

## Everything is declared

A model only satisfies a concept, or holds a relationship, if it says so with `satisfies`.
An operation only performs an intent if it says so.

The compiler doesn't infer these by looking at structure. A model with exactly the fields a concept needs, but no `satisfies`, isn't treated as that concept.
Inferring matches from structure may come in a later release, for concepts and relationships.

## Concepts can't be created

A concept has no fields and no data of its own. You can't use one as a field type, an operation parameter or return type, or a `given` value.
Anything that "returns a person" returns a model that satisfies `Person`.

The compiler reports `CONCEPT_NOT_CONSTRUCTIBLE` when a concept is used where a value would be created. See [Concepts](https://taxilang.org/docs/language/concepts#where-concepts-can-be-used) for the full list.

## Queries

TaxiQL has three new query forms:

| Query | What it does | Read more |
|---|---|---|
| `find { Person }` | Finds data from a model that satisfies `Person` | [Concepts](https://taxilang.org/docs/language/concepts#querying-concepts) |
| `call Hire` | Calls the operation that performs the `Hire` intent | [Intents](https://taxilang.org/docs/language/intents#calling-an-intent) |
| `worksFor::LegalEntityName` | Follows a relationship inside a projection | [Relationships](https://taxilang.org/docs/language/relationships#following-a-relationship-in-a-query) |

> **Warning: Query execution is coming**
>
> The Taxi compiler supports these query forms today: they compile, and they're type-checked against your schema. Support for running them is still being built in the query engine.

## Next steps

- [Concepts](https://taxilang.org/docs/language/concepts): declaring concepts, and models that satisfy them
- [Intents](https://taxilang.org/docs/language/intents): declaring actions, and operations that perform them
- [Relationships](https://taxilang.org/docs/language/relationships): declaring links between concepts, and following them in queries
