Agentic Research

Advanced Ontology Design: From a Reasonable Model to a Production-Grade Semantic Layer for Agents

2026/10/2430 min readBryan Chan閱讀中文原文
TopicsontologyOWLSHACLknowledge graphAI agents

Core thesis: An ontology is not a database. It constrains what may be said, not what is stored. Angle: Production trade-offs: OWL profile selection, SHACL constraints, entity resolution, bitemporal modelling and provenance, and agent tool contracts.

1. Who this is for

The introductory article already covered Class, Instance, Relation, Axiom, TBox/ABox, the 12 design dimensions, and the eight-step process. This one does not repeat the basics. It deals with what production systems always hit:

  • How do you trade expressivity against reasoning cost?
  • How do OWL and SHACL divide the work?
  • How does an ontology become modular, versioned, and scalable?
  • How do you handle multi-tenancy, time, entity resolution, and provenance?
  • How can an agent use the ontology safely instead of stuffing all of it into a prompt?
  • How do you quantify whether the ontology is actually useful?

2. Formal foundations: description logic, OWL 2, and the open world

2.1 The minimum intuition behind Description Logic

Description Logic (DL) splits a domain into three kinds of things:

  • Concept: Account, JournalEntry
  • Role (relation): debits, posts
  • Individual: Account#1000, JE#20261021-001

The value of DL is its model-theoretic semantics, which gives a precise definition of what “correct inference” means. For example:

  • CashAccount is a subclass of AssetAccount, and Account#1000 is a CashAccount, therefore Account#1000 must be an AssetAccount.
  • Every JournalEntry has at least one JournalLine; an entry with no lines makes the model inconsistent.

That is the division of labour between the TBox (concepts) and the ABox (instances).

2.2 The four OWL 2 profiles

OWL 2 is not a single language but a family of profiles. Choosing the wrong one usually means “reasoning is too slow” or “semantics are too weak”, not “the features are missing”.

The expressivity and tractability trade-off across OWL 2 profiles

ProfileExpressivityReasoning complexityBest for
OWL 2 ELMedium (existential quantification, intersection)Polynomial timeLarge-scale classification, enterprise master data
OWL 2 QLWeaker (rewriting-friendly)LowOntology-based data access, relational databases
OWL 2 RLRule-oriented (rule engines)Polynomial timeReal-time inference, data integration
OWL 2 DLHigh (SROIQ)HighComplex semantics, medical and academic ontologies

Practical advice:

  • Enterprise master data and chart-of-accounts classification: start with EL.
  • Mapping the ontology onto SQL and relational queries: use QL.
  • Rule engines and real-time inference: use RL.
  • Only reach for DL when you genuinely need complex axioms (cardinality, inverse roles, complex roles).

2.3 The open world and the unique name assumption

OWL assumes the open world (OWA): what has not been stated is not thereby false. SQL and SHACL usually assume the closed world (CWA): if it cannot be found, it does not exist.

There is another common trap: the unique name assumption (UNA). OWL does not assume that different URIs denote different individuals. If you need “different ID means different entity”, handle it explicitly in the business layer or in SHACL.

Conclusion:

  • Semantic reasoning: OWL (OWA).
  • Data integrity: SHACL and SQL (CWA).
  • Never conflate the two.

3. Constraint engineering: advanced SHACL

3.1 The three layers of SHACL

  1. Node Shape: constraints over a whole node of a given class.
  2. Property Shape: constraints on one property's values (datatype, minCount, pattern, in).
  3. SPARQL Constraint: complex constraints across properties and rows.

The SHACL validation pipeline

3.2 Balanced debits and credits with a SPARQL constraint

@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix ex: <https://example.com/accounting#> .

ex:JournalEntryShape a sh:NodeShape ;
    sh:targetClass ex:JournalEntry ;
    sh:sparql [
        sh:message "Debit total must equal credit total" ;
        sh:severity sh:Violation ;
        sh:select """
            SELECT $this
            WHERE {
                $this ex:totalDebit ?d ;
                      ex:totalCredit ?c .
                FILTER (?d != ?c)
            }
        """
    ] .

