Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
fix(tableaux): discover entities by axiom use, and answer consistency for the whole KB
Fabio Rovaif86876b2da49
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 18da4991..83997200 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,26 @@ All notable changes to Open Ontologies are documented here.## [Unreleased]### Fixed+- **The DL reasoner discovered entities by their typing declarations rather than+ by the axioms that use them, and its headline `consistent` flag answered for+ the TBox alone.** Three defects, one root cause, found from the outside by+ probing the shipped binary with deliberately broken ontologies rather than by+ reading the code. A class declared only through `rdfs:subClassOf`,+ `owl:equivalentClass` or `owl:disjointWith` - never typed `owl:Class` - was+ invisible to the satisfiability sweep, so an unsatisfiable class was reported+ satisfiable by omission. An individual typed into a plain class - never typed+ `owl:NamedIndividual`, which instance data in the wild almost never is - never+ reached the ABox check, so the textbook inconsistency of one individual in two+ disjoint classes sailed through. And even when the ABox check DID prove an+ inconsistency, `reason owl-dl` published `"consistent": true` in the same JSON+ object as the proof of false, because the flag was computed from the TBox+ alone. Classes are now harvested from all three axiom positions, individuals+ from any rdf:type pointing at a known class (with schema declarations+ excluded, so a class never doubles as an individual), and the headline flag is+ the conjunction of TBox and ABox verdicts, with `tbox_consistent` exposed+ separately. The three-valued discipline is preserved: an undecided ABox still+ defaults to consistent. The probes that exposed all three are preserved as+ regressions in `tests/tableaux_discovery_test.rs`.- **Three more tools answered about the default graph alone, so their answersdepended on the serialisation the data arrived in.** Same defect as the twohalves of #108, found by loading identical content as Turtle and as TriG anddiff --git a/src/tableaux.rs b/src/tableaux.rsindex c27c12a1..ad890d1c 100644--- a/src/tableaux.rs+++ b/src/tableaux.rs@@ -409,6 +409,20 @@ impl OwlParser {.map(move |(_, o)| (s.clone(), o.clone()))}).collect();+ // A class is a class because axioms treat it as one, not because someone remembered+ // to type it `owl:Class`. Real ontologies routinely declare a class only through+ // rdfs:subClassOf / owl:equivalentClass / owl:disjointWith; gating discovery on the+ // explicit typing silently dropped such classes from the satisfiability sweep, so an+ // unsatisfiable class could be reported satisfiable by omission. Blank-node class+ // expressions are excluded: only IRIs name checkable classes.+ for (a, b) in &subclass_pairs {+ for side in [a, b] {+ if side.starts_with('<') && side != OWL_THING && side != OWL_NOTHING {+ let id = self.interner.intern(side);+ named_classes.insert(id);+ }+ }+ }for (sub_str, sup_str) in subclass_pairs {let sub = self.parse_class_expr(&sub_str);let sup = self.parse_class_expr(&sup_str);@@ -428,6 +442,14 @@ impl OwlParser {.map(move |(_, o)| (s.clone(), o.clone()))}).collect();+ for (a, b) in &equiv_pairs {+ for side in [a, b] {+ if side.starts_with('<') && side != OWL_THING && side != OWL_NOTHING {+ let id = self.interner.intern(side);+ named_classes.insert(id);+ }+ }+ }for (a_str, b_str) in equiv_pairs {let a = self.parse_class_expr(&a_str);let b = self.parse_class_expr(&b_str);@@ -457,18 +479,58 @@ impl OwlParser {.map(move |(_, o)| (s.clone(), o.clone()))}).collect();+ for (a, b) in &disjoint_raw {+ for side in [a, b] {+ if side.starts_with('<') && side != OWL_THING && side != OWL_NOTHING {+ let id = self.interner.intern(side);+ named_classes.insert(id);+ }+ }+ }for (a_str, b_str) in disjoint_raw {let a = self.parse_class_expr(&a_str).to_nnf();let b = self.parse_class_expr(&b_str).to_nnf();disjoint_pairs.push((a, b));}- // Collect named individuals and their types + role assertions- let individual_subjects: Vec<String> = subject_types- .iter()- .filter(|(_, types)| types.iter().any(|t| t == OWL_NAMED_INDIVIDUAL))- .map(|(s, _)| s.clone())- .collect();+ // Collect individuals and their types + role assertions.+ //+ // An individual is discovered two ways: the explicit `owl:NamedIndividual` typing,+ // or an rdf:type pointing at a known class. Instance data in the wild almost never+ // carries the explicit typing, and gating on it meant the ABox check received no+ // individuals at all, so a textbook inconsistency - one individual typed into two+ // disjoint classes - sailed through as consistent. Subjects that are themselves+ // schema declarations are excluded, so a class or property never doubles as an+ // individual by accident.+ let schema_markers: [&str; 10] = [+ OWL_CLASS,+ OWL_OBJECT_PROPERTY,+ OWL_TRANSITIVE,+ "<http://www.w3.org/2002/07/owl#DatatypeProperty>",+ "<http://www.w3.org/2002/07/owl#AnnotationProperty>",+ "<http://www.w3.org/2002/07/owl#FunctionalProperty>",+ "<http://www.w3.org/2002/07/owl#InverseFunctionalProperty>",+ "<http://www.w3.org/2002/07/owl#SymmetricProperty>",+ "<http://www.w3.org/2002/07/owl#Ontology>",+ "<http://www.w3.org/2000/01/rdf-schema#Class>",+ ];+ let mut individual_subjects: Vec<String> = Vec::new();+ for (subject, types) in &subject_types {+ if types.iter().any(|t| schema_markers.contains(&t.as_str())) {+ continue;+ }+ let explicit = types.iter().any(|t| t == OWL_NAMED_INDIVIDUAL);+ let typed_by_known_class = types.iter().any(|t| {+ t.starts_with('<') && {+ let id = self.interner.intern(t);+ named_classes.contains(&id)+ }+ });+ if explicit || typed_by_known_class {+ individual_subjects.push(subject.clone());+ }+ }+ individual_subjects.sort_unstable();for ind_str in &individual_subjects {let ind_id = self.interner.intern(ind_str);let types = self.index.objects(ind_str, RDF_TYPE);@@ -2051,10 +2113,16 @@ impl DlReasoner {let reasoner = Self::from_graph(graph)?;let initial_triples = graph.triple_count();- let consistent = reasoner.is_consistent();+ let tbox_consistent = reasoner.is_consistent();let result = reasoner.classify_parallel();let abox_result = reasoner.check_abox();+ // The headline flag answers for the whole knowledge base. Reporting the TBox alone+ // while the ABox check has PROVEN an inconsistency in the same output is a false+ // claim; the three-valued discipline is preserved because an undecided ABox already+ // defaults to consistent inside check_abox.+ let consistent = tbox_consistent && abox_result.consistent;+// Collect explanations for unsatisfiable classeslet mut explanations: Vec<serde_json::Value> = Vec::new();for &cls in &result.unsatisfiable {@@ -2158,6 +2226,7 @@ impl DlReasoner {"algorithm": "tableaux","description_logic": "SHOIQ","consistent": consistent,+ "tbox_consistent": tbox_consistent,"named_classes": reasoner.named_classes.len(),"unsatisfiable_classes": unsat_names,"complete": complete,diff --git a/tests/tableaux_discovery_test.rs b/tests/tableaux_discovery_test.rsnew file mode 100644index 00000000..229abbc2--- /dev/null+++ b/tests/tableaux_discovery_test.rs@@ -0,0 +1,150 @@+//! Entity discovery must not depend on explicit typing declarations.+//!+//! Three defects shared one root cause and were found from the outside, by probing the+//! shipped binary with deliberately broken ontologies rather than by reading the code:+//!+//! 1. classes were discovered only through `a owl:Class`, so a class declared solely by+//! rdfs:subClassOf / owl:equivalentClass / owl:disjointWith never entered the+//! satisfiability sweep and an unsatisfiable class was reported satisfiable by+//! omission;+//! 2. individuals were discovered only through `a owl:NamedIndividual`, which instance+//! data in the wild almost never carries, so the ABox check received no individuals+//! and a textbook inconsistency - one individual typed into two disjoint classes -+//! sailed through;+//! 3. even when the ABox check DID prove an inconsistency, the headline `consistent`+//! flag reported the TBox alone, publishing `true` next to the proof of `false`.+//!+//! The probes that exposed all three are preserved here as regressions.++use open_ontologies::graph::GraphStore;+use std::sync::Arc;++fn reasoned(ttl: &str) -> serde_json::Value {+ let store = Arc::new(GraphStore::new());+ store.load_turtle(ttl, None).unwrap();+ let out = open_ontologies::tableaux::DlReasoner::run(&store, false).unwrap();+ serde_json::from_str(&out).unwrap()+}++fn unsat_classes(v: &serde_json::Value) -> Vec<String> {+ v["unsatisfiable_classes"]+ .as_array()+ .unwrap()+ .iter()+ .map(|c| c.as_str().unwrap().to_string())+ .collect()+}++#[test]+fn a_class_declared_only_by_subclassof_is_satisfiability_checked() {+ // Broken is never typed owl:Class; it exists only through its subclass axioms. It is+ // subsumed under two classes whose disjointness is inherited through a chain, so it is+ // unsatisfiable - and previously invisible.+ let v = reasoned(+ r#"+ @prefix : <http://ex.org/> .+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+ :Term a owl:Class .+ :Soc a owl:Class ; rdfs:subClassOf :Term .+ :Unresolved a owl:Class ; owl:disjointWith :Term .+ :Broken rdfs:subClassOf :Unresolved , :Soc .+ "#,+ );+ assert!(+ unsat_classes(&v).iter().any(|c| c.contains("Broken")),+ "untyped class missing from the sweep: {v}"+ );+}++#[test]+fn a_class_declared_only_by_disjointwith_is_discovered() {+ // Neither side of the disjointness is typed owl:Class. Both must still be named+ // classes, or the axiom checks nothing.+ let v = reasoned(+ r#"+ @prefix : <http://ex.org/> .+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+ :A owl:disjointWith :B .+ :C rdfs:subClassOf :A , :B .+ "#,+ );+ assert!(+ unsat_classes(&v).iter().any(|c| c.contains("C")),+ "disjointness between undeclared classes checked nothing: {v}"+ );+}++#[test]+fn an_instance_typed_by_a_plain_class_reaches_the_abox_check() {+ // x carries no owl:NamedIndividual typing, which is how instance data actually+ // arrives. It is typed into two disjoint classes: the textbook inconsistency.+ let v = reasoned(+ r#"+ @prefix : <http://ex.org/> .+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ :A a owl:Class .+ :B a owl:Class ; owl:disjointWith :A .+ :x a :A , :B .+ "#,+ );+ assert!(+ v["abox"]["individuals_checked"].as_u64().unwrap() >= 1,+ "plain-typed individual never reached the ABox check: {v}"+ );+ assert_eq!(v["consistent"], false, "proven ABox clash not reported: {v}");+}++#[test]+fn a_proven_abox_clash_flips_the_headline_flag() {+ // The explicitly-typed variant: the ABox check already proved this inconsistent, and+ // the headline flag used to publish `true` next to that proof.+ let v = reasoned(+ r#"+ @prefix : <http://ex.org/> .+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ :A a owl:Class .+ :B a owl:Class ; owl:disjointWith :A .+ :x a owl:NamedIndividual , :A , :B .+ "#,+ );+ assert_eq!(v["consistent"], false);+ assert_eq!(v["tbox_consistent"], true, "the TBox itself is fine: {v}");+ assert_eq!(v["abox"]["consistent"], false);+}++#[test]+fn schema_declarations_never_double_as_individuals() {+ // A pure schema: classes, a property, an ontology header. None of these subjects may+ // be swept into the ABox as individuals by the widened discovery.+ let v = reasoned(+ r#"+ @prefix : <http://ex.org/> .+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+ <http://ex.org/onto> a owl:Ontology .+ :A a owl:Class .+ :B a owl:Class ; rdfs:subClassOf :A .+ :p a owl:ObjectProperty .+ "#,+ );+ assert_eq!(v["abox"], serde_json::Value::Null, "schema swept into the ABox: {v}");+ assert_eq!(v["consistent"], true);+}++#[test]+fn consistent_instance_data_stays_consistent() {+ let v = reasoned(+ r#"+ @prefix : <http://ex.org/> .+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ :A a owl:Class .+ :B a owl:Class ; owl:disjointWith :A .+ :x a :A .+ :y a :B .+ "#,+ );+ assert_eq!(v["consistent"], true);+ assert_eq!(v["abox"]["individuals_checked"].as_u64().unwrap(), 2);+}