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.

fix(shacl): onto_shacl_check reads declarations from every graph (#122)

Nicolas44f00b401ffa
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bb3fbe67..c9dfda17 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,16 @@ All notable changes to Open Ontologies are documented here.
## [Unreleased]
### Fixed
+- **`onto_shacl_check` reported a class or property declared inside a named
+ graph as missing.** The three existence lookups behind `missing_target_class`,
+ `missing_class_constraint` and `missing_path` ran a bare triple pattern, whose
+ default dataset is the default graph only, so an ontology loaded from TriG or
+ N-Quads, where every declaration sits in a `GRAPH` block, produced one issue
+ per referenced term while the same data in Turtle produced none. A declaration
+ is a declaration wherever it lives: the lookups now read the union of the
+ default graph and every named graph, unconditionally, with no scope argument
+ and no new response key. A class declared in no graph at all is still flagged
+ (#108).
- **A daemon started with `[http] token` in config rejected every command it was
meant to serve.** `serve-http` falls back to the config token and then enforces
bearer auth, but `daemon start` recorded `token: null` in `daemon.json`
diff --git a/src/shacl.rs b/src/shacl.rs
index 3a5eb980..acff10ea 100644
--- a/src/shacl.rs
+++ b/src/shacl.rs
@@ -813,11 +813,22 @@ fn graph_sparql_select(
Ok(rows)
}
-/// Check whether `iri` is declared as `owl:Class` or `rdfs:Class` in the graph.
+/// Check whether `iri` is declared as `owl:Class` or `rdfs:Class` anywhere in
+/// the store.
+///
+/// A declaration is a declaration wherever it lives. The bare pattern only
+/// sees the default graph, so an ontology loaded from TriG or N-Quads, whose
+/// declarations sit inside a `GRAPH` block, looked undeclared and every
+/// shape that referenced it was reported as `missing_target_class` or
+/// `missing_class_constraint`. The lookup therefore reads the union of the
+/// default graph and every named graph, unconditionally: there is no scope
+/// argument and no flag that narrows it back to the default graph.
fn class_exists(graph: &Arc<GraphStore>, iri: &str) -> anyhow::Result<bool> {
let query = format!(
r#"SELECT ?x WHERE {{
- <{iri}> a ?type .
+ {{ <{iri}> a ?type }}
+ UNION
+ {{ GRAPH ?g {{ <{iri}> a ?type }} }}
FILTER(?type = <http://www.w3.org/2002/07/owl#Class>
|| ?type = <http://www.w3.org/2000/01/rdf-schema#Class>)
}} LIMIT 1"#
@@ -827,11 +838,17 @@ fn class_exists(graph: &Arc<GraphStore>, iri: &str) -> anyhow::Result<bool> {
}
/// Check whether `iri` is declared as an `owl:ObjectProperty`,
-/// `owl:DatatypeProperty`, or `rdf:Property` in the graph.
+/// `owl:DatatypeProperty`, or `rdf:Property` anywhere in the store.
+///
+/// Same rule as `class_exists`: the union of the default graph and every
+/// named graph, unconditionally, so a property declared inside a `GRAPH`
+/// block is not reported as `missing_path`.
fn property_exists(graph: &Arc<GraphStore>, iri: &str) -> anyhow::Result<bool> {
let query = format!(
r#"SELECT ?x WHERE {{
- <{iri}> a ?type .
+ {{ <{iri}> a ?type }}
+ UNION
+ {{ GRAPH ?g {{ <{iri}> a ?type }} }}
FILTER(?type = <http://www.w3.org/2002/07/owl#ObjectProperty>
|| ?type = <http://www.w3.org/2002/07/owl#DatatypeProperty>
|| ?type = <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
diff --git a/tests/shacl_check_test.rs b/tests/shacl_check_test.rs
index 02b4f490..a1d8fc1d 100644
--- a/tests/shacl_check_test.rs
+++ b/tests/shacl_check_test.rs
@@ -7,6 +7,7 @@
use open_ontologies::graph::GraphStore;
use open_ontologies::shacl::ShaclValidator;
+use oxigraph::io::RdfFormat;
use std::sync::Arc;
const ONTOLOGY: &str = r#"
@@ -24,12 +25,39 @@ const ONTOLOGY: &str = r#"
rdfs:range ex:Address .
"#;
+/// The same declarations as `ONTOLOGY`, but inside a named graph. This is
+/// what a TriG or N-Quads ontology looks like once `load_content` has loaded
+/// it verbatim: nothing sits in the default graph (issue #108).
+const ONTOLOGY_TRIG: &str = r#"
+ @prefix owl: <http://www.w3.org/2002/07/owl#> .
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+ @prefix ex: <http://example.org/> .
+
+ ex:schema {
+ ex:Person a owl:Class .
+ ex:Address a owl:Class .
+ ex:hasName a owl:DatatypeProperty ;
+ rdfs:domain ex:Person ;
+ rdfs:range <http://www.w3.org/2001/XMLSchema#string> .
+ ex:livesAt a owl:ObjectProperty ;
+ rdfs:domain ex:Person ;
+ rdfs:range ex:Address .
+ }
+"#;
+
fn loaded() -> Arc<GraphStore> {
let g = Arc::new(GraphStore::new());
g.load_turtle(ONTOLOGY, None).expect("load ontology");
g
}
+fn loaded_trig(dataset: &str) -> Arc<GraphStore> {
+ let g = Arc::new(GraphStore::new());
+ g.load_content(dataset, RdfFormat::TriG)
+ .expect("load TriG ontology");
+ g
+}
+
#[test]
fn well_formed_shapes_pass() {
let graph = loaded();
@@ -227,3 +255,146 @@ fn well_formed_shapes_carry_per_shape_diagnostic_detail() {
assert!(pc[0]["path_exists"].as_bool().unwrap());
assert!(pc[0]["datatype_recognised"].as_bool().unwrap());
}
+
+#[test]
+fn declarations_in_a_named_graph_are_seen_by_check_shapes() {
+ // Issue #108: the lookups used a bare triple pattern, whose default
+ // dataset is the default graph only, so an ontology loaded from TriG
+ // reported every class and property it declares as missing.
+ let graph = loaded_trig(ONTOLOGY_TRIG);
+
+ let shapes = r#"
+ @prefix sh: <http://www.w3.org/ns/shacl#> .
+ @prefix ex: <http://example.org/> .
+
+ ex:PersonShape a sh:NodeShape ;
+ sh:targetClass ex:Person ;
+ sh:property [
+ sh:path ex:hasName ;
+ sh:datatype <http://www.w3.org/2001/XMLSchema#string> ;
+ ] ;
+ sh:property [
+ sh:path ex:livesAt ;
+ sh:class ex:Address ;
+ ] .
+ "#;
+
+ let report_str = ShaclValidator::check_shapes(&graph, shapes).expect("check_shapes");
+ let report: serde_json::Value = serde_json::from_str(&report_str).expect("json");
+
+ assert!(
+ report["ok"].as_bool().unwrap(),
+ "declarations in a named graph must count; got:\n{}",
+ report_str
+ );
+ assert!(report["parses"].as_bool().unwrap());
+ assert_eq!(report["issue_count"].as_u64().unwrap(), 0);
+ assert_eq!(report["shape_count"].as_u64().unwrap(), 1);
+
+ let s = &report["shapes"].as_array().unwrap()[0];
+ assert!(s["target_class_exists"].as_bool().unwrap());
+ let pc = s["property_constraints"].as_array().unwrap();
+ assert_eq!(pc.len(), 2);
+ for constraint in pc {
+ assert!(
+ constraint["path_exists"].as_bool().unwrap(),
+ "path declared in a named graph must exist: {constraint}"
+ );
+ }
+ let with_class = pc
+ .iter()
+ .find(|c| c["class_constraint"].is_string())
+ .expect("one property carries sh:class");
+ assert!(with_class["class_constraint_exists"].as_bool().unwrap());
+}
+
+#[test]
+fn declarations_split_across_graphs_are_all_seen_by_check_shapes() {
+ // The target class lives in one named graph, the properties in another,
+ // and the sh:class target in the default graph. The lookup reads the
+ // union of all of them, so no issue is raised from any side.
+ let dataset = r#"
+ @prefix owl: <http://www.w3.org/2002/07/owl#> .
+ @prefix ex: <http://example.org/> .
+
+ ex:classes { ex:Person a owl:Class . }
+
+ ex:properties {
+ ex:hasName a owl:DatatypeProperty .
+ ex:livesAt a owl:ObjectProperty .
+ }
+
+ { ex:Address a owl:Class . }
+ "#;
+ let graph = loaded_trig(dataset);
+
+ let shapes = r#"
+ @prefix sh: <http://www.w3.org/ns/shacl#> .
+ @prefix ex: <http://example.org/> .
+
+ ex:PersonShape a sh:NodeShape ;
+ sh:targetClass ex:Person ;
+ sh:property [
+ sh:path ex:hasName ;
+ sh:datatype <http://www.w3.org/2001/XMLSchema#string> ;
+ ] ;
+ sh:property [
+ sh:path ex:livesAt ;
+ sh:class ex:Address ;
+ ] .
+ "#;
+
+ let report_str = ShaclValidator::check_shapes(&graph, shapes).expect("check_shapes");
+ let report: serde_json::Value = serde_json::from_str(&report_str).expect("json");
+
+ assert!(
+ report["ok"].as_bool().unwrap(),
+ "declarations split across graphs must all count; got:\n{}",
+ report_str
+ );
+ assert_eq!(report["issue_count"].as_u64().unwrap(), 0);
+ assert_eq!(report["shape_count"].as_u64().unwrap(), 1);
+}
+
+#[test]
+fn undeclared_target_class_is_still_flagged_when_ontology_is_in_a_named_graph() {
+ // The fix reads every graph; it is not a blanket "always exists". A class
+ // declared in no graph at all is still reported, and the declared ones
+ // beside it are not.
+ let graph = loaded_trig(ONTOLOGY_TRIG);
+
+ let shapes = r#"
+ @prefix sh: <http://www.w3.org/ns/shacl#> .
+ @prefix ex: <http://example.org/> .
+
+ ex:GhostShape a sh:NodeShape ;
+ sh:targetClass ex:DoesNotExist ;
+ sh:property [
+ sh:path ex:hasName ;
+ ] .
+ "#;
+
+ let report_str = ShaclValidator::check_shapes(&graph, shapes).expect("check_shapes");
+ let report: serde_json::Value = serde_json::from_str(&report_str).expect("json");
+
+ assert!(!report["ok"].as_bool().unwrap());
+ assert_eq!(
+ report["issue_count"].as_u64().unwrap(),
+ 1,
+ "exactly the undeclared class is flagged; got:\n{}",
+ report_str
+ );
+ let issues = report["issues"].as_array().unwrap();
+ assert!(
+ issues.iter().any(|i| {
+ i["kind"].as_str() == Some("missing_target_class")
+ && i["value"].as_str().unwrap_or("").contains("DoesNotExist")
+ }),
+ "expected a missing_target_class issue for ex:DoesNotExist; got: {:?}",
+ issues
+ );
+ let s = &report["shapes"].as_array().unwrap()[0];
+ assert!(!s["target_class_exists"].as_bool().unwrap());
+ let pc = s["property_constraints"].as_array().unwrap();
+ assert!(pc[0]["path_exists"].as_bool().unwrap());
+}