Skip to content

Ontologies for Developers and AI Engineers

OWL 2 · SHACL 1.0 · SPARQL 1.1 · RDF 1.1

An ontology is a shared, explicit, machine-readable model of what things exist in a domain and how they relate — written down separately from any one database or codebase. A schema tells a machine what data is shaped like; an ontology tells it what data means, so it can infer facts nobody stored. The twist that surprises developers: ontology languages assume an open world, where a missing fact is unknown rather than false — so validation is a separate tool.

ArtifactAddsStandard
GlossaryAgreed terms and definitions—
TaxonomyA broader/narrower hierarchySKOS
ThesaurusSynonyms and related termsSKOS
SchemaShape of recordsJSON Schema, SQL DDL
OntologyTyped relations, rules, inferenceRDFS, OWL

Most teams need less than a full ontology. A product catalog or tag system is a taxonomy: concepts with labels and a hierarchy, which SKOS expresses without any logic. Reach for an ontology when several systems, or an LLM, must agree on what relationships mean.

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ex: <https://example.com/cat#>
ex:laptops a skos:Concept ;
skos:prefLabel "Laptops"@en ;
skos:altLabel "Notebooks"@en ;
skos:broader ex:computers .

Tip: Start with SKOS. A taxonomy can later grow into an ontology; untangling a premature one is harder.

TermMeaning
Triplesubject – predicate – object
IRIGlobal identifier for a thing or relation
LiteralA typed value, such as "99.50"^^xsd:decimal
GraphA set of triples
TurtleThe readable text syntax for RDF

RDF stores every fact as a triple, and every name is an IRI, so two datasets that use the same IRI are talking about the same thing — that is how merging works with no join keys. The examples below all build on this one file, shop.ttl.

PREFIX ex: <https://example.com/shop#>
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#>
ex:Order a owl:Class .
ex:Customer a owl:Class .
ex:VipCustomer rdfs:subClassOf ex:Customer .
ex:placedBy a owl:ObjectProperty ;
rdfs:domain ex:Order ;
rdfs:range ex:Customer .
ex:o42 a ex:Order ;
ex:placedBy ex:ada ;
ex:total "99.50"^^xsd:decimal .

Warning: Mint IRIs on a domain you control and never reuse them for something else. They are as permanent as public URLs.

AxiomA reasoner concludes
A rdfs:subClassOf BEvery A is also a B
p rdfs:domain CAny subject of p is a C
p rdfs:range CAny object of p is a C
p rdfs:subPropertyOf qx p y implies x q y

The ontology part of shop.ttl is a set of axioms. A reasoner applies them to the data and adds the triples they entail — ex:ada never said she is a customer, but the range of ex:placedBy makes her one.

from rdflib import Graph, Namespace, RDF
from owlrl import DeductiveClosure, RDFS_Semantics
EX = Namespace("https://example.com/shop#")
g = Graph().parse("shop.ttl")
g.add((EX.o7, EX.placedBy, EX.widget)) # typo
DeductiveClosure(RDFS_Semantics).expand(g)
print((EX.ada, RDF.type, EX.Customer) in g)
# True
print((EX.widget, RDF.type, EX.Customer) in g)
# True — the typo became a customer

Gotcha: rdfs:domain and rdfs:range are not constraints. They never reject data; they classify whatever they are given.

AssumptionDatabasesOWL
Missing factFalseUnknown
Two namesTwo thingsMaybe the same thing
ContradictionRejected writeInconsistent ontology

OWL 2 adds richer axioms — disjoint classes, cardinality, inverse, transitive and functional properties. Its profiles (EL, QL, RL) trade expressiveness for fast reasoning; RL is the one rule engines and triple stores usually implement.

from rdflib import Graph, Namespace, OWL, RDF
from owlrl import (
DeductiveClosure, OWLRL_Semantics)
EX = Namespace("https://example.com/shop#")
g = Graph().parse("shop.ttl")
g.add((EX.placedBy, RDF.type,
OWL.FunctionalProperty))
g.add((EX.o42, EX.placedBy, EX.bob))
DeductiveClosure(OWLRL_Semantics).expand(g)
print((EX.ada, OWL.sameAs, EX.bob) in g)
# True — not an error

