Ontospy index format, v0.1

An index is a self-contained SQLite file that holds one ontology in a form a reader can browse and search without parsing RDF. ontospy index produces it (backlog #51); the first reader is the ontospy-ios app (its ADR 0001), and ontospy card (#52) should be able to render from it too.

Status: frozen at v0.1 on 2026-11-28, before the exporter loop. Changes from here on follow §2; the decisions behind the open points are recorded in §10.

1. The file

  • One SQLite 3 file per ontology, with the extension .ontospy-sqlite: specific enough for iOS to route it to the app by its type (§9), and readable as SQLite by anyone. ontospy index always writes this extension, and readers accept only it. Everything a reader needs is inside it, including the manifest, so a single file can be AirDropped.
  • The file is named <id>.ontospy-sqlite. ontospy index also writes <id>.manifest.json next to it, a copy of the manifest for the catalogue to read without opening SQLite. (Named after the id, so several indexes can share a folder.)
  • For download, the catalogue serves the file gzip-compressed (.ontospy-sqlite.gz). The size limit (25 MB, to be confirmed by ontospy-ios #6) applies to the compressed file.
  • Text is UTF-8. IRIs are stored in full, never as CURIEs.

2. Versioning

format_version is MAJOR.MINOR (this spec: 0.1).

  • MINOR changes only add things (a column, a table, a new kind or predicate). A reader that knows 0.1 can read 0.2, ignoring what it doesn’t know.
  • MAJOR changes break readers. A reader refuses an index whose MAJOR it doesn’t know, and says so to the user.
  • While MAJOR is 0, readers should also refuse a MINOR newer than they know: v0 is allowed to break.

3. Manifest

Stored in the manifest table as key/value text, and mirrored in <id>.manifest.json as one object.

Key Example Notes
format_version 0.1 §2
id fibo-fnd Lowercase slug. With the Origin, identifies the ontology in a Library
title FIBO Foundations From the catalogue entry or --title; else the owl:Ontology header’s dcterms:title or dc:title (FOAF has only dc:title); else its best label; else the id
description   Same order as title
version 2025Q2 owl:versionInfo, or --version
source_url   Where the source files came from
licence MIT SPDX id where there is one; free text otherwise
producer ontospy 2.8 (catalogue) Ontospy version, plus catalogue or user
build_date 2026-11-28T14:00:00Z ISO 8601, UTC
source_files JSON array The files merged into this index, in load order: URLs in full, local files by name only (an index shared with others shouldn’t carry the builder’s paths)
counts JSON object Entities per kind (§4), external entities excluded
default_lang en The language label was chosen for
namespaces JSON array The ontology’s own namespaces, e.g. ["http://xmlns.com/foaf/0.1/"]. From the catalogue entry or --namespace; else the most common namespace among the index’s entities that aren’t external (ties broken by sorting). Not the header’s IRI: SKOS’s header is …/skos/core, its entities …/skos/core#…. Readers use it to pick which ontology owns an IRI (§6.5)

4. Kinds

Every entity has one kind:

kind From App row
class all_classes Classes
object_property all_properties_object Object properties
datatype_property all_properties_datatype Datatype properties
annotation_property all_properties_annotation Annotation properties
property the rest of all_properties (plain rdf:Property, as in DC Terms and FOAF) Properties
individual all_individuals (built with hide_individuals=False) Individuals
concept all_skos_concepts Concepts
concept_scheme skos:ConceptScheme instances Shown on the ontology home, not as a row
ontology all_ontologies (each owl:Ontology header, e.g. one per FIBO module) Not a row; the home uses the manifest

If Ontospy puts an IRI in more than one list (a concept scheme that is also an individual, say), the first match in this order wins: ontology, class, the property kinds, concept, concept_scheme, individual.

SHACL shapes are left out of v0.1. They are planned for format 0.2, as kind = shape with sh:targetClass edges (shape → class) and the constraints left to the snippet. A structured constraints table comes only if a reader needs one (see #18).

5. Tables

CREATE TABLE manifest (
  key   TEXT PRIMARY KEY,
  value TEXT NOT NULL
);

CREATE TABLE prefixes (
  prefix    TEXT PRIMARY KEY,          -- '' for the default namespace
  namespace TEXT NOT NULL
);

CREATE TABLE entities (
  id          INTEGER PRIMARY KEY,     -- rowid, also the FTS rowid
  iri         TEXT NOT NULL UNIQUE,    -- exact, case-sensitive (see #40)
  kind        TEXT NOT NULL,           -- §4
  qname       TEXT NOT NULL,           -- e.g. foaf:Person; the full IRI if no prefix fits
  local_name  TEXT NOT NULL,
  namespace   TEXT NOT NULL,           -- the IRI minus local_name
  label       TEXT NOT NULL,           -- best label (§6.3), never empty
  description TEXT,                    -- best description (§6.3)
  defined_by  TEXT,                    -- rdfs:isDefinedBy, if any (e.g. the FIBO module)
  deprecated  INTEGER NOT NULL DEFAULT 0,  -- owl:deprecated true
  external    INTEGER NOT NULL DEFAULT 0,  -- 1 = referred to but not defined (§6.5)
  snippet     TEXT                     -- Turtle (§6.4); NULL for external entities
);
CREATE INDEX entities_kind ON entities(kind, label);

CREATE TABLE labels (
  entity_id INTEGER NOT NULL REFERENCES entities(id),
  predicate TEXT NOT NULL,             -- rdfs:label, skos:prefLabel, skos:altLabel (full IRIs)
  lang      TEXT NOT NULL DEFAULT '',  -- '' when untagged
  text      TEXT NOT NULL
);
CREATE INDEX labels_entity ON labels(entity_id);

CREATE TABLE edges (
  subject_id INTEGER NOT NULL REFERENCES entities(id),
  predicate  TEXT NOT NULL,            -- full IRI, one of §6.2
  object_id  INTEGER NOT NULL REFERENCES entities(id),
  inferred   INTEGER NOT NULL DEFAULT 0  -- 1 = not asserted, e.g. domain inherited from a superproperty
);
CREATE INDEX edges_subject ON edges(subject_id, predicate);
CREATE INDEX edges_object  ON edges(object_id, predicate);

CREATE TABLE annotations (
  entity_id INTEGER NOT NULL REFERENCES entities(id),
  predicate TEXT NOT NULL,             -- full IRI
  value     TEXT NOT NULL,             -- literal text, or an IRI
  is_iri    INTEGER NOT NULL DEFAULT 0,
  lang      TEXT NOT NULL DEFAULT '',
  datatype  TEXT                       -- for typed literals
);
CREATE INDEX annotations_entity ON annotations(entity_id);

CREATE VIRTUAL TABLE search USING fts5(
  label, other_labels, qname, words, description,
  content='',                          -- contentless: rowid = entities.id
  tokenize='unicode61 remove_diacritics 2',
  prefix='2 3'
);

6. Building rules

6.1 Sources and imports

  • The input is a list of files or URLs, loaded into one graph (Ontospy(uri_or_path=[...])). A catalogue entry lists every file that makes up the ontology, e.g. every FIBO FND module.
  • owl:imports is not followed. An import that isn’t in the list contributes nothing, and whatever it would have defined becomes an external entity.
  • hide_base_schemas defaults to on, as in Ontospy. --include-base-schemas turns it off; the RDF, RDFS and OWL indexes need that.
  • Individuals are always built (hide_individuals=False).

6.2 Edges

Edges are taken from the raw graph, not only from Ontospy’s resolved parents. Ontospy keeps a parent only if it built it as an entity, which would lose owl:Thing and every external parent.

Predicate Stored as Notes
rdfs:subClassOf subject → parent Blank-node objects (restrictions) are not edges; they stay in the snippet
owl:equivalentClass as asserted Blank-node objects skipped
rdfs:subPropertyOf subject → parent  
owl:inverseOf as asserted Readers treat it as symmetric
rdfs:domain, rdfs:range property → class Plus inferred = 1 rows for domains/ranges inherited from superproperties (Ontospy’s domains_inferred, #126). owl:unionOf domains are skipped in v0
rdf:type entity → type Includes property characteristics (owl:FunctionalProperty and so on) and the classes of individuals
skos:broader narrower → broader skos:narrower is inverted into skos:broader, so readers only query one direction
skos:related as asserted Symmetric
skos:inScheme concept → scheme skos:topConceptOf and inverted skos:hasTopConcept are also stored as skos:topConceptOf

Every object of an edge must be a row in entities: if the graph doesn’t define it, it is added as an external entity (§6.5). Self-loops (X subClassOf X) are dropped.

6.3 Labels and descriptions

  • labels holds every rdfs:label, skos:prefLabel and skos:altLabel.
  • label is the entity’s best label for default_lang, in this order: a label tagged default_lang; an untagged label; a label in any other language; the local name. rdfs:label is preferred over skos:prefLabel at each step, and ties are broken by sorting. This is bestLabel() once backlog #53 is fixed: the rule lives there, so the HTML docs and the index agree, and labels are the same on every run (today they depend on PYTHONHASHSEED).
  • description is bestDescription(): rdfs:comment, then dcterms:description, dc:description, skos:definition.

6.4 Snippets

snippet is the entity’s rdf_source(format="turtle"): every triple with the entity as subject, plus the blank nodes below it (entityTriples() already gathers them), serialised with the index’s prefixes. Prefix declarations are stripped from each snippet, because readers have the prefixes table. Triples with the entity as object are not included.

6.5 External entities

An entity is external when it is referred to (as an edge object) but no triple in the graph has it as subject. It gets a row with external = 1, a label from the local name, and no snippet.

While base schemas are hidden (§6.1, the default), every IRI in the RDF, RDFS, OWL and XSD namespaces is external, even when the graph says something about it: FOAF gives owl:Thing a label, which would otherwise make FOAF its definer. Readers resolve external entities by IRI against the other indexes in the Library.

An entity that the graph describes only in passing, such as geo:SpatialThing declared in FOAF with a label, is not external: the index defines it, and its namespace shows where it comes from.

An external entity’s kind comes from the edge that refers to it:

Object of kind
rdfs:subClassOf, owl:equivalentClass, rdfs:domain, rdfs:range, rdf:type class
rdfs:subPropertyOf, owl:inverseOf the subject’s property kind
skos:broader, skos:related concept
skos:inScheme, skos:topConceptOf concept_scheme

If two edges disagree, the first row of the table wins. A rdfs:range that is an XSD datatype (xsd:string) is still stored, as an external class, so readers can show the range; readers should show XSD types as datatypes rather than offer to look them up.

The test for the exporter is simple: Ontospy builds referenced classes as entities anyway (?z rdfs:subClassOf ?x), and an entity whose entityTriples() is empty is external.

The same IRI in several ontologies. Because describing something counts as defining it, two ontologies in a Library can both define an IRI (FOAF and the W3C geo ontology both define geo:SpatialThing). A reader resolving an IRI across the Library, or folding duplicate search results, should prefer the ontology with a manifest namespaces entry that the IRI starts with (the longest such entry wins), then any other ontology that defines it. It has to be a prefix match, not equality: FIBO’s IRIs look like …/fibo/ontology/FND/Parties/Parties/Party, while FND lists …/fibo/ontology/FND/. Only if none does is it shown as external.

What doesn’t get a row. Only edges create entity rows. An IRI that appears only as an annotation value (rdfs:seeAlso, dcterms:creator) or only inside a restriction (owl:someValuesFrom) gets no row, so readers can show it but not link it.

6.6 Annotations

annotations holds every other triple with the entity as subject whose object is a literal or an IRI, so long as that triple isn’t already in labels or edges and isn’t the description’s source triple. Blank-node objects are left to the snippet.

There is one search row per entity that isn’t external, with the same rowid as entities.id:

  • label: the best label;
  • other_labels: all other labels, space-separated;
  • qname;
  • words: the local name split on camelCase, _ and - (SpatialThing → Spatial Thing), so a search for “thing” finds it;
  • description.

Ranking is the reader’s job. The query in §7 shows the intended order.

7. What a reader runs

Roots of a kind (here, classes): entities that aren’t external and that have no parent that’s an entity in this index and isn’t external.

SELECT e.* FROM entities e
WHERE e.kind = 'class' AND e.external = 0
  AND NOT EXISTS (
    SELECT 1 FROM edges x JOIN entities p ON p.id = x.object_id
    WHERE x.subject_id = e.id
      AND x.predicate = 'http://www.w3.org/2000/01/rdf-schema#subClassOf'
      AND p.external = 0)
ORDER BY e.label;

Use rdfs:subPropertyOf for property kinds and skos:broader for concepts. Individuals are listed by class: rdf:type edges whose subject has kind = 'individual', grouped by object. The subject filter matters, because every class and property also has rdf:type edges (to owl:Class, owl:ObjectProperty…).

Children are edges by object_id. “Properties with this class as domain” are edges with predicate = rdfs:domain and object_id = class.

Search, ranked by exact label or local name, then label prefix, then any other label match, then a match in the description:

SELECT e.*, CASE
    WHEN lower(e.label) = lower(:q) OR lower(e.local_name) = lower(:q) THEN 0
    WHEN lower(e.label) LIKE lower(:q) || '%' THEN 1
    WHEN EXISTS (SELECT 1 FROM labels l WHERE l.entity_id = e.id
                 AND lower(l.text) LIKE '%' || lower(:q) || '%') THEN 2
    ELSE 3 END AS tier
FROM search s JOIN entities e ON e.id = s.rowid
WHERE search MATCH :fts
ORDER BY tier, length(e.label), bm25(search, 10.0, 5.0, 5.0, 3.0, 1.0)
LIMIT 50;

FTS5 can’t evaluate MATCH per column inside a CASE, so tier 2 checks the labels table directly. The MATCH in WHERE already limits the rows, so it stays cheap. :q is the user’s text as typed, used in the comparisons. :fts is the same text escaped for FTS5, as a quoted phrase with a prefix star ("pers"*), with any " inside it doubled. Within a tier, shorter labels come first, so “pers” puts Person before Personality.

IRIs are deliberately not in the search table: the tokenizer would split them into words, so “foaf” or “http” would match everything. A reader that gets a full IRI as the query looks it up exactly (SELECT * FROM entities WHERE iri = :q) and puts the match first.

8. Tests

The exporter’s tests build indexes for FOAF, SKOS and one FIBO module (FND/Parties/Parties.rdf, release 2026Q3: central, with classes, properties, restrictions and parents in OMG Commons), and compare them against golden files. They compare a normalised dump, not the SQLite bytes: ontospy index --dump writes each table as sorted JSON Lines with the ids replaced by IRIs, and build_date and producer blanked. A golden-file diff is then readable, and it is the pass/fail signal for the Ralph loop.

Checks beyond the golden files:

  • every edges object exists in entities;
  • no label is empty;
  • no two iri values differ only by case unless the source has both (#40);
  • the FIBO module’s index opens and answers the §7 queries;
  • an index whose format_version is unknown is refused by the reference reader in the tests.

9. Distribution notes

  • iOS needs an exported type identifier for .ontospy-sqlite (<reverse-domain>.ontospy.sqlite, conforming to public.database; the domain is settled with the app’s name, ontospy-ios plan § Delivery) so the share sheet and Files offer the app. The app declares it, but the extension is set here.
  • The catalogue’s catalogue.json lists each index’s manifest, plus url, sha256 and size_bytes (compressed).

10. Decisions on the open points

  1. bestLabel() order. Decided 2026-11-28: fix bestLabel() itself (backlog #53), which #51 then depends on.
  2. The external rule (§6.5). Decided 2026-11-28: “no triple with it as subject”, with kind taken from the referring edge and the manifest’s namespaces deciding which ontology owns an IRI.
  3. Shapes. Decided 2026-11-28: not in v0.1; level (A) planned for 0.2 (§4). Deferring keeps #40 off the exporter’s critical path.
  4. The FIBO FND file list. Decided 2026-11-28: release 2026Q3, every FND/**/*.rdf except the AllFND* aggregators (57 modules), as ontospy-examples/tools/fibo_merge.py does for BE. FIBO BE is a second catalogue entry with the same rule, its FND references resolved across the Library. OMG Commons and LCC, which FND imports, are their own catalogue entries. Golden module: FND/Parties/Parties.rdf (§8).
  5. The extension. Decided 2026-11-28: .ontospy-sqlite, and nothing else (no .sqlite fallback).