This is far more reliable than writing “please make sure debits equal credits” in a prompt, because it is executable, re-runnable, and auditable.

3.3 Validating with pySHACL

from pyshacl import validate
from rdflib import Graph

data = Graph().parse("data.ttl", format="turtle")
shapes = Graph().parse("shapes.ttl", format="turtle")

conforms, report_graph, report_text = validate(
    data, shacl_graph=shapes, inference="rdfs", abort_on_first=False
)
print(conforms)
print(report_text)

3.4 The limits of SHACL

  • SHACL does not reason; it only checks whether data conforms to shapes.
  • SPARQL constraints are powerful, but performance depends on the query and the indexes.
  • Error reports must point to business objects and fields, or users cannot act on them.

4. Query and inference: SPARQL, materialization, incremental

4.1 The three shapes of SPARQL

  • SELECT: tabular results (reports, reconciliation).
  • ASK: whether matching data exists (permissions, checks).
  • CONSTRUCT: build a new RDF graph from results (ETL, inference).

Example: total debits per account for a period.

SELECT ?account (SUM(?debit) AS ?totalDebit)
WHERE {
    ?entry a ex:JournalEntry ;
           ex:inPeriod ?period ;
           ex:posts ?line .
    ?line ex:debits ?account ;
          ex:amount ?debit .
    ?period ex:code "2026-10" .
}
GROUP BY ?account
ORDER BY DESC(?totalDebit)

4.2 Inference strategies

StrategyApproachStrengthWeakness
MaterializationPrecompute inferred triples into the graphFast readsSlow writes, large storage
Query RewritingRewrite queries into SQL/SPARQL at run timeSmall storageComplex queries
IncrementalRecompute only what changedBalancedHard to implement
Rule EngineOWL RL or business rulesExplainableRule explosion

Practical advice:

  • Read-heavy, write-light: materialization plus incremental updates.
  • Write-heavy, read-light: query rewriting or a rule engine.
  • Agent scenarios: fetch a subgraph and reason locally; avoid global materialization.

4.3 The limits of inference

Inference can classify, check consistency, and derive indirect relations. It cannot judge whether data is true, handle vague language, replace business rules, or guarantee real-time behaviour.

5. Modularity and ontology patterns

The four-layer Core / Domain / Application / Mapping architecture

5.1 Four layers of modules

  • Core Ontology: generic concepts such as Entity, Party, Event, Time, Document.
  • Domain Modules: Accounting, Banking, Tax, HR, each versioned independently.
  • Application Profiles: the subset an agent or report needs, plus JSON Schema and SHACL.
  • Mapping Layer: mappings for databases, APIs, PDFs, Excel, and bank feeds.

Benefits:

  • Each module can be tested and versioned on its own.
  • An agent loads only the application profile it needs instead of drowning in the global ontology.
  • Different clients can share the core while swapping domain and application layers.

5.2 Ontology design patterns

  • Role Pattern: a Party can be a Customer, a Supplier, and an Employee at once. Model roles, not subclasses.
  • Event Pattern: Event → Actor → Object → Time. Good for transactions, audit, logs.
  • Time Pattern: ValidFrom / ValidTo. Never overwrite history.
  • Provenance Pattern: Entity → Activity → Agent (W3C PROV).
  • N-ary Relation: an intermediate node for relations involving three or more parties, e.g. contract–property–tenant–period.

5.3 Versions and imports

  • Every module carries a semantic version (1.2.0).
  • Compose with owl:imports, but never let imports form a cycle.
  • Record every change: what changed, why, and which CQs were affected.
  • Backward compatibility: adding optional properties is safe; deleting or renaming needs a migration.

6. Entity resolution: the make-or-break step

The entity resolution pipeline

One supplier may have several names, IDs, and bank accounts. Without identity resolution, the ontology simply accumulates duplicates.

