Skip to content

fluree ontology check / from: see what an extraction model reads of an ontology, and rewrite or slice one - #2208

Merged
aaj3f merged 2 commits into
mainfrom
feat/ontology-check
Oct 30, 2026
Merged

aaj3f merged 2 commits into
mainfrom
feat/ontology-check

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

This adds a top-level fluree ontology command with two subcommands, check and from, plus a small library crate (fluree-db-ontology) that holds the logic so the Python package (or anything else) can reuse it later. Both are ontology tools for document extraction: an extraction model that reads documents against a caller's ontology only sees part of that ontology, and how the ontology is written turns out to matter a lot for what gets read (labels in words vs. column names, descriptions vs. none, a sliced vocabulary vs. all of schema.org). These commands make that visible locally, before anything is sent anywhere.

fluree ontology check <FILE> [--catalog <FILE>] [--seed <CLASSES>] [--json] applies the extraction model's ontology input policy (ontology-adapter-v1) and reports four things:

  • what a request carrying this ontology/catalog would be refused for (no class, a term that can't be given a label, a catalog record with no SKOS label or no IRI, a window over its 200K-token limit, a file over the 2 MB request limit);
  • what the model reads another way (skos:prefLabel/skos:definition standing in for a missing rdfs:label/rdfs:comment, a label made from the IRI's local name, OWL restrictions and other non-SHACL blank nodes dropped);
  • what it never sees, counted by predicate (rdfs:domain, owl:equivalentClass, a second rdfs:subClassOf parent, SHACL constraints it doesn't support, ...);
  • advice: missing descriptions, labels that read as identifiers (employer_id, employerId), IRIs whose local names read as key columns even when the label is fine, a vocabulary large enough that it should be sliced, domains/ranges that ought to be said in a description instead.

It exits 1 only when a request would be refused; adaptations and advice exit 0. The adapted / not_shown maps in --json use the exact keys an extraction response reports those same counts under (X-Fluree-Ontology-Adapted / -Not-Shown), so the two can be compared line for line. It also estimates the serialized tokens the ontology and catalog cost per window. The model's tokenizer isn't in the CLI, so that number is an estimate from character counts, fitted against reported token counts for saved requests (catalogs from 68 to 3,250 records), and is good to roughly a tenth; the docs say so.

fluree ontology from <FILE> [--seed <CLASSES>] [--format turtle|jsonld] [-o <FILE>] writes the ontology back out with what the model reads made explicit: SKOS text copied into RDFS, missing labels filled from the IRI, identifier-style labels rewritten in words (worksFor → "works for"), OWL declarations also declared as their RDFS equivalents, restrictions dropped. Domains/ranges and other axioms are kept. Catalog records with only rdfs:label (a vocabulary's enumeration members, e.g. schema.org's schema:Female) get skos:prefLabel since that is what catalog records are read by.

--seed slices a large vocabulary to what a set of seed classes reach: the seeds and their ancestors, then properties whose domain is one of them (rdfs:domain, schema.org's domainIncludes, or a member of an owl:unionOf domain) and the classes in their ranges, --depth times (default 1). I stopped "reachable" at a depth on purpose, since following every range out of a vocabulary rooted in one Thing class reaches nearly all of it. Against schema.org (1,016 classes, 1,694 properties), --seed Person,Organization keeps 64 classes and 127 properties, about 12.5K tokens instead of ~170K.

How I checked parity with the policy

The policy port in fluree-db-ontology/src/policy.rs is a line-for-line port of the extraction service's request reader (its adapt_ontology and its not-shown counter, plus the SHACL compile rules those depend on). I ran the service's own reader (CPU only, no model) and this command side by side over 31 inputs — saved benchmark requests with their catalogs, SHACL-heavy and range-overlay ontologies, and adversarial fixtures for each rule (multilingual SKOS, OWL restrictions, unionOf domains, second parents, an owl:Ontology header, unsupported SHACL constraints and non-direct paths, the failure cases) — and every adapted/not-shown count and every refusal (with the same reason) matched. The three adversarial fixtures are pinned in fluree-db-ontology/tests/parity.rs with the counts the reader produced, so drift shows up as a test failure; I mutated the shown-predicate list to confirm those tests actually fail when the port is wrong.

One known gap, deliberately: the policy also treats one event-class declaration type and one example predicate as shown, and those live in a vocabulary namespace that hasn't been settled yet, so I left them out for now. Until they're added, a file that uses them will see those triples counted under "not read" when the model would actually show them. It's a two-line addition once the namespace is final.

Scope notes

  • Reads Turtle and JSON-LD (including a JSON-LD dataset that uses the xt:ontology / xt:instances graph roles, so a saved request can be checked as-is). TriG isn't read yet: the only TriG driver in the workspace lives inside fluree-db-transact's RDF-text path and builds transaction templates, not a plain dataset, and I didn't want to pull that crate in here. check request.trig says so plainly. Happy to follow up by moving the TriG block locator somewhere shared if we think it's worth it.
  • Converters from non-RDF sources (CSV/XLSX headers, SQL DDL, semantic-layer YAML, etc.) and fluree catalog from come in follow-up PRs stacked on this one; from currently refuses non-RDF input with a clear message.
  • docs/cli/ontology.md documents both commands, the JSON report shape and finding codes; it's linked from docs/cli/README.md and SUMMARY.md (so docs_coverage stays green).
  • New CLI tests are in their own file (fluree-db-cli/tests/ontology.rs) rather than integration.rs.

cargo test -p fluree-db-ontology -p fluree-db-cli and cargo clippy -p fluree-db-ontology -p fluree-db-cli --all-targets --no-deps are clean locally.

🤖 Generated with Claude Code

https://claude.ai/code/session_016CjYmQVZNYkCgGd38GYUyX

…tology

A new top-level `fluree ontology` command and a small library crate,
`fluree-db-ontology`, with ontology tools for document extraction:

- `check` applies the extraction model's ontology input policy
  (ontology-adapter-v1) locally and reports what a request would be refused
  for, what the model reads another way (SKOS text for missing RDFS text,
  labels from IRIs, OWL restrictions dropped), what it never sees (by
  predicate, with the keys an extraction response uses), advice, and an
  estimate of the serialized tokens per window. Exits 1 only when a request
  would be refused. --json for machines, --catalog to check records with it.
- `from` writes an RDF ontology in the shape the model reads well (SKOS
  copied into RDFS, identifier-style labels in words, restrictions dropped,
  catalog records given SKOS labels).
- --seed slices a large vocabulary to the classes a seed set reaches.

The policy port matches the reference request reader's counts on saved
requests and adversarial fixtures; three are pinned in tests/parity.rs.
docs/cli/ontology.md documents both commands.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmQVZNYkCgGd38GYUyX
@aaj3f aaj3f added enhancement New feature or request area:cli fluree CLI UX, rdf toolkit, publish/export/insert flows labels Oct 29, 2026
@aaj3f
aaj3f marked this pull request as ready for review October 29, 2026 17:26
@aaj3f
aaj3f requested a review from bplatz October 29, 2026 17:26

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The policy port, the report and the slice are in good shape, and pinning the parity counts is a good guard. Below are a few things I'd like you to take a pass at. Since the rest of the stack builds on this, I'll leave it to you to decide where each belongs given where you're taking these tools: fix it here, fold it into one of the stacked PRs, or file a follow-up. I've left reviews on each of the stacked PRs too.

1. Which extractor check describes. check mirrors the HTTP reader's input policy (the reader #2207 adds to doc ingest). But the extractor fluree doc ingest already has on main reads rdfs:domain/rdfs:range, schema:domainIncludes and every parent class (the class and property queries in fluree-db-doc/src/model.rs). So for that path, "the extraction model doesn't see rdfs:domain" and the domain-range-not-shown / one-parent-shown advice are wrong, and the docs' "See also" sends readers to doc. I'd name the HTTP reader ([doc.reader]) in the docs, help and report text. This probably reads best landing after #2207.

2. A seed that names no class gives an empty result. resolve_seed returns a prefixed name or <IRI> without checking that it's a declared class. So --seed ex:Persn makes check report no-ontology, and from writes an empty ontology with exit 0. A bare Persn already errors with "no class in the ontology matches"; the prefixed and IRI forms should do the same.

3. from doesn't fix common OWL files. Take a Protégé-style file with [] a owl:AllDisjointClasses ; owl:members (ex:A ex:B) and dc:creator a owl:AnnotationProperty. The flat split files both as catalog records, since SCHEMA_TYPES doesn't include them. check then reports catalog-blank-node and catalog-no-label, and the output of from fails check the same way. Either the reader really refuses these files, in which case from should drop them (or set them aside as it does document nodes), or it doesn't, and the split needs to know about them. Either way, one of these is worth adding to the parity set.

Smaller:

  • The help and docs say SKOS labels and definitions are "copied" into rdfs:label/rdfs:comment, but rewrite moves them (the borrowed SKOS triple is removed). Either is fine; they should just agree.
  • label_words rewrites a camel-case label like "iPhone" to "i phone".
  • Follow-up, not for this PR: Dialect::TriG in fluree-graph-turtle (#2042) is on next. Once it reaches main, this crate can read the TriG requests #2207 sends without depending on fluree-db-transact.

Several of the converter issues in the stacked PRs come from the shared emitter, where two source names get merged into one IRI or something is dropped with no count. It may be worth settling one rule for those before the rest land.

…class, drop OWL machinery

Review follow-ups on the first ontology tools PR:

- `check` describes the input policy of an HTTP reader (an extraction
  model reached over HTTP, `fluree doc ingest`'s [doc.reader]), and now
  says so in its help, report and docs. `fluree doc ingest`'s
  language-model extractor reads domains, ranges and every parent class,
  so the "not read" counts and the domain/range/parent advice don't
  describe it, and the docs say that.
- A --seed given as a prefixed name or an IRI must name a class the file
  declares, as a label already had to: `--seed ex:Persn` is refused
  instead of giving an empty report or an empty ontology with exit 0.
- OWL axioms written as nodes (`[] a owl:AllDisjointClasses`, AllDifferent,
  ...) and declarations of annotation properties and datatypes are read
  as catalog records by a reader splitting a flat graph, which refuses
  them (the reference reader does, for both). `check` reports them as
  `owl-machinery` rather than as generic catalog errors, and `from` drops
  them and counts them. A Protégé-style fixture joins the parity tests.
- Docs and help say SKOS text is moved into RDFS (as `from` does), not
  copied.
- `from` no longer rewrites brand-style labels ("iPhone", "eBay") into
  words.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmQVZNYkCgGd38GYUyX
@aaj3f

aaj3f commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @bplatz, this was a really useful pass, and the point about the shared emitter turned out to be the big one. I went with fixing everything in the PR where it lives rather than filing follow-ups, so here's where each landed:

1. Which extractor check describes. You're right, I'd written it as if there were only one reader. Fixed in 80a7504: the help, report and docs/cli/ontology.md now say check describes an HTTP reader ([doc.reader] in fluree doc ingest), and the docs say plainly that doc ingest's language-model extractor reads domains, ranges and every parent class, so the "not read" counts and the domain-range-not-shown / one-parent-shown advice don't apply to it. I didn't hold this for #2207. The wording reads fine on its own, and it'll match once #2207 lands.

2. A seed that names no class. Fixed in 80a7504: a prefixed or <IRI> seed has to name a class the file declares, same as a bare label already did, so --seed ex:Persn errors instead of giving no-ontology or an empty ontology with exit 0. There are CLI tests for both forms.

3. Common OWL files. I checked this against the reference reader rather than guessing, and it really does refuse both shapes: [] a owl:AllDisjointClasses fails with "Schema terms and catalog identities must have IRIs", and dc:creator a owl:AnnotationProperty fails with "Catalog records require SKOS labels". So in 80a7504, check reports these as their own will-fail code, owl-machinery (OWL axioms written as nodes, plus declarations of annotation properties and datatypes), rather than as generic catalog errors, and from drops them and counts them in its summary. A Protégé-style fixture (tests/fixtures/protege.ttl) joins the parity set as protege_owl_machinery_is_refused.

Smaller:

The shared emitter. I took your rule as the one rule for every converter: two source names never become one term. That landed in #2210 (e98dfe8) and was merged up the stack:

  • A property's key is its column name plus its range, so same-named columns in different tables share one property, but nothing else merges.
  • A label another term already has gets qualified, so no two terms share a label.
  • IRIs that clash, and enumerated values that slug alike, get suffixes instead of merging.
  • Everything dropped is counted in the notes on stderr, and from fails on an empty ontology.

I've replied on each inline comment with the commit and the test that covers it. Both ends of the stack merge cleanly with main as of this morning. Since CI only runs on the stacked PRs once they target main, it's probably simplest to merge this one first and let each of the others retarget in turn. Happy to talk through any of it.

@aaj3f
aaj3f merged commit a035658 into main Oct 30, 2026
19 checks passed
@aaj3f
aaj3f deleted the feat/ontology-check branch October 30, 2026 16:39
bplatz added a commit that referenced this pull request Oct 31, 2026
Brings #2217 (SPARQL reads list members as values; CONSTRUCT writes lists),
#2218 (schema writes judged by modify policy), #2208, #2169, #2168, #2153,
#2149 and #2148 into next.

Resolutions:
- Nameservice: branch_records is the one branch listing (strict, retracted
  branches kept for GC) on every backend, resolved through next's bindings;
  Fluree::list_branches filters retracted ones. The lifecycle conformance
  suite reads live branches through a helper. DynamoDB's narrow listing is
  wired into its trait impl.
- JSON-LD adapter: main's graph scoping (Walk, Scope, node_graph, selectors)
  with next's triple terms, @annotation, @reifies, blank-label and number
  datatype handling. to_graph_events writes a JSON-LD named graph under its
  @id and refuses a graph selector. main's f:reifiesGraph anchor is dropped:
  next writes an annotation as an rdf:reifies link in its graph.
  emit_quad_list_item takes (graph, index).
- Bulk import: an annotation's link is written to the graph its slots name,
  and a triple-term statement in a named graph is spooled under that graph
  (both landed in the default graph); emit_quad_reified_triple is
  implemented. resolve_object_value keeps main's vector refusals and next's
  term-table path.
- Ledger manager: a store that fails to load fails the load or keeps the
  reloaded ledger's state (main), over next's load_decoding_root and
  activation swap rule.
- Novelty keeps main's KeptEffects without the retired arena's reifies
  capture. The pool's resolve_events_at and compaction's split_lifecycles
  follow fluree_db_core::same_t.
- Retired annotation arena files stay deleted.
- Tests from main adapted to next: one Fluree per storage directory, async
  genesis_ledger, storage-namespace content stores, u32 graph ids, a store
  broken by hiding a dictionary branch (next has no disk cache), a refused
  import leaving no ledger. next's tests that pinned bulk import refusing
  @graph and SPARQL's RDF list view now check what main changed them to.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli fluree CLI UX, rdf toolkit, publish/export/insert flows enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants