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
Decide the SPARQL multiplicity disagreement, in six triples (#180)
Fabio Rovai94dd1c9e97a9
diff --git a/src/shacl.rs b/src/shacl.rsindex 8951e75c..7bea1b09 100644--- a/src/shacl.rs+++ b/src/shacl.rs@@ -1839,10 +1839,22 @@ impl ShaclValidator {// and is not: SHACL 5.3.2 emits one result per SPARQL solution, so a// constraint returning several solutions for one focus node produces// several results that are identical wherever the extra solution binds- // nothing this report carries. pyshacl emits those too. On the- // 39-shape corpus from issue #132 both engines return 249 results over- // 245 distinct (focus node, shape) pairs, and deduplicating took this- // engine to 245 and broke an exact agreement.+ // nothing this report carries. On the 39-shape corpus from issue #132+ // both engines return 249 results over 245 distinct (focus node,+ // shape) pairs, and deduplicating took this engine to 245 and broke an+ // exact agreement.+ //+ // That corpus once justified the stronger claim that "pyshacl emits+ // those too", and it does NOT hold in general. Measured on 17+ // September 2026 over the six triples in+ // tests/fixtures/shacl-sparql/: rdflib and this engine both return+ // FOUR solutions for the constraint query, and pySHACL 0.40.1 emits+ // TWO validation results from them while this engine emits four. The+ // collapsing is in pySHACL's SHACL layer rather than in its SPARQL+ // engine, so on identical solutions the two do not agree and this+ // engine is the one following SHACL 5.2.1. The decision below is+ // unchanged and one of its old reasons was wrong.+ // tests/shacl_sparql_multiplicity_test.rs pins it.//// The real fix for the double-checked node is to take the union of a// shape's focus nodes across its target declarations rather than todiff --git a/tests/fixtures/shacl-sparql/knows-data.ttl b/tests/fixtures/shacl-sparql/knows-data.ttlnew file mode 100644index 00000000..d61f2127--- /dev/null+++ b/tests/fixtures/shacl-sparql/knows-data.ttl@@ -0,0 +1,3 @@+@prefix ex: <http://example.org/> .+ex:alice a ex:Person ; ex:knows ex:bob , ex:carol , ex:dave .+ex:bob a ex:Person ; ex:knows ex:carol .diff --git a/tests/fixtures/shacl-sparql/knows-shapes.ttl b/tests/fixtures/shacl-sparql/knows-shapes.ttlnew file mode 100644index 00000000..5773165b--- /dev/null+++ b/tests/fixtures/shacl-sparql/knows-shapes.ttl@@ -0,0 +1,13 @@+@prefix sh: <http://www.w3.org/ns/shacl#> .+@prefix ex: <http://example.org/> .+ex:PersonShape a sh:NodeShape ;+ sh:targetClass ex:Person ;+ sh:sparql [+ a sh:SPARQLConstraint ;+ sh:message "person has an acquaintance" ;+ sh:prefixes ex: ;+ sh:select """+ SELECT $this+ WHERE { $this <http://example.org/knows> ?x . }+ """+ ] .diff --git a/tests/shacl_sparql_multiplicity_test.rs b/tests/shacl_sparql_multiplicity_test.rsnew file mode 100644index 00000000..e80995c3--- /dev/null+++ b/tests/shacl_sparql_multiplicity_test.rs@@ -0,0 +1,125 @@+//! **A SPARQL constraint reports once per solution, and this pins which engine+//! is right about that.**+//!+//! Issue #169 records forty disagreements with pySHACL 0.40.1 on a private+//! corpus, thirty eight of them carrying `sh:SPARQLConstraintComponent`, with+//! this engine returning MORE results in every row. Twenty four of the forty+//! are consistent with duplication. The corpus cannot be published, so the+//! whole finding sat unreproducible and undecided.+//!+//! This is that class of disagreement in six triples, and it is decided rather+//! than merely recorded. Measured on 17 September 2026 against pySHACL 0.40.1+//! and rdflib, with the fixtures beside this file:+//!+//! | what was asked | answer |+//! | --- | --- |+//! | rdflib, `SELECT ?this WHERE { ?this ex:knows ?x }` | 4 solutions |+//! | this engine, the same query | 4 solutions |+//! | pySHACL, the shapes graph below | 2 validation results |+//! | this engine, the shapes graph below | 4 validation results |+//!+//! The two SPARQL engines AGREE on four. `ex:alice` has three `ex:knows`+//! edges and `ex:bob` has one, and SPARQL projection does not remove+//! duplicates: only `DISTINCT` and `REDUCED` do, per SPARQL 1.1 section 18.2.+//! So the disagreement is not about SPARQL at all. pySHACL's SHACL layer+//! collapses the three identical `ex:alice` solutions into one result, and+//! SHACL section 5.2.1 says a validation result is created for each solution.+//!+//! **This engine is therefore right and pySHACL under-reports**, which is the+//! opposite of what a reader would assume from a disagreement where we are the+//! outlier. It also means the twenty four duplication-consistent rows in #169+//! are very likely not defects here.+//!+//! The rows in #169 of the form "engine 1, pySHACL 0" are NOT explained by any+//! of this: nothing about multiplicity turns one result into none. Those stay+//! open and are the ones worth hunting.+//!+//! The reason a test is needed at all is that `src/shacl.rs` justified its+//! deliberate non-deduplication partly on the claim that "pyshacl emits those+//! too". On the corpus that claim was measured against it held, because the+//! extra solutions there bound something the report carried. For identical+//! solutions it does not hold, as the table above shows. The DECISION was+//! right and one of its stated reasons was not, which is worth pinning so that+//! nobody later "fixes" the count to match pySHACL and breaks conformance.++use open_ontologies::graph::GraphStore;+use open_ontologies::shacl::ShaclValidator;+use std::path::PathBuf;+use std::sync::Arc;++fn fixture(name: &str) -> String {+ let p = PathBuf::from(env!("CARGO_MANIFEST_DIR"))+ .join("tests")+ .join("fixtures")+ .join("shacl-sparql")+ .join(name);+ std::fs::read_to_string(&p).unwrap_or_else(|e| panic!("read {}: {e}", p.display()))+}++fn report() -> serde_json::Value {+ let graph = Arc::new(GraphStore::new());+ graph.load_turtle(&fixture("knows-data.ttl"), None).expect("data must parse");+ let json = ShaclValidator::validate(&graph, &fixture("knows-shapes.ttl")).expect("validate");+ serde_json::from_str(&json).expect("report is JSON")+}++/// Four solutions, four results. Three for `ex:alice` and one for `ex:bob`.+#[test]+fn a_sparql_constraint_reports_once_per_solution() {+ let r = report();+ assert_eq!(r["conforms"], serde_json::json!(false), "report: {r}");+ assert_eq!(+ r["violation_count"],+ serde_json::json!(4),+ "ex:alice has three ex:knows edges and ex:bob one, so the constraint query has \+ four solutions and SHACL 5.2.1 makes one result of each. Collapsing the three \+ identical ex:alice results would give 2, which is what pySHACL returns and what \+ this test exists to stop us copying: {r}"+ );+}++/// The multiplicity is per focus node, not smeared across the report. If a+/// future change deduplicated only partly, the total above could still be+/// right for the wrong reason.+#[test]+fn the_multiplicity_follows_the_focus_node_that_earned_it() {+ let r = report();+ let mut per_node: std::collections::BTreeMap<String, usize> = Default::default();+ for v in r["violations"].as_array().expect("violations is an array") {+ let f = v["focus_node"].as_str().expect("focus_node is a string").to_string();+ *per_node.entry(f).or_default() += 1;+ }+ assert_eq!(+ per_node.get("http://example.org/alice").copied(),+ Some(3),+ "ex:alice has three ex:knows edges: {per_node:?}"+ );+ assert_eq!(+ per_node.get("http://example.org/bob").copied(),+ Some(1),+ "ex:bob has one: {per_node:?}"+ );+ assert_eq!(per_node.len(), 2, "only the two ex:Person nodes are targets: {per_node:?}");+}++/// And the count is derived from the data rather than from this file. Add a+/// fourth `ex:knows` edge to `ex:alice` and the expectation moves with it, so+/// the numbers above cannot drift from the fixture they describe.+#[test]+fn the_expected_total_is_the_number_of_knows_edges_in_the_fixture() {+ let data = fixture("knows-data.ttl");+ // Both subjects are ex:Person, so every ex:knows edge is one solution.+ let edges = data.matches("ex:knows").count();+ let commas: usize = data+ .lines()+ .filter(|l| l.contains("ex:knows"))+ .map(|l| l.matches(',').count())+ .sum();+ let solutions = edges + commas;+ assert_eq!(+ solutions, 4,+ "the fixture should hold four ex:knows edges, counted as {edges} predicates plus \+ {commas} comma-separated objects"+ );+ assert_eq!(report()["violation_count"], serde_json::json!(solutions));+}