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.

A named class carrying owl:unionOf was not defined by it (#201)

Fabio Rovaia97144f4f348
diff --git a/src/tableaux.rs b/src/tableaux.rs
index a2e5767a..dccac846 100644
--- a/src/tableaux.rs
+++ b/src/tableaux.rs
@@ -634,6 +634,68 @@ impl OwlParser {
axioms.push((b_nnf, a_nnf));
}
+ // A NAMED class that carries owl:unionOf, owl:intersectionOf or
+ // owl:complementOf is DEFINED by that expression, and the definition
+ // used to be dropped on the floor.
+ //
+ // `parse_class_expr` reaches `try_parse_complex` only for blank nodes,
+ // so the three set constructors were read out of anonymous class
+ // expressions and nowhere else. A named IRI carrying one came back as an
+ // opaque atom, the definition vanished, and nothing recorded the loss:
+ // `unmodelled_constructs` returned empty and the table at the top of
+ // this file claimed `unionOf ✅`. That is the UNSOUND direction. An
+ // ontology inconsistent through such a definition was reported
+ // consistent, and a class unsatisfiable through one was reported
+ // satisfiable. Missing an entailment is incompleteness and this codebase
+ // says where it is incomplete; calling a contradiction fine is telling
+ // the reader something false.
+ //
+ // The OWL 2 Mapping to RDF Graphs settles the reading. The pattern
+ // `*:x rdf:type owl:Class . *:x owl:unionOf T(SEQ CE1 ... CEn)` with
+ // `*:x` an IRI maps to `EquivalentClasses(*:x ObjectUnionOf(...))`, so
+ // both directions go in, exactly as the `owl:equivalentClass` pass above
+ // does. A one-way `subClassOf` would lose the half that puts each
+ // disjunct inside the union.
+ //
+ // The atom is kept as the head rather than being replaced by its
+ // expansion, because the satisfiability sweep and the certificate layer
+ // both address classes by name and a class rewritten into a disjunction
+ // is no longer a name they can ask about.
+ let mut defined_named: Vec<String> = self
+ .index
+ .by_subject
+ .iter()
+ .filter(|(s, pairs)| {
+ s.starts_with('<')
+ && s.as_str() != OWL_THING
+ && s.as_str() != OWL_NOTHING
+ && pairs.iter().any(|(p, _)| {
+ p == OWL_UNION || p == OWL_INTERSECTION || p == OWL_COMPLEMENT
+ })
+ })
+ .map(|(s, _)| s.clone())
+ .collect();
+ // `by_subject` is a HashMap, so its iteration order is not the file's.
+ // Sorted for the same reason the node traversals are: the axiom list
+ // reaches the tableau in this order and two runs over one ontology must
+ // not differ.
+ defined_named.sort();
+ for subject in defined_named {
+ let Some(raw) = self.try_parse_complex(&subject) else {
+ continue;
+ };
+ let body = raw.to_nnf();
+ let id = self.interner.intern(&subject);
+ named_classes.insert(id);
+ let head = Concept::Atom(id);
+ // Realization needs the structural form, same as the equivalence
+ // pass, and for the same reason: it cannot be recovered once
+ // negation has been pushed through the GCI encoding.
+ definitions.entry(id).or_insert_with(|| body.clone());
+ axioms.push((head.clone(), body.clone()));
+ axioms.push((body, head));
+ }
+
// Collect DisjointWith axioms
let disjoint_raw: Vec<(String, String)> = self
.index
diff --git a/tests/named_class_definition_test.rs b/tests/named_class_definition_test.rs
new file mode 100644
index 00000000..0cc9c58d
--- /dev/null
+++ b/tests/named_class_definition_test.rs
@@ -0,0 +1,254 @@
+//! A named class carrying `owl:unionOf`, `owl:intersectionOf` or
+//! `owl:complementOf` defines that class, and the reasoner used to drop it.
+//!
+//! `parse_class_expr` gated complex class expressions on `node.starts_with("_:")`,
+//! so the constructors were read only out of blank nodes. A named IRI carrying
+//! one came back as an opaque atom and its definition vanished, with nothing
+//! recording that anything had been lost: `unmodelled_constructs` returned
+//! empty, and the file header claimed `unionOf ✅`.
+//!
+//! That is the UNSOUND direction. An ontology inconsistent through such a
+//! definition was reported consistent, and a class unsatisfiable through one was
+//! reported satisfiable. A reasoner that misses an entailment is incomplete and
+//! says so; one that reports a contradiction as fine is telling the user
+//! something false.
+//!
+//! The OWL 2 Mapping to RDF Graphs makes the reading explicit: the pattern
+//! `*:x rdf:type owl:Class . *:x owl:unionOf T(SEQ CE1 ... CEn)` with `*:x` an
+//! IRI maps to `EquivalentClasses(*:x ObjectUnionOf(CE1 ... CEn))`. The IRI is
+//! DEFINED by the expression, not merely related to it, which is why the
+//! contradictions below are real ones.
+
+use open_ontologies::graph::GraphStore;
+use open_ontologies::tableaux::DlReasoner;
+use std::sync::Arc;
+
+const PREFIXES: &str = r#"
+ @prefix : <http://ex.org/> .
+ @prefix owl: <http://www.w3.org/2002/07/owl#> .
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+"#;
+
+fn unsatisfiable(ttl: &str) -> Vec<String> {
+ let store = Arc::new(GraphStore::new());
+ store.load_turtle(&format!("{PREFIXES}{ttl}"), None).unwrap();
+ let out = DlReasoner::run(&store, false).unwrap();
+ // `run` returns the JSON as a String, so parse it, not a re-serialisation
+ // of the String, which is a JSON string and has no fields at all.
+ let json: serde_json::Value = serde_json::from_str(&out).unwrap();
+ fn collect(v: &serde_json::Value, out: &mut Vec<String>) {
+ match v {
+ serde_json::Value::Object(m) => {
+ for (k, x) in m {
+ if k == "unsatisfiable_classes"
+ && let Some(a) = x.as_array()
+ {
+ out.extend(a.iter().filter_map(|y| y.as_str().map(str::to_string)));
+ }
+ collect(x, out);
+ }
+ }
+ serde_json::Value::Array(a) => a.iter().for_each(|x| collect(x, out)),
+ _ => {}
+ }
+ }
+ let mut v = vec![];
+ collect(&json, &mut v);
+ v
+}
+
+/// `U ≡ A ⊔ B`, and `Bad` is in `U` and in `N`, which is disjoint from both.
+/// Every branch of the union contradicts `N`, so `Bad` is empty.
+#[test]
+fn a_named_class_defined_by_a_union_is_a_definition() {
+ let got = unsatisfiable(
+ r#"
+ :A a owl:Class . :B a owl:Class . :N a owl:Class .
+ :A owl:disjointWith :N . :B owl:disjointWith :N .
+ :U a owl:Class ; owl:unionOf ( :A :B ) .
+ :Bad a owl:Class ; rdfs:subClassOf :U , :N .
+ "#,
+ );
+ assert!(
+ got.iter().any(|c| c.contains("Bad")),
+ "a union definition on a NAMED class was dropped, so an unsatisfiable class \
+ was reported satisfiable; unsatisfiable_classes = {got:?}"
+ );
+}
+
+/// `I ≡ A ⊓ B`. Anything in `I` is in `A`, and `A` is disjoint from `N`, so
+/// `Bad` is empty. This one needs the intersection read off the named class.
+#[test]
+fn a_named_class_defined_by_an_intersection_is_a_definition() {
+ let got = unsatisfiable(
+ r#"
+ :A a owl:Class . :B a owl:Class . :N a owl:Class .
+ :A owl:disjointWith :N .
+ :I a owl:Class ; owl:intersectionOf ( :A :B ) .
+ :Bad a owl:Class ; rdfs:subClassOf :I , :N .
+ "#,
+ );
+ assert!(
+ got.iter().any(|c| c.contains("Bad")),
+ "an intersection definition on a NAMED class was dropped; \
+ unsatisfiable_classes = {got:?}"
+ );
+}
+
+/// `K ≡ ¬A`. `Bad` is in `A` and in `K`, which is a direct contradiction.
+#[test]
+fn a_named_class_defined_by_a_complement_is_a_definition() {
+ let got = unsatisfiable(
+ r#"
+ :A a owl:Class .
+ :K a owl:Class ; owl:complementOf :A .
+ :Bad a owl:Class ; rdfs:subClassOf :A , :K .
+ "#,
+ );
+ assert!(
+ got.iter().any(|c| c.contains("Bad")),
+ "a complement definition on a NAMED class was dropped; \
+ unsatisfiable_classes = {got:?}"
+ );
+}
+
+/// The definition works in the other direction too: a member of either disjunct
+/// is a member of the union. Pins that the equivalence is bidirectional rather
+/// than a one-way subclass axiom, which is what `owl:unionOf` on an IRI means
+/// and is the half a `subClassOf`-shaped fix would silently lose.
+#[test]
+fn the_union_definition_runs_both_ways() {
+ let got = unsatisfiable(
+ r#"
+ :A a owl:Class . :B a owl:Class . :N a owl:Class .
+ :U a owl:Class ; owl:unionOf ( :A :B ) .
+ :U owl:disjointWith :N .
+ :Bad a owl:Class ; rdfs:subClassOf :A , :N .
+ "#,
+ );
+ assert!(
+ got.iter().any(|c| c.contains("Bad")),
+ "A ⊑ U is the half that needs U ≡ A ⊔ B rather than U ⊑ A ⊔ B; \
+ unsatisfiable_classes = {got:?}"
+ );
+}
+
+/// A named class with no set constructor is untouched, and one whose
+/// constructor is genuinely absent must not become `owl:Thing` or `owl:Nothing`
+/// by accident. Both of those would be a fix that breaks more than it repairs.
+#[test]
+fn an_ordinary_named_class_is_not_disturbed() {
+ let got = unsatisfiable(
+ r#"
+ :A a owl:Class . :N a owl:Class .
+ :A owl:disjointWith :N .
+ :Fine a owl:Class ; rdfs:subClassOf :A .
+ :AlsoFine a owl:Class ; rdfs:subClassOf :N .
+ "#,
+ );
+ assert!(
+ got.is_empty(),
+ "nothing here is contradictory and nothing should be reported: {got:?}"
+ );
+}
+
+/// The extraction itself. Without this, every assertion above that expects an
+/// EMPTY list passes whether or not `unsatisfiable_classes` is ever read, which
+/// is a gate that cannot fail.
+#[test]
+fn the_helper_actually_sees_an_unsatisfiable_class() {
+ let got = unsatisfiable(
+ r#"
+ :A a owl:Class . :N a owl:Class .
+ :A owl:disjointWith :N .
+ :Bad a owl:Class ; rdfs:subClassOf :A , :N .
+ "#,
+ );
+ assert!(
+ got.iter().any(|c| c.contains("Bad")),
+ "the plainest possible contradiction must show up, or this file measures nothing: {got:?}"
+ );
+}
+
+/// The IRI subjects that carry a set constructor, asked of the loaded graph
+/// rather than of the file's syntax so the blank-node spelling cannot be
+/// miscounted.
+const NAMED_DEFINITION_QUERY: &str = "SELECT DISTINCT ?c WHERE { \
+ ?c ?p ?o . \
+ FILTER(?p = <http://www.w3.org/2002/07/owl#unionOf> \
+ || ?p = <http://www.w3.org/2002/07/owl#intersectionOf> \
+ || ?p = <http://www.w3.org/2002/07/owl#complementOf>) \
+ FILTER(isIRI(?c)) }";
+
+fn named_definition_count(store: &Arc<GraphStore>, what: &str) -> usize {
+ let res = store.sparql_select(NAMED_DEFINITION_QUERY).unwrap();
+ let json: serde_json::Value = serde_json::from_str(&res)
+ .unwrap_or_else(|e| panic!("sparql_select did not return JSON for {what}: {e}\n{res}"));
+ json["results"]
+ .as_array()
+ .unwrap_or_else(|| panic!("no results array for {what}: {json}"))
+ .len()
+}
+
+#[test]
+fn the_bundled_ontologies_do_not_use_the_named_definition_pattern() {
+ // How often this fires on the ontologies that ship with the repository: never.
+ //
+ // Worth pinning as a number rather than leaving as an impression. Every set
+ // constructor in `pizza-reference.owl`, all 86 of them, sits on a blank node,
+ // and those were always read correctly. So the defect this file fixes is
+ // LATENT in this repository: real, reachable, in the unsound direction, and
+ // triggered by no benchmark here. If a future reference ontology starts using
+ // the named-definition pattern, this count changes and the assertion below
+ // should be updated rather than deleted, because the interesting thing is the
+ // number and not the zero.
+
+ // The zero below means nothing unless this query can see the pattern when
+ // it IS there. A count that is always zero and a corpus that never uses the
+ // construct look identical from the assertion.
+ let control = Arc::new(GraphStore::new());
+ control
+ .load_turtle(
+ &format!("{PREFIXES}:A a owl:Class . :B a owl:Class .\n\
+ :U a owl:Class ; owl:unionOf ( :A :B ) ."),
+ None,
+ )
+ .unwrap();
+ assert_eq!(
+ named_definition_count(&control, "the control"),
+ 1,
+ "the query cannot see a named definition, so the zeros below measure nothing"
+ );
+
+ use std::path::Path;
+ let mut hits = vec![];
+ for name in [
+ "pizza-reference.owl",
+ "ies-core.ttl",
+ "ies4.ttl",
+ "boro-building-handcrafted.ttl",
+ ] {
+ let path = Path::new(env!("CARGO_MANIFEST_DIR"))
+ .join("benchmark/reference")
+ .join(name);
+ if !path.exists() {
+ continue;
+ }
+ let store = Arc::new(GraphStore::new());
+ if store.load_file(path.to_str().unwrap()).is_err() {
+ continue;
+ }
+ // A named class is DEFINED here only if an IRI subject carries one of
+ // the three constructors. Asked of the loaded graph rather than of the
+ // file's syntax, so the blank-node spelling cannot be miscounted.
+ let n = named_definition_count(&store, name);
+ if n > 0 {
+ hits.push(format!("{name}: {n}"));
+ }
+ }
+ assert!(
+ hits.is_empty(),
+ "a bundled ontology now uses the named-definition pattern, so this fix is no \
+ longer latent here; update the count rather than removing the check: {hits:?}"
+ );
+}