6.1 The pipeline

  1. Ingest: multiple sources (databases, invoices, banks, Excel).
  2. Normalize: strip whitespace, unify full-width and half-width characters, remove company suffixes (Ltd / Limited).
  3. Blocking: narrow the candidate set with rules (first 6 digits of the tax ID, first character of the name, postal district).
  4. Scoring: name similarity, tax ID, address, bank account, embeddings.
  5. Merge / Review: high score merges automatically; medium score goes to a human; low score stays separate.
  6. Canonical ID: assign a unique ID and keep aliases and merge history.

6.2 A scoring formula (example)

score = 0.35 * name_sim
      + 0.25 * tax_id_match
      + 0.20 * address_sim
      + 0.15 * bank_account_match
      + 0.05 * embedding_sim

Example thresholds:

  • score >= 0.92: merge automatically
  • 0.75 ≤ score < 0.92: human review
  • score < 0.75: do not merge

6.3 Merges and splits

  • A merge must keep merge_history: who merged, when, and on what evidence.
  • A split must be reversible: which alias moved to which new entity.
  • Every merge and split goes to the audit log.

7. Time, versions, and provenance

Valid time × transaction time, plus PROV-O

7.1 Bitemporal modelling

  • Valid time: when the fact was true in the real world (e.g. a lease from 2026-01-01 to 2026-12-31).
  • Transaction time: when the fact was recorded by the system (e.g. imported on 2026-10-21).

Keeping them separate is what lets you answer:

  • “As of 2026-06-30, what did we believe the validity of this contract was?”
  • “We only discovered the address change on 2026-10-21 — what did we record before that?”

7.2 PROV-O

Every fact must be able to answer:

  • Entity: which document, which data row?
  • Activity: which extraction or transformation?
  • Agent: which agent or person confirmed it?
  • Timestamp: when?

This matters most for accounting, audit, and compliance.

7.3 Event sourcing

Instead of storing only current state, store the event stream:

  • DocumentReceived
  • ExtractionCompleted
  • JournalDrafted
  • JournalApproved
  • JournalPosted
  • JournalReversed

Every event can be replayed, so the state at any point in time can be reconstructed.

8. Multi-tenancy, security, and governance

8.1 Isolation model

  • The TBox can be shared.
  • The ABox must be isolated per tenant: one named graph, schema, or namespace each.
  • Enforce the tenant filter in the query layer, not only in the application.
  • Permissions should reach the concept, property, and instance level.

8.2 Security

  • Tag and mask PII fields.
  • Keep the audit log append-only.
  • Exports and imports need permissions and expiry.
  • Agent tool calls must pass authorisation and parameter validation.

8.3 Governance

  • Ontology owner: who may change it?
  • Change advisory: who reviews?
  • A versioning and deprecation policy.
  • Regular quality audits: CQ coverage, SHACL pass rate, entity resolution F1.

9. Scale and performance

9.1 Storage choices

TypeStrengthWeaknessBest for
RDF store (GraphDB / Stardog / Jena)Standards, inference, SPARQLOperational costSemantic layer, data integration
Property graph (Neo4j)Fast traversal, mature ecosystemWeaker semanticsRelationship analysis, recommendations
Relational + JSONMature, strong transactionsWeak graph queriesTransactional systems, MVPs

9.2 Performance techniques

  • Fetch subgraphs only: an agent rarely needs the whole ontology.
  • Index on entity ID, type, relation, and tenant.
  • Partition by tenant, time, or domain.
  • Cache concept lookups, subgraphs, and validation results.
  • Avoid global materialization; prefer incremental updates or query rewriting.

9.3 Benchmarking

  • Query P50 / P95 / P99
  • Validation throughput (shapes per second)
  • Inference time
  • Graph size (triples / nodes / edges)
  • Update latency

10. Advanced agent integration: a tool contract, not prompt stuffing

The advanced agent flow

10.1 Three tools and one gate

  • search_concepts(query): find concepts by name or synonym.
  • get_subgraph(concept_id, depth, profile): fetch the relevant subgraph.
  • validate_payload(schema_id, payload): validate with JSON Schema or SHACL.
  • Pre-send gate: check ledger state, permissions, and duplicates before anything is sent.

