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
Pre-bind $this per focus node in sh:sparql, and name the shape on every violation
FABIOTESScb18d15cf94a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c45ac029..9f6ceaa6 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,25 @@ All notable changes to Open Ontologies are documented here.## [Unreleased]+### Fixed+- **`sh:sparql` reported `conforms: true` when any single focus node+ conformed (#132).** The author's SELECT was wrapped as a subquery under a+ `VALUES ?this` clause. A subquery is evaluated bottom-up with no outer+ variable in scope, so `$this` was unbound inside it, `FILTER NOT EXISTS`+ asked whether ANY node matched, and one clean record hid every dirty one;+ when every record failed the empty solution joined with all of them and the+ count looked right. `$this` is now pre-bound per focus node through+ Oxigraph's substitution, which is the mechanism SHACL-SPARQL 5.3.2+ specifies. Blank-node focus nodes, previously excluded because VALUES+ cannot name them, are evaluated. On the 39-shape case from the issue the+ engine and pyshacl now agree exactly: 249 results, 245 distinct+ record-shape pairs. A bound `?message` overrides `sh:message`, `?path`+ becomes `result_path` and `?value` becomes `value`.+- **Violations now name the shape and constraint component that produced+ them (#131).** Every violation carries `source_shape` (the shape IRI),+ `source_constraint_component` (the `sh:*ConstraintComponent` IRI) and+ `result_path` wherever a path is known. Existing keys are unchanged.+## [1.3.0] - 2026-09-04> `v1.2.1` was tagged from inside this range (`5208de8`) without a version bumpdiff --git a/src/graph.rs b/src/graph.rsindex d96fb0a6..87113387 100644--- a/src/graph.rs+++ b/src/graph.rs@@ -309,6 +309,61 @@ impl GraphStore {self.select_with_dataset(query, true)}+ /// Run a SELECT once per term, with `var` pre-bound to that term, over the+ /// union dataset. Returns the solutions per term, in the order given.+ ///+ /// Pre-binding is SPARQL substitution, the mechanism SHACL-SPARQL+ /// specifies for `$this` (section 5.3.2): the term is in scope everywhere+ /// in the query, inside `FILTER (NOT) EXISTS` and inside subqueries. A+ /// `VALUES` join is not the same thing. A subquery is evaluated bottom-up+ /// with no outer variable in scope, so a constraint wrapped that way ran+ /// with `$this` unbound, asked whether ANY node matched, and one clean+ /// record hid every dirty one (#132).+ ///+ /// The query is parsed once; each term gets its own substitution and+ /// execution. The pre-bound variable is present in every returned row+ /// whether or not the author projected it.+ pub fn sparql_select_union_prebound(+ &self,+ query: &str,+ var: &str,+ terms: &[Term],+ ) -> anyhow::Result<Vec<Vec<std::collections::HashMap<String, String>>>> {+ let store = &self.store;+ let mut prepared = SparqlEvaluator::new().parse_query(query)?;+ prepared.dataset_mut().set_default_graph_as_union();+ let variable = Variable::new(var)?;+ let mut out = Vec::with_capacity(terms.len());+ for term in terms {+ let bound = prepared+ .clone()+ .substitute_variable(variable.clone(), term.clone());+ let QueryResults::Solutions(solutions) = bound.on_store(store).execute()? else {+ anyhow::bail!("pre-bound evaluation needs a SELECT query");+ };+ let vars: Vec<String> = solutions+ .variables()+ .iter()+ .map(|v| v.as_str().to_string())+ .collect();+ let mut rows = Vec::new();+ for solution in solutions {+ let solution = solution?;+ let mut row = std::collections::HashMap::new();+ for v in &vars {+ if let Some(t) = solution.get(v.as_str()) {+ row.insert(v.clone(), t.to_string());+ }+ }+ row.entry(var.to_string())+ .or_insert_with(|| term.to_string());+ rows.push(row);+ }+ out.push(rows);+ }+ Ok(out)+ }+fn select_with_dataset(&self,query: &str,@@ -319,7 +374,7 @@ impl GraphStore {if union_default_graph {prepared.dataset_mut().set_default_graph_as_union();}- match prepared.on_store(&store).execute()? {+ match prepared.on_store(store).execute()? {QueryResults::Solutions(solutions) => {let vars: Vec<String> = solutions.variables()@@ -493,7 +548,7 @@ impl GraphStore {let count_from_query = |q: &str| -> usize {let Ok(prepared) = SparqlEvaluator::new().parse_query(q) else { return 0 };let Ok(QueryResults::Solutions(solutions)) = prepared- .on_store(&store)+ .on_store(store).execute()else { return 0 };let Some(Ok(row)) = solutions.into_iter().next() else { return 0 };diff --git a/src/shacl.rs b/src/shacl.rsindex 1633e35c..8e7a566e 100644--- a/src/shacl.rs+++ b/src/shacl.rs@@ -1,5 +1,6 @@use crate::graph::GraphStore;use oxigraph::io::{RdfFormat, RdfParser};+use oxigraph::model::Term;use oxigraph::sparql::{QueryResults, SparqlEvaluator};use oxigraph::store::Store;use std::collections::{HashMap, HashSet};@@ -40,6 +41,13 @@ pub struct ShaclValidator;impl ShaclValidator {/// Validate the data in `graph` against SHACL shapes (inline Turtle)./// Returns a JSON report: `{conforms, violation_count, violations[]}`.+ ///+ /// Every violation names the shape that produced it (`source_shape`), the+ /// W3C constraint component (`source_constraint_component`) and, where a+ /// path is known, `result_path`, so a report written as one shape per rule+ /// can be read back as the findings table it is (#131). The keys that+ /// predate those (`constraint`, `focus_node`, `message`, `severity`,+ /// `path`) are unchanged.pub fn validate(graph: &Arc<GraphStore>, shapes_ttl: &str) -> anyhow::Result<String> {// 1. Parse shapes Turtle into a temporary storelet shapes_store = Store::new()?;@@ -667,12 +675,12 @@ impl ShaclValidator {} else {node_message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": "Violation","focus_node": strip_angle_brackets(focus),"constraint": "or","message": msg,- }));+ })));}}}@@ -791,13 +799,13 @@ impl ShaclValidator {} else {q_message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": q_severity,"focus_node": strip_angle_brackets(focus),"path": q_path,"constraint": constraint,"message": msg,- }));+ })));}}}@@ -898,13 +906,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "minCount","message": msg,- }));+ })));}}}@@ -932,13 +940,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "maxCount","message": msg,- }));+ })));}}}@@ -967,13 +975,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "class","message": msg,- }));+ })));}}}@@ -1016,13 +1024,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "datatype","message": msg,- }));+ })));}}}@@ -1048,13 +1056,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "pattern","message": msg,- }));+ })));}}}@@ -1090,13 +1098,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "or","message": msg,- }));+ })));}}}@@ -1136,13 +1144,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": constraint,"message": msg,- }));+ })));}}}@@ -1179,13 +1187,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": constraint,"message": msg,- }));+ })));}}}@@ -1215,13 +1223,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "node","message": msg,- }));+ })));}}}@@ -1257,13 +1265,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "not","message": msg,- }));+ })));}}}@@ -1288,13 +1296,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "in","message": msg,- }));+ })));}}}@@ -1333,13 +1341,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "nodeKind","message": msg,- }));+ })));}}}@@ -1372,13 +1380,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": "hasValue","message": msg,- }));+ })));}}}@@ -1416,13 +1424,13 @@ impl ShaclValidator {} else {message.clone()};- violations.push(serde_json::json!({+ violations.push(attribute(&shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,"constraint": key,"message": msg,- }));+ })));}}}@@ -1460,30 +1468,29 @@ impl ShaclValidator {continue;}- // Focus nodes for this shape. Blank nodes are excluded because they- // cannot be named in a VALUES clause; excluding them is recorded- // rather than assumed harmless.+ // Focus nodes for this shape, read back as terms. Blank nodes are+ // included. They used to be excluded because a blank node cannot+ // be named in a VALUES clause; there is no VALUES clause any more.let focus_rows = graph_sparql_select(graph,&format!("SELECT ?this WHERE {{ {this_pattern} }}"),)?;- let focus_nodes: Vec<String> = focus_rows- .iter()- .filter_map(|r| r.get("this"))- .filter(|t| t.starts_with('<'))- .cloned()- .collect();- let blank_focus = focus_rows.len() - focus_nodes.len();- if blank_focus > 0 {- skipped.push(serde_json::json!({- "shape": strip_angle_brackets(&shape_iri),- "reason": format!(- "{} blank-node focus nodes excluded from sh:sparql evaluation (blank nodes cannot be bound in a VALUES clause)",- blank_focus- ),- }));+ let mut focus_terms: Vec<Term> = Vec::new();+ for row in &focus_rows {+ let Some(t) = row.get("this") else { continue };+ match t.parse::<Term>() {+ Ok(term) => focus_terms.push(term),+ Err(e) => skipped.push(serde_json::json!({+ "shape": strip_angle_brackets(&shape_iri),+ "constraint": "sparql",+ "reason": format!("focus node {t} could not be read back as a term: {e}"),+ })),+ }}- if focus_nodes.is_empty() {+ // Store iteration order is not a contract; the report's is.+ focus_terms.sort_by_key(|t| t.to_string());+ focus_terms.dedup();+ if focus_terms.is_empty() {continue;}@@ -1509,36 +1516,59 @@ impl ShaclValidator {}).unwrap_or_else(|| "Violation".to_string());- // SHACL pre-binds $this to the focus node. Rewrite it to the- // ordinary variable ?this and bind it through a VALUES clause,- // wrapping the author's SELECT as a subquery so that nothing is- // spliced into the middle of their query text.- let inner = select_raw.replace("$this", "?this");- // The author's own PREFIX and BASE declarations have to be lifted- // out of the subquery position and put where SPARQL allows them,- // or the wrapper cannot parse at all. See split_sparql_prologue.- let (author_prologue, inner_body) = split_sparql_prologue(&inner);- let values = focus_nodes.join(" ");- let wrapped = format!(- "{prefix_block}{author_prologue}SELECT ?this WHERE \- {{ VALUES ?this {{ {values} }} {{ {inner_body} }} }}"- );-- match graph_sparql_select(graph, &wrapped) {- Ok(rows) => {- for row in &rows {- if let Some(focus) = row.get("this") {- let msg = if message.is_empty() {- "SPARQL constraint violated".to_string()- } else {- message.clone()- };- violations.push(serde_json::json!({+ // SHACL-SPARQL 5.3.2: `$this` is pre-bound to the focus node,+ // one evaluation per node. Pre-binding is SPARQL substitution,+ // so the term is in scope inside FILTER (NOT) EXISTS and inside+ // subqueries, which is what makes a constraint written as+ // `FILTER NOT EXISTS { $this ... }` mean "this node lacks" and+ // not "every node lacks".+ //+ // The previous evaluation wrapped the author's SELECT as a+ // subquery under a VALUES clause. A subquery is evaluated+ // bottom-up with no outer variable in scope, so `$this` was+ // unbound inside it, the filter asked whether ANY node matched+ // the pattern, and the single empty solution either survived+ // (joining with every focus node) or died (reporting none).+ // One clean record therefore hid every dirty one (#132).+ //+ // `$this` and `?this` name the same variable in SPARQL, so the+ // author's text runs as written, behind the prefixes the+ // shapes graph declares. An author prologue of its own is+ // legal after those: the grammar allows any number of+ // PREFIX and BASE declarations in any order.+ let query = format!("{prefix_block}{select_raw}");+ match graph.sparql_select_union_prebound(&query, "this", &focus_terms) {+ Ok(per_focus) => {+ for (focus, rows) in focus_terms.iter().zip(per_focus) {+ let focus_str = focus.to_string();+ for row in rows {+ // 5.3.2 again: a bound ?message overrides+ // sh:message, ?path is sh:resultPath and+ // ?value is sh:value.+ let msg = row+ .get("message")+ .map(|m| strip_quotes(m))+ .filter(|m| !m.is_empty())+ .unwrap_or_else(|| {+ if message.is_empty() {+ "SPARQL constraint violated".to_string()+ } else {+ message.clone()+ }+ });+ let mut v = serde_json::json!({"severity": severity,- "focus_node": strip_angle_brackets(focus),+ "focus_node": strip_angle_brackets(&focus_str),"constraint": "sparql","message": msg,- }));+ });+ if let Some(p) = row.get("path") {+ v["result_path"] = serde_json::json!(strip_angle_brackets(p));+ }+ if let Some(val) = row.get("value") {+ v["value"] = serde_json::json!(strip_angle_brackets(val));+ }+ violations.push(attribute(&shape_iri, v));}}}@@ -2182,57 +2212,6 @@ fn datatype_is_indistinguishable_in_store(datatype: &str) -> bool {)}-/// Split a SPARQL query into its prologue (PREFIX and BASE declarations, with any-/// leading comments) and the body that follows.-///-/// SPARQL permits PREFIX and BASE only in the prologue, at the very start of a-/// query. SHACL pre-binds `$this`, and this validator binds it by wrapping the-/// author's SELECT as a subquery under a VALUES clause, which puts any prologue-/// the author wrote into a position where it cannot parse and takes the whole-/// query down with it. Hoisting it to the front of the wrapper is the fix.-///-/// Declaring prefixes inside `sh:select` is the portable way to write a SPARQL-/// constraint and is what pyshacl accepts. All seven `sh:sparql` constraints in-/// the banking vertical were being reported as unrunnable for this reason alone,-/// found by the differential run against pyshacl rather than by any unit test.-fn split_sparql_prologue(query: &str) -> (String, &str) {- let mut prologue = String::new();- let mut rest = query;- loop {- let trimmed = rest.trim_start();- if let Some(line) = trimmed.strip_prefix('#') {- // A comment carries no meaning to the parser but may carry a lot to- // a reader, so it is moved rather than dropped.- let end = line.find('\n').map(|i| i + 2).unwrap_or(trimmed.len());- prologue.push_str(&trimmed[..end]);- rest = &trimmed[end..];- continue;- }- let lower = trimmed.to_ascii_lowercase();- let keyword_len = if lower.starts_with("prefix") {- 6- } else if lower.starts_with("base") {- 4- } else {- return (prologue, trimmed);- };- // Require a separator after the keyword so an identifier merely starting- // with those letters is not mistaken for a declaration.- match trimmed[keyword_len..].chars().next() {- Some(c) if c.is_whitespace() || c == '<' => {}- _ => return (prologue, trimmed),- }- // Every declaration ends at the closing '>' of its IRI.- match trimmed.find('>') {- Some(i) => {- prologue.push_str(&trimmed[..=i]);- prologue.push('\n');- rest = &trimmed[i + 1..];- }- None => return (prologue, trimmed),- }- }-}/// The SPARQL pattern selecting the focus nodes of one target, bound to `var`.///@@ -2281,6 +2260,45 @@ fn count_focus_nodes(graph: &Arc<GraphStore>, focus_pattern: &str) -> anyhow::Re.unwrap_or(0))}+/// Name the shape and the constraint component that produced a violation.+///+/// The W3C validation report vocabulary carries `sh:sourceShape` and+/// `sh:sourceConstraintComponent` on every result so that a consumer can tell+/// which rule fired. Without them a report written as one shape per rule, the+/// natural form of a findings table, could only be attributed by parsing an+/// identifier back out of `sh:message` (#131). `result_path` mirrors `path`+/// under the vocabulary's name wherever a path is known; the keys consumers+/// already read are left as they were.+fn attribute(shape: &str, mut v: serde_json::Value) -> serde_json::Value {+ v["source_shape"] = serde_json::Value::String(strip_angle_brackets(shape));+ let constraint = v["constraint"].as_str().unwrap_or("").to_string();+ v["source_constraint_component"] =+ serde_json::Value::String(constraint_component(&constraint));+ if v.get("result_path").is_none()+ && let Some(path) = v.get("path").cloned()+ {+ v["result_path"] = path;+ }+ v+}++/// The `sh:*ConstraintComponent` IRI for the short constraint name a violation+/// carries in `constraint`. Every W3C core component is the parameter name with+/// its first letter capitalised, except SPARQL.+fn constraint_component(constraint: &str) -> String {+ let local = constraint.strip_prefix("sh:").unwrap_or(constraint);+ let name = if local.eq_ignore_ascii_case("sparql") {+ "SPARQL".to_string()+ } else {+ let mut chars = local.chars();+ match chars.next() {+ Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),+ None => String::new(),+ }+ };+ format!("http://www.w3.org/ns/shacl#{name}ConstraintComponent")+}+fn strip_angle_brackets(s: &str) -> String {let s = s.trim();if s.starts_with('<') && s.ends_with('>') {diff --git a/tests/shacl_source_shape_test.rs b/tests/shacl_source_shape_test.rsnew file mode 100644index 00000000..91b2e0cf--- /dev/null+++ b/tests/shacl_source_shape_test.rs@@ -0,0 +1,153 @@+//! Regression tests for issue #131.+//!+//! Violation objects carried `constraint`, `focus_node`, `message` and+//! `severity` and nothing that named the shape that produced them. A report+//! built as one shape per rule, which is how a findings table is written, could+//! not be attributed except by parsing free text out of `message`. The W3C+//! validation report vocabulary carries `sh:sourceShape` and+//! `sh:sourceConstraintComponent` on every result for exactly this reason.+//!+//! The existing keys are unchanged; three are added: `source_shape`,+//! `source_constraint_component`, and `result_path` wherever a path is known.++use open_ontologies::graph::GraphStore;+use open_ontologies::shacl::ShaclValidator;+use std::sync::Arc;++const SH: &str = "http://www.w3.org/ns/shacl#";++fn store() -> Arc<GraphStore> {+ let store = Arc::new(GraphStore::new());+ let ttl = r#"+ @prefix ex: <http://example.org/> .+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .+ ex:w a ex:Widget ; ex:size 99 ; ex:colour "red" ; ex:part ex:notAPart ; ex:tag "a" , "b" .+ ex:notAPart a ex:Other .+ "#;+ store.load_turtle(ttl, None).unwrap();+ store+}++fn report(store: &Arc<GraphStore>, shapes: &str) -> serde_json::Value {+ serde_json::from_str(&ShaclValidator::validate(store, shapes).unwrap()).unwrap()+}++/// One shape per rule, the pattern the issue describes.+const SHAPES: &str = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .++ ex:MinCountRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:missing ; sh:minCount 1 ] .+ ex:MaxCountRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:tag ; sh:maxCount 1 ] .+ ex:DatatypeRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:size ; sh:datatype xsd:string ] .+ ex:ClassRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:part ; sh:class ex:Part ] .+ ex:PatternRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:colour ; sh:pattern "^blue$" ] .+ ex:InRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:colour ; sh:in ( "blue" "green" ) ] .+ ex:NodeKindRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:colour ; sh:nodeKind sh:IRI ] .+ ex:HasValueRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:colour ; sh:hasValue "blue" ] .+ ex:MaxInclusiveRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:size ; sh:maxInclusive 10 ] .+ ex:MinLengthRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:colour ; sh:minLength 10 ] .+ ex:SparqlRule a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:sparql [ sh:message "too big" ;+ sh:select "SELECT $this WHERE { $this <http://example.org/size> ?s . FILTER(?s > 10) }" ] .+"#;++fn violations_of(r: &serde_json::Value, shape: &str) -> Vec<serde_json::Value> {+ r["violations"]+ .as_array()+ .unwrap()+ .iter()+ .filter(|v| v["source_shape"] == shape)+ .cloned()+ .collect()+}++#[test]+fn every_violation_names_its_source_shape_and_component() {+ let r = report(&store(), SHAPES);+ let violations = r["violations"].as_array().unwrap();+ assert!(violations.len() >= 11, "expected one violation per rule at least: {r}");+ for v in violations {+ let shape = v["source_shape"].as_str().unwrap_or_else(|| panic!("no source_shape: {v}"));+ assert!(shape.starts_with("http://example.org/"), "source_shape is the shape IRI: {v}");+ let component = v["source_constraint_component"]+ .as_str()+ .unwrap_or_else(|| panic!("no source_constraint_component: {v}"));+ assert!(+ component.starts_with(SH) && component.ends_with("ConstraintComponent"),+ "component must be a sh: constraint component IRI: {v}"+ );+ // The keys the issue asked to keep are still there.+ for key in ["constraint", "focus_node", "message", "severity"] {+ assert!(v.get(key).is_some(), "existing key {key} must survive: {v}");+ }+ if let Some(path) = v.get("path") {+ assert_eq!(v["result_path"], *path, "result_path mirrors path: {v}");+ }+ }+}++#[test]+fn components_are_the_w3c_names() {+ let r = report(&store(), SHAPES);+ let expect = [+ ("http://example.org/MinCountRule", "MinCountConstraintComponent", Some("http://example.org/missing")),+ ("http://example.org/MaxCountRule", "MaxCountConstraintComponent", Some("http://example.org/tag")),+ ("http://example.org/DatatypeRule", "DatatypeConstraintComponent", Some("http://example.org/size")),+ ("http://example.org/ClassRule", "ClassConstraintComponent", Some("http://example.org/part")),+ ("http://example.org/PatternRule", "PatternConstraintComponent", Some("http://example.org/colour")),+ ("http://example.org/InRule", "InConstraintComponent", Some("http://example.org/colour")),+ ("http://example.org/NodeKindRule", "NodeKindConstraintComponent", Some("http://example.org/colour")),+ ("http://example.org/HasValueRule", "HasValueConstraintComponent", Some("http://example.org/colour")),+ ("http://example.org/MaxInclusiveRule", "MaxInclusiveConstraintComponent", Some("http://example.org/size")),+ ("http://example.org/MinLengthRule", "MinLengthConstraintComponent", Some("http://example.org/colour")),+ ("http://example.org/SparqlRule", "SPARQLConstraintComponent", None),+ ];+ for (shape, component, path) in expect {+ let vs = violations_of(&r, shape);+ assert!(!vs.is_empty(), "{shape} produced no violation: {r}");+ for v in &vs {+ assert_eq!(v["source_constraint_component"], format!("{SH}{component}"), "{v}");+ if let Some(p) = path {+ assert_eq!(v["result_path"], p, "{v}");+ }+ }+ }+}++#[test]+fn the_shape_is_attributed_even_when_messages_are_identical() {+ // The failure the issue describes: two rules with the same message were+ // indistinguishable. Now the shape key tells them apart.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:RuleA a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:sparql [ sh:message "cell not evidenced" ;+ sh:select "SELECT $this WHERE { FILTER NOT EXISTS { $this <http://example.org/a> ?x } }" ] .+ ex:RuleB a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:sparql [ sh:message "cell not evidenced" ;+ sh:select "SELECT $this WHERE { FILTER NOT EXISTS { $this <http://example.org/b> ?x } }" ] .+ "#;+ let r = report(&store(), shapes);+ assert_eq!(r["violation_count"], 2, "{r}");+ let mut shapes_seen: Vec<String> = r["violations"]+ .as_array()+ .unwrap()+ .iter()+ .map(|v| v["source_shape"].as_str().unwrap().to_string())+ .collect();+ shapes_seen.sort();+ assert_eq!(shapes_seen, vec!["http://example.org/RuleA", "http://example.org/RuleB"]);+}diff --git a/tests/shacl_sparql_prebinding_test.rs b/tests/shacl_sparql_prebinding_test.rsnew file mode 100644index 00000000..78ee2a6d--- /dev/null+++ b/tests/shacl_sparql_prebinding_test.rs@@ -0,0 +1,140 @@+//! Regression tests for issue #132.+//!+//! A `sh:sparql` constraint was evaluated by wrapping the author's SELECT as a+//! subquery under a `VALUES ?this { ... }` clause. SPARQL evaluates a subquery+//! bottom-up with no outer variable in scope, so inside it `$this` was unbound:+//! a `FILTER NOT EXISTS { $this ... }` asked "does ANY node satisfy this", the+//! single empty solution either survived (joining with every focus node, so+//! every node was reported) or died (so none was). One clean record therefore+//! hid every dirty one. SHACL-SPARQL section 5.3 says `$this` is pre-bound per+//! focus node; Oxigraph's `substitute_variable` is that mechanism.++use open_ontologies::graph::GraphStore;+use open_ontologies::shacl::ShaclValidator;+use std::sync::Arc;++const RECORDS: &str = r#"+ @prefix ex: <http://example.org/> .+ ex:r1 a ex:Record ; ex:has ex:e1 .+ ex:e1 ex:field ex:F ; ex:status "present" .+ ex:r2 a ex:Record ; ex:has ex:e2 .+ ex:e2 ex:field ex:F ; ex:status "absent" .+ ex:r3 a ex:Record .+"#;++const NEEDS_F: &str = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:NeedsF a sh:NodeShape ; sh:targetClass ex:Record ;+ sh:sparql [ sh:message "record lacks a present F" ;+ sh:select """SELECT $this WHERE { FILTER NOT EXISTS {+ $this <http://example.org/has> ?e .+ ?e <http://example.org/field> <http://example.org/F> ;+ <http://example.org/status> "present" . } }""" ] .+"#;++fn store(ttl: &str) -> Arc<GraphStore> {+ let store = Arc::new(GraphStore::new());+ store.load_turtle(ttl, None).unwrap();+ store+}++fn report(store: &Arc<GraphStore>, shapes: &str) -> serde_json::Value {+ serde_json::from_str(&ShaclValidator::validate(store, shapes).unwrap()).unwrap()+}++fn focus_nodes(r: &serde_json::Value) -> Vec<String> {+ let mut v: Vec<String> = r["violations"]+ .as_array()+ .unwrap()+ .iter()+ .map(|x| x["focus_node"].as_str().unwrap().to_string())+ .collect();+ v.sort();+ v+}++#[test]+fn one_clean_record_does_not_hide_the_dirty_ones() {+ // The exact reproduction from issue #132. pyshacl 0.40.1: conforms False,+ // focus nodes r2 and r3.+ let r = report(&store(RECORDS), NEEDS_F);+ assert_eq!(r["conforms"], serde_json::json!(false), "issue #132: {r}");+ assert_eq!(+ focus_nodes(&r),+ vec!["http://example.org/r2".to_string(), "http://example.org/r3".to_string()],+ "{r}"+ );+ assert_eq!(r["violation_count"], 2);+}++#[test]+fn every_dirty_record_is_still_reported_when_none_is_clean() {+ // The degenerate case that made the old evaluation look right.+ let all_dirty = RECORDS.replace("\"present\"", "\"absent\"");+ let r = report(&store(&all_dirty), NEEDS_F);+ assert_eq!(r["conforms"], serde_json::json!(false));+ assert_eq!(r["violation_count"], 3, "{r}");+}++#[test]+fn a_clean_data_set_still_conforms() {+ let all_clean = r#"+ @prefix ex: <http://example.org/> .+ ex:r1 a ex:Record ; ex:has ex:e1 .+ ex:e1 ex:field ex:F ; ex:status "present" .+ "#;+ let r = report(&store(all_clean), NEEDS_F);+ assert_eq!(r["conforms"], serde_json::json!(true), "{r}");+ assert_eq!(r["violation_count"], 0);+}++#[test]+fn blank_node_focus_nodes_are_evaluated_not_skipped() {+ // Blank nodes could not be named in a VALUES clause, so they were excluded+ // and the run was marked undetermined. Substitution binds a term, and a+ // blank node is a term.+ let data = r#"+ @prefix ex: <http://example.org/> .+ [] a ex:Record .+ ex:r1 a ex:Record ; ex:has ex:e1 .+ ex:e1 ex:field ex:F ; ex:status "present" .+ "#;+ let r = report(&store(data), NEEDS_F);+ assert_eq!(r["conforms"], serde_json::json!(false), "{r}");+ assert_eq!(r["violation_count"], 1, "{r}");+ assert!(+ r["violations"][0]["focus_node"].as_str().unwrap().starts_with("_:"),+ "the blank node is the violator: {r}"+ );+ assert!(r["skipped_constraints"].is_null(), "nothing was skipped: {r}");+}++#[test]+fn solution_bindings_flow_into_the_result() {+ // SHACL-SPARQL 5.3.2: ?value becomes sh:value, ?path becomes sh:resultPath+ // and ?message overrides the constraint's sh:message.+ let data = r#"+ @prefix ex: <http://example.org/> .+ ex:big a ex:Widget ; ex:size 99 .+ ex:small a ex:Widget ; ex:size 1 .+ "#;+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:sparql [ sh:message "oversized" ;+ sh:select """SELECT $this ?value ?path ?message WHERE {+ $this <http://example.org/size> ?value .+ BIND(<http://example.org/size> AS ?path)+ BIND(CONCAT("too big: ", STR(?value)) AS ?message)+ FILTER(?value > 10) }""" ] .+ "#;+ let r = report(&store(data), shapes);+ assert_eq!(r["violation_count"], 1, "{r}");+ let v = &r["violations"][0];+ assert_eq!(v["focus_node"], "http://example.org/big");+ assert!(v["value"].as_str().unwrap().contains("99"), "{v}");+ assert_eq!(v["result_path"], "http://example.org/size");+ assert_eq!(v["message"], "too big: 99");+}