---
question: How do I write an ontology a module sends to the schema graph?
kind: task
via_web: soon
via_cli: available
via_python: available
via_api: na
via_config: na
ref_web: File › New Class
ref_cli: abi new module
ref_python: triple_store.load_schema
prereq: You need a module of your project. [Create a module](/capabilities/build/modules/creating-a-module) first.
next:
  - /capabilities/build/knowledge-graph/enable-ontology
  - /capabilities/build/knowledge-graph/creating-a-pipeline
---

# Create an ontology

Ontologies are the schema layer of ABI's knowledge graph. They define classes, properties, and axioms in OWL Turtle format.

An ontology is a `.ttl` file under the `ontologies/` folder of a module's package. Once the module is up, it sends every `*.ttl` there to the engine's schema graph. There is no `abi new ontology` command and no REST call that writes the file: you write Turtle.

## Design before you code

Before writing Turtle, answer these questions:

1. **What entities do you model?** (Continuants: organizations, persons, products)
2. **What events/processes do you model?** (Occurrents: acts of partnership, employment, purchase)
3. **What properties link them?** (Object properties: `hasOwner`, `isPartOf`)
4. **What data properties do they carry?** (Datatype properties: `name`, `url`, `externalId`)
5. **What upper ontology classes do they extend?** (ABI's `abi:` classes, CCO classes, BFO categories)

Do not create a class that already exists in BFO or CCO. Look it up first, and keep a module-local name with `owl:equivalentClass` or `rdfs:subClassOf` pointing at the CCO class, so your pipelines keep stable names.

## The seven buckets

Every class traces to one of the seven BFO buckets. Subclass the matching `abi:` class and the trace comes with it:

| Bucket | Question | Subclass |
|---|---|---|
| WHAT | what is happening | `abi:Process` |
| WHEN | over what interval | `abi:TemporalRegion` |
| WHO | which participant | `abi:MaterialEntity`, `abi:Person` |
| WHERE | at what place | `abi:Site` |
| HOW WE KNOW | which record | `abi:GenericallyDependentContinuant` |
| HOW IT IS | which quality | `abi:Quality` |
| WHY | which role or disposition | `abi:Role`, `abi:Disposition` |

Give qualities, roles and dispositions an `abi:inheresIn` restriction that names their bearer. Use the `abi:` properties (`abi:hasParticipant`, `abi:occursIn`, `abi:concretizes`, `abi:inheresIn`, ...) rather than raw `bfo:` ones, so their inverses carry over.

## Ontology file structure

`abi new module` writes the starter file and the folder's rules:

```text
src/my_module/ontologies/
├── AGENTS.md
└── modules/
    └── MyModuleOntology.ttl      # shared classes and properties
```

Any folder under `ontologies/` works: the module finds `*.ttl` recursively, and skips every path with `sandbox` in it, so a `sandbox/` folder holds work in progress. Keep one process per file in `processes/` if you split them out; the workspace catalog lists only files that declare `a owl:Ontology` outside `processes/`.

Anything in these files lands in the engine's live triple store, so keep named individuals out: they belong in pipeline output.

## Web UI

Coming soon. The workspace Ontology screen has **New Class** and **New Object Property** in its File menu, but both are disabled: "Creation is unavailable until changes can be saved to an ontology file." Write the Turtle with the CLI or Python steps on this page. To bring in an existing file, see [Import an ontology](/capabilities/build/knowledge-graph/import-an-ontology).

## CLI

### Scaffold the files

There is no `abi new ontology` command. A module comes with a starter ontology file. From the project root:

```bash
abi new module my_module
```

It writes `src/my_module/ontologies/modules/MyModuleOntology.ttl` and `src/my_module/ontologies/AGENTS.md`, which states the rules, and declares `triple_store` in the module's dependencies. The command is [`abi new module`](/user-guide/reference/cli#abi-new-module).

### Write the classes

Edit the starter file, or add others under `ontologies/`. The shape of a module ontology is under [Writing a Turtle file](#writing-a-turtle-file) below.

### Check that it parses

From `src/my_module/ontologies`:

```bash
uv run --with rdflib python -c "import rdflib, pathlib; [rdflib.Graph().parse(p) for p in pathlib.Path('.').rglob('*.ttl')]"
```

The engine refuses a file that is not Turtle (`INVALID_ARGUMENT`). The module logs that error once and does not send the file again until it restarts: fix the file, then restart the module. Only a transient failure, such as the engine being down or busy, is retried in the background.

### Restart and check

Restart the module. Once it logs `READY`, it sends the files. Query the schema graph for your classes. With `abi dev up`, `abi dev ports` prints the triple store's port (`oxigraph`):

```bash
curl -s -X POST "http://localhost:<oxigraph-port>/query" \
  -H "Content-Type: application/sparql-query" \
  -H "Accept: application/sparql-results+json" \
  --data 'SELECT ?class WHERE { GRAPH <http://ontology.naas.ai/graph/schema> {
      ?class a <http://www.w3.org/2002/07/owl#Class>
      FILTER(STRSTARTS(STR(?class), "http://ontology.naas.ai/my_module/")) } }'
```

Your new class should appear. With the Docker stack the triple store is Fuseki (http://localhost:3030); the Nexus Ontology screen shows the same classes once the file is [enabled in the workspace](/capabilities/build/knowledge-graph/enable-ontology).

## Python

### Write the Turtle file

Save it under the package's `ontologies/` folder, for example `src/my_module/ontologies/modules/MyModuleOntology.ttl`. The shape is under [Writing a Turtle file](#writing-a-turtle-file) below.

### Declare the service

```python title="src/my_module/__init__.py"
class ABIModule(BaseModule[MyModuleConfiguration]):
    dependencies = ModuleDependencies(
        services=("model_registry", "document", "triple_store")
    )
    # ontologies = None   # the default: every ontologies/**/*.ttl, except under "sandbox"
    # ontologies = ("modules/MyModuleOntology.ttl",)   # or name the files; () sends none
```

A module with ontologies that does not declare `triple_store` stops at start, with a message. So does a file name the engine would refuse: letters, digits, `_`, `.` and `-`, ending in `.ttl`.

### Send an ontology built at run time

`run_module` sends the files of `ontologies/`. For an ontology the module builds itself, call the triple store service from a method of the module:

```python
changed = await self.engine.services.triple_store.load_schema(
    "module/my_project.my_module/generated/MyGeneratedOntology.ttl", turtle_text
)
```

The source is `module/<module_id>/<path ending in .ttl>`, under the module's own id (`PERMISSION_DENIED` otherwise). `abi new module my_module` in the project `my_project` sets `module_id = "my_project.my_module"`. `load_schema` returns `True` when it wrote the schema graph and `False` when that source already held this content. Content that is not Turtle is refused (`INVALID_ARGUMENT`).

## REST API

Not applicable. There is no REST endpoint that writes an ontology file. The Nexus `/api/ontology` routes read the ontologies; none writes one. Write Turtle with the CLI or Python steps, or [import a file in Nexus](/capabilities/build/knowledge-graph/import-an-ontology). The process list is the [API](/api).

## Config

Not applicable. No `config.yaml` key declares classes or picks the files: the module sends its own `ontologies/`, and `ontologies = (...)` on the module class narrows that list.

## Writing a Turtle file

A module ontology declares itself, imports the ABI ontology, and then defines properties and classes. This is the shape of the scaffolded `modules/MyModuleOntology.ttl` (abridged):

```turtle title="ontologies/modules/MyModuleOntology.ttl"
@prefix owl:  <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix dc:   <http://purl.org/dc/terms/> .
@prefix abi:  <http://ontology.naas.ai/abi/> .
@prefix my_module: <http://ontology.naas.ai/my_module/> .

my_module:MyModuleOntology a owl:Ontology ;
  # One owl:imports statement per IRI. The engine does not resolve them: the
  # ABI ontologies are already in its schema graph.
  owl:imports <http://ontology.naas.ai/abi/Ontology> ;
  owl:imports <http://ontology.naas.ai/abi/PersonOntology> ;
  dc:title "MyModule Ontology"@en .

# Pair every relation with its inverse, and subclass an abi: relation.
my_module:hasRecord a owl:ObjectProperty ;
    rdfs:subPropertyOf abi:isCarrierOf ;
    owl:inverseOf my_module:isRecordOf ;
    rdfs:label "has record"@en ;
    skos:definition "Relates a person to a record that generically depends on them."@en ;
    rdfs:domain abi:Person ;
    rdfs:range my_module:MyModuleRecord .

my_module:record_id a owl:DatatypeProperty ;
    rdfs:label "record id"@en ;
    skos:definition "Identifier of the record in the source system."@en ;
    rdfs:domain my_module:MyModuleRecord ;
    rdfs:range xsd:string .

# HOW WE KNOW: a record about something
my_module:MyModuleRecord a owl:Class ;
    rdfs:subClassOf abi:GenericallyDependentContinuant ,
      [ a owl:Restriction ; owl:onProperty my_module:isRecordOf ; owl:someValuesFrom abi:Person ] ;
    rdfs:label "My Module Record"@en ;
    skos:definition "An information content entity that documents something about a person."@en ;
    skos:example "A record with the identifier 12345 in the source system."@en .
```

- **Absolute IRIs only**, under `http://ontology.naas.ai/my_module/`. A relative IRI resolves against `http://ontology.naas.ai/module/<module_id>/<path>`.
- Do not use the engine's own `http://triple-store.internal` vocabulary: the engine refuses the file.

A pipeline that writes data with these classes is on [Create a pipeline](/capabilities/build/knowledge-graph/creating-a-pipeline).

## Ontology best practices

1. **Start with existing classes**: reuse BFO, CCO and `abi:` classes before minting your own.
2. **One concept per class**: keep classes focused; avoid "God classes" with many unrelated properties.
3. **Document everything**: `rdfs:label`, `skos:definition`, and `skos:example` for every class and property. Agents use these descriptions.
4. **Pair every relation with its inverse**, and subclass an `abi:` relation.
5. **Stable IRIs** under `http://ontology.naas.ai/<module>/`.
6. **Remove on purpose**: loading is additive, so a class you delete from the file stays in the schema graph. Adding classes and properties is safe; removing or renaming one needs its triples removed from the schema graph too.