Gotcha: “At most one customer” does not reject a second one. Without a unique-name assumption, OWL concludes the two customers are the same individual.

ConstraintChecks
sh:targetClassWhich nodes the shape applies to
sh:minCount / sh:maxCountHow many values
sh:datatypeLiteral type
sh:classType of the linked node
sh:minInclusiveNumeric range

SHACL is the closed-world counterpart: shapes describe what valid data looks like and a validator returns a report. Use OWL to state meaning and SHACL to gate writes, the way you use types and runtime checks.

PREFIX ex: <https://example.com/shop#>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
ex:OrderShape a sh:NodeShape ;
sh:targetClass ex:Order ;
sh:property [
sh:path ex:placedBy ;
sh:minCount 1 ; sh:maxCount 1 ;
sh:class ex:Customer ] ;
sh:property [
sh:path ex:total ;
sh:datatype xsd:decimal ;
sh:minInclusive 0 ] .

Gotcha: SHACL does no inference. Validating shop.ttl fails with “Value does not have class ex:Customer”, because ex:ada is a customer only by inference. Type data explicitly or run pySHACL with inference="rdfs".

PatternDoes
?s ex:p ?o .Match triples
;Same subject, next predicate
p*Zero or more hops
FILTER (...)Keep matching rows
OPTIONAL { }Left join

SPARQL matches graph patterns: every triple in WHERE must hold for the same variable bindings. Property paths let a query walk a class hierarchy itself, which covers many cases where you would otherwise run a reasoner first.

PREFIX ex: <https://example.com/shop#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?order ?who ?total WHERE {
?type rdfs:subClassOf* ex:Customer .
?who a ?type .
?order ex:placedBy ?who ;
ex:total ?total .
FILTER (?total > 50)
}

With ex:ada a ex:VipCustomer added, this returns ex:o42, ex:ada, 99.50.

StepPractice
ScopeWrite competency questions it must answer
ReuseImport existing vocabularies before minting terms
ModelClasses, then properties, then constraints
TestEach competency question becomes a SPARQL test
Versionowl:versionIRI and changelog; deprecate, never delete

Competency questions — “Which VIP customers ordered over 50 this month?” — keep scope honest: a term no question needs does not belong yet. Reuse shared vocabularies such as schema.org, Dublin Core or your industry’s standard so partners already understand your terms.

Gotcha: Modeling every noun as a class is the classic beginner mistake. “Shipped” is a status value, not a subclass of Order.

UseHow the ontology helps
LLM extractionAllowed types and predicates bound the output
GraphRAGTyped relations make graph hops meaningful
Agent toolsOne vocabulary for tool arguments and results
Data contractsOne model generates code-level types

An LLM turns unstructured text into triples well only when it knows the allowed terms. Derive the structured-output schema from the ontology, so the model cannot invent a predicate, then run SHACL on the result. LinkML takes the opposite route: one YAML model generates Pydantic classes, JSON Schema, SHACL and OWL.

from rdflib import Graph, RDF, OWL
g = Graph().parse("shop.ttl")
preds = sorted(
g.qname(p) for p in
g.subjects(RDF.type, OWL.ObjectProperty))
triple = {
"type": "object",
"properties": {
"s": {"type": "string"},
"p": {"enum": preds}, # ["ex:placedBy"]
"o": {"type": "string"}},
"required": ["s", "p", "o"]}

Warning: An LLM-drafted ontology looks plausible and is often subtly wrong. Treat it as a draft for a domain expert, not a source of truth.

NeedTools
Author OWLProtégé (desktop), WebProtégé (shared)
Govern taxonomiesPoolParty, TopBraid EDG
Store and queryGraphDB, Stardog, Apache Jena Fuseki
Coderdflib, pySHACL, OWL-RL, Apache Jena
Schema-firstLinkML
Property graphNeo4j with neosemantics

Pick by job, not by feature list. Editors model; governance platforms add workflow, approval and stewardship for business users; triple stores query and reason at scale. Data catalogs such as OvalEdge govern glossaries and lineage and pair with a separate editor for deep OWL modeling.

Note: Vendor “best tools” roundups rank their own product first. Shortlist by the job above, then prototype on your own data.