Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

Add compiled claim verification (claimcheck): token-bitset engine with reasoner parity

FABIOTESS2b9fecb25ba8
diff --git a/.gitignore b/.gitignore
index bed1f7fb..2d36629e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,10 @@ data/
!benchmark/ontoaxiom/data/**
# Ignore generated OWL files in root, but keep benchmark references
/*.owl
+
+# Reasoner benchmark artifacts (downloaded corpora, jars, generated files)
+benchmark/reasoner/corpora/
+benchmark/reasoner/lib/
+benchmark/reasoner/*.class
+benchmark/reasoner/results/
+__pycache__/
diff --git a/README.md b/README.md
index 7db777d0..87ea6e3d 100644
--- a/README.md
+++ b/README.md
@@ -379,18 +379,42 @@ All 32 marketplace ontologies fetched, `owl:imports` resolved, loaded, and reaso
32/32 ontologies loaded, imports resolved, and reasoned. RDFS adds 18% more triples. OWL-RL adds **41%** — transitive/symmetric/inverse properties and equivalentClass expansion discover significantly more implicit knowledge. Schema.org jumps from +4,031 (RDFS) to +13,670 (OWL-RL) inferred triples in 117ms.
-### Reasoning Performance — vs HermiT
+### Compiled Claim Verification — measured vs HermiT
-**LUBM Scaling (load + reason cycle)**
+The `claimcheck` module compiles an ontology **once** (inferred hierarchy +
+disjointness, including pairs derived by sound propagation rules) into
+per-class token bitsets, then verifies candidate claims — the "is this set of
+triples consistent with the ontology?" question — with **no reasoning at query
+time**: two 64-bit ANDs per class pair, witness axiom extracted for the
+explanation.
-| Axioms | Open Ontologies | HermiT | Speedup |
-| --- | --- | --- | --- |
-| 1,000 | 15ms | 112ms | **7.5×** |
-| 5,000 | 14ms | 410ms | **29×** |
-| 10,000 | 14ms | 1,200ms | **86×** |
-| 50,000 | 15ms | 24,490ms | **1,633×** |
+Same ontology (canonical `pizza.owl`), same task, same machine, verdicts
+cross-checked against HermiT 1.4.3.456:
-Full benchmark writeup: [docs/benchmarks.md](docs/benchmarks.md)
+| Per-claim consistency check | median | p95 | throughput |
+| --- | --- | --- | --- |
+| HermiT (warm JVM, ontology pre-loaded) | 4,936 µs | — | ~200/s |
+| **open-ontologies compiled check** | **0.3 µs** | **0.4 µs** | **3.1M/s (11.2M/s batched)** |
+
+Correctness before speed:
+
+- **0 disagreements with HermiT** across 78,884 exhaustively-audited class
+ pairs (13 ontologies) and 793 structurally adversarial claims.
+- **Sound by construction**: a `Rejected` verdict is backed by a derivable
+ contradiction and names the witnessing axiom. 100% contradiction recall on
+ both fully-audited ontologies.
+- **Explicit incompleteness envelope**: anything the compiled surface cannot
+ decide returns `Undetermined` and routes to a reasoner-backed residual tier
+ — it is never guessed.
+- Closed-world vocabulary checks catch hallucinated classes/properties that
+ open-world OWL semantics structurally cannot flag.
+
+Offline compile: one classification pass (~120 ms for Pizza) via any complete
+OWL reasoner; the shipped hot path is pure Rust with no JVM dependency.
+Reproduction scripts: [benchmark/layer3-prototype/](benchmark/layer3-prototype/).
+
+Design, measurements and envelope: [docs/layer3-compiled-reasoning.md](docs/layer3-compiled-reasoning.md).
+Full benchmark methodology: [docs/benchmarks.md](docs/benchmarks.md)
### OAEI Ontology Alignment — Anatomy Track
diff --git a/benchmark/layer3-prototype/bench_layer3.py b/benchmark/layer3-prototype/bench_layer3.py
new file mode 100644
index 00000000..09a2e41a
--- /dev/null
+++ b/benchmark/layer3-prototype/bench_layer3.py
@@ -0,0 +1,77 @@
+"""Measure the DuckDB Layer 3 hot path against the 500ms budget."""
+from __future__ import annotations
+
+import statistics
+import time
+
+from onto_check import check_claim, prepare
+from onto_compile import compile_ontology
+
+P = "http://www.co-ode.org/ontologies/pizza/pizza.owl#"
+
+# Realistic candidate claims, the kind Layer 1 would emit from LLM prose.
+CLAIMS = {
+ "valid: a pizza with a topping": (
+ [("ex:p1", P + "American"), ("ex:t1", P + "MozzarellaTopping")],
+ [("ex:p1", P + "hasTopping", "ex:t1")],
+ ),
+ "CONTRADICTION: one thing is both a pizza and a topping": (
+ [("ex:x", P + "American"), ("ex:x", P + "AnchoviesTopping")],
+ [],
+ ),
+ "CONTRADICTION: two named pizzas at once": (
+ [("ex:x", P + "American"), ("ex:x", P + "AmericanHot")],
+ [],
+ ),
+ "CONTRADICTION: functional property with two values": (
+ [("ex:p2", P + "American")],
+ [("ex:p2", P + "hasBase", "ex:b1"), ("ex:p2", P + "hasBase", "ex:b2")],
+ ),
+ "CONTRADICTION: domain violation (topping hasTopping)": (
+ [("ex:t9", P + "MozzarellaTopping")],
+ [("ex:t9", P + "hasTopping", "ex:t8")],
+ ),
+ "HALLUCINATION: invented class": (
+ [("ex:z", P + "QuantumPineappleTopping")],
+ [],
+ ),
+ "HALLUCINATION: invented property": (
+ [("ex:p3", P + "American")],
+ [("ex:p3", P + "hasVibes", "ex:v1")],
+ ),
+}
+
+
+def main() -> None:
+ con, stats = compile_ontology("/tmp/pizza_real.owl")
+ print("=== OFFLINE COMPILE (once per ontology) ===")
+ for k, v in stats.items():
+ print(f" {k:20s} {v}")
+
+ prepare(con)
+
+ print("\n=== PER-CLAIM VERDICTS ===")
+ for name, (types, rels) in CLAIMS.items():
+ r = check_claim(con, types, rels)
+ verdict = "CONSISTENT" if r["consistent"] else "REJECTED"
+ fired = ",".join(r["violations"]) or "-"
+ print(f" {verdict:11s} {r['elapsed_ms']:7.3f} ms [{fired:38s}] {name}")
+
+ # Latency distribution over a realistic mixed stream.
+ print("\n=== LATENCY OVER 1000 CLAIMS (mixed) ===")
+ keys = list(CLAIMS)
+ lat = []
+ for i in range(1000):
+ types, rels = CLAIMS[keys[i % len(keys)]]
+ lat.append(check_claim(con, types, rels)["elapsed_ms"])
+ lat.sort()
+ print(f" mean {statistics.mean(lat):.3f} ms")
+ print(f" median {lat[len(lat)//2]:.3f} ms")
+ print(f" p95 {lat[int(len(lat)*0.95)]:.3f} ms")
+ print(f" p99 {lat[int(len(lat)*0.99)]:.3f} ms")
+ print(f" max {lat[-1]:.3f} ms")
+ print(f"\n 500ms budget headroom at p95: {500/lat[int(len(lat)*0.95)]:.0f}x")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/benchmark/layer3-prototype/bench_lazy.py b/benchmark/layer3-prototype/bench_lazy.py
new file mode 100644
index 00000000..71c4bd03
--- /dev/null
+++ b/benchmark/layer3-prototype/bench_lazy.py
@@ -0,0 +1,87 @@
+"""Does lazy beat eager? Measure cache saturation over a realistic claim stream.
+
+The eager matrix pays O(n^2) reasoner calls before answering a single claim.
+The lazy matrix pays one call per DISTINCT pair actually asked about. The bet is
+that a real claim stream touches a small, heavily-repeated working set, so the
+lazy total collapses to a tiny fraction of the eager total.
+
+Claim streams are Zipf-distributed over classes, because real domain traffic is:
+a handful of classes dominate, most are rare. Uniform sampling is the pessimal
+case for a cache, so we report both.
+"""
+from __future__ import annotations
+
+import random
+import sys
+import time
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from lazy_matrix import LazyMatrix # noqa: E402
+
+import duckdb # noqa: E402
+
+
+def classes_of(ontology_csv: str) -> list[str]:
+ con = duckdb.connect()
+ con.execute(f"CREATE TABLE m AS SELECT * FROM read_csv_auto('{ontology_csv}')")
+ return sorted({r[0] for r in con.execute("SELECT a FROM m").fetchall()})
+
+
+def run(ontology: str, classes: list[str], n_claims: int, dist: str) -> dict:
+ lm = LazyMatrix(ontology)
+ rng = random.Random(20260727)
+
+ # Zipf: rank-ordered popularity, the realistic case.
+ if dist == "zipf":
+ weights = [1.0 / (i + 1) for i in range(len(classes))]
+ else:
+ weights = [1.0] * len(classes)
+
+ milestones = {}
+ t_start = time.perf_counter()
+ for i in range(n_claims):
+ picked = rng.choices(classes, weights=weights, k=2)
+ if picked[0] == picked[1]:
+ continue
+ lm.check_claim([("x", picked[0]), ("x", picked[1])])
+ if (i + 1) in (10, 50, 100, 500, 1000, 2000, 5000):
+ s = lm.stats
+ milestones[i + 1] = s["hit_rate"]
+ wall = time.perf_counter() - t_start
+
+ s = lm.stats
+ lm.close()
+ n = len(classes)
+ eager_pairs = n * (n - 1) // 2
+ return {
+ "dist": dist,
+ "classes": n,
+ "claims": n_claims,
+ "distinct_pairs_needed": s["cached_pairs"],
+ "eager_pairs": eager_pairs,
+ "fraction_of_eager": s["cached_pairs"] / eager_pairs if eager_pairs else 0,
+ "hit_rate": s["hit_rate"],
+ "oracle_ms_mean": s["oracle_ms_mean"],
+ "wall_s": round(wall, 2),
+ "milestones": milestones,
+ }
+
+
+if __name__ == "__main__":
+ ont = sys.argv[1]
+ matrix_csv = sys.argv[2]
+ n_claims = int(sys.argv[3]) if len(sys.argv) > 3 else 2000
+
+ classes = classes_of(matrix_csv)
+ print(f"classes with a contradiction surface: {len(classes)}")
+ for dist in ("zipf", "uniform"):
+ r = run(ont, classes, n_claims, dist)
+ print(f"\n=== {r['dist'].upper()} over {r['claims']} claims ===")
+ print(f" distinct pairs actually needed : {r['distinct_pairs_needed']:,}")
+ print(f" pairs an eager matrix computes : {r['eager_pairs']:,}")
+ print(f" fraction of eager work done : {r['fraction_of_eager']*100:.2f}%")
+ print(f" final cache hit rate : {r['hit_rate']*100:.1f}%")
+ print(f" mean reasoner call : {r['oracle_ms_mean']} ms")
+ print(f" wall clock : {r['wall_s']} s")
+ print(f" hit rate at N claims : {r['milestones']}")
diff --git a/benchmark/layer3-prototype/lazy_matrix.py b/benchmark/layer3-prototype/lazy_matrix.py
new file mode 100644
index 00000000..b82828bd
--- /dev/null
+++ b/benchmark/layer3-prototype/lazy_matrix.py
@@ -0,0 +1,117 @@
+"""Lazy compatibility matrix: compute pairs on demand, cache forever.
+
+Removes the O(n^2) offline compile. Instead of precomputing every pair, we
+consult a DuckDB cache and only ask a warm reasoner about pairs we have never
+seen. Claim streams touch a small working set of classes, so the cache
+saturates quickly and steady-state cost collapses to a lookup.
+
+Cache is persistent, so the cost is paid once across the lifetime of a
+deployment rather than once per process.
+"""
+from __future__ import annotations
+
+import json
+import subprocess
+from pathlib import Path
+
+import duckdb
+
+REASONER_DIR = Path(__file__).resolve().parent.parent / "reasoner"
+JAVA = "/opt/homebrew/opt/openjdk@17/bin/java"
+CP = f".:{REASONER_DIR}/lib/*"
+
+
+class LazyMatrix:
+ """On-demand class-compatibility oracle with a persistent DuckDB cache."""
+
+ def __init__(self, ontology: str, cache_path: str = ":memory:"):
+ self.ontology = ontology
+ self.con = duckdb.connect(cache_path)
+ self.con.execute(
+ "CREATE TABLE IF NOT EXISTS pair_cache("
+ " a VARCHAR, b VARCHAR, compatible BOOLEAN, PRIMARY KEY (a, b))"
+ )
+ self.proc = None
+ self.warmup_ms = None
+ self.oracle_calls = 0
+ self.cache_hits = 0
+ self.oracle_ms_total = 0.0
+
+ # ── oracle lifecycle ────────────────────────────────────────────────
+ def _ensure_oracle(self) -> None:
+ """Start the reasoner only when a cache miss actually needs it."""
+ if self.proc is not None:
+ return
+ self.proc = subprocess.Popen(
+ [JAVA, "-cp", CP, "PairOracle", self.ontology],
+ cwd=REASONER_DIR, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ stderr=subprocess.DEVNULL, text=True, bufsize=1,
+ )
+ while True:
+ line = self.proc.stdout.readline()
+ if not line:
+ raise RuntimeError("oracle failed to start")
+ if line.startswith("{"):
+ self.warmup_ms = json.loads(line)["warmup_ms"]
+ return
+
+ def close(self) -> None:
+ if self.proc is not None:
+ self.proc.stdin.close()
+ self.proc.wait(timeout=30)
+ self.proc = None
+
+ # ── the lookup ──────────────────────────────────────────────────────
+ def compatible(self, a: str, b: str) -> bool:
+ """True if A ⊓ B is satisfiable. Cached after the first ask."""
+ if a > b:
+ a, b = b, a # canonical order halves the cache
+
+ hit = self.con.execute(
+ "SELECT compatible FROM pair_cache WHERE a=? AND b=?", [a, b]
+ ).fetchone()
+ if hit is not None:
+ self.cache_hits += 1
+ return hit[0]
+
+ self._ensure_oracle()
+ self.proc.stdin.write(f"{a}\t{b}\n")
+ self.proc.stdin.flush()
+ line = self.proc.stdout.readline()
+ d = json.loads(line)
+ self.oracle_calls += 1
+ self.oracle_ms_total += d["ms"]
+
+ self.con.execute(
+ "INSERT OR REPLACE INTO pair_cache VALUES (?,?,?)", [a, b, d["compatible"]]
+ )
+ return d["compatible"]
+
+ def check_claim(self, types: list[tuple[str, str]]) -> tuple[bool, list]:
+ """Check one claim. Returns (consistent, offending_pairs)."""
+ by_subject: dict[str, list[str]] = {}
+ for subj, cls in types:
+ by_subject.setdefault(subj, []).append(cls)
+
+ offending = []
+ for subj, classes in by_subject.items():
+ for i, a in enumerate(classes):
+ for b in classes[i + 1:]:
+ if not self.compatible(a, b):
+ offending.append((subj, a, b))
+ return (not offending), offending
+
+ @property
+ def stats(self) -> dict:
+ total = self.cache_hits + self.oracle_calls
+ return {
+ "warmup_ms": self.warmup_ms,
+ "cache_hits": self.cache_hits,
+ "oracle_calls": self.oracle_calls,
+ "hit_rate": round(self.cache_hits / total, 4) if total else 0.0,
+ "oracle_ms_total": round(self.oracle_ms_total, 1),
+ "oracle_ms_mean": round(self.oracle_ms_total / self.oracle_calls, 3)
+ if self.oracle_calls else 0.0,
+ "cached_pairs": self.con.execute(
+ "SELECT count(*) FROM pair_cache").fetchone()[0],
+ }
diff --git a/benchmark/layer3-prototype/onto_check.py b/benchmark/layer3-prototype/onto_check.py
new file mode 100644
index 00000000..f3312b85
--- /dev/null
+++ b/benchmark/layer3-prototype/onto_check.py
@@ -0,0 +1,119 @@
+"""Per-claim consistency checking as non-recursive columnar SQL.
+
+This is the Tardygrada Layer 3 hot path. A claim is 5-50 triples. Every check
+below is a flat join against tables precomputed by onto_compile.py. No
+recursion, no model construction, no backtracking, so no way to hang.
+
+Each check returns the contradicting evidence, which is exactly what an
+explainable verdict needs: the layer that failed AND the axioms that clash.
+"""
+from __future__ import annotations
+
+import time
+
+import duckdb
+
+# ── the checks ──────────────────────────────────────────────────────────
+# Each is one SQL statement over (claim_type, claim_rel) plus compiled tables.
+
+Q_DISJOINT = """
+SELECT ct1.subj AS subject, ct1.cls AS class_a, ct2.cls AS class_b,
+ d.a AS via_a, d.b AS via_b
+FROM claim_type ct1
+JOIN claim_type ct2 ON ct1.subj = ct2.subj AND ct1.cls < ct2.cls
+JOIN sub_closure s1 ON s1.sub = ct1.cls
+JOIN sub_closure s2 ON s2.sub = ct2.cls
+JOIN disjoint_closure d ON d.a = s1.sup AND d.b = s2.sup
+LIMIT 25
+"""
+
+Q_DOMAIN = """
+SELECT r.subj AS subject, r.prop, dm.cls AS required_class
+FROM claim_rel r
+JOIN domain dm ON dm.prop = r.prop
+WHERE NOT EXISTS (
+ SELECT 1 FROM claim_type ct
+ JOIN sub_closure sc ON sc.sub = ct.cls
+ WHERE ct.subj = r.subj AND sc.sup = dm.cls
+)
+LIMIT 25
+"""
+
+Q_RANGE = """
+SELECT r.obj AS object, r.prop, rg.cls AS required_class
+FROM claim_rel r
+JOIN range rg ON rg.prop = r.prop
+WHERE NOT EXISTS (
+ SELECT 1 FROM claim_type ct
+ JOIN sub_closure sc ON sc.sub = ct.cls
+ WHERE ct.subj = r.obj AND sc.sup = rg.cls
+)
+LIMIT 25
+"""
+
+Q_FUNCTIONAL = """
+SELECT r.subj AS subject, r.prop, COUNT(DISTINCT r.obj) AS distinct_values
+FROM claim_rel r
+JOIN functional f ON f.prop = r.prop
+GROUP BY r.subj, r.prop
+HAVING COUNT(DISTINCT r.obj) > 1
+LIMIT 25
+"""
+
+# Closed-world. Open-world OWL cannot flag an invented term; this can.
+Q_UNKNOWN_CLASS = """
+SELECT DISTINCT ct.cls AS undeclared_class
+FROM claim_type ct
+LEFT JOIN declared_class dc ON dc.iri = ct.cls
+WHERE dc.iri IS NULL
+LIMIT 25
+"""
+
+Q_UNKNOWN_PROP = """
+SELECT DISTINCT r.prop AS undeclared_property
+FROM claim_rel r
+LEFT JOIN declared_prop dp ON dp.iri = r.prop
+WHERE dp.iri IS NULL
+LIMIT 25
+"""
+
+CHECKS = [
+ ("disjointness", Q_DISJOINT),
+ ("domain", Q_DOMAIN),
+ ("range", Q_RANGE),
+ ("functionality", Q_FUNCTIONAL),
+ ("unknown_class", Q_UNKNOWN_CLASS),
+ ("unknown_property", Q_UNKNOWN_PROP),
+]
+
+
+def prepare(con: duckdb.DuckDBPyConnection) -> None:
+ """Create the per-claim staging tables once; reuse across claims."""
+ con.execute("CREATE OR REPLACE TABLE claim_type(subj VARCHAR, cls VARCHAR)")
+ con.execute("CREATE OR REPLACE TABLE claim_rel(subj VARCHAR, prop VARCHAR, obj VARCHAR)")
+
+
+def check_claim(con: duckdb.DuckDBPyConnection,
+ types: list[tuple[str, str]],
+ rels: list[tuple[str, str, str]]) -> dict:
+ """Check one candidate claim. Returns a verdict with the failing evidence."""
+ t0 = time.perf_counter()
+ con.execute("DELETE FROM claim_type")
+ con.execute("DELETE FROM claim_rel")
+ if types:
+ con.executemany("INSERT INTO claim_type VALUES (?,?)", types)
+ if rels:
+ con.executemany("INSERT INTO claim_rel VALUES (?,?,?)", rels)
+
+ violations = {}
+ for name, sql in CHECKS:
+ rows = con.execute(sql).fetchall()
+ if rows:
+ violations[name] = rows
+ elapsed_ms = (time.perf_counter() - t0) * 1000.0
+
+ return {
+ "consistent": not violations,
+ "violations": violations,
+ "elapsed_ms": round(elapsed_ms, 3),
+ }
diff --git a/benchmark/layer3-prototype/onto_compile.py b/benchmark/layer3-prototype/onto_compile.py
new file mode 100644
index 00000000..122cb979
--- /dev/null
+++ b/benchmark/layer3-prototype/onto_compile.py
@@ -0,0 +1,152 @@
+"""Compile an OWL ontology into flat DuckDB tables — the Kalgera pattern.
+
+The ontology stays the SPECIFICATION. Execution is native columnar SQL.
+
+Key move: all the recursion (subsumption closure, disjointness propagation)
+happens ONCE, offline, here. The runtime hot path is then non-recursive
+analytic SQL over flat tables, which is what columnar engines are good at.
+This sidesteps the usual objection that recursive CTEs in a general SQL engine
+are slower than a purpose-built Datalog engine: at query time there is no
+recursion left to do.
+
+Emitted tables:
+ sub_closure(sub, sup) transitive reflexive subclass closure
+ disjoint_closure(a, b) disjointness propagated DOWN both hierarchies
+ domain(prop, cls) rdfs:domain
+ range(prop, cls) rdfs:range
+ functional(prop) owl:FunctionalProperty
+ declared_class(iri) closed-world vocabulary
+ declared_prop(iri)
+"""
+from __future__ import annotations
+
+import sys
+import time
+
+import duckdb
+from rdflib import OWL, RDF, RDFS, Graph, URIRef
+
+
+def compile_ontology(path: str, db_path: str = ":memory:") -> tuple[duckdb.DuckDBPyConnection, dict]:
+ t0 = time.perf_counter()
+ g = Graph()
+ g.parse(path)
+ parse_s = time.perf_counter() - t0
+
+ t0 = time.perf_counter()
+
+ # ── direct subclass edges between NAMED classes ─────────────────────
+ named = {s for s in g.subjects(RDF.type, OWL.Class) if isinstance(s, URIRef)}
+ direct: dict[URIRef, set[URIRef]] = {c: set() for c in named}
+ for s, o in g.subject_objects(RDFS.subClassOf):
+ if isinstance(s, URIRef) and isinstance(o, URIRef):
+ direct.setdefault(s, set()).add(o)
+ named.add(s)
+ named.add(o)
+ # equivalentClass gives subsumption in both directions
+ for s, o in g.subject_objects(OWL.equivalentClass):
+ if isinstance(s, URIRef) and isinstance(o, URIRef):
+ direct.setdefault(s, set()).add(o)
+ direct.setdefault(o, set()).add(s)
+ named.update({s, o})
+
+ # ── transitive reflexive closure, computed ONCE ─────────────────────
+ closure: dict[URIRef, set[URIRef]] = {}
+ for c in named:
+ seen = {c}
+ stack = [c]
+ while stack:
+ cur = stack.pop()
+ for sup in direct.get(cur, ()):
+ if sup not in seen:
+ seen.add(sup)
+ stack.append(sup)
+ closure[c] = seen
+
+ # ── disjointness, propagated DOWN both sides ────────────────────────
+ # If A disjoint B, then every subclass of A is disjoint from every
+ # subclass of B. Doing this once turns a reasoning step into a lookup.
+ sub_of: dict[URIRef, set[URIRef]] = {c: set() for c in named}
+ for c, sups in closure.items():
+ for s in sups:
+ sub_of.setdefault(s, set()).add(c)
+
+ disj: set[tuple[str, str]] = set()
+ for a, b in g.subject_objects(OWL.disjointWith):
+ if not (isinstance(a, URIRef) and isinstance(b, URIRef)):
+ continue
+ for x in sub_of.get(a, {a}) | {a}:
+ for y in sub_of.get(b, {b}) | {b}:
+ disj.add((str(x), str(y)))
+ disj.add((str(y), str(x)))
+ # owl:AllDisjointClasses
+ for adc in g.subjects(RDF.type, OWL.AllDisjointClasses):
+ members = []
+ for lst in g.objects(adc, OWL.members):
+ members = [m for m in g.items(lst) if isinstance(m, URIRef)]
+ for i, a in enumerate(members):
+ for b in members[i + 1:]:
+ for x in sub_of.get(a, {a}) | {a}:
+ for y in sub_of.get(b, {b}) | {b}:
+ disj.add((str(x), str(y)))
+ disj.add((str(y), str(x)))
+
+ domains = [(str(p), str(c)) for p, c in g.subject_objects(RDFS.domain)
+ if isinstance(p, URIRef) and isinstance(c, URIRef)]
+ ranges = [(str(p), str(c)) for p, c in g.subject_objects(RDFS.range)
+ if isinstance(p, URIRef) and isinstance(c, URIRef)]
+ funcs = [(str(p),) for p in g.subjects(RDF.type, OWL.FunctionalProperty)
+ if isinstance(p, URIRef)]
+ props = {str(p) for p in g.subjects(RDF.type, OWL.ObjectProperty) if isinstance(p, URIRef)}
+ props |= {str(p) for p in g.subjects(RDF.type, OWL.DatatypeProperty) if isinstance(p, URIRef)}
+
+ compile_s = time.perf_counter() - t0
+
+ # ── load into DuckDB ────────────────────────────────────────────────
+ t0 = time.perf_counter()
+ con = duckdb.connect(db_path)
+ con.execute("CREATE TABLE sub_closure(sub VARCHAR, sup VARCHAR)")
+ con.executemany("INSERT INTO sub_closure VALUES (?,?)",
+ [(str(c), str(s)) for c, sups in closure.items() for s in sups])
+ con.execute("CREATE TABLE disjoint_closure(a VARCHAR, b VARCHAR)")
+ if disj:
+ con.executemany("INSERT INTO disjoint_closure VALUES (?,?)", sorted(disj))
+ con.execute("CREATE TABLE domain(prop VARCHAR, cls VARCHAR)")
+ if domains:
+ con.executemany("INSERT INTO domain VALUES (?,?)", domains)
+ con.execute("CREATE TABLE range(prop VARCHAR, cls VARCHAR)")
+ if ranges:
+ con.executemany("INSERT INTO range VALUES (?,?)", ranges)
+ con.execute("CREATE TABLE functional(prop VARCHAR)")
+ if funcs:
+ con.executemany("INSERT INTO functional VALUES (?)", funcs)
+ con.execute("CREATE TABLE declared_class(iri VARCHAR)")
+ con.executemany("INSERT INTO declared_class VALUES (?)", [(str(c),) for c in named])
+ con.execute("CREATE TABLE declared_prop(iri VARCHAR)")
+ con.executemany("INSERT INTO declared_prop VALUES (?)", [(p,) for p in sorted(props)])
+
+ # Indexes: this is what makes the per-claim path a lookup, not a scan.
+ con.execute("CREATE INDEX i_sub ON sub_closure(sub)")
+ con.execute("CREATE INDEX i_disj ON disjoint_closure(a)")
+ con.execute("CREATE INDEX i_dom ON domain(prop)")
+ con.execute("CREATE INDEX i_rng ON range(prop)")
+ load_s = time.perf_counter() - t0
+
+ stats = {
+ "parse_s": round(parse_s, 3),
+ "compile_s": round(compile_s, 3),
+ "load_s": round(load_s, 3),
+ "named_classes": len(named),
+ "sub_closure_rows": sum(len(v) for v in closure.values()),
+ "disjoint_rows": len(disj),
+ "domain_rows": len(domains),
+ "range_rows": len(ranges),
+ "functional_props": len(funcs),
+ }
+ return con, stats
+
+
+if __name__ == "__main__":
+ con, stats = compile_ontology(sys.argv[1])
+ for k, v in stats.items():
+ print(f"{k:20s} {v}")
diff --git a/benchmark/layer3-prototype/ore_sweep.py b/benchmark/layer3-prototype/ore_sweep.py
new file mode 100644
index 00000000..e56d7044
--- /dev/null
+++ b/benchmark/layer3-prototype/ore_sweep.py
@@ -0,0 +1,193 @@
+"""ORE 2015 sweep: parity and coverage of the lazy compiled path.
+
+Answers the two questions that decide whether this architecture is claimable:
+
+ 1. COVERAGE What fraction of real ontologies actually have a contradiction
+ surface? Where there is none, contradiction checking is a no-op
+ and only the closed-world vocabulary checks apply.
+ 2. PARITY On ontologies that do, does the compiled path agree with HermiT
+ doing full ABox consistency? False negatives are disqualifying.
+
+Reports per-ontology and in aggregate. Ontologies that fail to load or time out
+are counted, not silently dropped, because a sweep that hides its failures is
+how the last benchmark went wrong.
+"""
+from __future__ import annotations
+
+import json
+import random
+import re
+import subprocess
+import sys
+import time
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from lazy_matrix import LazyMatrix # noqa: E402
+
+REASONER_DIR = Path(__file__).resolve().parent.parent / "reasoner"
+JAVA = "/opt/homebrew/opt/openjdk@17/bin/java"
+CP = f".:{REASONER_DIR}/lib/*"
+
+CLASS_RE = re.compile(r"Declaration\(Class\(<([^>]+)>\)\)")
+RDFXML_CLASS_RE = re.compile(r'owl:Class rdf:about="([^"]+)"')
+
+
+def classes_of(path: Path) -> list[str]:
+ txt = path.read_text(errors="ignore")
+ found = set(CLASS_RE.findall(txt)) or set(RDFXML_CLASS_RE.findall(txt))
+ return sorted(c for c in found if "error#Error" not in c)
+
+
+def hermit_baseline(ont: str, claims: list[dict], timeout: int) -> dict:
+ payload = "\n".join(json.dumps(c) for c in claims)
+ try:
+ p = subprocess.run([JAVA, "-cp", CP, "ClaimConsistency", ont],
+ cwd=REASONER_DIR, input=payload, capture_output=True,
+ text=True, timeout=timeout)
+ except subprocess.TimeoutExpired:
+ return {}
+ out = {}
+ for line in p.stdout.splitlines():
+ if line.startswith("{"):
+ d = json.loads(line)
+ out[d["id"]] = d["consistent"]
+ return out
+
+
+def sweep_one(path: Path, n_claims: int, timeout: int,
+ budget_s: float = 90.0) -> dict:
+ res = {"ontology": path.name, "status": "ok"}
+ classes = classes_of(path)
+ res["classes"] = len(classes)
+ if len(classes) < 2:
+ res["status"] = "no_classes"
+ return res
+
+ rng = random.Random(20260727)
+ weights = [1.0 / (i + 1) for i in range(len(classes))]
+
+ lm = LazyMatrix(str(path))
+ claims, compiled = [], {}
+ try:
+ t0 = time.perf_counter()
+ for i in range(n_claims):
+ # Large ontologies carry a real one-off reasoner warm-up (measured
+ # ~15 s at 6,929 classes, plus ~15 s on the first pair). Budget it
+ # and record the truncation rather than stalling the sweep.
+ if time.perf_counter() - t0 > budget_s:
+ res["status"] = "budget_exceeded"
+ res["claims_done"] = len(compiled)
+ break
+ picked = rng.choices(classes, weights=weights, k=2)
+ if picked[0] == picked[1]:
+ continue
+ cid = f"c{i}"
+ ok, _ = lm.check_claim([("x", picked[0]), ("x", picked[1])])
+ compiled[cid] = ok
+ claims.append({"id": cid,
+ "types": [["x", picked[0]], ["x", picked[1]]],
+ "rels": []})
+ res["compiled_s"] = round(time.perf_counter() - t0, 3)
+ res.update({k: lm.stats[k] for k in ("cache_hits", "oracle_calls", "cached_pairs")})
+ except Exception as e:
+ res["status"] = f"compiled_error:{type(e).__name__}"
+ try:
+ lm.close()
+ except Exception:
+ pass
+ return res
+ finally:
+ try:
+ lm.close()
+ except Exception:
+ pass
+
+ # Does this ontology have any contradiction surface at all?
+ res["rejected_by_compiled"] = sum(1 for v in compiled.values() if not v)
+ if res["rejected_by_compiled"] == 0:
+ res["status"] = "no_contradiction_surface"
+
+ if not claims:
+ res["status"] = "no_claims_completed"
+ return res
+
+ t0 = time.perf_counter()
+ base = hermit_baseline(str(path), claims, timeout)
+ res["hermit_s"] = round(time.perf_counter() - t0, 3)
+ if not base:
+ res["status"] = "baseline_timeout_or_error"
+ return res
+
+ agree = fn = fp = 0
+ for cid, c in compiled.items():
+ b = base.get(cid)
+ if b is None:
+ continue
+ if c == b:
+ agree += 1
+ elif c and not b:
+ fn += 1
+ else:
+ fp += 1
+ res.update({"compared": agree + fn + fp, "agree": agree,
+ "false_negatives": fn, "false_positives": fp})
+ return res
+
+
+def main() -> None:
+ corpus = Path(sys.argv[1])
+ n_ont = int(sys.argv[2]) if len(sys.argv) > 2 else 25
+ n_claims = int(sys.argv[3]) if len(sys.argv) > 3 else 60
+ timeout = 120
+
+ files = sorted(corpus.glob("*.owl"))
+ rng = random.Random(20260727)
+ sample = rng.sample(files, min(n_ont, len(files)))
+
+ rows = []
+ for i, f in enumerate(sample, 1):
+ try:
+ r = sweep_one(f, n_claims, timeout)
+ except Exception as e:
+ r = {"ontology": f.name, "status": f"fatal:{type(e).__name__}"}
+ rows.append(r)
+ print(f"[{i}/{len(sample)}] {r['ontology'][:28]:30s} "
+ f"{r.get('status'):26s} cls={r.get('classes','-')} "
+ f"agree={r.get('agree','-')} FN={r.get('false_negatives','-')}",
+ flush=True)
+
+ print("\n================ AGGREGATE ================")
+ total = len(rows)
+ by_status: dict[str, int] = {}
+ for r in rows:
+ by_status[r["status"]] = by_status.get(r["status"], 0) + 1
+ for k, v in sorted(by_status.items(), key=lambda x: -x[1]):
+ print(f" {k:32s} {v:3d}/{total}")
+
+ usable = [r for r in rows if r.get("compared")]
+ if usable:
+ c = sum(r["compared"] for r in usable)
+ a = sum(r["agree"] for r in usable)
+ fn = sum(r["false_negatives"] for r in usable)
+ fp = sum(r["false_positives"] for r in usable)
+ cs = sum(r["compiled_s"] for r in usable)
+ hs = sum(r["hermit_s"] for r in usable)
+ print(f"\n ontologies with a baseline : {len(usable)}")
+ print(f" claims compared : {c}")
+ print(f" agreement : {a}/{c} ({100*a/c:.2f}%)")
+ print(f" FALSE NEGATIVES (dangerous) : {fn}")
+ print(f" false positives : {fp}")
+ print(f" compiled total : {cs:.2f} s")
+ print(f" hermit total : {hs:.2f} s")
+ print(f" speedup : {hs/cs:.1f}x")
+ surf = [r for r in usable if r.get("rejected_by_compiled", 0) > 0]
+ print(f" with a contradiction surface : {len(surf)}/{len(usable)}"
+ f" ({100*len(surf)/len(usable):.0f}%)")
+
+ Path("/tmp/ore_sweep_results.json").write_text(json.dumps(rows, indent=2))
+ print("\n raw results -> /tmp/ore_sweep_results.json")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/benchmark/layer3-prototype/oxigraph_vs_duckdb.py b/benchmark/layer3-prototype/oxigraph_vs_duckdb.py
new file mode 100644
index 00000000..3800313c
--- /dev/null
+++ b/benchmark/layer3-prototype/oxigraph_vs_duckdb.py
@@ -0,0 +1,99 @@
+"""Should the compiled lookups live in Oxigraph (already a dependency) or DuckDB?
+
+Fair test: identical compiled data, identical question, two substrates.
+
+ Oxigraph the incompatible-pair table as RDF triples, queried with SPARQL ASK
+ DuckDB the same table as a relation, queried with SQL
+
+This matters because Oxigraph is ALREADY a dependency of open-ontologies.
+If it is fast enough, DuckDB is an avoidable dependency, and "one store" is a
+better engineering story than two.
+"""
+from __future__ import annotations
+
+import statistics
+import time
+
+import duckdb
+import pyoxigraph as ox
+
+INCOMPAT = "http://tardygrada.example/incompatibleWith"
+
+
+def load_pairs(csv_path: str) -> list[tuple[str, str]]:
+ con = duckdb.connect()
+ rows = con.execute(
+ f"SELECT a, b FROM read_csv_auto('{csv_path}')"
+ ).fetchall()
+ con.close()
+ return rows
+
+
+def bench_duckdb(pairs, probes, reps):
+ con = duckdb.connect()
+ con.execute("CREATE TABLE incompatible(a VARCHAR, b VARCHAR)")
+ con.executemany("INSERT INTO incompatible VALUES (?,?)", pairs)
+ con.execute("CREATE INDEX ix ON incompatible(a)")
+
+ lat = []
+ for i in range(reps):
+ a, b = probes[i % len(probes)]
+ t0 = time.perf_counter()
+ con.execute(
+ "SELECT 1 FROM incompatible WHERE a=? AND b=? LIMIT 1", [a, b]
+ ).fetchone()
+ lat.append((time.perf_counter() - t0) * 1000)
+ con.close()
+ return lat
+
+
+def bench_oxigraph(pairs, probes, reps):
+ store = ox.Store()
+ pred = ox.NamedNode(INCOMPAT)
+ quads = [
+ ox.Quad(ox.NamedNode(a), pred, ox.NamedNode(b))
+ for a, b in pairs
+ if a.startswith("http") and b.startswith("http")
+ ]
+ store.extend(quads)
+
+ lat = []
+ for i in range(reps):
+ a, b = probes[i % len(probes)]
+ q = f"ASK {{ <{a}> <{INCOMPAT}> <{b}> }}"
+ t0 = time.perf_counter()
+ store.query(q)
+ lat.append((time.perf_counter() - t0) * 1000)
+ return lat, len(quads)
+
+
+def summarise(name, lat):
+ lat = sorted(lat)
+ return (f" {name:10s} median {lat[len(lat)//2]:7.4f} ms | "
+ f"p95 {lat[int(len(lat)*0.95)]:7.4f} ms | "
+ f"mean {statistics.mean(lat):7.4f} ms")
+
+
+if __name__ == "__main__":
+ import sys
+
+ csv_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp/pizza_disjoint.csv"
+ reps = int(sys.argv[2]) if len(sys.argv) > 2 else 3000
+
+ pairs = load_pairs(csv_path)
+ print(f"compiled pairs: {len(pairs):,}")
+
+ # Probe with a mix of hits and misses, which is what a real stream does.
+ hits = pairs[:50]
+ misses = [(a, a) for a, _ in pairs[:50]]
+ probes = [p for pair in zip(hits, misses) for p in pair]
+
+ d = bench_duckdb(pairs, probes, reps)
+ o, nquads = bench_oxigraph(pairs, probes, reps)
+ print(f"oxigraph quads: {nquads:,}\n")
+ print(summarise("DuckDB", d))
+ print(summarise("Oxigraph", o))
+ dm = statistics.mean(d)
+ om = statistics.mean(o)
+ faster = "Oxigraph" if om < dm else "DuckDB"
+ print(f"\n {faster} is {max(dm, om)/min(dm, om):.1f}x faster on mean latency")
diff --git a/benchmark/layer3-prototype/parity.py b/benchmark/layer3-prototype/parity.py
new file mode 100644
index 00000000..b7ca3ea4
--- /dev/null
+++ b/benchmark/layer3-prototype/parity.py
@@ -0,0 +1,155 @@
+"""Parity test: compiled DuckDB path vs HermiT full ABox consistency.
+
+The Kalgera discipline. The fast path is only usable if it is pinned against
+the audited baseline, otherwise it is an optimisation with no correctness story.
+
+Generates random candidate claims from the ontology's own vocabulary, asks both
+engines, and reports agreement. The asymmetry that matters:
+
+ FALSE NEGATIVE compiled says consistent, HermiT says inconsistent
+ -> a contradiction slipped through. This is the dangerous one.
+ FALSE POSITIVE compiled says inconsistent, HermiT says consistent
+ -> we rejected a valid claim. Annoying, not dangerous.
+
+Usage: python parity.py <ontology.owl> [n_claims]
+"""
+from __future__ import annotations
+
+import json
+import random
+import subprocess
+import sys
+import time
+from pathlib import Path
+
+import duckdb
+
+REASONER_DIR = Path(__file__).resolve().parent.parent / "reasoner"
+JAVA = "/opt/homebrew/opt/openjdk@17/bin/java"
+CP = f".:{REASONER_DIR}/lib/*"
+
+
+def compile_matrix(ont: str, out_csv: str) -> dict:
+ r = subprocess.run(
+ [JAVA, "-cp", CP, "DisjointnessMatrix", ont, out_csv],
+ cwd=REASONER_DIR, capture_output=True, text=True,
+ )
+ for line in r.stdout.splitlines():
+ if line.startswith("{"):
+ return json.loads(line)
+ raise RuntimeError(f"compile failed: {r.stdout[-400:]} {r.stderr[-400:]}")
+
+
+def load_db(csv_path: str, ont: str) -> tuple[duckdb.DuckDBPyConnection, list[str]]:
+ con = duckdb.connect()
+ con.execute(
+ f"CREATE TABLE incompatible AS SELECT * FROM read_csv_auto('{csv_path}')"
+ )
+ con.execute("CREATE INDEX ix ON incompatible(a)")
+ con.execute("CREATE TABLE claim_type(subj VARCHAR, cls VARCHAR)")
+ classes = sorted({r[0] for r in con.execute("SELECT a FROM incompatible").fetchall()})
+ return con, classes
+
+
+CHECK = """
+SELECT 1 FROM claim_type c1
+JOIN claim_type c2 ON c1.subj = c2.subj AND c1.cls < c2.cls
+JOIN incompatible i ON i.a = c1.cls AND i.b = c2.cls
+LIMIT 1
+"""
+
+
+def compiled_verdict(con, types) -> bool:
+ """True = consistent."""
+ con.execute("DELETE FROM claim_type")
+ con.executemany("INSERT INTO claim_type VALUES (?,?)", types)
+ return con.execute(CHECK).fetchone() is None
+
+
+def main() -> None:
+ ont = sys.argv[1]
+ n = int(sys.argv[2]) if len(sys.argv) > 2 else 300
+ csv_path = "/tmp/parity_matrix.csv"
+
+ print(f"=== compiling {Path(ont).name} ===")
+ stats = compile_matrix(ont, csv_path)
+ print(f" {stats}")
+
+ con, classes = load_db(csv_path, ont)
+ print(f" classes in matrix: {len(classes)}")
+ if len(classes) < 2:
+ # No incompatible pairs at all: the ontology declares no disjointness
+ # (explicit or inferred), so there is nothing for a contradiction check
+ # to find. Worth reporting rather than crashing — a meaningful fraction
+ # of real ontologies are in this state, and for them this layer adds
+ # no value and the vocabulary checks are the only useful guard.
+ print("\n NO CONTRADICTION SURFACE: ontology has no unsatisfiable "
+ "class pairs.\n Compiled contradiction checking is a no-op here; "
+ "only the closed-world\n vocabulary checks would apply.")
+ return
+
+ # Generate claims: random pairs of classes asserted of one individual.
+ rng = random.Random(20260727)
+ claims = []
+ for i in range(n):
+ k = rng.choice([2, 2, 2, 3])
+ picked = rng.sample(classes, min(k, len(classes)))
+ claims.append({"id": f"c{i}", "types": [["x", c] for c in picked], "rels": []})
+
+ # Compiled path
+ t0 = time.perf_counter()
+ compiled = {c["id"]: compiled_verdict(con, [(t[0], t[1]) for t in c["types"]])
+ for c in claims}
+ compiled_s = time.perf_counter() - t0
+
+ # HermiT baseline
+ payload = "\n".join(json.dumps(c) for c in claims)
+ t0 = time.perf_counter()
+ proc = subprocess.run(
+ [JAVA, "-cp", CP, "ClaimConsistency", ont],
+ cwd=REASONER_DIR, input=payload, capture_output=True, text=True,
+ )
+ hermit_s = time.perf_counter() - t0
+ baseline = {}
+ for line in proc.stdout.splitlines():
+ if line.startswith("{"):
+ d = json.loads(line)
+ baseline[d["id"]] = d["consistent"]
+
+ if not baseline:
+ print("BASELINE FAILED:", proc.stderr[-500:])
+ return
+
+ agree = fn = fp = 0
+ fn_examples = []
+ for cid, comp in compiled.items():
+ base = baseline.get(cid)
+ if base is None:
+ continue
+ if comp == base:
+ agree += 1
+ elif comp and not base:
+ fn += 1
+ if len(fn_examples) < 3:
+ types = next(c["types"] for c in claims if c["id"] == cid)
+ fn_examples.append([t[1].rsplit("#", 1)[-1] for t in types])
+ else:
+ fp += 1
+
+ total = len(baseline)
+ print(f"\n=== PARITY over {total} claims ===")
+ print(f" agreement {agree}/{total} ({100.0*agree/total:.1f}%)")
+ print(f" FALSE NEGATIVES {fn} (contradiction missed - DANGEROUS)")
+ print(f" false positives {fp} (valid claim rejected)")
+ for ex in fn_examples:
+ print(f" missed: {' + '.join(ex)}")
+ print(f"\n=== SPEED over {total} claims ===")
+ print(f" compiled+DuckDB {compiled_s*1000:9.1f} ms total "
+ f"({compiled_s/total*1000:.3f} ms/claim)")
+ print(f" HermiT baseline {hermit_s*1000:9.1f} ms total "
+ f"({hermit_s/total*1000:.3f} ms/claim)")
+ print(f" speedup {hermit_s/compiled_s:.0f}x")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/benchmark/layer3-prototype/structural_parity.py b/benchmark/layer3-prototype/structural_parity.py
new file mode 100644
index 00000000..52e068b4
--- /dev/null
+++ b/benchmark/layer3-prototype/structural_parity.py
@@ -0,0 +1,209 @@
+"""Parity with STRUCTURAL claim generation.
+
+The previous sweep sampled random class pairs. Only 11.5% of the resulting
+claims exercised a contradiction, so 88% of the "100% agreement" result was two
+engines agreeing that an unremarkable claim was fine. That is weak evidence, and
+it also mislabelled two ontologies as having no contradiction surface when they
+declare 5 and 63 disjointness axioms respectively.
+
+This generator derives claims from the compiled structure instead, in three
+deliberately adversarial classes:
+
+ CONTRADICTORY built from a disjoint pair, then pushed DOWN the hierarchy to
+ subclasses so the contradiction is only reachable by
+ inference. Both engines must reject.
+ PLAUSIBLE two classes sharing a superclass but not known disjoint.
+ These are where the join's incompleteness would show up.
+ BENIGN a class with one of its own superclasses. Must never be
+ rejected; a rejection here is an unsoundness bug.
+
+The BENIGN class is the important one: it is a targeted soundness probe, not
+padding.
+"""
+from __future__ import annotations
+
+import json
+import random
+import subprocess
+import sys
+from collections import defaultdict
+from pathlib import Path
+
+REASONER_DIR = Path(__file__).resolve().parent.parent / "reasoner"
+JAVA = "/opt/homebrew/opt/openjdk@17/bin/java"
+CP = f".:{REASONER_DIR}/lib/*"
+
+
+def compile_ontology(path: str, out: str) -> dict | None:
+ try:
+ p = subprocess.run([JAVA, "-cp", CP, "CompileOntology", path, out],
+ cwd=REASONER_DIR, capture_output=True, text=True, timeout=900)
+ except subprocess.TimeoutExpired:
+ return None
+ for line in p.stdout.splitlines():
+ if line.startswith("{"):
+ return json.loads(line)
+ return None
+
+
+def build(compiled: dict):
+ supers = defaultdict(set)
+ subs = defaultdict(set)
+ for a, b in compiled["subsumptions"]:
+ supers[a].add(b)
+ subs[b].add(a)
+ disj = set()
+ for a, b in compiled["disjoint"]:
+ disj.add((a, b))
+ disj.add((b, a))
+ return supers, subs, disj
+
+
+def generate(compiled: dict, n: int, rng: random.Random):
+ supers, subs, disj = build(compiled)
+ classes = sorted(supers)
+ claims = []
+
+ disj_pairs = sorted({tuple(sorted(p)) for p in disj})
+
+ # CONTRADICTORY: descend from a disjoint pair to its subclasses, so the
+ # clash is only visible through the inferred hierarchy.
+ for a, b in disj_pairs:
+ da = sorted(subs.get(a, set()) | {a})
+ db = sorted(subs.get(b, set()) | {b})
+ if not da or not db:
+ continue
+ claims.append(("contradictory", rng.choice(da), rng.choice(db)))
+ if len(claims) >= n // 2:
+ break
+
+ # PLAUSIBLE: siblings under a shared superclass, not known disjoint.
+ by_super = defaultdict(list)
+ for c in classes:
+ for s in supers[c]:
+ if s != c:
+ by_super[s].append(c)
+ sibling_groups = [g for g in by_super.values() if len(g) >= 2]
+ target = n // 4
+ tries = 0
+ added = 0
+ while added < target and sibling_groups and tries < target * 40:
+ tries += 1
+ g = rng.choice(sibling_groups)
+ x, y = rng.sample(g, 2)
+ if (x, y) not in disj:
+ claims.append(("plausible", x, y))
+ added += 1
+
+ # BENIGN: a class plus one of its own superclasses. Must never be rejected.
+ added = 0
+ tries = 0
+ while added < n - len(claims) and tries < n * 40:
+ tries += 1
+ c = rng.choice(classes)
+ sup = [s for s in supers[c] if s != c]
+ if sup:
+ claims.append(("benign", c, rng.choice(sup)))
+ added += 1
+
+ return claims
+
+
+def baseline(ont: str, claims, timeout: int) -> dict:
+ payload = "\n".join(
+ json.dumps({"id": f"c{i}", "types": [["x", a], ["x", b]], "rels": []})
+ for i, (_, a, b) in enumerate(claims)
+ )
+ try:
+ p = subprocess.run([JAVA, "-cp", CP, "ClaimConsistency", ont],
+ cwd=REASONER_DIR, input=payload, capture_output=True,
+ text=True, timeout=timeout)
+ except subprocess.TimeoutExpired:
+ return {}
+ out = {}
+ for line in p.stdout.splitlines():
+ if line.startswith("{"):
+ d = json.loads(line)
+ out[d["id"]] = d["consistent"]
+ return out
+
+
+def join_fn(compiled: dict):
+ supers, _, disj = build(compiled)
+ unsat = set(compiled["unsatisfiable"])
+
+ def f(a, b):
+ if a in unsat or b in unsat:
+ return True
+ return any((x, y) in disj for x in supers.get(a, {a}) for y in supers.get(b, {b}))
+
+ return f
+
+
+def main() -> None:
+ ont = sys.argv[1]
+ n = int(sys.argv[2]) if len(sys.argv) > 2 else 120
+
+ compiled_meta = compile_ontology(ont, "/tmp/sp_compiled.json")
+ if compiled_meta is None:
+ print("compile failed/timeout")
+ return
+ compiled = json.load(open("/tmp/sp_compiled.json"))
+ print(f"{Path(ont).name}: {compiled_meta}")
+
+ rng = random.Random(20260727)
+ claims = generate(compiled, n, rng)
+ if not claims:
+ print(" no claims generated (no hierarchy/disjointness)")
+ return
+ kinds = defaultdict(int)
+ for k, _, _ in claims:
+ kinds[k] += 1
+ print(f" generated {len(claims)} claims: {dict(kinds)}")
+
+ f = join_fn(compiled)
+ base = baseline(ont, claims, 300)
+ if not base:
+ print(" baseline timeout")
+ return
+
+ stats = defaultdict(lambda: [0, 0, 0, 0]) # compared, agree, FN, FP
+ unsound_examples = []
+ for i, (kind, a, b) in enumerate(claims):
+ hb = base.get(f"c{i}")
+ if hb is None:
+ continue
+ join_rejects = f(a, b)
+ truth_rejects = not hb
+ s = stats[kind]
+ s[0] += 1
+ if join_rejects == truth_rejects:
+ s[1] += 1
+ elif truth_rejects and not join_rejects:
+ s[2] += 1 # missed: goes to tier 2
+ else:
+ s[3] += 1 # UNSOUND
+ if len(unsound_examples) < 3:
+ unsound_examples.append((kind, a.rsplit("#", 1)[-1], b.rsplit("#", 1)[-1]))
+
+ print(f"\n {'kind':14s} {'n':>5s} {'agree':>6s} {'tier2':>6s} {'UNSOUND':>8s}")
+ tot = [0, 0, 0, 0]
+ for k in ("contradictory", "plausible", "benign"):
+ c, a, fn, fp = stats[k]
+ if c:
+ print(f" {k:14s} {c:5d} {a:6d} {fn:6d} {fp:8d}")
+ for j in range(4):
+ tot[j] += stats[k][j]
+ print(f" {'TOTAL':14s} {tot[0]:5d} {tot[1]:6d} {tot[2]:6d} {tot[3]:8d}")
+ if tot[0]:
+ rate = 100 * (tot[0] - tot[2] - tot[3]) / tot[0]
+ print(f"\n exercised a real contradiction: "
+ f"{sum(1 for i,(k,a,b) in enumerate(claims) if base.get(f'c{i}') is False)}"
+ f"/{tot[0]}")
+ print(f" join agreement: {rate:.1f}% UNSOUND: {tot[3]}")
+ for e in unsound_examples:
+ print(f" !! UNSOUND {e}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/benchmark/layer3-prototype/verify_join_soundness.py b/benchmark/layer3-prototype/verify_join_soundness.py
new file mode 100644
index 00000000..2a543c2c
--- /dev/null
+++ b/benchmark/layer3-prototype/verify_join_soundness.py
@@ -0,0 +1,161 @@
+"""Is the 2-hop join SOUND across ontologies, not just on Pizza?
+
+The tiered design depends on one property:
+
+ if the join says "incompatible", it IS incompatible
+
+A single counterexample kills the design, because tier 1 rejects claims without
+consulting a reasoner. Completeness is not required (tier 2 handles the
+residual), but soundness is non-negotiable.
+
+Ground truth is the brute-force pairwise matrix computed by HermiT, so this is
+restricted to ontologies small enough for n^2 to be feasible.
+"""
+from __future__ import annotations
+
+import csv
+import itertools
+import json
+import re
+import subprocess
+import sys
+from pathlib import Path
+
+REASONER_DIR = Path(__file__).resolve().parent.parent / "reasoner"
+JAVA = "/opt/homebrew/opt/openjdk@17/bin/java"
+CP = f".:{REASONER_DIR}/lib/*"
+
+
+def run(cls: str, *args: str, timeout: int = 600) -> dict | None:
+ try:
+ p = subprocess.run([JAVA, "-cp", CP, cls, *args], cwd=REASONER_DIR,
+ capture_output=True, text=True, timeout=timeout)
+ except subprocess.TimeoutExpired:
+ return None
+ for line in p.stdout.splitlines():
+ if line.startswith("{"):
+ return json.loads(line)
+ return None
+
+
+def join_incompatible(compiled: dict):
+ supers: dict[str, set[str]] = {}
+ for sub, sup in compiled["subsumptions"]:
+ supers.setdefault(sub, set()).add(sup)
+ disj = set()
+ for a, b in compiled["disjoint"]:
+ disj.add((a, b))
+ disj.add((b, a))
+ unsat = set(compiled["unsatisfiable"])
+
+ def f(a: str, b: str) -> bool:
+ if a in unsat or b in unsat:
+ return True
+ return any((x, y) in disj
+ for x in supers.get(a, {a})
+ for y in supers.get(b, {b}))
+
+ return f, sorted(supers)
+
+
+def check(path: Path, max_classes: int) -> dict:
+ res = {"ontology": path.name}
+ n = len(re.findall(r"Declaration\(Class\(<", path.read_text(errors="ignore")))
+ res["declared_classes"] = n
+ if n > max_classes:
+ res["status"] = "skipped_too_large"
+ return res
+
+ compiled = run("CompileOntology", str(path), "/tmp/vjs_compiled.json")
+ if compiled is None:
+ res["status"] = "compile_failed"
+ return res
+ res["classify_ms"] = compiled["classify_ms"]
+ res["compiled_rows"] = compiled["subsumptions"] + compiled["disjoint_axioms"]
+
+ matrix = run("DisjointnessMatrix", str(path), "/tmp/vjs_matrix.csv")
+ if matrix is None:
+ res["status"] = "matrix_timeout"
+ return res
+ res["matrix_ms"] = matrix["compile_ms"]
+ res["matrix_pairs"] = matrix["incompatible_pairs"]
+
+ data = json.load(open("/tmp/vjs_compiled.json"))
+ f, classes = join_incompatible(data)
+ truth = set()
+ with open("/tmp/vjs_matrix.csv") as fh:
+ rd = csv.reader(fh)
+ next(rd, None)
+ for a, b in rd:
+ truth.add((a, b))
+
+ settled = unsound = residual = missed = 0
+ examples = []
+ for a, b in itertools.combinations(classes, 2):
+ if f(a, b):
+ settled += 1
+ if (a, b) not in truth:
+ unsound += 1
+ if len(examples) < 3:
+ examples.append((a.rsplit("#", 1)[-1], b.rsplit("#", 1)[-1]))
+ else:
+ residual += 1
+ if (a, b) in truth:
+ missed += 1
+
+ total = settled + residual
+ res.update({
+ "status": "ok",
+ "pairs": total,
+ "settled_by_join": settled,
+ "settled_pct": round(100 * settled / total, 1) if total else 0.0,
+ "UNSOUND": unsound,
+ "unsound_examples": examples,
+ "residual": residual,
+ "missed_by_join": missed,
+ })
+ return res
+
+
+def main() -> None:
+ corpus = Path(sys.argv[1])
+ max_classes = int(sys.argv[2]) if len(sys.argv) > 2 else 250
+
+ targets = [p for p in sorted(corpus.glob("*.owl"))]
+ rows, done = [], 0
+ for p in targets:
+ if done >= 12:
+ break
+ r = check(p, max_classes)
+ if r["status"] == "skipped_too_large":
+ continue
+ done += 1
+ rows.append(r)
+ print(f"{r['ontology'][:26]:28s} {r['status']:16s} "
+ f"cls={r.get('declared_classes','-'):>5} "
+ f"settled={r.get('settled_pct','-')}% "
+ f"UNSOUND={r.get('UNSOUND','-')} "
+ f"missed={r.get('missed_by_join','-')}", flush=True)
+ if r.get("UNSOUND"):
+ print(f" !! counterexamples: {r['unsound_examples']}")
+
+ ok = [r for r in rows if r["status"] == "ok"]
+ print("\n================ SOUNDNESS VERDICT ================")
+ print(f" ontologies checked : {len(ok)}")
+ if ok:
+ tu = sum(r["UNSOUND"] for r in ok)
+ tp = sum(r["pairs"] for r in ok)
+ ts = sum(r["settled_by_join"] for r in ok)
+ tm = sum(r["missed_by_join"] for r in ok)
+ print(f" pairs compared : {tp:,}")
+ print(f" settled by join : {ts:,} ({100*ts/tp:.1f}%)")
+ print(f" UNSOUND (join wrongly rejects) : {tu}")
+ print(f" missed by join (goes to tier 2): {tm:,}")
+ print(f" compile: join {sum(r['classify_ms'] for r in ok):,} ms"
+ f" vs matrix {sum(r['matrix_ms'] for r in ok):,} ms")
+ print(f"\n {'SOUND on this sample' if tu == 0 else 'UNSOUND - DESIGN INVALID'}")
+ Path("/tmp/join_soundness.json").write_text(json.dumps(rows, indent=2))
+
+
+if __name__ == "__main__":
+ main()
diff --git a/benchmark/reasoner/ClaimConsistency.java b/benchmark/reasoner/ClaimConsistency.java
new file mode 100644
index 00000000..d34425cf
--- /dev/null
+++ b/benchmark/reasoner/ClaimConsistency.java
@@ -0,0 +1,118 @@
+/*
+ * Parity baseline: full ABox consistency checking of candidate claims.
+ *
+ * Reads claims as JSON-lines on stdin, each of the form
+ * {"id":"c1","types":[["x","IRI"],...],"rels":[["x","propIRI","y"],...]}
+ *
+ * For each claim, asserts it into a fresh copy of the ontology and asks HermiT
+ * whether the resulting knowledge base is consistent. This is the ground truth
+ * the compiled DuckDB path must agree with.
+ *
+ * Emits JSON-lines: {"id":"c1","consistent":true,"ms":12}
+ *
+ * Usage: java ClaimConsistency <ontology> < claims.jsonl > verdicts.jsonl
+ */
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.*;
+import org.semanticweb.owlapi.reasoner.OWLReasoner;
+import org.semanticweb.HermiT.ReasonerFactory;
+
+public class ClaimConsistency {
+
+ /** Minimal JSON array-of-arrays extractor; the input is machine-written. */
+ static List<String[]> tuples(String json, String key, int arity) {
+ List<String[]> out = new ArrayList<>();
+ int k = json.indexOf("\"" + key + "\"");
+ if (k < 0) return out;
+ int start = json.indexOf('[', k);
+ if (start < 0) return out;
+ int depth = 0, i = start;
+ for (; i < json.length(); i++) {
+ char c = json.charAt(i);
+ if (c == '[') depth++;
+ else if (c == ']') { depth--; if (depth == 0) break; }
+ }
+ String body = json.substring(start + 1, i);
+ int p = 0;
+ while (true) {
+ int a = body.indexOf('[', p);
+ if (a < 0) break;
+ int b = body.indexOf(']', a);
+ if (b < 0) break;
+ String[] parts = body.substring(a + 1, b).split(",");
+ String[] vals = new String[arity];
+ boolean ok = parts.length >= arity;
+ for (int j = 0; ok && j < arity; j++) {
+ String s = parts[j].trim();
+ if (s.length() >= 2 && s.charAt(0) == '"') s = s.substring(1, s.length() - 1);
+ vals[j] = s;
+ }
+ if (ok) out.add(vals);
+ p = b + 1;
+ }
+ return out;
+ }
+
+ static String field(String json, String key) {
+ int k = json.indexOf("\"" + key + "\"");
+ if (k < 0) return "";
+ int c = json.indexOf(':', k);
+ int q1 = json.indexOf('"', c);
+ int q2 = json.indexOf('"', q1 + 1);
+ return json.substring(q1 + 1, q2);
+ }
+
+ public static void main(String[] args) throws Exception {
+ File ontFile = new File(args[0]);
+ OWLOntologyManager m = OWLManager.createOWLOntologyManager();
+ OWLOntology base = m.loadOntologyFromOntologyDocument(ontFile);
+ OWLDataFactory df = m.getOWLDataFactory();
+ String IND = "http://tardygrada.example/claim#";
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ String line;
+ while ((line = in.readLine()) != null) {
+ line = line.trim();
+ if (line.isEmpty()) continue;
+ String id = field(line, "id");
+
+ List<OWLAxiom> added = new ArrayList<>();
+ for (String[] t : tuples(line, "types", 2)) {
+ added.add(df.getOWLClassAssertionAxiom(
+ df.getOWLClass(IRI.create(t[1])),
+ df.getOWLNamedIndividual(IRI.create(IND + t[0]))));
+ }
+ for (String[] r : tuples(line, "rels", 3)) {
+ added.add(df.getOWLObjectPropertyAssertionAxiom(
+ df.getOWLObjectProperty(IRI.create(r[1])),
+ df.getOWLNamedIndividual(IRI.create(IND + r[0])),
+ df.getOWLNamedIndividual(IRI.create(IND + r[2]))));
+ }
+
+ long t0 = System.currentTimeMillis();
+ boolean consistent;
+ try {
+ m.addAxioms(base, new java.util.HashSet<>(added));
+ OWLReasoner r = new ReasonerFactory().createReasoner(base);
+ consistent = r.isConsistent();
+ r.dispose();
+ } catch (Exception e) {
+ consistent = true; // could not decide; do not claim a clash
+ } finally {
+ m.removeAxioms(base, new java.util.HashSet<>(added));
+ }
+ long ms = System.currentTimeMillis() - t0;
+
+ System.out.println("{\"id\":\"" + id + "\",\"consistent\":"
+ + consistent + ",\"ms\":" + ms + "}");
+ System.out.flush();
+ }
+ }
+}
diff --git a/benchmark/reasoner/CompileOntology.java b/benchmark/reasoner/CompileOntology.java
new file mode 100644
index 00000000..d5265bda
--- /dev/null
+++ b/benchmark/reasoner/CompileOntology.java
@@ -0,0 +1,386 @@
+/*
+ * The mature compile step: ONE classification, then read the axioms off.
+ *
+ * Emits, as JSON:
+ * subsumptions the INFERRED class hierarchy (reflexive-transitive)
+ * disjoint the ASSERTED disjointness axioms, minimal set
+ * unsatisfiable classes equivalent to owl:Nothing
+ *
+ * Incompatibility is then DERIVED at query time by a two-hop join:
+ *
+ * A ⊓ B unsatisfiable iff ∃ A' ⊒ A, B' ⊒ B with disjoint(A', B')
+ *
+ * This replaces materialising the O(n^2) pairwise matrix. Pizza needs 3,944
+ * stored pairs the brute-force way; the same information is carried by ~400
+ * subsumptions plus a few dozen disjointness axioms, and costs ONE
+ * classification instead of n^2/2 satisfiability tests.
+ *
+ * Caveat this design must be measured against, not assumed away: the two-hop
+ * join captures incompatibility that follows from subsumption + disjointness.
+ * It does NOT capture incompatibility arising some other way (e.g. cardinality
+ * clashes, or a class unsatisfiable only in combination via property
+ * restrictions). CompareCompiled checks exactly that gap.
+ *
+ * Usage: java CompileOntology <ontology> <out.json>
+ */
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.*;
+import org.semanticweb.owlapi.reasoner.OWLReasoner;
+import org.semanticweb.owlapi.reasoner.InferenceType;
+import org.semanticweb.owlapi.reasoner.Node;
+import org.semanticweb.HermiT.ReasonerFactory;
+
+public class CompileOntology {
+
+ static String q(String s) {
+ return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
+ }
+
+ public static void main(String[] args) throws Exception {
+ OWLOntologyManager m = OWLManager.createOWLOntologyManager();
+ OWLOntology ont = m.loadOntologyFromOntologyDocument(new File(args[0]));
+ OWLReasoner r = new ReasonerFactory().createReasoner(ont);
+
+ long t0 = System.currentTimeMillis();
+ r.precomputeInferences(InferenceType.CLASS_HIERARCHY);
+ long classifyMs = System.currentTimeMillis() - t0;
+
+ // ── inferred hierarchy ──────────────────────────────────────────
+ List<String> subs = new ArrayList<>();
+ List<String> unsat = new ArrayList<>();
+ for (OWLClass c : ont.getClassesInSignature()) {
+ if (c.isOWLThing() || c.isOWLNothing()) continue;
+ if (!r.isSatisfiable(c)) {
+ unsat.add(q(c.getIRI().toString()));
+ continue;
+ }
+ // Reflexive.
+ subs.add("[" + q(c.getIRI().toString()) + "," + q(c.getIRI().toString()) + "]");
+ for (OWLClass sup : r.getSuperClasses(c, false).getFlattened()) {
+ if (sup.isOWLThing()) continue;
+ subs.add("[" + q(c.getIRI().toString()) + "," + q(sup.getIRI().toString()) + "]");
+ }
+ // Equivalent classes subsume each other.
+ Node<OWLClass> eq = r.getEquivalentClasses(c);
+ for (OWLClass e : eq.getEntities()) {
+ if (e.isOWLThing() || e.isOWLNothing() || e.equals(c)) continue;
+ subs.add("[" + q(c.getIRI().toString()) + "," + q(e.getIRI().toString()) + "]");
+ }
+ }
+
+ // ── asserted disjointness, the MINIMAL set ──────────────────────
+ List<String> disj = new ArrayList<>();
+ for (OWLAxiom ax : ont.getAxioms()) {
+ if (ax instanceof OWLDisjointClassesAxiom dca) {
+ List<OWLClassExpression> ops = new ArrayList<>(dca.getClassExpressionsAsList());
+ for (int i = 0; i < ops.size(); i++) {
+ for (int j = i + 1; j < ops.size(); j++) {
+ if (ops.get(i).isAnonymous() || ops.get(j).isAnonymous()) continue;
+ disj.add("[" + q(ops.get(i).asOWLClass().getIRI().toString()) + ","
+ + q(ops.get(j).asOWLClass().getIRI().toString()) + "]");
+ }
+ }
+ }
+ }
+
+ // ── restriction-mediated disjointness propagation ───────────────
+ //
+ // Two sound rules, run to fixpoint over derived disjointness:
+ // R1 A ⊑ ∃R.C, B ⊑ ∀R.(D1⊔…⊔Dn), ∀i disj*(C,Di) ⟹ disj(A,B)
+ // (the A⊓B individual needs an R-successor in C, and all its
+ // R-successors fall in the union — the successor would be in
+ // C ⊓ Di for some i, which is empty)
+ // R2 functional(R), A ⊑ ∃R.C, B ⊑ ∃R.D, disj*(C,D) ⟹ disj(A,B)
+ // (the unique R-value cannot be in two disjoint classes)
+ //
+ // disj* is disjointness up to inferred subsumption (the same two-hop
+ // the runtime join performs). Fixpoint matters: R2 derives
+ // topping-level disjointness that R1 then lifts to pizza level.
+ //
+ // Deliberately NOT handled in v1, so the envelope is explicit:
+ // inverse/anonymous properties, role hierarchies (∃R vs ∀S with
+ // R ⊑ S), and nominal fillers (hasValue). All are sound extensions.
+
+ // Named reflexive ancestor sets from the inferred hierarchy.
+ Map<String, Set<String>> anc = new HashMap<>();
+ for (OWLClass c : ont.getClassesInSignature()) {
+ if (c.isOWLThing() || c.isOWLNothing() || !r.isSatisfiable(c)) continue;
+ Set<String> a = new HashSet<>();
+ a.add(c.getIRI().toString());
+ for (OWLClass sup : r.getSuperClasses(c, false).getFlattened())
+ if (!sup.isOWLThing()) a.add(sup.getIRI().toString());
+ for (OWLClass eq : r.getEquivalentClasses(c).getEntities())
+ if (!eq.isOWLThing() && !eq.isOWLNothing()) a.add(eq.getIRI().toString());
+ anc.put(c.getIRI().toString(), a);
+ }
+
+ // Directly asserted/defined restrictions per named class, then
+ // inherited along the inferred hierarchy.
+ Map<String, List<String[]>> ex = new HashMap<>(); // class -> [prop, filler...]
+ Map<String, List<String[]>> all = new HashMap<>(); // class -> [prop, filler...]
+ Map<String, List<String[]>> unions = new HashMap<>(); // class -> union operands
+ Map<String, List<String[]>> hv = new HashMap<>(); // class -> [dataProp, datatype, lexical]
+ Map<String, Set<String>> dmax1 = new HashMap<>(); // class -> dataProps with card <= 1
+ for (OWLAxiom ax : ont.getAxioms()) {
+ OWLClass named = null;
+ List<OWLClassExpression> exprs = new ArrayList<>();
+ if (ax instanceof OWLSubClassOfAxiom sca && !sca.getSubClass().isAnonymous()) {
+ named = sca.getSubClass().asOWLClass();
+ exprs.add(sca.getSuperClass());
+ } else if (ax instanceof OWLEquivalentClassesAxiom eca) {
+ for (OWLClassExpression e : eca.getClassExpressions())
+ if (!e.isAnonymous()) named = e.asOWLClass();
+ if (named != null) exprs.addAll(eca.getClassExpressions());
+ }
+ if (named == null) continue;
+ // Unfold top-level intersections so definition conjuncts count.
+ List<OWLClassExpression> flat = new ArrayList<>();
+ for (OWLClassExpression e : exprs) {
+ if (e instanceof OWLObjectIntersectionOf io) flat.addAll(io.getOperands());
+ else flat.add(e);
+ }
+ for (OWLClassExpression e : flat) {
+ if (e instanceof OWLObjectSomeValuesFrom sv
+ && !sv.getProperty().isAnonymous()
+ && !sv.getFiller().isAnonymous()) {
+ ex.computeIfAbsent(named.getIRI().toString(), x -> new ArrayList<>())
+ .add(new String[]{sv.getProperty().getNamedProperty().getIRI().toString(),
+ sv.getFiller().asOWLClass().getIRI().toString()});
+ } else if (e instanceof OWLObjectMinCardinality mc
+ && mc.getCardinality() >= 1
+ && !mc.getProperty().isAnonymous()
+ && !mc.getFiller().isAnonymous()) {
+ ex.computeIfAbsent(named.getIRI().toString(), x -> new ArrayList<>())
+ .add(new String[]{mc.getProperty().getNamedProperty().getIRI().toString(),
+ mc.getFiller().asOWLClass().getIRI().toString()});
+ } else if (e instanceof OWLObjectUnionOf uo2) {
+ // B ⊑ (D1 ⊔ … ⊔ Dn). Only usable when EVERY operand is
+ // named: deriving from a subset of the operands would
+ // claim a tighter constraint than the axiom states, which
+ // is unsound.
+ List<String> ops = new ArrayList<>();
+ boolean allNamed2 = true;
+ for (OWLClassExpression u : uo2.getOperands()) {
+ if (u.isAnonymous()) { allNamed2 = false; break; }
+ ops.add(u.asOWLClass().getIRI().toString());
+ }
+ if (allNamed2 && !ops.isEmpty()) {
+ unions.computeIfAbsent(named.getIRI().toString(), x -> new ArrayList<>())
+ .add(ops.toArray(new String[0]));
+ }
+ } else if (e instanceof OWLDataHasValue dhv
+ && dhv.getProperty() instanceof OWLDataProperty dp) {
+ OWLLiteral lit = dhv.getFiller();
+ hv.computeIfAbsent(named.getIRI().toString(), x -> new ArrayList<>())
+ .add(new String[]{dp.getIRI().toString(),
+ lit.getDatatype().getIRI().toString(),
+ lit.getLiteral()});
+ } else if (e instanceof OWLDataMaxCardinality dmc
+ && dmc.getCardinality() <= 1
+ && dmc.getProperty() instanceof OWLDataProperty dp2) {
+ dmax1.computeIfAbsent(named.getIRI().toString(), x -> new HashSet<>())
+ .add(dp2.getIRI().toString());
+ } else if (e instanceof OWLDataExactCardinality dec
+ && dec.getCardinality() <= 1
+ && dec.getProperty() instanceof OWLDataProperty dp3) {
+ dmax1.computeIfAbsent(named.getIRI().toString(), x -> new HashSet<>())
+ .add(dp3.getIRI().toString());
+ } else if (e instanceof OWLObjectAllValuesFrom av
+ && !av.getProperty().isAnonymous()) {
+ List<String> fillers = new ArrayList<>();
+ OWLClassExpression f = av.getFiller();
+ if (!f.isAnonymous()) {
+ fillers.add(f.asOWLClass().getIRI().toString());
+ } else if (f instanceof OWLObjectUnionOf uo) {
+ boolean allNamed = true;
+ for (OWLClassExpression u : uo.getOperands()) {
+ if (u.isAnonymous()) { allNamed = false; break; }
+ fillers.add(u.asOWLClass().getIRI().toString());
+ }
+ if (!allNamed) continue;
+ } else continue;
+ String[] rec = new String[fillers.size() + 1];
+ rec[0] = av.getProperty().getNamedProperty().getIRI().toString();
+ for (int i = 0; i < fillers.size(); i++) rec[i + 1] = fillers.get(i);
+ all.computeIfAbsent(named.getIRI().toString(), x -> new ArrayList<>())
+ .add(rec);
+ }
+ }
+ }
+ // Inherit restrictions from inferred ancestors.
+ Map<String, List<String[]>> exInh = new HashMap<>();
+ Map<String, List<String[]>> allInh = new HashMap<>();
+ Map<String, List<String[]>> unionInh = new HashMap<>();
+ Map<String, List<String[]>> hvInh = new HashMap<>();
+ Map<String, Set<String>> dmax1Inh = new HashMap<>();
+ for (String c : anc.keySet()) {
+ for (String a : anc.get(c)) {
+ if (ex.containsKey(a))
+ exInh.computeIfAbsent(c, x -> new ArrayList<>()).addAll(ex.get(a));
+ if (all.containsKey(a))
+ allInh.computeIfAbsent(c, x -> new ArrayList<>()).addAll(all.get(a));
+ if (unions.containsKey(a))
+ unionInh.computeIfAbsent(c, x -> new ArrayList<>()).addAll(unions.get(a));
+ if (hv.containsKey(a))
+ hvInh.computeIfAbsent(c, x -> new ArrayList<>()).addAll(hv.get(a));
+ if (dmax1.containsKey(a))
+ dmax1Inh.computeIfAbsent(c, x -> new HashSet<>()).addAll(dmax1.get(a));
+ }
+ }
+
+ Set<String> functional = new HashSet<>();
+ for (OWLFunctionalObjectPropertyAxiom fa : ont.getAxioms(AxiomType.FUNCTIONAL_OBJECT_PROPERTY))
+ if (!fa.getProperty().isAnonymous())
+ functional.add(fa.getProperty().getNamedProperty().getIRI().toString());
+ Set<String> functionalData = new HashSet<>();
+ for (OWLFunctionalDataPropertyAxiom fda : ont.getAxioms(AxiomType.FUNCTIONAL_DATA_PROPERTY))
+ if (fda.getProperty() instanceof OWLDataProperty fdp)
+ functionalData.add(fdp.getIRI().toString());
+
+ // Symmetric pair set seeded from the asserted axioms collected above.
+ Set<String> disjSet = new HashSet<>();
+ List<String[]> disjPairsList = new ArrayList<>();
+ for (String d : disj) {
+ // parse back "["a","b"]"
+ String body = d.substring(2, d.length() - 2);
+ String[] ab = body.split("\",\"");
+ disjSet.add(ab[0] + "|" + ab[1]);
+ disjSet.add(ab[1] + "|" + ab[0]);
+ disjPairsList.add(ab);
+ }
+
+ java.util.function.BiPredicate<String, String> disjStar = (a, b) -> {
+ Set<String> aa = anc.getOrDefault(a, java.util.Collections.singleton(a));
+ Set<String> bb = anc.getOrDefault(b, java.util.Collections.singleton(b));
+ for (String x : aa) for (String y : bb)
+ if (disjSet.contains(x + "|" + y)) return true;
+ return false;
+ };
+
+ List<String> derived = new ArrayList<>();
+
+ // RD: distinct required literal values for a <=1-valued data property.
+ // A ⊑ hasValue(p, v1), B ⊑ hasValue(p, v2), v1 ≠ v2,
+ // and functional(p) OR some ancestor of A or B carries card<=1 on p
+ // ⟹ disj(A, B): the A⊓B individual would need two distinct values.
+ // Literal distinctness is claimed ONLY for same-datatype string/boolean
+ // literals, where lexically-distinct implies value-distinct. Numerics
+ // are excluded ("1" vs "01" differ lexically but not in value).
+ List<String> hvClasses = new ArrayList<>(hvInh.keySet());
+ for (int i = 0; i < hvClasses.size(); i++) {
+ String a = hvClasses.get(i);
+ for (int j = i + 1; j < hvClasses.size(); j++) {
+ String b = hvClasses.get(j);
+ if (disjStar.test(a, b)) continue;
+ outer:
+ for (String[] va : hvInh.get(a)) {
+ for (String[] vb : hvInh.get(b)) {
+ if (!va[0].equals(vb[0])) continue; // same property
+ if (!va[1].equals(vb[1])) continue; // same datatype
+ boolean stringy = va[1].endsWith("#string") || va[1].endsWith("#boolean")
+ || va[1].equals("http://www.w3.org/2000/01/rdf-schema#Literal");
+ if (!stringy || va[2].equals(vb[2])) continue; // distinct lexical
+ String prop = va[0];
+ boolean bounded = functionalData.contains(prop)
+ || dmax1Inh.getOrDefault(a, java.util.Collections.emptySet()).contains(prop)
+ || dmax1Inh.getOrDefault(b, java.util.Collections.emptySet()).contains(prop);
+ if (bounded) {
+ disjSet.add(a + "|" + b); disjSet.add(b + "|" + a);
+ derived.add("[" + q(a) + "," + q(b) + "]");
+ break outer;
+ }
+ }
+ }
+ }
+ }
+
+ int rounds = 0;
+ boolean changed = true;
+ while (changed && rounds++ < 20) {
+ changed = false;
+ for (String a : exInh.keySet()) {
+ for (String[] rx : exInh.get(a)) {
+ String prop = rx[0], c = rx[1];
+ // R1: against every ALL-restriction on the same property.
+ for (String b : allInh.keySet()) {
+ if (a.equals(b) || disjStar.test(a, b)) continue;
+ for (String[] ry : allInh.get(b)) {
+ if (!ry[0].equals(prop)) continue;
+ boolean allDisj = true;
+ for (int i = 1; i < ry.length; i++)
+ if (!disjStar.test(c, ry[i])) { allDisj = false; break; }
+ if (allDisj) {
+ disjSet.add(a + "|" + b); disjSet.add(b + "|" + a);
+ derived.add("[" + q(a) + "," + q(b) + "]");
+ changed = true;
+ break;
+ }
+ }
+ }
+ // R2: functional property, against every EX on the same property.
+ if (!functional.contains(prop)) continue;
+ for (String b : exInh.keySet()) {
+ if (a.equals(b) || disjStar.test(a, b)) continue;
+ for (String[] ry : exInh.get(b)) {
+ if (!ry[0].equals(prop)) continue;
+ if (disjStar.test(c, ry[1])) {
+ disjSet.add(a + "|" + b); disjSet.add(b + "|" + a);
+ derived.add("[" + q(a) + "," + q(b) + "]");
+ changed = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+ // RU: B is subsumed by a union of named classes, and A is
+ // disjoint from EVERY operand ⟹ disj(A, B). An A⊓B individual
+ // must fall in some operand, each of which excludes A. Runs
+ // inside the fixpoint because disj* grows as R1/R2/RU fire
+ // (e.g. category-level pairs derived here lift pizza-level pairs
+ // through R1 next round).
+ for (String b : unionInh.keySet()) {
+ for (String[] ops : unionInh.get(b)) {
+ for (String a : anc.keySet()) {
+ if (a.equals(b) || disjStar.test(a, b)) continue;
+ boolean allDisj = true;
+ for (String d : ops)
+ if (!disjStar.test(a, d)) { allDisj = false; break; }
+ if (allDisj) {
+ disjSet.add(a + "|" + b); disjSet.add(b + "|" + a);
+ derived.add("[" + q(a) + "," + q(b) + "]");
+ changed = true;
+ }
+ }
+ }
+ }
+ }
+ disj.addAll(derived);
+
+ PrintWriter out = new PrintWriter(args[1]);
+ out.println("{\"classify_ms\":" + classifyMs
+ + ",\"subsumptions\":[" + String.join(",", subs) + "]"
+ + ",\"disjoint\":[" + String.join(",", disj) + "]"
+ + ",\"disjoint_derived\":[" + String.join(",", derived) + "]"
+ + ",\"unsatisfiable\":[" + String.join(",", unsat) + "]}");
+ out.close();
+
+ System.out.println("{\"classify_ms\":" + classifyMs
+ + ",\"subsumptions\":" + subs.size()
+ + ",\"disjoint_axioms\":" + disj.size()
+ + ",\"disjoint_derived\":" + derived.size()
+ + ",\"propagation_rounds\":" + rounds
+ + ",\"unsatisfiable\":" + unsat.size() + "}");
+ r.dispose();
+ }
+}
diff --git a/benchmark/reasoner/DisjointnessMatrix.java b/benchmark/reasoner/DisjointnessMatrix.java
new file mode 100644
index 00000000..4953494a
--- /dev/null
+++ b/benchmark/reasoner/DisjointnessMatrix.java
@@ -0,0 +1,71 @@
+/*
+ * Offline compile step: compute the pairwise class-compatibility matrix.
+ *
+ * For every pair of named classes (A,B), ask a real OWL reasoner whether
+ * A ⊓ B is satisfiable. The pairs where it is NOT are the complete
+ * contradiction surface of the ontology, including every disjointness that
+ * follows by INFERENCE rather than by an explicit owl:disjointWith axiom.
+ *
+ * This is expensive (O(n^2) satisfiability tests) but it runs ONCE per
+ * ontology, offline. The output is a flat table that a columnar engine can
+ * check with a single indexed lookup at query time.
+ *
+ * Usage: java DisjointnessMatrix <ontology> <out.csv>
+ */
+
+import java.io.PrintWriter;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.*;
+import org.semanticweb.owlapi.reasoner.OWLReasoner;
+import org.semanticweb.HermiT.ReasonerFactory;
+
+public class DisjointnessMatrix {
+ public static void main(String[] args) throws Exception {
+ String ontPath = args[0];
+ String outPath = args[1];
+
+ OWLOntologyManager m = OWLManager.createOWLOntologyManager();
+ OWLOntology ont = m.loadOntologyFromOntologyDocument(new File(ontPath));
+ OWLDataFactory df = m.getOWLDataFactory();
+ OWLReasoner r = new ReasonerFactory().createReasoner(ont);
+
+ long tStart = System.currentTimeMillis();
+ r.precomputeInferences();
+
+ List<OWLClass> classes = new ArrayList<>();
+ for (OWLClass c : ont.getClassesInSignature()) {
+ if (!c.isOWLThing() && !c.isOWLNothing() && r.isSatisfiable(c)) {
+ classes.add(c);
+ }
+ }
+
+ PrintWriter out = new PrintWriter(outPath);
+ out.println("a,b");
+ int pairs = 0, incompatible = 0;
+ for (int i = 0; i < classes.size(); i++) {
+ for (int j = i + 1; j < classes.size(); j++) {
+ OWLClass a = classes.get(i), b = classes.get(j);
+ OWLObjectIntersectionOf both =
+ df.getOWLObjectIntersectionOf(a, b);
+ pairs++;
+ if (!r.isSatisfiable(both)) {
+ incompatible++;
+ out.println("\"" + a.getIRI() + "\",\"" + b.getIRI() + "\"");
+ out.println("\"" + b.getIRI() + "\",\"" + a.getIRI() + "\"");
+ }
+ }
+ }
+ out.close();
+ long ms = System.currentTimeMillis() - tStart;
+
+ System.out.println("{\"classes\":" + classes.size()
+ + ",\"pairs_tested\":" + pairs
+ + ",\"incompatible_pairs\":" + incompatible
+ + ",\"compile_ms\":" + ms + "}");
+ r.dispose();
+ }
+}
diff --git a/benchmark/reasoner/PairOracle.java b/benchmark/reasoner/PairOracle.java
new file mode 100644
index 00000000..7ba06165
--- /dev/null
+++ b/benchmark/reasoner/PairOracle.java
@@ -0,0 +1,81 @@
+/*
+ * Lazy pair oracle: keeps a reasoner warm and answers "is A ⊓ B satisfiable?"
+ * one pair at a time, on demand.
+ *
+ * This is what removes the O(n^2) blocker. Precomputing the full compatibility
+ * matrix is quadratic and dies past ~1000 classes (659 classes took 274s). But
+ * a claim stream only ever mentions a small working set of classes, so we only
+ * need the pairs that are actually asked for. Ask, cache, never ask again.
+ *
+ * Protocol: one line in, one line out.
+ * in: <classIRI-A>\t<classIRI-B>
+ * out: {"a":"...","b":"...","compatible":true,"ms":3}
+ *
+ * The ontology is loaded and precomputed ONCE at start-up, so per-pair cost is
+ * a single satisfiability test against a warm reasoner.
+ *
+ * Usage: java PairOracle <ontology>
+ */
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.InputStreamReader;
+
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.*;
+import org.semanticweb.owlapi.reasoner.OWLReasoner;
+import org.semanticweb.HermiT.ReasonerFactory;
+
+public class PairOracle {
+ public static void main(String[] args) throws Exception {
+ OWLOntologyManager m = OWLManager.createOWLOntologyManager();
+ OWLOntology ont = m.loadOntologyFromOntologyDocument(new File(args[0]));
+ OWLDataFactory df = m.getOWLDataFactory();
+ OWLReasoner r = new ReasonerFactory().createReasoner(ont);
+
+ // Deliberately NOT precomputeInferences(). Full classification is
+ // exactly the O(n * expensive) cost the lazy design exists to avoid,
+ // and it is not needed: a satisfiability test on A ⊓ B does not
+ // require the class hierarchy. On a 6,929-class ORE ontology the
+ // precompute alone ran past 14 minutes, which defeats the purpose.
+ // Touch consistency only, so the reasoner initialises its internal
+ // structures without classifying.
+ long t0 = System.currentTimeMillis();
+ r.isConsistent();
+ long warmMs = System.currentTimeMillis() - t0;
+
+ // Signal readiness with the one-off warm-up cost so callers can
+ // account for it separately from per-pair latency.
+ System.out.println("{\"ready\":true,\"warmup_ms\":" + warmMs
+ + ",\"classes\":" + ont.getClassesInSignature().size() + "}");
+ System.out.flush();
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ String line;
+ while ((line = in.readLine()) != null) {
+ line = line.trim();
+ if (line.isEmpty()) continue;
+ String[] parts = line.split("\t");
+ if (parts.length < 2) continue;
+
+ long s = System.nanoTime();
+ boolean compatible;
+ try {
+ OWLClassExpression both = df.getOWLObjectIntersectionOf(
+ df.getOWLClass(IRI.create(parts[0])),
+ df.getOWLClass(IRI.create(parts[1])));
+ compatible = r.isSatisfiable(both);
+ } catch (Exception e) {
+ // Undecided: report compatible so we never fabricate a clash.
+ compatible = true;
+ }
+ double ms = (System.nanoTime() - s) / 1e6;
+
+ System.out.println("{\"a\":\"" + parts[0] + "\",\"b\":\"" + parts[1]
+ + "\",\"compatible\":" + compatible
+ + ",\"ms\":" + String.format("%.3f", ms) + "}");
+ System.out.flush();
+ }
+ r.dispose();
+ }
+}
diff --git a/benchmark/reasoner/regressions/README.md b/benchmark/reasoner/regressions/README.md
new file mode 100644
index 00000000..53e1bc1b
--- /dev/null
+++ b/benchmark/reasoner/regressions/README.md
@@ -0,0 +1,76 @@
+# Reasoner regressions
+
+Minimal reproductions of reasoner defects. Each file is the smallest input
+found that triggers the behaviour.
+
+## `nominals_blowup.ttl` — owl-dl does not terminate on nominals
+
+**Found:** 2026-07-27, while repairing the LUBM benchmark harness.
+
+`owl:hasValue` introduces a nominal (`∃R.{o}`), the "O" in SHOIQ. Nominals
+break the tree-model property that ordinary subset blocking relies on, and the
+current tableaux implementation appears to have no nominal-aware blocking. Cost
+per additional nominal-bearing class is multiplicative, not additive:
+
+| Nominal classes | Triples | `reason --profile owl-dl` |
+| --------------- | ------- | ------------------------- |
+| 1 | 11 | 0.030s |
+| 2 | 19 | 0.029s |
+| 3 | 27 | 0.047s |
+| 4 | 35 | **> 20s (killed)** |
+
+Reproduce:
+
+```bash
+printf 'load benchmark/reasoner/regressions/nominals_blowup.ttl\nreason --profile owl-dl\n' \
+ | ./target/release/open-ontologies batch -
+```
+
+This is why the LUBM benchmark never completes at any size: `generate_lubm.py`
+emits one `owl:equivalentClass [ owl:hasValue ... ]` per department, so even the
+1,000-axiom file carries dozens of nominals.
+
+The Pizza ontology contains no `owl:hasValue` and classifies in ~0.04s, so the
+defect is specific to nominals, not to scale.
+
+**Status: contained, not fixed.**
+
+The underlying blowup is still there. What changed on 2026-07-27 is that it can
+no longer hang the process or corrupt the answer:
+
+- `Tableau::expand` previously returned `false` on budget exhaustion, the same
+ value it returns for a genuine clash. Callers therefore read "I ran out of
+ room" as "this concept is impossible", which in classification becomes an
+ asserted `owl:Nothing` subsumption. That was a **soundness** defect, not a
+ performance one.
+- Satisfiability is now three-valued (`Verdict::Satisfiable` /
+ `Unsatisfiable` / `Unknown`). Only a completed, clash-terminated search yields
+ `Unsatisfiable`. Exhaustion yields `Unknown` and the class lands in
+ `undetermined_classes`.
+- A per-test wall-clock deadline was added (`tableaux_test_timeout_ms`, default
+ 10s), because the node and depth budgets bound the size of the tableau but not
+ the number of BRANCHES explored. Nominal blowup is branch explosion, so only a
+ clock catches it.
+- Reasoner output now carries `complete: true|false`. A caller can finally tell
+ a proof from a run that gave up.
+
+Current behaviour on this file: terminates in ~10s with
+
+```json
+{"complete": false,
+ "undetermined_classes": ["...Department1", "...FacultyOf1", ...],
+ "unsatisfiable_classes": [],
+ "consistent": true}
+```
+
+Ten classes undetermined, zero fabricated unsatisfiability. That is the correct
+answer to give when you cannot decide.
+
+**Still open:** the reasoner cannot actually classify nominal-bearing
+ontologies. The fix is the NI-rule of Motik, Shearer and Horrocks, *Optimizing
+the Nominal Introduction Rule in (Hyper)Tableau Calculi*, which bounds root
+individual creation at `n` per `≤n R⁻.A` restriction instead of generating them
+freely. Separately, `update_blocking` implements ancestor **subset** blocking,
+while SHIQ and SHOIQ require **pairwise** blocking once inverse roles and number
+restrictions are present (Horrocks and Sattler). Both are prerequisites for any
+published performance claim on a real corpus.
diff --git a/benchmark/reasoner/regressions/nominals_blowup.ttl b/benchmark/reasoner/regressions/nominals_blowup.ttl
new file mode 100644
index 00000000..0970f8c4
--- /dev/null
+++ b/benchmark/reasoner/regressions/nominals_blowup.ttl
@@ -0,0 +1,14 @@
+@prefix owl: <http://www.w3.org/2002/07/owl#> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix ub: <http://lubm.example.org/> .
+ub:Faculty a owl:Class .
+ub:Department a owl:Class .
+ub:worksFor a owl:ObjectProperty .
+ub:Department1 a owl:Class ; rdfs:subClassOf ub:Department .
+ub:FacultyOf1 a owl:Class ; rdfs:subClassOf ub:Faculty ; owl:equivalentClass [ a owl:Restriction ; owl:onProperty ub:worksFor ; owl:hasValue ub:Department1 ] .
+ub:Department2 a owl:Class ; rdfs:subClassOf ub:Department .
+ub:FacultyOf2 a owl:Class ; rdfs:subClassOf ub:Faculty ; owl:equivalentClass [ a owl:Restriction ; owl:onProperty ub:worksFor ; owl:hasValue ub:Department2 ] .
+ub:Department3 a owl:Class ; rdfs:subClassOf ub:Department .
+ub:FacultyOf3 a owl:Class ; rdfs:subClassOf ub:Faculty ; owl:equivalentClass [ a owl:Restriction ; owl:onProperty ub:worksFor ; owl:hasValue ub:Department3 ] .
+ub:Department4 a owl:Class ; rdfs:subClassOf ub:Department .
+ub:FacultyOf4 a owl:Class ; rdfs:subClassOf ub:Faculty ; owl:equivalentClass [ a owl:Restriction ; owl:onProperty ub:worksFor ; owl:hasValue ub:Department4 ] .
\ No newline at end of file
diff --git a/benchmark/reasoner/results/lubm_results.json b/benchmark/reasoner/results/lubm_results.json
deleted file mode 100644
index 05411d66..00000000
--- a/benchmark/reasoner/results/lubm_results.json
+++ /dev/null
@@ -1,22 +0,0 @@
-[
- {
- "size": 1000,
- "oo_ms": 15,
- "hermit_ms": 112
- },
- {
- "size": 5000,
- "oo_ms": 14,
- "hermit_ms": 410
- },
- {
- "size": 10000,
- "oo_ms": 14,
- "hermit_ms": 1200
- },
- {
- "size": 50000,
- "oo_ms": 15,
- "hermit_ms": 24490
- }
-]
\ No newline at end of file
diff --git a/benchmark/reasoner/run_lubm_performance.sh b/benchmark/reasoner/run_lubm_performance.sh
index c76f1e77..5695029e 100755
--- a/benchmark/reasoner/run_lubm_performance.sh
+++ b/benchmark/reasoner/run_lubm_performance.sh
@@ -30,12 +30,48 @@ for SIZE in $SIZES; do
echo "--- Size: $SIZE axioms ---"
# Open Ontologies
+ #
+ # MUST be a single process. Each CLI invocation calls setup(), which
+ # builds a fresh empty in-memory GraphStore (src/main.rs), so `load` in
+ # one process and `reason` in another would time reasoning over ZERO
+ # triples — flat ~15ms of process start-up regardless of input size.
+ # Batch mode shares one store across commands, which is the only valid
+ # way to time this.
+ #
+ # Errors are NOT swallowed. A benchmark that silently reports a failed
+ # run as a fast run is worse than no benchmark, so the harness asserts
+ # triples > 0 before trusting any timing.
START_MS=$(python3 -c "import time; print(int(time.time()*1000))")
- $OO_BIN load "$OWL_FILE" > /dev/null 2>&1
- OO_OUT=$($OO_BIN reason --profile owl-dl 2>/dev/null || echo '{"error":"failed"}')
+ OO_OUT=$(printf 'load %s\nstats\nreason --profile owl-dl\n' "$OWL_FILE" \
+ | $OO_BIN batch -)
+ OO_STATUS=$?
END_MS=$(python3 -c "import time; print(int(time.time()*1000))")
OO_MS=$((END_MS - START_MS))
- echo " Open Ontologies: ${OO_MS}ms"
+
+ # Verify the store was actually populated before trusting the timing.
+ OO_TRIPLES=$(printf '%s' "$OO_OUT" | python3 -c "
+import json,sys
+n = 0
+for line in sys.stdin:
+ line = line.strip()
+ if not line:
+ continue
+ try:
+ rec = json.loads(line)
+ except json.JSONDecodeError:
+ continue
+ if rec.get('command') == 'stats':
+ n = rec.get('result', {}).get('triples', 0)
+print(n)
+")
+
+ if [ "$OO_STATUS" -ne 0 ] || [ "${OO_TRIPLES:-0}" -eq 0 ]; then
+ echo " Open Ontologies: FAILED (exit $OO_STATUS, $OO_TRIPLES triples in store)"
+ printf '%s\n' "$OO_OUT" | head -5
+ OO_MS="null"
+ else
+ echo " Open Ontologies: ${OO_MS}ms (${OO_TRIPLES} triples loaded)"
+ fi
# Java reasoners (if available)
HERMIT_MS="null"
diff --git a/docs/benchmarks.md b/docs/benchmarks.md
index d0dce7bd..31354498 100644
--- a/docs/benchmarks.md
+++ b/docs/benchmarks.md
@@ -74,26 +74,61 @@ Given the Manchester Pizza OWL and a 13-row restaurant CSV, map the data into th
Full writeup: [`benchmark/ontoaxiom/ONTOAXIOM_SHOWDOWN.md`](../benchmark/ontoaxiom/ONTOAXIOM_SHOWDOWN.md)
-## Reasoning Performance — HermiT vs Open Ontologies
+## Claim Verification — Compiled Reasoning vs HermiT
+
+The claim-verification benchmark measures the workload the `claimcheck` module
+is built for: a fixed ontology, compiled once, against a stream of candidate
+claims (small sets of type/relation assertions), each answered consistent /
+rejected / undetermined.
+
+### Methodology
+
+- **Task-matched**: both engines answer the identical question on the identical
+ file. The baseline (`ClaimConsistency.java`) asserts each claim as ABox
+ axioms and asks HermiT 1.4.3.456 for full KB consistency, warm JVM, ontology
+ loaded once and amortised across all claims.
+- **Ground truth**: `DisjointnessMatrix.java` exhaustively tests every named
+ class pair A ⊓ B for satisfiability with HermiT — the complete contradiction
+ surface of the ontology, used to audit recall and soundness.
+- **Adversarial claim generation**: `structural_parity.py` derives claims from
+ the compiled structure itself — disjoint pairs pushed down to subclasses
+ (contradictions reachable only by inference), sibling pairs (where
+ incompleteness would show), and class-plus-own-superclass probes (which must
+ never be rejected). Random pair sampling exercises a real contradiction only
+ ~11% of the time; structural generation reaches ~60%.
+- LUBM is deliberately not used: it is an ABox query benchmark with very simple
+ schemas, and the literature (e.g. Lam et al., DMKG 2023) warns against using
+ it to compare reasoners.
+
+### Results (canonical pizza.owl, 1,944 triples, 99 classes; Apple M3 Max)
+
+| Per-claim check | median | p95 | throughput |
+| --- | --- | --- | --- |
+| HermiT, warm | 4,936 µs | — | ~200 claims/s |
+| compiled token-bitset check | 0.3 µs | 0.4 µs | 3.1M/s seq, 11.2M/s batched |
+
+| Correctness | result |
+| --- | --- |
+| agreement with HermiT, 78,884 audited pairs (13 ontologies) | 100% |
+| agreement on 793 structurally adversarial claims (8 ontologies) | 100%, 0 false negatives |
+| contradiction recall, pizza.owl | 3,944/3,944 (100%) |
+| contradiction recall, ore_ont_10230 | 232/232 (100%) |
+| unsound rejections, all sweeps | 0 |
+
+The compiled surface is sound but deliberately incomplete: pairs it cannot
+settle return `Undetermined` and route to a reasoner-backed residual tier
+(4.9% of adversarial claims in aggregate; per-ontology tier-1 coverage varies
+with modelling style). The envelope and the propagation rules that close it
+per idiom are documented in
+[layer3-compiled-reasoning.md](layer3-compiled-reasoning.md).
+
+### Offline compile cost
+
+One classification pass plus a propagation fixpoint, per ontology:
+pizza.owl ~120 ms; 659-class ore_ont_12925 ~270 ms for the compile step itself;
+a 3,539-class ontology with 27k inferred subsumptions took 28.5 s to classify —
+a build-time cost to budget for, not a query-time one.
-**Pizza Ontology (4,179 triples)**
-
-| Tool | Time | Result |
-| ---- | ---- | ------ |
-| HermiT | 213ms | 312 subsumptions |
-| Open Ontologies (OWL-RL) | 43ms | Load + rule-based inference |
-| Open Ontologies (OWL-DL) | 19ms | Consistency check, SHOIQ tableaux |
-
-**LUBM Scaling (load + reason cycle)**
-
-| Axioms | Open Ontologies | HermiT | Speedup |
-| ------ | --------------- | ------- | ------- |
-| 1,000 | 15ms | 112ms | **7.5x** |
-| 5,000 | 14ms | 410ms | **29x** |
-| 10,000 | 14ms | 1,200ms | **86x** |
-| 50,000 | 15ms | 24,490ms | **1,633x** |
-
-Scripts and results: [`benchmark/reasoner/`](../benchmark/reasoner/)
## Running Benchmarks
diff --git a/docs/layer3-compiled-reasoning.md b/docs/layer3-compiled-reasoning.md
new file mode 100644
index 00000000..8010961f
--- /dev/null
+++ b/docs/layer3-compiled-reasoning.md
@@ -0,0 +1,201 @@
+# Compiled claim verification: design, guarantees, measurements
+
+**Module:** `src/claimcheck.rs` · **Compile step:** `benchmark/reasoner/CompileOntology.java`
+**Verification harness:** `benchmark/layer3-prototype/` · **Status:** measured 2026-07-27
+
+## The problem shape
+
+A verification gate for machine-generated claims has a workload unlike ontology
+engineering: one **fixed** ontology known in advance, a **stream** of small
+candidate claims (a handful of type and relation assertions each), and a hard
+latency budget per verdict. General-purpose DL reasoning pays its cost per
+query; this workload wants the cost paid once.
+
+> Keep the ontology as the **specification**. Run a complete reasoner **once,
+> offline**. Compile its conclusions into flat indexed structures. At query
+> time, do **no reasoning at all** — only lookups. Pin the two paths together
+> with a parity test against an independent reasoner.
+
+## Architecture
+
+### Offline compile — one classification pass
+
+`CompileOntology.java` classifies the ontology (HermiT today; any complete
+reasoner) and emits:
+
+- the **inferred** class hierarchy (reflexive-transitive, including
+ equivalences),
+- the **asserted** disjointness axioms,
+- disjointness pairs **derived** by the propagation rules below,
+- unsatisfiable classes.
+
+Incompatibility is represented, not materialised pairwise: the runtime derives
+it by a two-hop join,
+
+```
+A ⊓ B unsatisfiable if ∃ A' ⊒ A, B' ⊒ B with disjoint(A', B')
+```
+
+which keeps the compiled artifact near-linear in the ontology (Pizza: ~1k rows)
+rather than quadratic, and costs one classification instead of n²/2
+satisfiability tests.
+
+The compile MUST use a real reasoner: the join is only as good as the hierarchy
+it climbs, and classes are routinely subsumed by definitions only inference can
+see (Margherita ⊑ VegetarianPizza holds by inference, not assertion).
+
+### Propagation rules (compile-time, run to fixpoint)
+
+Each rule is a sound inference specialised to one modelling idiom observed in
+real ontologies; each lands derived pairs in the same compiled surface with
+zero hot-path cost:
+
+```
+R1 A ⊑ ∃R.C, B ⊑ ∀R.(D1⊔…⊔Dn), disj*(C, every Di) ⟹ disj(A,B)
+R2 functional(R), A ⊑ ∃R.C, B ⊑ ∃R.D, disj*(C,D) ⟹ disj(A,B)
+RU B ⊑ (D1 ⊔ … ⊔ Dn), disj*(A, every Di) ⟹ disj(A,B)
+RD A ⊑ hasValue(p,v1), B ⊑ hasValue(p,v2), v1 ≠ v2,
+ and functional(p) or an ancestor of A or B carries ≤1 p ⟹ disj(A,B)
+```
+
+Soundness guards worth noting: RU requires every union operand named (deriving
+from a subset would claim a tighter constraint than the axiom states); RD
+claims literal distinctness only for same-datatype string/boolean literals,
+because lexically-distinct numerics can be value-equal ("1" vs "01"). The
+fixpoint lets rules compose — e.g. R2 derives topping-level pairs from a
+functional property that R1 then lifts to pizza level through closure axioms,
+and category-level RU pairs are lifted by the runtime join to all subclasses.
+
+### Runtime — token bitsets
+
+IRIs are interned to `u32` at index build. Each disjointness axiom
+`disjoint(X_k, Y_k)` gets a token `k`; each class `C` gets two bitsets over
+tokens, `L(C) = {k : X_k ⊒ C}` and `R(C) = {k : Y_k ⊒ C}`. The join becomes
+
+```
+A ⊓ B unsatisfiable ⟺ L(A) ∧ R(B) ≠ ∅ ∨ R(A) ∧ L(B) ≠ ∅
+```
+
+— two bitwise ANDs over ⌈m/64⌉ words (Pizza: 447 axioms → 7 words), no
+allocation, no store access, no string comparison on the hot path. The first
+set bit is the **witness axiom**, so every rejection names the exact clashing
+disjointness pair.
+
+Batches fan out over rayon against the immutable `Arc`-shared index. Locks are
+acquired once per batch, not per claim: at sub-microsecond per-claim work,
+per-claim lock acquisition costs more than the work itself (measured: per-claim
+locking made the parallel path 3x slower than sequential; per-batch locking
+scales 3.6–4.3x).
+
+### The verdict contract
+
+The compiled surface is **sound but deliberately incomplete**, and the API
+makes that impossible to ignore:
+
+- `Rejected` — a contradiction was derived; trustworthy, with a witness.
+- `Undetermined` — nothing fired, which is NOT evidence of consistency. The
+ undecided class pairs are returned as `residual_pairs`.
+- `Consistent` — only reachable after the residual is discharged by tier 2
+ (`check_with_oracle`) or explicitly confirmed.
+
+Tier 2 (`ResidualOracle`) consults a real reasoner only for pairs the join
+leaves open; verdicts are cached back (incompatibilities as disjointness pairs,
+so tier 1 settles them thereafter; a tier-1 rejection never spends an oracle
+call). An oracle that cannot decide propagates `Undetermined`, never
+`Consistent`. Closed-world vocabulary checks (`declared_class`/`declared_prop`
+anti-membership) fire before anything else — open-world OWL semantics cannot
+flag an invented term, and for LLM-generated claims that is the most common
+failure.
+
+## Measurements
+
+All on Apple M3 Max, canonical `pizza.owl` (1,944 triples, 99 classes) unless
+stated, verdicts cross-checked against HermiT 1.4.3.456.
+
+### Latency and throughput
+
+| Per-claim check | median | p95 |
+| --- | --- | --- |
+| HermiT, warm JVM, amortised load | 4,936 µs | — |
+| compiled, Oxigraph-probe variant | 35.4 µs | 61.5 µs |
+| **compiled, token bitsets** | **0.3 µs** | **0.4 µs** |
+
+Throughput: 3.1M claims/s single-threaded, 11.2M/s batched.
+
+Substrate note: for store-backed point lookups, Oxigraph's `quads_for_pattern`
+measured 165x faster than parameterised SQL in DuckDB on identical data —
+point lookup is a triple store's home turf, analytic scans are a columnar
+engine's. The final hot path uses neither: interned arrays beat both once the
+data is static.
+
+### Correctness
+
+Audited against exhaustive HermiT satisfiability matrices (every named class
+pair) and adversarial structural claims:
+
+| | result |
+| --- | --- |
+| unsound rejections, 78,884 pairs, 13 ontologies | **0** |
+| agreement, 793 structural claims, 8 ontologies | **100%**, 0 false negatives |
+| contradiction recall, pizza.owl | **3,944/3,944 (100%)** |
+| contradiction recall, ore_ont_10230 | **232/232 (100%)** |
+| tier-2 residual, structural corpus aggregate | 4.9% of claims |
+
+Adversarial claim generation matters: random pair sampling exercises a real
+contradiction only ~11% of the time, so `structural_parity.py` derives claims
+from the compiled structure — disjoint pairs pushed down to subclasses
+(contradictions reachable only by inference), sibling pairs (where
+incompleteness would show), and class-plus-own-superclass probes (which must
+never be rejected).
+
+### Compile cost
+
+One classification + fixpoint: Pizza ~120 ms. Scales with ontology hardness,
+not just size — a 3,539-class ontology with 27k inferred subsumptions took
+28.5 s. Build-time cost; budget it per ontology revision.
+
+### Break-even
+
+The speed is amortisation, not faster reasoning: below a few hundred claims
+per ontology, calling HermiT directly is cheaper than compiling. A deployment
+verifying a claim stream against a fixed ontology crosses break-even in the
+first seconds of operation.
+
+## Envelope — stated, not implied
+
+Outside the compiled surface (and correctly routed to `Undetermined`/tier 2):
+
+- contradiction idioms not covered by R1/R2/RU/RD — observed in the wild:
+ one 27-axiom style at 92.1% tier-1 coverage; each new idiom is one small
+ sound rule away,
+- inverse properties, role hierarchies (∃R vs ∀S with R ⊑ S), nominal
+ fillers, numeric literal comparison,
+- **ontologies that declare no disjointness at all** — a substantial fraction
+ of large real-world ontologies. There the contradiction tier is empty by
+ construction and the closed-world vocabulary checks carry the verification
+ value. Any deployment claim must be conditioned on the target ontology's
+ declared (plus derivable) contradiction surface.
+
+Memory scales as n·m bits (classes × disjointness axioms, two bitsets each):
+fine to ~10k classes, needs sparse rows at SNOMED scale.
+
+Structural-parity claim samples vary across compile runs (Java set iteration
+order), so per-ontology structural numbers are comparable within a run; the
+exhaustive-matrix audit is the stable metric.
+
+## Reproduction
+
+```
+# compile an ontology
+java -cp ".:lib/*" CompileOntology <ont.owl> compiled.json
+
+# exhaustive ground-truth matrix
+java -cp ".:lib/*" DisjointnessMatrix <ont.owl> matrix.csv
+
+# parity + adversarial structural claims
+python benchmark/layer3-prototype/structural_parity.py <ont.owl> 120
+python benchmark/layer3-prototype/verify_join_soundness.py <corpus_dir> 250
+
+# Rust bench (loads compiled.json)
+cargo test --release --test claimcheck_pizza_bench -- --nocapture
+```
diff --git a/src/claimcheck.rs b/src/claimcheck.rs
new file mode 100644
index 00000000..58602f10
--- /dev/null
+++ b/src/claimcheck.rs
@@ -0,0 +1,922 @@
+//! Compiled claim verification: the Tardygrada Layer 3 hot path.
+//!
+//! Design and measurements: `docs/layer3-compiled-reasoning.md`.
+//!
+//! # The shape
+//!
+//! A reasoner runs ONCE, offline, and classifies the ontology. We store the
+//! *inferred* hierarchy plus the *asserted* disjointness axioms. At query time
+//! there is no reasoning, only the two-hop join:
+//!
+//! ```text
+//! A ⊓ B unsatisfiable if ∃ A' ⊒ A, B' ⊒ B with disjoint(A', B')
+//! ```
+//!
+//! # How the join is executed: token bitsets, not store probes
+//!
+//! The first Rust implementation ran the join as nested `quads_for_pattern`
+//! probes against an Oxigraph store: O(|supers(A)| × |supers(B)|) indexed
+//! lookups per pair, each allocating and validating IRIs. Correct, and already
+//! 140x faster than a warm reasoner — but the compiled data is *static*, so
+//! the mature move is to finish the compilation:
+//!
+//! * every IRI is interned to a `u32` once, at index build;
+//! * every asserted disjointness axiom `disjoint(X_k, Y_k)` gets a token `k`;
+//! * every class `C` gets two bitsets over tokens:
+//! `L(C) = { k : X_k ⊒ C }` and `R(C) = { k : Y_k ⊒ C }`.
+//!
+//! Then:
+//!
+//! ```text
+//! A ⊓ B unsatisfiable ⟺ L(A) ∧ R(B) ≠ ∅ or R(A) ∧ L(B) ≠ ∅
+//! ```
+//!
+//! Two bitwise ANDs over `⌈m/64⌉` words, where `m` is the number of
+//! disjointness axioms (Pizza: 398 axioms → 7 words). No allocation, no string
+//! comparison, no store. The hierarchy is consulted only at build time, when
+//! the token bitsets are computed from the inferred ancestor sets. The first
+//! set bit in the intersection is the *witness* axiom, which is exactly what
+//! the explainable verdict needs.
+//!
+//! # Soundness and completeness, measured
+//!
+//! Verified against a brute-force HermiT matrix over 78,884 pairs across 12
+//! ORE ontologies plus Pizza, and against 793 structurally adversarial claims
+//! over 8 ontologies:
+//!
+//! * **Sound**: 0 cases where the join rejected a satisfiable pair. A `Rejected`
+//! verdict from this module is therefore trustworthy.
+//! * **Incomplete**: catches 94.6% of true incompatibilities in aggregate
+//! (98.1% on Pizza, but as low as 25.9% on one ontology). The residual needs
+//! a reasoner call, because those contradictions follow from property
+//! restrictions rather than from subsumption plus disjointness.
+//!
+//! That asymmetry drives the API. "The join found nothing" is NOT evidence of
+//! consistency, so it yields [`Verdict::Undetermined`], never
+//! [`Verdict::Consistent`], unless the residual is discharged by tier 2
+//! ([`CompiledOntology::check_with_oracle`]) or explicitly confirmed.
+//!
+//! # Parallelism
+//!
+//! Claims are independent, so batches parallelise trivially:
+//! [`CompiledOntology::check_batch`] fans out over rayon. Per-claim parallelism
+//! would be counterproductive at these latencies — the work per claim is
+//! microseconds, far below fork/join overhead.
+
+use std::collections::{BTreeMap, HashMap, HashSet};
+use std::sync::{Arc, RwLock};
+
+use anyhow::Result;
+use rayon::prelude::*;
+use serde::{Deserialize, Serialize};
+
+/// A candidate claim, already decomposed into triples by Layer 1.
+#[derive(Debug, Clone, Default, Serialize, Deserialize)]
+pub struct Claim {
+ /// `(individual, class)` type assertions.
+ pub types: Vec<(String, String)>,
+ /// `(subject, property, object)` relation assertions.
+ pub rels: Vec<(String, String, String)>,
+}
+
+/// Which check fired, and the evidence that made it fire.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct Violation {
+ pub check: String,
+ pub detail: String,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub enum Verdict {
+ /// Every check passed AND the residual (the pairs the join cannot decide)
+ /// has been discharged. Only reachable via
+ /// [`CompiledOntology::check_with_oracle`] or
+ /// [`CheckResult::confirm_residual`].
+ Consistent,
+ /// A check fired. Sound: verified against a reasoner over 78,884 pairs.
+ Rejected,
+ /// Nothing fired, but the join is incomplete, so nothing is proven.
+ Undetermined,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct CheckResult {
+ pub verdict: Verdict,
+ pub violations: Vec<Violation>,
+ /// Class pairs the join could not settle. A caller wanting `Consistent`
+ /// must discharge these with a reasoner.
+ pub residual_pairs: Vec<(String, String)>,
+ pub elapsed_us: u128,
+}
+
+impl CheckResult {
+ /// Upgrade `Undetermined` to `Consistent` once the residual pairs have
+ /// been discharged by a reasoner (tier 2). Never downgrades a rejection.
+ pub fn confirm_residual(mut self) -> Self {
+ if self.verdict == Verdict::Undetermined && self.violations.is_empty() {
+ self.verdict = Verdict::Consistent;
+ self.residual_pairs.clear();
+ }
+ self
+ }
+}
+
+// ── bitset primitives ───────────────────────────────────────────────────
+
+/// A fixed-width bitset over disjointness tokens. Plain `Vec<u64>`; the widths
+/// involved (one bit per disjointness axiom) do not justify a dependency.
+type Bits = Vec<u64>;
+
+fn bits_new(nbits: usize) -> Bits {
+ vec![0u64; nbits.div_ceil(64)]
+}
+
+fn bits_set(b: &mut Bits, i: usize) {
+ b[i / 64] |= 1u64 << (i % 64);
+}
+
+/// First common set bit of `a ∧ b`, if any. This is the whole join.
+fn bits_first_common(a: &Bits, b: &Bits) -> Option<usize> {
+ for (w, (x, y)) in a.iter().zip(b.iter()).enumerate() {
+ let and = x & y;
+ if and != 0 {
+ return Some(w * 64 + and.trailing_zeros() as usize);
+ }
+ }
+ None
+}
+
+// ── the compiled index ──────────────────────────────────────────────────
+
+/// Raw load data, kept so the index can be (re)built after incremental loads.
+#[derive(Default)]
+struct RawData {
+ subsumptions: Vec<(String, String)>,
+ disjoint: Vec<(String, String)>,
+ declared_classes: HashSet<String>,
+ declared_props: HashSet<String>,
+ functional: HashSet<String>,
+}
+
+impl RawData {
+ fn is_empty(&self) -> bool {
+ self.subsumptions.is_empty()
+ && self.disjoint.is_empty()
+ && self.declared_classes.is_empty()
+ && self.declared_props.is_empty()
+ && self.functional.is_empty()
+ }
+}
+
+/// The immutable query structure. Built once per load generation; shared
+/// across threads via `Arc`, so batch checking needs no locks on the hot path.
+struct Index {
+ /// IRI → interned id, for classes only.
+ ids: HashMap<String, u32>,
+ /// id → IRI, for witness reporting.
+ names: Vec<String>,
+ /// Disjointness axioms as interned pairs; index = token.
+ pair_left: Vec<u32>,
+ pair_right: Vec<u32>,
+ /// Per class id: tokens whose left / right side subsumes this class.
+ l_tokens: Vec<Bits>,
+ r_tokens: Vec<Bits>,
+}
+
+impl Index {
+ fn build(raw: &RawData) -> Self {
+ // Intern every class mentioned anywhere.
+ let mut ids: HashMap<String, u32> = HashMap::new();
+ let mut names: Vec<String> = Vec::new();
+ let mut intern = |s: &str, ids: &mut HashMap<String, u32>, names: &mut Vec<String>| {
+ if let Some(&i) = ids.get(s) {
+ return i;
+ }
+ let i = names.len() as u32;
+ ids.insert(s.to_string(), i);
+ names.push(s.to_string());
+ i
+ };
+ for (a, b) in raw.subsumptions.iter().chain(raw.disjoint.iter()) {
+ intern(a, &mut ids, &mut names);
+ intern(b, &mut ids, &mut names);
+ }
+ for c in &raw.declared_classes {
+ intern(c, &mut ids, &mut names);
+ }
+ let n = names.len();
+
+ // Ancestor sets from the (already inferred, already transitive) load
+ // data, made reflexive defensively.
+ let mut ancestors: Vec<HashSet<u32>> = (0..n).map(|i| HashSet::from([i as u32])).collect();
+ for (sub, sup) in &raw.subsumptions {
+ let (s, p) = (ids[sub.as_str()], ids[sup.as_str()]);
+ ancestors[s as usize].insert(p);
+ }
+ // Invert: descendants(x) = every class with x among its ancestors.
+ let mut descendants: Vec<Vec<u32>> = vec![Vec::new(); n];
+ for (c, ancs) in ancestors.iter().enumerate() {
+ for &a in ancs {
+ descendants[a as usize].push(c as u32);
+ }
+ }
+
+ // Tokenise disjointness and stamp the bitsets down both hierarchies.
+ let m = raw.disjoint.len();
+ let mut pair_left = Vec::with_capacity(m);
+ let mut pair_right = Vec::with_capacity(m);
+ let mut l_tokens: Vec<Bits> = (0..n).map(|_| bits_new(m)).collect();
+ let mut r_tokens: Vec<Bits> = (0..n).map(|_| bits_new(m)).collect();
+ for (k, (x, y)) in raw.disjoint.iter().enumerate() {
+ let (xi, yi) = (ids[x.as_str()], ids[y.as_str()]);
+ pair_left.push(xi);
+ pair_right.push(yi);
+ for &c in &descendants[xi as usize] {
+ bits_set(&mut l_tokens[c as usize], k);
+ }
+ for &c in &descendants[yi as usize] {
+ bits_set(&mut r_tokens[c as usize], k);
+ }
+ }
+
+ Self { ids, names, pair_left, pair_right, l_tokens, r_tokens }
+ }
+
+ /// The two-hop join as two bitset intersections. Returns the witnessing
+ /// disjointness axiom `(left, right)` when incompatible.
+ ///
+ /// Symmetric by construction: both orientations are tested, so callers
+ /// never need to try both argument orders.
+ fn incompatible(&self, a: &str, b: &str) -> Option<(&str, &str)> {
+ let (&ai, &bi) = (self.ids.get(a)?, self.ids.get(b)?);
+ let (ai, bi) = (ai as usize, bi as usize);
+ if let Some(k) = bits_first_common(&self.l_tokens[ai], &self.r_tokens[bi]) {
+ return Some((
+ self.names[self.pair_left[k] as usize].as_str(),
+ self.names[self.pair_right[k] as usize].as_str(),
+ ));
+ }
+ if let Some(k) = bits_first_common(&self.r_tokens[ai], &self.l_tokens[bi]) {
+ return Some((
+ self.names[self.pair_right[k] as usize].as_str(),
+ self.names[self.pair_left[k] as usize].as_str(),
+ ));
+ }
+ None
+ }
+}
+
+/// The compiled ontology.
+///
+/// Loads accumulate raw data; the query index is built lazily on first check
+/// and invalidated by further loads (including tier-2 learning, which is how a
+/// learned incompatibility becomes a tier-1 answer). The index itself is
+/// immutable and `Arc`-shared, so concurrent checking is lock-free once built.
+pub struct CompiledOntology {
+ raw: RwLock<RawData>,
+ index: RwLock<Option<Arc<Index>>>,
+ /// Pairs tier 2 has confirmed compatible, in canonical order. Kept outside
+ /// the index so learning them does not force a rebuild.
+ known_compatible: RwLock<HashSet<(String, String)>>,
+}
+
+fn canon(a: &str, b: &str) -> (String, String) {
+ if a <= b {
+ (a.to_string(), b.to_string())
+ } else {
+ (b.to_string(), a.to_string())
+ }
+}
+
+impl CompiledOntology {
+ pub fn new() -> Result<Self> {
+ Ok(Self {
+ raw: RwLock::new(RawData::default()),
+ index: RwLock::new(None),
+ known_compatible: RwLock::new(HashSet::new()),
+ })
+ }
+
+ fn invalidate(&self) {
+ *self.index.write().unwrap() = None;
+ }
+
+ fn index(&self) -> Arc<Index> {
+ if let Some(ix) = self.index.read().unwrap().as_ref() {
+ return Arc::clone(ix);
+ }
+ let mut slot = self.index.write().unwrap();
+ // Double-checked: another thread may have built it while we waited.
+ if let Some(ix) = slot.as_ref() {
+ return Arc::clone(ix);
+ }
+ let built = Arc::new(Index::build(&self.raw.read().unwrap()));
+ *slot = Some(Arc::clone(&built));
+ built
+ }
+
+ // ── loading the compile output ──────────────────────────────────────
+
+ /// Inferred subsumptions, `(sub, sup)`. The compile step emits the full
+ /// inferred closure including reflexive pairs; reflexivity is enforced
+ /// here anyway, since the join relies on `A ⊒ A`.
+ pub fn load_subsumptions(&self, pairs: &[(String, String)]) -> Result<usize> {
+ self.raw.write().unwrap().subsumptions.extend_from_slice(pairs);
+ self.invalidate();
+ Ok(pairs.len())
+ }
+
+ /// Asserted disjointness axioms. Orientation does not matter: the join
+ /// tests both directions.
+ pub fn load_disjoint(&self, pairs: &[(String, String)]) -> Result<usize> {
+ self.raw.write().unwrap().disjoint.extend_from_slice(pairs);
+ self.invalidate();
+ Ok(pairs.len())
+ }
+
+ pub fn load_declared_classes(&self, iris: &[String]) -> Result<usize> {
+ self.raw.write().unwrap().declared_classes.extend(iris.iter().cloned());
+ self.invalidate();
+ Ok(iris.len())
+ }
+
+ pub fn load_declared_props(&self, iris: &[String]) -> Result<usize> {
+ self.raw.write().unwrap().declared_props.extend(iris.iter().cloned());
+ self.invalidate();
+ Ok(iris.len())
+ }
+
+ pub fn load_functional(&self, iris: &[String]) -> Result<usize> {
+ self.raw.write().unwrap().functional.extend(iris.iter().cloned());
+ self.invalidate();
+ Ok(iris.len())
+ }
+
+ pub fn is_empty(&self) -> Result<bool> {
+ Ok(self.raw.read().unwrap().is_empty())
+ }
+
+ // ── the hot path ────────────────────────────────────────────────────
+
+ pub fn check(&self, claim: &Claim) -> Result<CheckResult> {
+ // An empty store proves nothing about anything.
+ if self.is_empty()? {
+ return Ok(CheckResult {
+ verdict: Verdict::Undetermined,
+ violations: Vec::new(),
+ residual_pairs: Vec::new(),
+ elapsed_us: 0,
+ });
+ }
+ let ix = self.index();
+ let raw = self.raw.read().unwrap();
+ Ok(Self::check_inner(&raw, &ix, claim))
+ }
+
+ /// The lock-free hot path. Everything it touches is an immutable borrow,
+ /// so batches can fan out over threads with zero contention — the locks
+ /// are acquired ONCE per batch, not once per claim. Infallible by
+ /// construction: interning happened at index build, so there is no IRI
+ /// validation or store access left to fail.
+ fn check_inner(raw: &RawData, ix: &Index, claim: &Claim) -> CheckResult {
+ let start = std::time::Instant::now();
+ let mut violations = Vec::new();
+ let mut residual = Vec::new();
+
+ // 1. Closed-world vocabulary. Open-world OWL structurally cannot flag
+ // an invented term; this can, and for LLM output it is the check
+ // that fires most often. One hash lookup per term.
+ for (_, cls) in &claim.types {
+ if !raw.declared_classes.contains(cls) {
+ violations.push(Violation {
+ check: "unknown_class".into(),
+ detail: format!("class not declared in ontology: {cls}"),
+ });
+ }
+ }
+ for (_, prop, _) in &claim.rels {
+ if !raw.declared_props.contains(prop) {
+ violations.push(Violation {
+ check: "unknown_property".into(),
+ detail: format!("property not declared in ontology: {prop}"),
+ });
+ }
+ }
+
+ // 2. Incompatibility between types asserted of one individual: two
+ // bitset ANDs per pair, witness axiom extracted for the detail.
+ let mut by_subject: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
+ for (subj, cls) in &claim.types {
+ by_subject.entry(subj).or_default().push(cls);
+ }
+ for (subj, classes) in &by_subject {
+ for (i, a) in classes.iter().enumerate() {
+ for b in &classes[i + 1..] {
+ if let Some((wa, wb)) = ix.incompatible(a, b) {
+ violations.push(Violation {
+ check: "disjointness".into(),
+ detail: format!(
+ "{subj} cannot be both {a} and {b}: {wa} is declared \
+ disjoint with {wb}"
+ ),
+ });
+ } else {
+ // NOT proven compatible. The join is incomplete.
+ residual.push((a.to_string(), b.to_string()));
+ }
+ }
+ }
+ }
+
+ // 3. Functional property given two distinct values.
+ let mut seen: BTreeMap<(&str, &str), Vec<&str>> = BTreeMap::new();
+ for (s, p, o) in &claim.rels {
+ seen.entry((s, p)).or_default().push(o);
+ }
+ for ((s, p), objs) in &seen {
+ let mut d: Vec<&&str> = objs.iter().collect();
+ d.sort();
+ d.dedup();
+ if d.len() > 1 && raw.functional.contains(*p) {
+ violations.push(Violation {
+ check: "functionality".into(),
+ detail: format!(
+ "{s} has {} distinct values for functional property {p}",
+ d.len()
+ ),
+ });
+ }
+ }
+
+ let verdict = if !violations.is_empty() {
+ Verdict::Rejected
+ } else {
+ // Nothing fired. Because the join is incomplete this is NOT a
+ // clean bill of health. The caller must discharge `residual_pairs`
+ // before treating the claim as consistent.
+ Verdict::Undetermined
+ };
+
+ CheckResult {
+ verdict,
+ violations,
+ residual_pairs: residual,
+ elapsed_us: start.elapsed().as_micros(),
+ }
+ }
+
+ /// Check a batch of claims in parallel. Claims are independent, so this is
+ /// a straight rayon fan-out over the shared immutable index; there is no
+ /// lock contention on the hot path.
+ pub fn check_batch(&self, claims: &[Claim]) -> Result<Vec<CheckResult>> {
+ if self.is_empty()? {
+ // Same semantics as check() on an empty store, per claim.
+ return Ok(claims
+ .iter()
+ .map(|_| CheckResult {
+ verdict: Verdict::Undetermined,
+ violations: Vec::new(),
+ residual_pairs: Vec::new(),
+ elapsed_us: 0,
+ })
+ .collect());
+ }
+ // Acquire the index and the raw-data read lock ONCE, then fan out a
+ // pure function. The first version of this method called self.check()
+ // per item, which meant two RwLock acquisitions per claim; at ~0.35 us
+ // of real work per claim the resulting cache-line ping-pong made the
+ // parallel path 3x SLOWER than sequential. Locks per batch, not per
+ // claim.
+ let ix = self.index();
+ let raw = self.raw.read().unwrap();
+ Ok(claims
+ .par_iter()
+ .map(|c| Self::check_inner(&raw, &ix, c))
+ .collect())
+ }
+}
+
+// ── Tier 2: the residual oracle ─────────────────────────────────────────
+
+/// Settles class pairs the two-hop join cannot decide.
+///
+/// The join is sound but incomplete: measured recall is 94.6% of true
+/// incompatibilities across 13 ontologies, and as low as 25.9% on one. The
+/// residual arises from contradictions that follow from property restrictions
+/// rather than from subsumption plus disjointness, so deciding them needs a
+/// real reasoner.
+///
+/// Implementations are expected to be expensive (milliseconds) and are
+/// therefore consulted only for pairs the join leaves open, and only once per
+/// pair thanks to [`CompiledOntology::learn_incompatible`] /
+/// [`CompiledOntology::learn_compatible`].
+pub trait ResidualOracle {
+ /// `Some(true)` = compatible, `Some(false)` = incompatible,
+ /// `None` = the oracle could not decide either.
+ ///
+ /// Returning `None` must never be treated as "compatible": it propagates
+ /// to [`Verdict::Undetermined`].
+ fn compatible(&self, a: &str, b: &str) -> Result<Option<bool>>;
+}
+
+impl CompiledOntology {
+ /// Record an oracle verdict of "incompatible" so the join settles it next
+ /// time. Stored as a direct disjointness axiom, which is sound: the pair
+ /// IS disjoint, we simply could not derive it structurally. Invalidates
+ /// the index, so the next check absorbs it into the token bitsets.
+ pub fn learn_incompatible(&self, a: &str, b: &str) -> Result<()> {
+ self.load_disjoint(&[(a.to_string(), b.to_string())])?;
+ Ok(())
+ }
+
+ /// Record an oracle verdict of "compatible" so the pair is not re-asked.
+ /// Kept outside the index: learning compatibility must not force a
+ /// rebuild.
+ pub fn learn_compatible(&self, a: &str, b: &str) -> Result<()> {
+ self.known_compatible.write().unwrap().insert(canon(a, b));
+ Ok(())
+ }
+
+ fn known_compatible(&self, a: &str, b: &str) -> Result<bool> {
+ Ok(self.known_compatible.read().unwrap().contains(&canon(a, b)))
+ }
+
+ /// Full two-tier check. Tier 1 is the join; anything it leaves open goes to
+ /// `oracle`, and the answer is cached back.
+ ///
+ /// Returns [`Verdict::Consistent`] only when every residual pair was
+ /// positively confirmed compatible. An oracle that returns `None` leaves
+ /// the verdict [`Verdict::Undetermined`], never `Consistent`.
+ pub fn check_with_oracle<O: ResidualOracle>(
+ &self,
+ claim: &Claim,
+ oracle: &O,
+ ) -> Result<CheckResult> {
+ let mut r = self.check(claim)?;
+ if r.verdict == Verdict::Rejected {
+ return Ok(r); // Tier 1 rejections are sound; do not spend an oracle call.
+ }
+
+ let residual = std::mem::take(&mut r.residual_pairs);
+ let mut undecided = Vec::new();
+ for (a, b) in residual {
+ if self.known_compatible(&a, &b)? {
+ continue;
+ }
+ match oracle.compatible(&a, &b)? {
+ Some(true) => self.learn_compatible(&a, &b)?,
+ Some(false) => {
+ self.learn_incompatible(&a, &b)?;
+ r.violations.push(Violation {
+ check: "disjointness_tier2".into(),
+ detail: format!(
+ "{a} and {b} are incompatible; established by reasoner, not \
+ derivable from subsumption plus disjointness"
+ ),
+ });
+ }
+ None => undecided.push((a, b)),
+ }
+ }
+
+ r.residual_pairs = undecided;
+ r.verdict = if !r.violations.is_empty() {
+ Verdict::Rejected
+ } else if r.residual_pairs.is_empty() {
+ Verdict::Consistent
+ } else {
+ Verdict::Undetermined
+ };
+ Ok(r)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const PIZZA: &str = "http://www.co-ode.org/ontologies/pizza/pizza.owl#";
+
+ fn iri(l: &str) -> String {
+ format!("{PIZZA}{l}")
+ }
+
+ /// Mirrors the real compile output: the INFERRED hierarchy plus ASSERTED
+ /// disjointness. Margherita is a VegetarianPizza only by inference, and
+ /// that is exactly what makes the two-hop join work where a syntactic
+ /// subclass walk fails.
+ fn fixture() -> CompiledOntology {
+ let c = CompiledOntology::new().unwrap();
+ c.load_declared_classes(&[
+ iri("Margherita"), iri("MeatyPizza"), iri("VegetarianPizza"),
+ iri("CheeseyPizza"), iri("American"), iri("AmericanHot"), iri("NamedPizza"),
+ ]).unwrap();
+ c.load_declared_props(&[iri("hasBase"), iri("hasTopping")]).unwrap();
+ c.load_subsumptions(&[
+ // reflexive
+ (iri("Margherita"), iri("Margherita")),
+ (iri("MeatyPizza"), iri("MeatyPizza")),
+ (iri("VegetarianPizza"), iri("VegetarianPizza")),
+ (iri("American"), iri("American")),
+ (iri("AmericanHot"), iri("AmericanHot")),
+ (iri("CheeseyPizza"), iri("CheeseyPizza")),
+ // inferred
+ (iri("Margherita"), iri("VegetarianPizza")),
+ (iri("Margherita"), iri("CheeseyPizza")),
+ (iri("Margherita"), iri("NamedPizza")),
+ (iri("American"), iri("MeatyPizza")),
+ (iri("American"), iri("NamedPizza")),
+ (iri("AmericanHot"), iri("MeatyPizza")),
+ (iri("AmericanHot"), iri("NamedPizza")),
+ ]).unwrap();
+ c.load_disjoint(&[
+ (iri("VegetarianPizza"), iri("MeatyPizza")),
+ (iri("American"), iri("AmericanHot")),
+ ]).unwrap();
+ c.load_functional(&[iri("hasBase")]).unwrap();
+ c
+ }
+
+ #[test]
+ fn empty_store_never_claims_consistency() {
+ let c = CompiledOntology::new().unwrap();
+ let claim = Claim { types: vec![("x".into(), iri("Margherita"))], ..Default::default() };
+ assert_eq!(c.check(&claim).unwrap().verdict, Verdict::Undetermined);
+ }
+
+ /// The case a syntactic subclass walk misses. Margherita and MeatyPizza
+ /// have no direct disjointness axiom; the clash is only reachable through
+ /// the INFERRED Margherita ⊑ VegetarianPizza.
+ #[test]
+ fn two_hop_join_catches_inferred_disjointness() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("MeatyPizza"))],
+ ..Default::default()
+ };
+ let r = c.check(&claim).unwrap();
+ assert_eq!(r.verdict, Verdict::Rejected, "{r:?}");
+ assert_eq!(r.violations[0].check, "disjointness");
+ // The witness axiom must be named in the detail.
+ assert!(r.violations[0].detail.contains("VegetarianPizza"), "{r:?}");
+ }
+
+ #[test]
+ fn join_is_symmetric() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("MeatyPizza")), ("x".into(), iri("Margherita"))],
+ ..Default::default()
+ };
+ assert_eq!(c.check(&claim).unwrap().verdict, Verdict::Rejected);
+ }
+
+ #[test]
+ fn both_sides_climb_the_hierarchy() {
+ // American ⊑ MeatyPizza and Margherita ⊑ VegetarianPizza, and the
+ // disjointness sits between the two SUPERclasses. Neither side alone
+ // is enough; the join has to climb both.
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("American"))],
+ ..Default::default()
+ };
+ assert_eq!(c.check(&claim).unwrap().verdict, Verdict::Rejected);
+ }
+
+ /// The soundness/completeness asymmetry, pinned. A clean join is NOT a
+ /// clean bill of health, because the join misses ~5% of contradictions.
+ #[test]
+ fn clean_join_is_undetermined_not_consistent() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("CheeseyPizza"))],
+ ..Default::default()
+ };
+ let r = c.check(&claim).unwrap();
+ assert_eq!(r.verdict, Verdict::Undetermined, "{r:?}");
+ assert!(r.violations.is_empty());
+ assert_eq!(r.residual_pairs.len(), 1, "the undecided pair must be reported");
+ }
+
+ #[test]
+ fn residual_can_be_discharged_by_tier_two() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("CheeseyPizza"))],
+ ..Default::default()
+ };
+ let r = c.check(&claim).unwrap().confirm_residual();
+ assert_eq!(r.verdict, Verdict::Consistent);
+ assert!(r.residual_pairs.is_empty());
+ }
+
+ #[test]
+ fn confirming_residual_never_rescues_a_rejection() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("MeatyPizza"))],
+ ..Default::default()
+ };
+ let r = c.check(&claim).unwrap().confirm_residual();
+ assert_eq!(r.verdict, Verdict::Rejected, "a rejection must survive confirmation");
+ }
+
+ #[test]
+ fn hallucinated_class_is_caught() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("QuantumPineappleTopping"))],
+ ..Default::default()
+ };
+ let r = c.check(&claim).unwrap();
+ assert_eq!(r.verdict, Verdict::Rejected);
+ assert_eq!(r.violations[0].check, "unknown_class");
+ }
+
+ #[test]
+ fn hallucinated_property_is_caught() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita"))],
+ rels: vec![("x".into(), iri("hasVibes"), "y".into())],
+ };
+ let r = c.check(&claim).unwrap();
+ assert_eq!(r.verdict, Verdict::Rejected);
+ assert_eq!(r.violations[0].check, "unknown_property");
+ }
+
+ #[test]
+ fn functional_property_with_two_values_is_caught() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita"))],
+ rels: vec![
+ ("x".into(), iri("hasBase"), "b1".into()),
+ ("x".into(), iri("hasBase"), "b2".into()),
+ ],
+ };
+ let r = c.check(&claim).unwrap();
+ assert!(r.violations.iter().any(|v| v.check == "functionality"), "{r:?}");
+ }
+
+ #[test]
+ fn non_functional_property_with_two_values_is_fine() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita"))],
+ rels: vec![
+ ("x".into(), iri("hasTopping"), "t1".into()),
+ ("x".into(), iri("hasTopping"), "t2".into()),
+ ],
+ };
+ let r = c.check(&claim).unwrap();
+ assert!(r.violations.is_empty(), "{r:?}");
+ }
+
+ // ── tier 2 ──────────────────────────────────────────────────────────
+
+ /// An oracle that knows one extra incompatibility the join cannot derive,
+ /// modelling the property-restriction cases (e.g. American + SpicyPizza).
+ struct MockOracle {
+ incompatible: Vec<(String, String)>,
+ undecidable: Vec<(String, String)>,
+ pub calls: std::cell::RefCell<usize>,
+ }
+
+ impl ResidualOracle for MockOracle {
+ fn compatible(&self, a: &str, b: &str) -> Result<Option<bool>> {
+ *self.calls.borrow_mut() += 1;
+ let hit = |v: &Vec<(String, String)>| {
+ v.iter().any(|(x, y)| (x == a && y == b) || (x == b && y == a))
+ };
+ if hit(&self.undecidable) {
+ return Ok(None);
+ }
+ Ok(Some(!hit(&self.incompatible)))
+ }
+ }
+
+ fn oracle(incompatible: Vec<(String, String)>, undecidable: Vec<(String, String)>) -> MockOracle {
+ MockOracle { incompatible, undecidable, calls: std::cell::RefCell::new(0) }
+ }
+
+ #[test]
+ fn tier2_catches_what_the_join_misses() {
+ let c = fixture();
+ let o = oracle(vec![(iri("Margherita"), iri("CheeseyPizza"))], vec![]);
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("CheeseyPizza"))],
+ ..Default::default()
+ };
+ // Tier 1 alone cannot see this.
+ assert_eq!(c.check(&claim).unwrap().verdict, Verdict::Undetermined);
+ let r = c.check_with_oracle(&claim, &o).unwrap();
+ assert_eq!(r.verdict, Verdict::Rejected, "{r:?}");
+ assert_eq!(r.violations[0].check, "disjointness_tier2");
+ }
+
+ #[test]
+ fn tier2_confirms_consistency() {
+ let c = fixture();
+ let o = oracle(vec![], vec![]);
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("CheeseyPizza"))],
+ ..Default::default()
+ };
+ assert_eq!(c.check_with_oracle(&claim, &o).unwrap().verdict, Verdict::Consistent);
+ }
+
+ #[test]
+ fn undecidable_oracle_yields_undetermined_not_consistent() {
+ let c = fixture();
+ let o = oracle(vec![], vec![(iri("Margherita"), iri("CheeseyPizza"))]);
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("CheeseyPizza"))],
+ ..Default::default()
+ };
+ let r = c.check_with_oracle(&claim, &o).unwrap();
+ assert_eq!(r.verdict, Verdict::Undetermined, "an undecided oracle must not yield Consistent");
+ assert_eq!(r.residual_pairs.len(), 1);
+ }
+
+ #[test]
+ fn oracle_verdicts_are_cached() {
+ let c = fixture();
+ let o = oracle(vec![], vec![]);
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("CheeseyPizza"))],
+ ..Default::default()
+ };
+ c.check_with_oracle(&claim, &o).unwrap();
+ let after_first = *o.calls.borrow();
+ c.check_with_oracle(&claim, &o).unwrap();
+ assert_eq!(*o.calls.borrow(), after_first, "second check must hit the cache");
+ }
+
+ #[test]
+ fn tier1_rejection_spends_no_oracle_call() {
+ let c = fixture();
+ let o = oracle(vec![], vec![]);
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("MeatyPizza"))],
+ ..Default::default()
+ };
+ let r = c.check_with_oracle(&claim, &o).unwrap();
+ assert_eq!(r.verdict, Verdict::Rejected);
+ assert_eq!(*o.calls.borrow(), 0, "a sound tier-1 rejection must not consult tier 2");
+ }
+
+ #[test]
+ fn learned_incompatibility_is_settled_by_tier1_next_time() {
+ let c = fixture();
+ let o = oracle(vec![(iri("Margherita"), iri("CheeseyPizza"))], vec![]);
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("CheeseyPizza"))],
+ ..Default::default()
+ };
+ c.check_with_oracle(&claim, &o).unwrap();
+ // Tier 1 alone should now settle it, with no oracle involved.
+ assert_eq!(c.check(&claim).unwrap().verdict, Verdict::Rejected);
+ }
+
+ // ── batch / parallelism ─────────────────────────────────────────────
+
+ #[test]
+ fn batch_matches_sequential() {
+ let c = fixture();
+ let claims: Vec<Claim> = vec![
+ Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("MeatyPizza"))],
+ ..Default::default()
+ },
+ Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("CheeseyPizza"))],
+ ..Default::default()
+ },
+ Claim {
+ types: vec![("x".into(), iri("QuantumPineappleTopping"))],
+ ..Default::default()
+ },
+ ];
+ let batch = c.check_batch(&claims).unwrap();
+ for (claim, br) in claims.iter().zip(&batch) {
+ let sr = c.check(claim).unwrap();
+ assert_eq!(br.verdict, sr.verdict);
+ assert_eq!(br.violations.len(), sr.violations.len());
+ }
+ }
+
+ #[test]
+ fn batch_is_deterministic_under_parallelism() {
+ let c = fixture();
+ let claim = Claim {
+ types: vec![("x".into(), iri("Margherita")), ("x".into(), iri("MeatyPizza"))],
+ ..Default::default()
+ };
+ let claims: Vec<Claim> = (0..1000).map(|_| claim.clone()).collect();
+ let out = c.check_batch(&claims).unwrap();
+ assert_eq!(out.len(), 1000);
+ assert!(out.iter().all(|r| r.verdict == Verdict::Rejected));
+ }
+}
diff --git a/src/graph.rs b/src/graph.rs
index c1aa744c..bb415648 100644
--- a/src/graph.rs
+++ b/src/graph.rs
@@ -111,7 +111,7 @@ impl GraphStore {
pub fn load_file(&self, path: &str) -> anyhow::Result<usize> {
let content = std::fs::read_to_string(path)?;
- let format = Self::detect_format(path);
+ let format = Self::detect_format_sniffed(path, &content);
let store = self.store.lock().unwrap();
let reader = Cursor::new(content.as_bytes());
let parser = RdfParser::from_format(format).for_reader(reader);
@@ -142,7 +142,7 @@ impl GraphStore {
pub fn validate_file(path: &str) -> anyhow::Result<usize> {
let content = std::fs::read_to_string(path)?;
- let format = Self::detect_format(path);
+ let format = Self::detect_format_sniffed(path, &content);
let reader = Cursor::new(content.as_bytes());
let parser = RdfParser::from_format(format).for_reader(reader);
let mut count = 0;
@@ -473,6 +473,41 @@ impl GraphStore {
}
}
+ /// Format detection that consults the file body, not just the extension.
+ ///
+ /// `.owl` is ambiguous in the wild: the extension says "an OWL ontology"
+ /// and says nothing about the serialisation. Both RDF/XML and Turtle are
+ /// routinely published as `.owl`, so trusting the extension alone makes a
+ /// perfectly valid file fail to parse. Sniff the first non-blank,
+ /// non-comment line and let the content decide.
+ fn detect_format_sniffed(path: &str, content: &str) -> RdfFormat {
+ let ext_format = Self::detect_format(path);
+
+ let head = content
+ .lines()
+ .map(str::trim)
+ .find(|l| !l.is_empty() && !l.starts_with('#'))
+ .unwrap_or("");
+
+ // XML declaration or an opening tag means RDF/XML regardless of name.
+ if head.starts_with("<?xml") || head.starts_with("<rdf:") || head.starts_with("<RDF") {
+ return RdfFormat::RdfXml;
+ }
+
+ // Turtle/TriG directives. `<` alone is not a signal: it also opens an
+ // N-Triples subject IRI, so only treat explicit directives as proof.
+ let is_turtle_directive = head.starts_with("@prefix")
+ || head.starts_with("@base")
+ || head.to_uppercase().starts_with("PREFIX ")
+ || head.to_uppercase().starts_with("BASE ");
+
+ if is_turtle_directive && matches!(ext_format, RdfFormat::RdfXml) {
+ return RdfFormat::Turtle;
+ }
+
+ ext_format
+ }
+
fn parse_format(name: &str) -> anyhow::Result<RdfFormat> {
match name.to_lowercase().as_str() {
"turtle" | "ttl" => Ok(RdfFormat::Turtle),
diff --git a/src/lib.rs b/src/lib.rs
index b785a5f5..ddf2b61a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,6 +3,8 @@ pub mod align_fuzzy;
pub mod batch;
pub mod borderline_loop;
pub mod cache;
+/// Compiled claim verification (Tardygrada Layer 3 hot path).
+pub mod claimcheck;
pub mod civex;
#[cfg(feature = "causal-pywhy")]
pub mod civex_pywhy;
diff --git a/src/runtime.rs b/src/runtime.rs
index d876acca..28e99ea6 100644
--- a/src/runtime.rs
+++ b/src/runtime.rs
@@ -23,6 +23,11 @@ use crate::config::{
// ── Defaults match the previous hardcoded constants exactly ─────────────
const DEFAULT_TABLEAUX_MAX_DEPTH: usize = 100;
const DEFAULT_TABLEAUX_MAX_NODES: usize = 10_000;
+/// 10s per satisfiability test. Generous for well-behaved ontologies, and the
+/// difference between a reported Unknown and an unbounded hang for the rest.
+const DEFAULT_TABLEAUX_TEST_TIMEOUT_MS: usize = 10_000;
+/// 180s for a whole classification, matching the ORE competition timeout.
+const DEFAULT_CLASSIFY_TIMEOUT_MS: usize = 180_000;
// Original reason.rs used 50; section 3 of the audit recommends 64 as a
// slightly more generous, explicitly-documented value. We adopt 64 as the
// new default since neither value affects fixpoint correctness — they only
@@ -38,6 +43,9 @@ const DEFAULT_WEBHOOK_TIMEOUT: u64 = 10;
static TABLEAUX_MAX_DEPTH: AtomicUsize = AtomicUsize::new(DEFAULT_TABLEAUX_MAX_DEPTH);
static TABLEAUX_MAX_NODES: AtomicUsize = AtomicUsize::new(DEFAULT_TABLEAUX_MAX_NODES);
+static TABLEAUX_TEST_TIMEOUT_MS: AtomicUsize =
+ AtomicUsize::new(DEFAULT_TABLEAUX_TEST_TIMEOUT_MS);
+static CLASSIFY_TIMEOUT_MS: AtomicUsize = AtomicUsize::new(DEFAULT_CLASSIFY_TIMEOUT_MS);
static REASONER_MAX_ITER: AtomicUsize = AtomicUsize::new(DEFAULT_REASONER_MAX_ITER);
static CACHE_HASH_PREFIX: AtomicUsize = AtomicUsize::new(DEFAULT_CACHE_HASH_PREFIX);
static FB_SUPPRESS: AtomicI64 = AtomicI64::new(DEFAULT_FB_SUPPRESS);
@@ -111,6 +119,47 @@ fn apply_webhook(w: &WebhookConfig) {
pub fn tableaux_max_depth() -> usize { TABLEAUX_MAX_DEPTH.load(Ordering::Relaxed) }
pub fn tableaux_max_nodes() -> usize { TABLEAUX_MAX_NODES.load(Ordering::Relaxed) }
+
+/// Wall-clock cut-off for a single tableau satisfiability test, in
+/// milliseconds. `None` (value 0) means no time limit.
+///
+/// This exists because the node and depth budgets do not bound the number of
+/// BRANCHES explored. A tableau can stay small and shallow while backtracking
+/// through exponentially many disjunction and merge choices, which is exactly
+/// how nominal-bearing ontologies hang the reasoner. A clock is the only
+/// budget that catches that.
+pub fn tableaux_test_timeout_ms() -> Option<u64> {
+ match TABLEAUX_TEST_TIMEOUT_MS.load(Ordering::Relaxed) {
+ 0 => None,
+ ms => Some(ms as u64),
+ }
+}
+
+/// Override the per-test timeout. Used by the CLI `--reason-timeout-ms` flag
+/// and by tests.
+pub fn set_tableaux_test_timeout_ms(ms: usize) {
+ TABLEAUX_TEST_TIMEOUT_MS.store(ms, Ordering::Relaxed);
+}
+
+/// Wall-clock cut-off for an ENTIRE classification run, in milliseconds.
+/// `None` (value 0) means no limit.
+///
+/// Distinct from the per-test timeout, which bounds one satisfiability check.
+/// Classification performs one check per class plus one per ordered pair, so
+/// the per-test budget multiplied by the pair count is the real worst case and
+/// it is enormous. This is the budget that actually bounds the run.
+///
+/// Default matches the ORE competition convention of 180s.
+pub fn classify_timeout_ms() -> Option<u64> {
+ match CLASSIFY_TIMEOUT_MS.load(Ordering::Relaxed) {
+ 0 => None,
+ ms => Some(ms as u64),
+ }
+}
+
+pub fn set_classify_timeout_ms(ms: usize) {
+ CLASSIFY_TIMEOUT_MS.store(ms, Ordering::Relaxed);
+}
pub fn reasoner_max_iterations() -> usize { REASONER_MAX_ITER.load(Ordering::Relaxed) }
pub fn cache_hash_prefix_bytes() -> usize { CACHE_HASH_PREFIX.load(Ordering::Relaxed) }
pub fn feedback_suppress_threshold() -> i64 { FB_SUPPRESS.load(Ordering::Relaxed) }
diff --git a/src/tableaux.rs b/src/tableaux.rs
index 6d1140c2..c8d3c3d1 100644
--- a/src/tableaux.rs
+++ b/src/tableaux.rs
@@ -29,7 +29,7 @@
//! - **Explanation Agent**: Clash tracing and justification extraction
//! - **ABox Agent**: Individual consistency checking and type inference
-use std::collections::{HashMap, HashSet};
+use std::collections::{BTreeSet, HashMap, HashSet};
use std::sync::Arc;
use std::time::Instant;
@@ -804,12 +804,67 @@ impl ExplanationTrace {
// ── Tableau ─────────────────────────────────────────────────────────────
+/// Outcome of a satisfiability test.
+///
+/// A two-valued `bool` cannot express the difference between "I constructed a
+/// clash, this concept is impossible" and "I ran out of budget before I could
+/// decide". Conflating them is a soundness bug: the caller turns exhaustion
+/// into an asserted `owl:Nothing` subsumption. This enum makes the third
+/// outcome unavoidable at the type level.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Verdict {
+ /// A complete, clash-free model was constructed.
+ Satisfiable,
+ /// Every branch clashed. This is a proof.
+ Unsatisfiable,
+ /// A resource budget was hit first. Nothing is proven either way.
+ Unknown,
+}
+
+impl Verdict {
+ /// True only when the concept is PROVEN satisfiable. `Unknown` is not.
+ ///
+ /// Note that this is NOT the negation of [`Verdict::is_unsat`]: both return
+ /// false for `Unknown`. Callers must handle the third case explicitly,
+ /// which is the whole point of the type.
+ pub fn is_sat(self) -> bool {
+ matches!(self, Verdict::Satisfiable)
+ }
+
+ /// True only when unsatisfiability is PROVEN. `Unknown` is not.
+ pub fn is_unsat(self) -> bool {
+ matches!(self, Verdict::Unsatisfiable)
+ }
+
+ /// True when no decision was reached. Callers that treat this as either
+ /// answer are reintroducing the bug this enum exists to prevent.
+ pub fn is_unknown(self) -> bool {
+ matches!(self, Verdict::Unknown)
+ }
+}
+
+/// Why a tableau run stopped short of a decision.
+#[derive(Debug, Clone, Copy, Default)]
+struct Budget {
+ /// Wall-clock cut-off for a single satisfiability test.
+ deadline: Option<Instant>,
+ /// Set when any budget was hit during expansion.
+ exhausted: bool,
+}
+
+impl Budget {
+ fn expired(&self) -> bool {
+ self.deadline.is_some_and(|d| Instant::now() >= d)
+ }
+}
+
#[derive(Clone)]
struct Tableau {
nodes: HashMap<u32, TNode>,
next_id: u32,
tbox: Arc<ProcessedTBox>,
trace: ExplanationTrace,
+ budget: Budget,
}
impl Tableau {
@@ -819,26 +874,41 @@ impl Tableau {
next_id: 0,
tbox,
trace: ExplanationTrace::new(false),
+ budget: Budget::default(),
}
}
+ fn with_deadline(tbox: Arc<ProcessedTBox>, deadline: Option<Instant>) -> Self {
+ let mut t = Self::new(tbox);
+ t.budget.deadline = deadline;
+ t
+ }
+
fn new_with_tracing(tbox: Arc<ProcessedTBox>) -> Self {
Self {
nodes: HashMap::new(),
next_id: 0,
tbox,
trace: ExplanationTrace::new(true),
+ budget: Budget::default(),
}
}
- fn is_satisfiable(&mut self, concept: &Concept) -> bool {
+ fn decide(&mut self, concept: &Concept) -> Verdict {
let root = self.fresh_node(None, None);
self.add_label(root, concept.clone());
// Add GCIs to root
for gci in self.tbox.gcis.clone() {
self.add_label(root, gci);
}
- self.expand(0)
+ if self.expand(0) {
+ Verdict::Satisfiable
+ } else if self.budget.exhausted {
+ // We never completed the search, so we have proven nothing.
+ Verdict::Unknown
+ } else {
+ Verdict::Unsatisfiable
+ }
}
fn fresh_node(&mut self, parent: Option<u32>, parent_role: Option<u32>) -> u32 {
@@ -1015,12 +1085,25 @@ impl Tableau {
fn expand(&mut self, depth: usize) -> bool {
let max_depth = crate::runtime::tableaux_max_depth();
let max_nodes = crate::runtime::tableaux_max_nodes();
- if depth > max_depth || self.nodes.len() > max_nodes {
+ // Hitting a budget is NOT a clash. Record it so the caller can report
+ // Unknown instead of asserting unsatisfiability, then unwind. The
+ // `false` return here means only "this branch produced no model".
+ if depth > max_depth || self.nodes.len() > max_nodes || self.budget.expired() {
+ self.budget.exhausted = true;
return false;
}
// Apply deterministic rules until fixpoint
loop {
+ // The deadline must be checked HERE, not only on entry to expand().
+ // This fixpoint loop can run for a very long time on a single call
+ // (every iteration re-runs blocking over every node), so a check
+ // only at function entry lets one expansion overrun the budget
+ // without bound.
+ if self.budget.expired() {
+ self.budget.exhausted = true;
+ return false;
+ }
if self.any_clash() {
return false;
}
@@ -1186,6 +1269,9 @@ impl Tableau {
*self = branch;
return true;
}
+ // Carry the budget flag back: a branch that ran out
+ // of room does not license "all merges clash".
+ self.budget.exhausted |= branch.budget.exhausted;
}
}
return false; // All merges lead to clash
@@ -1240,6 +1326,8 @@ impl Tableau {
if branch.expand(depth + 1) {
return true;
}
+ // Same here: an exhausted disjunct is not a refuted one.
+ self.budget.exhausted |= branch.budget.exhausted;
}
return false; // All branches clash
}
@@ -1271,25 +1359,91 @@ impl Tableau {
fn update_blocking(&mut self) {
let node_ids: Vec<u32> = self.nodes.keys().copied().collect();
for &nid in &node_ids {
- if self.nodes[&nid].parent.is_none() {
- continue;
- }
- let node_labels = self.nodes[&nid].labels.clone();
- let mut ancestor = self.nodes[&nid].parent;
- let mut found = false;
- while let Some(anc_id) = ancestor {
- if let Some(anc_node) = self.nodes.get(&anc_id) {
- if node_labels.is_subset(&anc_node.labels) {
- found = true;
- break;
- }
- ancestor = anc_node.parent;
- } else {
- break;
+ let blocked = self.is_pairwise_blocked(nid);
+ self.nodes.get_mut(&nid).unwrap().blocked = blocked;
+ }
+ }
+
+ /// The set of roles labelling the edge from `from` to `to`.
+ fn roles_between(&self, from: u32, to: u32) -> BTreeSet<u32> {
+ match self.nodes.get(&from) {
+ Some(n) => n
+ .edges
+ .iter()
+ .filter(|(_, targets)| targets.contains(&to))
+ .map(|(r, _)| *r)
+ .collect(),
+ None => BTreeSet::new(),
+ }
+ }
+
+ /// Pairwise (double) ancestor blocking.
+ ///
+ /// Horrocks and Sattler, *A Tableau Decision Procedure for SHOIQ* (JAR 39,
+ /// 2007): once a logic has BOTH inverse roles and number restrictions,
+ /// single-node blocking is not sound. The reason is unravelling. To build a
+ /// model from a completion graph you replicate the fragment between the
+ /// blocked node and its blocker infinitely often, and that only yields a
+ /// model if the blocked node behaves like its blocker *in its context*.
+ /// With inverse roles a node constrains its predecessor, so the contexts
+ /// have to match too.
+ ///
+ /// The condition, for `s` a blockable successor of `s'` and `t` a blockable
+ /// successor of `t'`, is that `t` blocks `s` iff
+ ///
+ /// t ≺ s, L(s) = L(t), L(s') = L(t'), L(s,s') = L(t,t'), L(s',s) = L(t',t)
+ ///
+ /// Note `=`, not `⊆`. This replaces the previous implementation, which used
+ /// ancestor SUBSET blocking on the node label alone and ignored parents and
+ /// edge labels entirely. That was adequate for ALC but unsound for the
+ /// SHOIQ this reasoner advertises.
+ ///
+ /// This is deliberately the classical ancestor variant rather than HermiT's
+ /// "anywhere" blocking. Anywhere blocking yields smaller models but is a
+ /// further optimisation on top of a correct base; get the base right first.
+ fn is_pairwise_blocked(&self, s: u32) -> bool {
+ // Only blockable successors can be blocked. A node with no parent is
+ // a root and is never blocked.
+ let Some(s_parent) = self.nodes.get(&s).and_then(|n| n.parent) else {
+ return false;
+ };
+ let Some(s_node) = self.nodes.get(&s) else {
+ return false;
+ };
+ let Some(s_parent_node) = self.nodes.get(&s_parent) else {
+ return false;
+ };
+
+ let s_labels = &s_node.labels;
+ let s_parent_labels = &s_parent_node.labels;
+ let s_down = self.roles_between(s_parent, s);
+ let s_up = self.roles_between(s, s_parent);
+
+ // Walk strict ancestors, looking for a blocker.
+ let mut cur = s_node.parent;
+ while let Some(t) = cur {
+ let Some(t_node) = self.nodes.get(&t) else {
+ break;
+ };
+ if let Some(t_parent) = t_node.parent {
+ // Node ids are allocated monotonically, so `t < s` is the
+ // strict ordering the calculus requires. It also guarantees we
+ // never create a blocking cycle.
+ if t < s
+ && t_node.labels == *s_labels
+ && self
+ .nodes
+ .get(&t_parent)
+ .is_some_and(|tp| tp.labels == *s_parent_labels)
+ && self.roles_between(t_parent, t) == s_down
+ && self.roles_between(t, t_parent) == s_up
+ {
+ return true;
}
}
- self.nodes.get_mut(&nid).unwrap().blocked = found;
+ cur = t_node.parent;
}
+ false
}
}
@@ -1303,6 +1457,9 @@ pub struct DlReasoner {
nothing_id: u32,
individual_types: HashMap<u32, HashSet<u32>>,
role_assertions: Vec<(u32, u32, u32)>,
+ /// Wall-clock cut-off applied to each individual satisfiability test.
+ /// `None` means no time limit (the node/depth budgets still apply).
+ deadline: Option<Instant>,
}
impl DlReasoner {
@@ -1329,33 +1486,57 @@ impl DlReasoner {
nothing_id: result.nothing_id,
individual_types: result.individual_types,
role_assertions: result.role_assertions,
+ deadline: crate::runtime::tableaux_test_timeout_ms()
+ .map(|ms| Instant::now() + std::time::Duration::from_millis(ms)),
})
}
- /// Thread-safe satisfiability test — each call creates its own Tableau.
+ /// Three-valued satisfiability test — each call creates its own Tableau.
+ ///
+ /// Returns `Unknown` when a resource budget was hit before a decision was
+ /// reached. Callers MUST NOT read `Unknown` as either answer.
+ pub fn decide_satisfiable(&self, concept: &Concept) -> Verdict {
+ let mut tableau = Tableau::with_deadline(Arc::clone(&self.tbox), self.deadline);
+ tableau.decide(concept)
+ }
+
+ /// Thread-safe satisfiability test.
+ ///
+ /// Collapses `Unknown` to `true`, which is the SAFE direction: an
+ /// undecided class is left in the hierarchy rather than being declared
+ /// impossible. Prefer `decide_satisfiable` where the distinction matters.
pub fn is_satisfiable(&self, concept: &Concept) -> bool {
- let mut tableau = Tableau::new(Arc::clone(&self.tbox));
- tableau.is_satisfiable(concept)
+ !self.decide_satisfiable(concept).is_unsat()
}
/// Check if sub ⊑ sup (sub is subsumed by sup).
+ ///
+ /// A subsumption is asserted ONLY when the negated test concept is proven
+ /// unsatisfiable. `Unknown` yields `false`: we decline to claim a
+ /// subsumption we could not establish.
pub fn is_subsumed(&self, sub: &Concept, sup: &Concept) -> bool {
+ self.decide_subsumption(sub, sup).is_unsat()
+ }
+
+ /// Subsumption as a three-valued verdict. `Unsatisfiable` on the negated
+ /// test concept means the subsumption holds.
+ pub fn decide_subsumption(&self, sub: &Concept, sup: &Concept) -> Verdict {
let mut test = vec![sub.clone(), sup.negate()];
test.sort();
- let test_concept = Concept::And(test);
- !self.is_satisfiable(&test_concept)
+ self.decide_satisfiable(&Concept::And(test))
}
- /// Check TBox consistency.
+ /// Check TBox consistency. An undecided run reports consistent, which is
+ /// the safe direction: we do not condemn an ontology we failed to refute.
pub fn is_consistent(&self) -> bool {
- self.is_satisfiable(&Concept::Top)
+ !self.decide_satisfiable(&Concept::Top).is_unsat()
}
/// Explain why a class is unsatisfiable. Returns None if satisfiable.
pub fn explain_unsatisfiable(&self, class_id: u32) -> Option<Vec<String>> {
let concept = Concept::Atom(class_id);
let mut tableau = Tableau::new_with_tracing(Arc::clone(&self.tbox));
- if tableau.is_satisfiable(&concept) {
+ if !tableau.decide(&concept).is_unsat() {
return None;
}
Some(tableau.trace.steps)
@@ -1371,8 +1552,8 @@ impl DlReasoner {
test.sort();
let test_concept = Concept::And(test);
let mut tableau = Tableau::new_with_tracing(Arc::clone(&self.tbox));
- let sat = tableau.is_satisfiable(&test_concept);
- (!sat, tableau.trace.steps)
+ let verdict = tableau.decide(&test_concept);
+ (verdict.is_unsat(), tableau.trace.steps)
}
/// Compute told-subsumer transitive closure (for pruning).
@@ -1425,21 +1606,47 @@ impl DlReasoner {
.copied()
.collect();
+ // Global budget for the WHOLE classification.
+ //
+ // The per-test deadline bounds one satisfiability check. It does not
+ // bound classification, which runs one check per class plus one per
+ // ordered pair of classes. On the Pizza ontology that is ~100 classes
+ // and ~10,000 pairs; at the 10s per-test budget the worst case is over
+ // a day. A global deadline is what actually stops that.
+ let global_deadline = crate::runtime::classify_timeout_ms()
+ .map(|ms| Instant::now() + std::time::Duration::from_millis(ms));
+ let out_of_time = || global_deadline.is_some_and(|d| Instant::now() >= d);
+
// ── Satisfiability Agent ─────────────────────────────────────
let sat_start = Instant::now();
- let sat_results: Vec<(u32, bool)> = classes
+ let sat_results: Vec<(u32, Verdict)> = classes
.par_iter()
- .map(|&cls| (cls, self.is_satisfiable(&Concept::Atom(cls))))
+ .map(|&cls| {
+ if out_of_time() {
+ // Budget gone: report Unknown rather than guessing.
+ return (cls, Verdict::Unknown);
+ }
+ (cls, self.decide_satisfiable(&Concept::Atom(cls)))
+ })
.collect();
+ // Only PROVEN unsatisfiable classes go in the unsatisfiable list.
+ // Classes we ran out of budget on are undetermined, and are kept in
+ // the satisfiable set for downstream subsumption testing so they stay
+ // in the hierarchy rather than silently vanishing.
let satisfiable: Vec<u32> = sat_results
.iter()
- .filter(|(_, s)| *s)
+ .filter(|(_, v)| !v.is_unsat())
.map(|(c, _)| *c)
.collect();
let unsatisfiable: Vec<u32> = sat_results
.iter()
- .filter(|(_, s)| !*s)
+ .filter(|(_, v)| v.is_unsat())
+ .map(|(c, _)| *c)
+ .collect();
+ let undetermined: Vec<u32> = sat_results
+ .iter()
+ .filter(|(_, v)| matches!(v, Verdict::Unknown))
.map(|(c, _)| *c)
.collect();
let sat_time = sat_start.elapsed();
@@ -1458,11 +1665,22 @@ impl DlReasoner {
}
}
+ let subsumption_cut_short = std::sync::atomic::AtomicBool::new(false);
let inferred: Vec<(u32, u32)> = pairs
.par_iter()
- .filter(|(sub, sup)| self.is_subsumed(&Concept::Atom(*sub), &Concept::Atom(*sup)))
+ .filter(|(sub, sup)| {
+ if out_of_time() {
+ // Untested pairs are NOT non-subsumptions. Flag the run as
+ // incomplete so no caller reads absence as refutation.
+ subsumption_cut_short.store(true, std::sync::atomic::Ordering::Relaxed);
+ return false;
+ }
+ self.is_subsumed(&Concept::Atom(*sub), &Concept::Atom(*sup))
+ })
.cloned()
.collect();
+ let subsumption_cut_short =
+ subsumption_cut_short.load(std::sync::atomic::Ordering::Relaxed);
let sub_time = sub_start.elapsed();
// Build hierarchy (told + inferred)
@@ -1496,6 +1714,8 @@ impl DlReasoner {
AgentClassificationResult {
hierarchy,
unsatisfiable,
+ undetermined,
+ subsumption_cut_short,
equivalences,
inferred_subsumptions: inferred.len(),
agents: AgentMetrics {
@@ -1520,12 +1740,17 @@ impl DlReasoner {
if self.individual_types.is_empty() {
return ABoxResult {
consistent: true,
+ undecided: false,
individuals_checked: 0,
inferred_types: HashMap::new(),
};
}
- let mut tableau = Tableau::new(Arc::clone(&self.tbox));
+ // The ABox check builds ONE tableau containing every named individual,
+ // so it is the largest single expansion the reasoner ever performs. It
+ // must carry the same deadline as every other test, otherwise it is an
+ // unbounded hole in the budget.
+ let mut tableau = Tableau::with_deadline(Arc::clone(&self.tbox), self.deadline);
let mut ind_to_node: HashMap<u32, u32> = HashMap::new();
// Create nodes for each individual
@@ -1555,11 +1780,18 @@ impl DlReasoner {
}
}
- let consistent = tableau.expand(0);
+ // Same three-valued discipline as everywhere else: exhausting the
+ // budget is not a proof of inconsistency. Declaring an ABox
+ // inconsistent is a strong, user-visible claim and must never be the
+ // by-product of giving up. Default to consistent when undecided.
+ let expanded = tableau.expand(0);
+ let abox_undecided = !expanded && tableau.budget.exhausted;
+ let consistent = expanded || abox_undecided;
- // Infer additional types for each individual
+ // Infer additional types for each individual. Only when the expansion
+ // genuinely completed: a partial tableau's labels are not conclusions.
let mut inferred: HashMap<u32, HashSet<u32>> = HashMap::new();
- if consistent {
+ if expanded {
for (&ind, &node_id) in &ind_to_node {
if let Some(node) = tableau.nodes.get(&node_id) {
for label in &node.labels {
@@ -1574,6 +1806,7 @@ impl DlReasoner {
ABoxResult {
consistent,
+ undecided: abox_undecided,
individuals_checked: self.individual_types.len(),
inferred_types: inferred,
}
@@ -1605,6 +1838,18 @@ impl DlReasoner {
.map(|&id| reasoner.interner.resolve(id))
.collect();
+ // Completeness signal. A caller must be able to tell a proof from a
+ // run that gave up; without this the two are indistinguishable in the
+ // output and every consumer silently over-trusts the result.
+ let undetermined_names: Vec<&str> = result
+ .undetermined
+ .iter()
+ .map(|&id| reasoner.interner.resolve(id))
+ .collect();
+ let complete = undetermined_names.is_empty()
+ && !result.subsumption_cut_short
+ && !abox_result.undecided;
+
let mut hierarchy_json: Vec<serde_json::Value> = Vec::new();
for (&cls, supers) in &result.hierarchy {
let cls_name = reasoner.interner.resolve(cls);
@@ -1644,6 +1889,7 @@ impl DlReasoner {
}
serde_json::json!({
"consistent": abox_result.consistent,
+ "undecided": abox_result.undecided,
"individuals_checked": abox_result.individuals_checked,
"inferred": ind_json,
})
@@ -1680,6 +1926,9 @@ impl DlReasoner {
"consistent": consistent,
"named_classes": reasoner.named_classes.len(),
"unsatisfiable_classes": unsat_names,
+ "complete": complete,
+ "undetermined_classes": undetermined_names,
+ "subsumption_sweep_cut_short": result.subsumption_cut_short,
"inferred_subsumptions": result.inferred_subsumptions,
"equivalences": equiv_json,
"classification": hierarchy_json,
@@ -1780,7 +2029,16 @@ impl DlReasoner {
pub struct AgentClassificationResult {
pub hierarchy: HashMap<u32, HashSet<u32>>,
+ /// Classes PROVEN to have no instances.
pub unsatisfiable: Vec<u32>,
+ /// Classes on which a resource budget was hit before a decision. Neither
+ /// proven satisfiable nor proven unsatisfiable. Their presence means the
+ /// classification as a whole is incomplete.
+ pub undetermined: Vec<u32>,
+ /// True when the subsumption sweep ran out of global budget, so some pairs
+ /// were never tested. Absence of a subsumption in the output is then not
+ /// evidence that it does not hold.
+ pub subsumption_cut_short: bool,
pub equivalences: Vec<(u32, u32)>,
pub inferred_subsumptions: usize,
pub agents: AgentMetrics,
@@ -1800,7 +2058,11 @@ pub struct AgentTaskMetrics {
}
pub struct ABoxResult {
+ /// Reported consistent unless inconsistency was PROVEN. See `undecided`.
pub consistent: bool,
+ /// True when the ABox expansion ran out of budget. `consistent` is then a
+ /// default, not a finding.
+ pub undecided: bool,
pub individuals_checked: usize,
pub inferred_types: HashMap<u32, HashSet<u32>>,
}
diff --git a/tests/claimcheck_pizza_bench.rs b/tests/claimcheck_pizza_bench.rs
new file mode 100644
index 00000000..c9ee39b2
--- /dev/null
+++ b/tests/claimcheck_pizza_bench.rs
@@ -0,0 +1,148 @@
+//! Latency of the compiled claim check on the REAL Pizza ontology.
+//!
+//! Loads the output of `CompileOntology.java` (`/tmp/pizza_compiled.json`) so
+//! this measures the real inferred hierarchy and real disjointness axioms, not
+//! a hand-written fixture. Skips cleanly when that file is absent, so it does
+//! not break a checkout that has not run the Java compile step.
+//!
+//! Regenerate with:
+//! java -cp ".:lib/*" CompileOntology /tmp/pizza_real.owl /tmp/pizza_compiled.json
+
+use std::path::Path;
+
+use open_ontologies::claimcheck::{Claim, CompiledOntology, Verdict};
+use serde_json::Value;
+
+const COMPILED: &str = "/tmp/pizza_compiled.json";
+const PIZZA: &str = "http://www.co-ode.org/ontologies/pizza/pizza.owl#";
+
+fn iri(l: &str) -> String {
+ format!("{PIZZA}{l}")
+}
+
+fn load() -> Option<CompiledOntology> {
+ if !Path::new(COMPILED).exists() {
+ eprintln!("SKIP: {COMPILED} not present; run CompileOntology first");
+ return None;
+ }
+ let v: Value = serde_json::from_str(&std::fs::read_to_string(COMPILED).ok()?).ok()?;
+ let c = CompiledOntology::new().ok()?;
+
+ let pairs = |key: &str| -> Vec<(String, String)> {
+ v[key]
+ .as_array()
+ .map(|a| {
+ a.iter()
+ .filter_map(|p| {
+ Some((
+ p.get(0)?.as_str()?.to_string(),
+ p.get(1)?.as_str()?.to_string(),
+ ))
+ })
+ .collect()
+ })
+ .unwrap_or_default()
+ };
+
+ let subs = pairs("subsumptions");
+ let disj = pairs("disjoint");
+ // Everything mentioned anywhere counts as declared, so the vocabulary
+ // check does not fire spuriously in this benchmark.
+ let mut classes: Vec<String> = subs
+ .iter()
+ .flat_map(|(a, b)| [a.clone(), b.clone()])
+ .chain(disj.iter().flat_map(|(a, b)| [a.clone(), b.clone()]))
+ .collect();
+ classes.sort();
+ classes.dedup();
+
+ c.load_subsumptions(&subs).ok()?;
+ c.load_disjoint(&disj).ok()?;
+ c.load_declared_classes(&classes).ok()?;
+ eprintln!(
+ "loaded {} subsumptions, {} disjoint axioms, {} classes",
+ subs.len(),
+ disj.len(),
+ classes.len()
+ );
+ Some(c)
+}
+
+#[test]
+fn real_pizza_verdicts_and_latency() {
+ let Some(c) = load() else { return };
+
+ // Ground truth established earlier against a brute-force HermiT matrix.
+ let cases: &[(&str, &str, bool)] = &[
+ ("Margherita", "MeatyPizza", true),
+ ("American", "AmericanHot", true),
+ ("Margherita", "CheeseyPizza", false),
+ ("American", "MeatyPizza", false),
+ ];
+
+ for (a, b, expect_reject) in cases {
+ let claim = Claim {
+ types: vec![("x".into(), iri(a)), ("x".into(), iri(b))],
+ ..Default::default()
+ };
+ let r = c.check(&claim).unwrap();
+ let rejected = r.verdict == Verdict::Rejected;
+ assert_eq!(
+ rejected, *expect_reject,
+ "{a} + {b}: got {:?}, expected reject={expect_reject}",
+ r.verdict
+ );
+ }
+
+ // Latency over a mixed stream.
+ let claims: Vec<Claim> = cases
+ .iter()
+ .map(|(a, b, _)| Claim {
+ types: vec![("x".into(), iri(a)), ("x".into(), iri(b))],
+ ..Default::default()
+ })
+ .collect();
+
+ let mut lat: Vec<u128> = Vec::with_capacity(4000);
+ for i in 0..4000 {
+ let t = std::time::Instant::now();
+ let _ = c.check(&claims[i % claims.len()]).unwrap();
+ lat.push(t.elapsed().as_nanos());
+ }
+ lat.sort_unstable();
+ let us = |n: u128| n as f64 / 1000.0;
+ eprintln!(
+ "per-claim: median {:.1} us | p95 {:.1} us | p99 {:.1} us | max {:.1} us",
+ us(lat[lat.len() / 2]),
+ us(lat[lat.len() * 95 / 100]),
+ us(lat[lat.len() * 99 / 100]),
+ us(lat[lat.len() - 1])
+ );
+
+ // The 500 ms pipeline budget is 500,000 us. Anything near it is a defect.
+ assert!(
+ us(lat[lat.len() * 95 / 100]) < 1000.0,
+ "p95 exceeded 1 ms, which would be a regression against the DuckDB baseline"
+ );
+
+ // Throughput: sequential loop vs rayon batch over the same mixed stream.
+ let big: Vec<Claim> = (0..200_000).map(|i| claims[i % claims.len()].clone()).collect();
+
+ let t = std::time::Instant::now();
+ for cl in &big {
+ let _ = c.check(cl).unwrap();
+ }
+ let seq = t.elapsed();
+
+ let t = std::time::Instant::now();
+ let out = c.check_batch(&big).unwrap();
+ let par = t.elapsed();
+ assert_eq!(out.len(), big.len());
+
+ let seq_rate = big.len() as f64 / seq.as_secs_f64();
+ let par_rate = big.len() as f64 / par.as_secs_f64();
+ eprintln!(
+ "throughput: sequential {:.0} claims/s | batch(rayon) {:.0} claims/s | scaling {:.1}x",
+ seq_rate, par_rate, par_rate / seq_rate
+ );
+}
diff --git a/tests/owl2_conformance_test.rs b/tests/owl2_conformance_test.rs
index 6d53610c..00d2fef4 100644
--- a/tests/owl2_conformance_test.rs
+++ b/tests/owl2_conformance_test.rs
@@ -1,5 +1,20 @@
//! OWL 2 Conformance Validation Oracle
//!
+//! # Timeouts are disabled here, deliberately
+//!
+//! The reasoner carries a wall-clock budget per satisfiability test (see
+//! `runtime::tableaux_test_timeout_ms`). That is correct in production: it is
+//! what turns an unbounded hang into an honest `Undetermined`. It is wrong in
+//! a conformance suite, because under heavy parallel load a test that normally
+//! runs in microseconds can hit the budget and return `Undetermined`, failing
+//! an assertion that expects a subsumption. That is a flaky test measuring the
+//! clock rather than the calculus, and it was observed once during a full
+//! `cargo test` run on 2026-07-27.
+//!
+//! `disable_reasoner_timeouts()` pins the budgets off so these tests measure
+//! what they claim to measure. Timeout BEHAVIOUR is covered separately in
+//! `tests/reasoner_soundness_test.rs`.
+//!
//! Test cases derived from the W3C OWL 2 Test Cases specification and
//! established DL reasoning benchmarks. Each test has a documented expected
//! result that matches the output of HermiT/Pellet/FaCT++ reference reasoners.
@@ -16,12 +31,22 @@ use open_ontologies::graph::GraphStore;
use open_ontologies::reason::Reasoner;
use std::sync::Arc;
+/// Pin the reasoner's wall-clock budgets off for conformance testing.
+/// Idempotent and safe to call from every test; the underlying store is a
+/// process-global atomic.
+fn disable_reasoner_timeouts() {
+ open_ontologies::runtime::set_tableaux_test_timeout_ms(0);
+ open_ontologies::runtime::set_classify_timeout_ms(0);
+}
+
+
// ── W3C-style Consistency Tests ─────────────────────────────────────────
// Reference: OWL 2 Structural Specification §11 (consistency)
// Oracle: HermiT 1.4.3
#[test]
fn w3c_consistent_empty_ontology() {
+ disable_reasoner_timeouts();
// An empty ontology is trivially consistent.
// HermiT: consistent ✓
let store = Arc::new(GraphStore::new());
@@ -32,6 +57,7 @@ fn w3c_consistent_empty_ontology() {
#[test]
fn w3c_consistent_simple_hierarchy() {
+ disable_reasoner_timeouts();
// A ⊑ B ⊑ C — simple subsumption chain is consistent.
// HermiT: consistent ✓, A ⊑ C inferred
let store = Arc::new(GraphStore::new());
@@ -60,6 +86,7 @@ fn w3c_consistent_simple_hierarchy() {
#[test]
fn w3c_inconsistent_subclass_disjoint() {
+ disable_reasoner_timeouts();
// A ⊑ B, A disjointWith B → A unsatisfiable
// HermiT: A unsatisfiable ✓
let store = Arc::new(GraphStore::new());
@@ -92,6 +119,7 @@ fn w3c_inconsistent_subclass_disjoint() {
#[test]
fn w3c_entailment_equivalent_classes() {
+ disable_reasoner_timeouts();
// A ≡ B → A ⊑ B and B ⊑ A
// HermiT: equivalence detected ✓
let store = Arc::new(GraphStore::new());
@@ -116,6 +144,7 @@ fn w3c_entailment_equivalent_classes() {
#[test]
fn w3c_entailment_intersection_subsumption() {
+ disable_reasoner_timeouts();
// C ≡ A ⊓ B → C ⊑ A and C ⊑ B
// HermiT: C subClassOf A ✓, C subClassOf B ✓
let store = Arc::new(GraphStore::new());
@@ -161,6 +190,7 @@ fn w3c_entailment_intersection_subsumption() {
#[test]
fn w3c_entailment_complement_unsatisfiable() {
+ disable_reasoner_timeouts();
// A ⊑ B, A ⊑ ¬B → A unsatisfiable
// HermiT: A unsatisfiable ✓
let store = Arc::new(GraphStore::new());
@@ -195,6 +225,7 @@ fn w3c_entailment_complement_unsatisfiable() {
#[test]
fn w3c_exists_forall_direct_clash() {
+ disable_reasoner_timeouts();
// A ⊑ ∃R.C, A ⊑ ∀R.¬C → A unsatisfiable
// Every R-successor must have C (from ∃) and ¬C (from ∀). Clash.
// HermiT: A unsatisfiable ✓
@@ -236,6 +267,7 @@ fn w3c_exists_forall_direct_clash() {
#[test]
fn w3c_exists_forall_transitive_clash() {
+ disable_reasoner_timeouts();
// Pizza pattern: Pizza ⊑ ∀hasTopping.Veg, NonVegPizza ⊑ Pizza ⊓ ∃hasTopping.Meat,
// Veg disjoint Meat → NonVegPizza unsatisfiable
// HermiT: NonVegPizza unsatisfiable ✓
@@ -284,6 +316,7 @@ fn w3c_exists_forall_transitive_clash() {
#[test]
fn w3c_min_card_satisfiable() {
+ disable_reasoner_timeouts();
// A ⊑ ≥2 R.B — satisfiable (just needs 2 R-successors with B)
// HermiT: consistent ✓
let store = Arc::new(GraphStore::new());
@@ -319,6 +352,7 @@ fn w3c_min_card_satisfiable() {
#[test]
fn w3c_max_min_card_clash() {
+ disable_reasoner_timeouts();
// A ⊑ ≥3 R.B ⊓ ≤1 R.B → unsatisfiable (needs ≥3 but max 1)
// HermiT: A unsatisfiable ✓
let store = Arc::new(GraphStore::new());
@@ -360,6 +394,7 @@ fn w3c_max_min_card_clash() {
#[test]
fn w3c_functional_property_max_one() {
+ disable_reasoner_timeouts();
// R is Functional → ≤1 R.⊤ universally.
// A ⊑ ≥2 R.⊤ should be unsatisfiable.
// HermiT: A unsatisfiable ✓
@@ -397,6 +432,7 @@ fn w3c_functional_property_max_one() {
#[test]
fn w3c_inverse_role_consistency() {
+ disable_reasoner_timeouts();
// R inverseOf S — basic inverse declaration is consistent.
// HermiT: consistent ✓
let store = Arc::new(GraphStore::new());
@@ -421,6 +457,7 @@ fn w3c_inverse_role_consistency() {
#[test]
fn w3c_symmetric_role_consistency() {
+ disable_reasoner_timeouts();
// R is SymmetricProperty — consistent ontology with symmetric role.
// HermiT: consistent ✓
let store = Arc::new(GraphStore::new());
@@ -452,6 +489,7 @@ fn w3c_symmetric_role_consistency() {
#[test]
fn w3c_union_satisfiable() {
+ disable_reasoner_timeouts();
// A ≡ B ⊔ C — satisfiable, A is the union.
// HermiT: consistent ✓, B ⊑ A, C ⊑ A
let store = Arc::new(GraphStore::new());
@@ -480,6 +518,7 @@ fn w3c_union_satisfiable() {
#[test]
fn w3c_disjoint_union_coverage() {
+ disable_reasoner_timeouts();
// A ⊑ B ⊔ C, A disjoint B → A ⊑ C (via disjunction + elimination)
// This tests that the ⊔-rule with backtracking correctly determines:
// if A can't be B (disjoint), then A must be C.
@@ -530,6 +569,7 @@ fn w3c_disjoint_union_coverage() {
#[test]
fn w3c_transitive_forall_propagation() {
+ disable_reasoner_timeouts();
// R is transitive, A ⊑ ∀R.B.
// If x:A and x R y and y R z, then z should be B.
// The ∀-rule with transitive roles should propagate ∀R.B through chains.
@@ -573,6 +613,7 @@ fn w3c_transitive_forall_propagation() {
#[test]
fn w3c_pizza_vegetarian_classification() {
+ disable_reasoner_timeouts();
// MeatTopping and VegTopping are disjoint.
// VegetarianPizza ≡ Pizza ⊓ ∀hasTopping.VegTopping
// Margherita ⊑ Pizza ⊓ ∃hasTopping.Mozzarella ⊓ ∃hasTopping.Tomato
@@ -660,6 +701,7 @@ fn w3c_pizza_vegetarian_classification() {
#[test]
fn w3c_pizza_non_veg_unsatisfiable() {
+ disable_reasoner_timeouts();
// Same as exists_forall_clash but more structured.
// MeatPizza ⊑ Pizza ⊓ ∃hasTopping.Meat
// Pizza ⊑ ∀hasTopping.Veg
@@ -707,6 +749,7 @@ fn w3c_pizza_non_veg_unsatisfiable() {
#[test]
fn w3c_abox_consistent_individual() {
+ disable_reasoner_timeouts();
// john:Person, mary:Person — consistent ABox
// HermiT: consistent ✓
let store = Arc::new(GraphStore::new());
@@ -730,6 +773,7 @@ fn w3c_abox_consistent_individual() {
#[test]
fn w3c_abox_inconsistent_disjoint_types() {
+ disable_reasoner_timeouts();
// john: Male AND Female, Male disjoint Female → inconsistent ABox
// HermiT: inconsistent ✓
let store = Arc::new(GraphStore::new());
@@ -762,6 +806,7 @@ fn w3c_abox_inconsistent_disjoint_types() {
#[test]
fn w3c_oracle_agent_metrics() {
+ disable_reasoner_timeouts();
// Verify that the reasoner reports agent metrics in the output.
let store = Arc::new(GraphStore::new());
store
diff --git a/tests/reasoner_soundness_test.rs b/tests/reasoner_soundness_test.rs
new file mode 100644
index 00000000..7d8a961c
--- /dev/null
+++ b/tests/reasoner_soundness_test.rs
@@ -0,0 +1,192 @@
+//! Soundness regressions for the OWL-DL tableaux reasoner.
+//!
+//! These tests use ontologies whose satisfiability is a mathematical certainty,
+//! so they need no reference reasoner to establish ground truth:
+//!
+//! An ontology containing no negation, no disjointness, no owl:Nothing and no
+//! max-cardinality restriction is ALWAYS satisfiable, and every class in it is
+//! satisfiable. Witness model: a single element x, every role interpreted as
+//! {(x,x)}, and x a member of every class. Every GCI C ⊑ D holds because x is
+//! in both; every ∃R.C holds because R(x,x) and x ∈ C.
+//!
+//! So if the reasoner reports "unsatisfiable" for any class in such an ontology,
+//! it is wrong, full stop.
+
+use std::sync::Arc;
+
+use open_ontologies::graph::GraphStore;
+use open_ontologies::tableaux::DlReasoner;
+
+/// Build a Turtle ontology that is a chain of `n` existential restrictions:
+/// C0 ⊑ ∃R.C1, C1 ⊑ ∃R.C2, ... No negation anywhere.
+fn existential_chain(n: usize) -> String {
+ let mut s = String::from(
+ "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n\
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\
+ @prefix ex: <http://example.org/> .\n\
+ ex:R a owl:ObjectProperty .\n",
+ );
+ for i in 0..=n {
+ s.push_str(&format!("ex:C{i} a owl:Class .\n"));
+ }
+ for i in 0..n {
+ s.push_str(&format!(
+ "ex:C{i} rdfs:subClassOf [ a owl:Restriction ; owl:onProperty ex:R ; \
+ owl:someValuesFrom ex:C{next} ] .\n",
+ next = i + 1
+ ));
+ }
+ s
+}
+
+fn classify(ttl: &str) -> serde_json::Value {
+ let graph = Arc::new(GraphStore::new());
+ graph.load_turtle(ttl, None).expect("ontology must parse");
+ let out = DlReasoner::run(&graph, false).expect("reasoner must return a result");
+ serde_json::from_str(&out).expect("reasoner output must be JSON")
+}
+
+/// A negation-free ontology is satisfiable by construction. Every class in it
+/// must be reported satisfiable.
+#[test]
+fn negation_free_chain_has_no_unsatisfiable_classes() {
+ let result = classify(&existential_chain(40));
+
+ let unsat = result
+ .get("unsatisfiable_classes")
+ .and_then(|v| v.as_array())
+ .cloned()
+ .unwrap_or_default();
+
+ assert!(
+ unsat.is_empty(),
+ "negation-free ontology cannot have unsatisfiable classes, but the \
+ reasoner reported {unsat:?}. Full result: {result}"
+ );
+
+ assert_eq!(
+ result.get("consistent").and_then(|v| v.as_bool()),
+ Some(true),
+ "negation-free ontology must be consistent. Full result: {result}"
+ );
+}
+
+/// The important one. Resource exhaustion is NOT evidence of unsatisfiability.
+///
+/// `Tableau::expand` returns `bool`, where `false` means "clash found, this
+/// branch is unsatisfiable". It also returns `false` when the node budget or
+/// branch-depth budget is exhausted. Those two situations are not the same
+/// thing, and conflating them makes the reasoner assert subsumptions that do
+/// not hold.
+///
+/// This test drives a chain long enough to stress the expansion and asserts the
+/// reasoner never converts "I ran out of budget" into "this class is impossible".
+#[test]
+fn resource_exhaustion_is_not_reported_as_unsatisfiability() {
+ // Long enough to be expensive, short enough to keep the test quick.
+ let result = classify(&existential_chain(200));
+
+ let unsat = result
+ .get("unsatisfiable_classes")
+ .and_then(|v| v.as_array())
+ .cloned()
+ .unwrap_or_default();
+
+ assert!(
+ unsat.is_empty(),
+ "reasoner reported unsatisfiable classes {unsat:?} in an ontology with \
+ no negation, no disjointness and no max-cardinality. Every class here \
+ is satisfiable in a one-element model. This is a soundness failure, \
+ most likely resource exhaustion in Tableau::expand being returned as a \
+ clash. Full result: {result}"
+ );
+}
+
+/// Build a branching ontology: C0 ⊑ ≥3 R.C1, C1 ⊑ ≥3 R.C2, ...
+///
+/// Expanding C0 forces 3^depth blockable successors, blowing past the 10,000
+/// node budget with only a handful of classes. Still negation-free, so still
+/// satisfiable by construction: take a 3-element domain, interpret R as the
+/// complete relation on it, and put all three elements in every class. Every
+/// ≥3 R.C is then satisfied and no axiom can be violated because none is
+/// negative.
+fn branching_chain(depth: usize) -> String {
+ let mut s = String::from(
+ "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n\
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\
+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\
+ @prefix ex: <http://example.org/> .\n\
+ ex:R a owl:ObjectProperty .\n",
+ );
+ for i in 0..=depth {
+ s.push_str(&format!("ex:C{i} a owl:Class .\n"));
+ }
+ for i in 0..depth {
+ s.push_str(&format!(
+ "ex:C{i} rdfs:subClassOf [ a owl:Restriction ; owl:onProperty ex:R ; \
+ owl:minQualifiedCardinality \"3\"^^xsd:nonNegativeInteger ; \
+ owl:onClass ex:C{next} ] .\n",
+ next = i + 1
+ ));
+ }
+ s
+}
+
+/// THE WITNESS. Exceeds the tableau node budget on a provably satisfiable
+/// ontology.
+///
+/// `Tableau::expand` opens with:
+///
+/// ```ignore
+/// if depth > max_depth || self.nodes.len() > max_nodes {
+/// return false;
+/// }
+/// ```
+///
+/// `false` is the same value the function returns for a genuine clash, so the
+/// caller reads "I exhausted my budget" as "this concept is unsatisfiable".
+/// In classification that becomes a spurious `owl:Nothing` subsumption: the
+/// reasoner asserts, with no hedging, that a perfectly consistent class cannot
+/// have any instances.
+#[test]
+fn node_budget_exhaustion_does_not_fabricate_unsatisfiability() {
+ // 3^12 successors dwarfs the 10,000-node default budget.
+ let result = classify(&branching_chain(12));
+
+ let unsat = result
+ .get("unsatisfiable_classes")
+ .and_then(|v| v.as_array())
+ .cloned()
+ .unwrap_or_default();
+
+ assert!(
+ unsat.is_empty(),
+ "SOUNDNESS FAILURE: reasoner reported {unsat:?} as unsatisfiable. This \
+ ontology has no negation, no disjointness, no max-cardinality and no \
+ owl:Nothing, so it is satisfiable in a 3-element model where R is the \
+ complete relation and every class contains every element. The reasoner \
+ has converted resource exhaustion into a claim of impossibility. \
+ Full result: {result}"
+ );
+}
+
+/// Whatever the reasoner decides, it must not silently claim a complete answer
+/// when it gave up. If any budget was hit, that has to be visible in the output.
+#[test]
+fn incomplete_runs_are_declared_in_the_output() {
+ let result = classify(&existential_chain(200));
+
+ // The reasoner must expose SOME field telling the caller whether the run
+ // was complete. Absence of such a field means a caller cannot distinguish
+ // "proved" from "gave up", which is the defect this test guards.
+ let has_completeness_signal = result.get("complete").is_some()
+ || result.get("incomplete").is_some()
+ || result.get("exhausted").is_some()
+ || result.get("limits_hit").is_some();
+
+ assert!(
+ has_completeness_signal,
+ "reasoner output carries no completeness signal, so a caller cannot \
+ tell a proof from a timeout. Full result: {result}"
+ );
+}