10.2 Shared across agents

  • All agents use the same concept IDs, relations, and constraints.
  • Share one ontology service; do not let each agent maintain its own.
  • Share the audit log so every decision is traceable.

10.3 Hallucination control

  • Enforce schema validation on every tool argument.
  • Require important conclusions to cite an ontology path or a source.
  • Low confidence becomes an exception, not an automatic action.
  • Never paste the full ontology into a prompt; use subgraphs and examples.

10.4 RAG versus ontology

NeedRAGOntology
Find relevant documentsStrongWeak
Entity resolutionWeakStrong
Relational reasoningWeakStrong
Parameter validationWeakStrong
AuditabilityMediumStrong

The best combination: RAG finds material, the ontology constrains meaning, the agent decides.

11. Evaluation framework

The six ontology evaluation metrics

11.1 Offline metrics

  • CQ coverage: answerable CQs divided by total CQs
  • SHACL pass rate: conforming instances divided by total instances
  • Consistency: number of unsatisfiable classes and logical conflicts
  • Entity resolution F1: precision and recall
  • Query latency: P50 / P95
  • Maintenance cost: change lead time, number of regression defects

11.2 Online metrics

  • Agent task success rate
  • Human exception rate
  • Average handling time
  • User correction rate
  • Audit traceability rate

11.3 Drift monitoring

  • Falling concept usage suggests obsolescence.
  • A surge of new entity types signals a needed extension.
  • A rising SHACL failure rate means the data source or the rules changed.
  • More entity resolution merge errors means the thresholds need tuning.

12. Advanced case study: the AutoLedger accounting ontology v1.0

Accounting ontology example (8 entities, 6 relations)

12.1 Core axioms

  1. For every JournalEntry, the debit total equals the credit total in the base currency.
  2. JournalLine.account must exist and be is_active.
  3. JournalEntry.date must fall inside an open period.
  4. Foreign-currency entries need fx_rate > 0 and must store both the original and the base amount.
  5. Every JournalLine must carry a source_document or a source_ref.

12.2 An OWL fragment

Prefix(:=<https://example.com/accounting#>)
Ontology(<https://example.com/accounting>
  SubClassOf(:CashAccount :AssetAccount)
  SubClassOf(:AssetAccount :Account)
  ObjectPropertyDomain(:debits :JournalLine)
  ObjectPropertyRange(:debits :Account)
  FunctionalObjectProperty(:hasPeriod)
)

12.3 A reconciliation query in SPARQL

SELECT ?bankTx ?journalLine
WHERE {
    ?bankTx a ex:BankTransaction ;
            ex:amount ?amt ;
            ex:date ?date .
    ?journalLine a ex:JournalLine ;
                 ex:amount ?amt ;
                 ex:date ?date .
    FILTER NOT EXISTS { ?bankTx ex:matchedTo ?journalLine }
}

12.4 The event stream

DocumentReceived -> ExtractionCompleted -> JournalDrafted
-> JournalApproved -> JournalPosted -> JournalReconciled

Each event records the actor, the timestamp, and a payload hash. The stream can be replayed and audited.

13. Anti-patterns and fixes

Anti-patternSymptomRoot causeFix
Monolithic ontologyNobody dares to change itNo modularitySplit into core / domain / application
Schema treated as ontologyTables but no meaningCopying the database schemaAdd CQs, relations, constraints
No CQsModelling driftsModelling for its own sakeWrite 20 CQs first
Using OWL for validationSlow reasoning, uncertain resultsConfusing OWA and CWAUse SHACL or JSON Schema
No entity resolutionOne entity, many IDsNo blocking or scoringBuild an identity pipeline
No time dimensionHistory is overwrittenStoring only current valuesBitemporal modelling or event sourcing
No provenanceNothing is auditableStoring only resultsPROV-O and source references
No versioningSemantic driftNo ownerSemantic versioning plus a change log
Whole ontology in the promptContext explosionTreating the ontology as a documentSubgraphs plus tools
Each agent maintains its ownContradictory conclusionsNo shared serviceOne ontology service

14. A 90-day advanced rollout plan

14.1 Weeks 1–4: formalisation and CQs

  • Choose an OWL profile (EL / QL / RL)
  • Write 30 CQs
  • Build the core and accounting modules
  • Check consistency in Protégé

14.2 Weeks 5–8: constraints and identity

  • Build SHACL shapes (required fields, value domains, cross-row rules)
  • Build the entity resolution pipeline
  • Add provenance
  • Wire pySHACL into CI

14.3 Weeks 9–12: agents and evaluation

  • The three ontology tools
  • The pre-send gate and audit trail
  • Measure CQ coverage, SHACL pass rate, and agent success rate
  • Ship v1.0 with a runbook

15. Labs

15.1 Lab 1: write CQs

Write 5 CQs for bank reconciliation and name the concepts and relations each one needs.

Reference answers:

  • Which bank transactions are unmatched? → BankTransaction, matchedTo
  • Which journal lines have several bank transactions? → JournalLine, BankTransaction
  • What is the total unmatched amount this month? → amount, date, status
  • Does the same reference number appear twice? → reference, count
  • Which transactions straddle periods? → date, Period

15.2 Lab 2: choose an OWL profile

Your system needs large-scale account classification, fast queries, and occasional inference. Which profile?

Reference answer: OWL 2 EL. If the workload is mostly query rewriting, choose QL; if you need a rule engine, choose RL.

15.3 Lab 3: write SHACL

Write a shape requiring JournalLine.amount > 0 and a mandatory account_code.

Reference answer:

ex:JournalLineShape a sh:NodeShape ;
    sh:targetClass ex:JournalLine ;
    sh:property [
        sh:path ex:amount ;
        sh:datatype xsd:decimal ;
        sh:minExclusive 0
    ] ;
    sh:property [
        sh:path ex:accountCode ;
        sh:minCount 1 ;
        sh:datatype xsd:string
    ] .

15.4 Lab 4: SPARQL

Query the total debits per account for the 2026-10 period.

Reference answer: see the query in section 4.1.

15.5 Lab 5: entity resolution

Two suppliers have similar names but different tax IDs. Should they be merged?

Reference answer: no automatic merge. The tax ID is a strong identifier, so the pair goes to human review and both canonical IDs are preserved.

16. Conclusion: the ontology is the semantic layer of a production system

Advanced ontology design is not about drawing something more complicated. It is about:

  • using the weakest expressivity that satisfies the CQs
  • making constraints executable with SHACL
  • controlling complexity through modules
  • guaranteeing trust through identity, time, and provenance
  • letting agents use it safely through a tool contract
  • proving it works with metrics

Three sentences to remember:

  1. Expressivity is a cost, not a badge.
  2. Constraints must be executable, not written into a prompt.
  3. An ontology that cannot be maintained has no claim to mastery.

17. Appendix: standards, tools, and further reading

  • Standards: RDF 1.1, RDFS, OWL 2 (EL / QL / RL / DL), SHACL, SKOS, JSON-LD, SPARQL 1.1, PROV-O
  • Methodologies: Ontology 101, NeOn, METHONTOLOGY, competency questions, ontology design patterns
  • Tools: Protégé, TopBraid Composer, GraphDB, Stardog, Apache Jena, pySHACL, Neo4j
  • Further: schema.org, BFO, SUMO, Palantir Ontology, W3C Data on the Web Best Practices

Advanced does not mean bigger. It means more computable, more verifiable, more operable.

Next steps

Pick one CQ you would genuinely ask, then:

  1. model it with the weakest OWL profile that works, and confirm the reasoner runs;
  2. write a SHACL shape for it and run Conformance once with pySHACL;
  3. measure three numbers: SHACL pass rate, P95 query latency, agent task success rate;
  4. write the result as a five-line acceptance record and paste it into the ontology repository's README.

An ontology is not finished when it is written. It is finished when it can be verified.