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
Stop the SHACL validator guessing, truncating, and answering for shapes it was not asked about
FABIOTESSd6636158fed4
diff --git a/src/shacl.rs b/src/shacl.rsindex 8e7a566e..7aab8cb4 100644--- a/src/shacl.rs+++ b/src/shacl.rs@@ -1,6 +1,6 @@use crate::graph::GraphStore;use oxigraph::io::{RdfFormat, RdfParser};-use oxigraph::model::Term;+use oxigraph::model::{Term, Variable};use oxigraph::sparql::{QueryResults, SparqlEvaluator};use oxigraph::store::Store;use std::collections::{HashMap, HashSet};@@ -274,13 +274,26 @@ impl ShaclValidator {// 3. Find property constraints for this shapelet shape_iri = shape_term.clone();+ // Bound into the queries below rather than spliced into their text.+ // See `query_solutions_bound`: a blank node shape spliced as+ // `_:label` is a wildcard, not a reference.+ let shape_node = match shape_iri.parse::<Term>() {+ Ok(t) => t,+ Err(e) => {+ skipped.push(serde_json::json!({+ "shape": strip_angle_brackets(&shape_iri),+ "reason": format!("shape term could not be read back as a term: {e}"),+ }));+ continue;+ }+ };- let props = query_solutions(+ let props = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>- SELECT ?prop ?path ?invPath ?minCount ?maxCount ?datatype ?class ?pattern ?hasValue ?nodeKind ?minInclusive ?maxInclusive ?minExclusive ?maxExclusive ?minLength ?maxLength ?lessThan ?lessThanOrEquals ?node ?message ?severity WHERE {{+ SELECT ?shape ?prop ?path ?invPath ?minCount ?maxCount ?datatype ?class ?pattern ?hasValue ?nodeKind ?minInclusive ?maxInclusive ?minExclusive ?maxExclusive ?minLength ?maxLength ?lessThan ?lessThanOrEquals ?node ?message ?severity WHERE {{{} sh:property ?prop .?prop sh:path ?path .OPTIONAL {{ ?path sh:inversePath ?invPath }}@@ -304,8 +317,10 @@ impl ShaclValidator {OPTIONAL {{ ?prop sh:severity ?severity }}}}"#,- shape_iri+ "?shape"),+ "shape",+ &shape_node,)?;// Any constraint predicate on a property shape that this implementation@@ -313,12 +328,12 @@ impl ShaclValidator {// `sh:not` was invisible: it was never collected, never evaluated, and// never recorded, so a shape whose only constraint was `sh:not` returned// `conforms: true` over data that violated it.- let unknown = query_solutions(+ let unknown = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>- SELECT DISTINCT ?pred WHERE {{+ SELECT DISTINCT ?shape ?pred WHERE {{{} sh:property ?prop .?prop ?pred ?o .FILTER(?pred NOT IN (@@ -337,8 +352,10 @@ impl ShaclValidator {))}}"#,- shape_iri+ "?shape"),+ "shape",+ &shape_node,)?;for row in &unknown {if let Some(pred) = row.get("pred") {@@ -367,13 +384,13 @@ impl ShaclValidator {// and the run came back CLEAN. Same defect in sh:in and sh:not.let mut or_alternatives: HashMap<String, Vec<String>> = HashMap::new();let mut or_unsupported: HashSet<String> = HashSet::new();- let or_rows = query_solutions(+ let or_rows = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>- SELECT ?prop ?path ?datatype ?class ?hasValue ?other WHERE {{+ SELECT ?shape ?prop ?path ?datatype ?class ?hasValue ?other WHERE {{{} sh:property ?prop .?prop sh:path ?path .?prop sh:or/rdf:rest*/rdf:first ?member .@@ -386,8 +403,10 @@ impl ShaclValidator {}}}}"#,- shape_iri+ "?shape"),+ "shape",+ &shape_node,)?;let mut or_paths: HashMap<String, String> = HashMap::new();for row in &or_rows {@@ -445,13 +464,13 @@ impl ShaclValidator {// and 198 real violations were reported as a clean run.let mut not_clauses: HashMap<String, Vec<String>> = HashMap::new();let mut not_unsupported: HashSet<String> = HashSet::new();- let not_rows = query_solutions(+ let not_rows = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>- SELECT ?prop ?path ?datatype ?class ?hasValue ?pattern ?other WHERE {{+ SELECT ?shape ?prop ?path ?datatype ?class ?hasValue ?pattern ?other WHERE {{{} sh:property ?prop .?prop sh:path ?path .?prop sh:not ?inner .@@ -467,8 +486,10 @@ impl ShaclValidator {}}}}"#,- shape_iri+ "?shape"),+ "shape",+ &shape_node,)?;let mut not_paths: HashMap<String, String> = HashMap::new();for row in ¬_rows {@@ -533,13 +554,13 @@ impl ShaclValidator {// and grouped by the member's printed term, the same way property// shapes are handled. That confines this form to one level: a member// that itself nests sh:node is not compiled, and says so.- let node_or_rows = query_solutions(+ let node_or_rows = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>- SELECT ?member ?path ?minCount ?maxCount ?class ?datatype ?nodeKind ?hasValue ?other WHERE {{+ SELECT ?shape ?member ?path ?minCount ?maxCount ?class ?datatype ?nodeKind ?hasValue ?other WHERE {{{} sh:or/rdf:rest*/rdf:first ?member .?member sh:property ?prop .?prop sh:path ?path .@@ -561,8 +582,10 @@ impl ShaclValidator {}}}}"#,- shape_term+ "?shape"),+ "shape",+ &shape_node,)?;if !node_or_rows.is_empty() {let mut per_member: HashMap<String, Vec<String>> = HashMap::new();@@ -675,7 +698,7 @@ impl ShaclValidator {} else {node_message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&shape_iri, &shape_iri, serde_json::json!({"severity": "Violation","focus_node": strip_angle_brackets(focus),"constraint": "or",@@ -699,13 +722,13 @@ impl ShaclValidator {// Binding ?prop keeps two blocks distinct even when they share a path// and a nested class, which DISTINCT over the other columns would fold// together.- let qualified_rows = query_solutions(+ let qualified_rows = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>- SELECT ?prop ?path ?class ?datatype ?hasValue ?other ?qmin ?qmax ?message ?severity WHERE {{+ SELECT ?shape ?prop ?path ?class ?datatype ?hasValue ?other ?qmin ?qmax ?message ?severity WHERE {{{} sh:property ?prop .?prop sh:path ?path ; sh:qualifiedValueShape ?q .OPTIONAL {{ ?q sh:class ?class }}@@ -721,13 +744,16 @@ impl ShaclValidator {OPTIONAL {{ ?prop sh:severity ?severity }}}}"#,- shape_iri+ "?shape"),+ "shape",+ &shape_node,)?;for row in &qualified_rows {let Some(path_raw) = row.get("path") else {continue;};+ let q_shape = row.get("prop").cloned().unwrap_or_else(|| shape_iri.clone());let q_path = strip_angle_brackets(path_raw);let q_message = row.get("message").map(|m| strip_quotes(m)).unwrap_or_default();let q_severity = row@@ -799,7 +825,7 @@ impl ShaclValidator {} else {q_message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&q_shape, &shape_iri, serde_json::json!({"severity": q_severity,"focus_node": strip_angle_brackets(focus),"path": q_path,@@ -813,20 +839,22 @@ impl ShaclValidator {// sh:in alternatives, collected per shape and keyed by path for the// same blank-node reason as sh:or above.let mut in_alternatives: HashMap<String, Vec<String>> = HashMap::new();- let in_rows = query_solutions(+ let in_rows = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>- SELECT ?prop ?path ?member WHERE {{+ SELECT ?shape ?prop ?path ?member WHERE {{{} sh:property ?prop .?prop sh:path ?path .?prop sh:in/rdf:rest*/rdf:first ?member .}}"#,- shape_iri+ "?shape"),+ "shape",+ &shape_node,)?;for row in &in_rows {if let (Some(p), Some(m)) = (row.get("prop"), row.get("member")) {@@ -844,6 +872,11 @@ impl ShaclValidator {// by this term; keying them by path merged sibling blocks into// one and returned a clean run over data that broke both.let prop_key = prop.get("prop").cloned();+ // The shape that carries these constraints, for `source_shape`.+ // A blank property shape reports its blank label, which is what+ // pyshacl does and is still more identity than collapsing every+ // sibling onto the node shape's IRI.+ let constraint_shape = prop_key.clone().unwrap_or_else(|| shape_iri.clone());let raw_path = match prop.get("path") {Some(p) => strip_angle_brackets(p),None => continue,@@ -906,7 +939,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -940,7 +973,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -975,7 +1008,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1024,7 +1057,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1056,7 +1089,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1098,7 +1131,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1144,7 +1177,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1187,7 +1220,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1223,7 +1256,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1265,7 +1298,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1296,7 +1329,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1341,7 +1374,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1380,7 +1413,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1424,7 +1457,7 @@ impl ShaclValidator {} else {message.clone()};- violations.push(attribute(&shape_iri, serde_json::json!({+ violations.push(attribute(&constraint_shape, &shape_iri, serde_json::json!({"severity": severity,"focus_node": strip_angle_brackets(focus),"path": path,@@ -1448,22 +1481,70 @@ impl ShaclValidator {for (shape_term, kind, target_value) in &targets {let this_pattern = target_pattern(kind, target_value, "this");let shape_iri = shape_term.clone();+ let shape_node = match shape_iri.parse::<Term>() {+ Ok(t) => t,+ Err(e) => {+ skipped.push(serde_json::json!({+ "shape": strip_angle_brackets(&shape_iri),+ "constraint": "sparql",+ "reason": format!("shape term could not be read back as a term: {e}"),+ }));+ continue;+ }+ };- let constraints = query_solutions(+ let constraints = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>- SELECT ?select ?message ?severity WHERE {{+ SELECT ?shape ?c ?select ?message ?severity ?deactivated WHERE {{{} sh:sparql ?c .?c sh:select ?select .OPTIONAL {{ ?c sh:message ?message }}OPTIONAL {{ ?c sh:severity ?severity }}+ OPTIONAL {{ ?c sh:deactivated ?deactivated }}}}"#,- shape_iri+ "?shape"),+ "shape",+ &shape_node,)?;++ // A predicate on the constraint node that this validator does not+ // implement has to reach `skipped_constraints` like any other, or+ // the module's stated invariant ("every constraint is either+ // executed or recorded") holds only for the places someone+ // remembered. There was no complement over `sh:SPARQLConstraint`+ // nodes at all, so any `sh:` predicate written there was invisible.+ for row in &query_solutions_bound(+ &shapes_store,+ &format!(+ r#"+ PREFIX sh: <http://www.w3.org/ns/shacl#>+ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>+ SELECT DISTINCT ?shape ?pred WHERE {{+ {} sh:sparql ?c .+ ?c ?pred ?o .+ FILTER(STRSTARTS(STR(?pred), "http://www.w3.org/ns/shacl#") && ?pred NOT IN (+ sh:select, sh:message, sh:severity, sh:deactivated, sh:prefixes+ ))+ }}+ "#,+ "?shape"+ ),+ "shape",+ &shape_node,+ )? {+ if let Some(pred) = row.get("pred") {+ skipped.push(serde_json::json!({+ "shape": strip_angle_brackets(&shape_iri),+ "constraint": strip_angle_brackets(pred),+ "reason": "predicate on a sh:sparql constraint node is not implemented; it was not evaluated",+ }));+ }+ }if constraints.is_empty() {continue;}@@ -1494,13 +1575,36 @@ impl ShaclValidator {continue;}- let prefix_block = sparql_prefix_block(&shapes_store)?;-for constraint in &constraints {let select_raw = match constraint.get("select") {Some(s) => strip_quotes(s),None => continue,};+ // SHACL 5.3: "There are no validation results if the+ // SPARQL-based constraint has true as a value for the property+ // sh:deactivated." The predicate was honoured on node shapes+ // and on property shapes and ignored here, so a switched-off+ // constraint ran anyway and the report came back with a+ // confident `conforms: false` over conforming data. Read by+ // value, so any lexical spelling of true counts.+ if constraint+ .get("deactivated")+ .map(|d| strip_quotes(d).eq_ignore_ascii_case("true"))+ .unwrap_or(false)+ {+ continue;+ }+ let prefix_block = match sparql_prologue(&shapes_store, constraint.get("c"))? {+ Prologue::Declarations(block) => block,+ Prologue::Ambiguous(reason) => {+ skipped.push(serde_json::json!({+ "shape": strip_angle_brackets(&shape_iri),+ "constraint": "sparql",+ "reason": reason,+ }));+ continue;+ }+ };let message = constraint.get("message").map(|m| strip_quotes(m))@@ -1568,7 +1672,7 @@ impl ShaclValidator {if let Some(val) = row.get("value") {v["value"] = serde_json::json!(strip_angle_brackets(val));}- violations.push(attribute(&shape_iri, v));+ violations.push(attribute(&shape_iri, &shape_iri, v));}}}@@ -1617,6 +1721,21 @@ impl ShaclValidator {let nothing_matched = (!targets.is_empty() && focus_nodes_total == 0)|| (targets.is_empty() && declared_any_shape);+ // A validation report is a SET of results. One shape may carry two+ // target declarations that select the same node (a `sh:targetClass` and+ // a `sh:targetNode` for one of its instances), and the loop above walks+ // targets, so that node was checked twice and every violation on it was+ // reported twice. Two results identical in every field are one result.+ //+ // This does not touch `focus_nodes`, which is still a sum over targets+ // and so still counts such a node twice. Making that a distinct count+ // means enumerating focus nodes rather than counting them in SPARQL,+ // which is a memory trade on large graphs and a separate decision.+ {+ let mut seen: HashSet<String> = HashSet::new();+ violations.retain(|v| seen.insert(v.to_string()));+ }+let mut report = serde_json::json!({"violation_count": violations.len(),"violations": violations,@@ -1709,6 +1828,9 @@ impl ShaclValidator {Some(s) => s.clone(),None => continue,};+ let Ok(shape_node) = shape_iri.parse::<Term>() else {+ continue;+ };let target_class = match shape.get("targetClass") {Some(tc) => strip_angle_brackets(tc),None => continue,@@ -1727,20 +1849,22 @@ impl ShaclValidator {}));}- let props = query_solutions(+ let props = query_solutions_bound(&shapes_store,&format!(r#"PREFIX sh: <http://www.w3.org/ns/shacl#>- SELECT ?prop ?path ?class ?datatype WHERE {{+ SELECT ?shape ?prop ?path ?class ?datatype WHERE {{{} sh:property ?prop .?prop sh:path ?path .OPTIONAL {{ ?prop sh:class ?class }}OPTIONAL {{ ?prop sh:datatype ?datatype }}}}"#,- shape_iri+ "?shape"),+ "shape",+ &shape_node,)?;let mut prop_reports: Vec<serde_json::Value> = Vec::new();@@ -1830,6 +1954,53 @@ impl ShaclValidator {/// Run a SPARQL SELECT against a temporary shapes `Store` and return results/// as a vec of maps (variable name -> string value).+/// Run a query against the shapes graph with `var` pre-bound to `term`.+///+/// The shape being examined used to be spliced into the query text. For an+/// IRI that is harmless; for a shape written `[] a sh:NodeShape` the printed+/// term is `_:label`, and a blank node label in a SPARQL query body is a+/// NON-DISTINGUISHED VARIABLE, not a reference to that node. `_:b0 sh:property+/// ?prop` therefore means "anything that has a property shape", so every blank+/// node shape in a file collected every property shape in that file and+/// applied all of them to its own targets. Two `[] a sh:NodeShape` blocks with+/// disjoint targets reported conforming data as non-conforming.+///+/// Substitution binds a term, and a blank node is a term. This is the same+/// mechanism `$this` pre-binding uses for focus nodes, applied to the other+/// place this validator was splicing terms into query text.+fn query_solutions_bound(+ store: &Store,+ query: &str,+ var: &str,+ term: &Term,+) -> anyhow::Result<Vec<HashMap<String, String>>> {+ let prepared = SparqlEvaluator::new()+ .parse_query(query)?+ .substitute_variable(Variable::new(var)?, term.clone());+ match prepared.on_store(store).execute()? {+ QueryResults::Solutions(solutions) => {+ 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 = HashMap::new();+ for v in &vars {+ if let Some(t) = solution.get(v.as_str()) {+ row.insert(v.clone(), t.to_string());+ }+ }+ rows.push(row);+ }+ Ok(rows)+ }+ _ => Ok(Vec::new()),+ }+}+fn query_solutions(store: &Store,query: &str,@@ -1862,7 +2033,7 @@ fn query_solutions(}}-/// Run a SPARQL SELECT against the main `GraphStore` and return results+/// Run a SPARQL SELECT ?shape against the main `GraphStore` and return results/// as a vec of maps, using the existing `sparql_select` JSON output.////// Every data-side query in this module runs over the union of every graph in@@ -2269,8 +2440,19 @@ fn count_focus_nodes(graph: &Arc<GraphStore>, focus_pattern: &str) -> anyhow::Re/// 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));+///+/// `source_shape` is the shape that CARRIES the constraint, which for a+/// constraint under `sh:property` is the property shape and not the node shape+/// containing it. That distinction is the whole point of the field: two+/// property shapes on one path under one node shape are exactly the case #131+/// was filed about, and naming the node shape for both leaves them as+/// indistinguishable as they were before. pyshacl returns the property shape+/// here, and this now agrees with it. The enclosing node shape is still+/// reported, under `node_shape`, because it is what selected the focus node+/// and a consumer that wants to group by rule set needs it.+fn attribute(source_shape: &str, node_shape: &str, mut v: serde_json::Value) -> serde_json::Value {+ v["source_shape"] = serde_json::Value::String(strip_angle_brackets(source_shape));+ v["node_shape"] = serde_json::Value::String(strip_angle_brackets(node_shape));let constraint = v["constraint"].as_str().unwrap_or("").to_string();v["source_constraint_component"] =serde_json::Value::String(constraint_component(&constraint));@@ -2308,23 +2490,33 @@ fn strip_angle_brackets(s: &str) -> String {}}-/// Trim quotes and handle typed literals like `"1"^^<http://...>`.+/// The lexical value of a term the store printed.+///+/// The store renders terms in N-Triples form, so a literal arrives quoted, with+/// its escapes still written as escapes and its datatype or language tag+/// appended. Recovering the value is the parser's job, not string surgery's.+///+/// The previous implementation searched the WHOLE string for `^^` and for `"@`+/// and truncated there, so any value that CONTAINED those two characters was+/// cut. A SHACL-SPARQL constraint comparing against a typed literal, which is+/// to say most date and numeric constraints anyone writes+/// (`FILTER(?d < "2025-01-01"^^xsd:date)`), was chopped mid-query, failed to+/// parse, and was recorded as a constraint that could not be executed with a+/// parse error that blamed the author. The verdict then came back `null` with+/// zero violations and exit 0, so the rule most likely to be written was the+/// rule most likely never to run. `sh:pattern` and `sh:message` were corrupted+/// by the same mechanism. Pinned by `tests/shacl_literal_value_test.rs`.fn strip_quotes(s: &str) -> String {let s = s.trim();- // Handle typed literals: "value"^^<datatype>- let s = if let Some(idx) = s.find("^^") {- &s[..idx]- } else {- s- };- // Handle language-tagged literals: "value"@en- let s = if let Some(idx) = s.find("\"@") {- &s[..idx + 1]- } else {- s- };- let s = s.trim_matches('"');- unescape_literal(s)+ match s.parse::<Term>() {+ Ok(Term::Literal(lit)) => lit.value().to_string(),+ // An IRI or a blank node has no quoting to undo.+ Ok(_) => s.to_string(),+ // Not a term at all. Keep the old best-effort reading rather than+ // return the input untouched, because a caller that parsed a number+ // out of it still needs the quotes gone.+ Err(_) => unescape_literal(s.trim_matches('"')),+ }}/// Undo the N-Triples escaping that Oxigraph applies when rendering a literal@@ -2373,30 +2565,95 @@ fn unescape_literal(s: &str) -> String {/// Build a SPARQL PREFIX block from any `sh:declare` blocks in the shapes graph.///-/// SHACL lets a `sh:sparql` constraint reference prefixed names and point at its-/// prefix declarations with `sh:prefixes`. Rather than resolve that pointer-/// strictly, every declaration present in the shapes graph is collected, which is-/// permissive but never wrong: an unused PREFIX line changes no result, whereas a-/// missing one turns an executable constraint into an unevaluated one.-fn sparql_prefix_block(shapes_store: &Store) -> anyhow::Result<String> {- let rows = query_solutions(- shapes_store,- r#"- PREFIX sh: <http://www.w3.org/ns/shacl#>- SELECT ?prefix ?namespace WHERE {- ?decl sh:prefix ?prefix ; sh:namespace ?namespace .- }- "#,- )?;- let mut block = String::new();+/// What a resolved prefix prologue can be.+enum Prologue {+ /// PREFIX lines, sorted so that one shapes graph always yields one prologue.+ Declarations(String),+ /// One prefix bound to two namespaces in the set that applies here. Running+ /// the constraint would mean guessing which binding the author meant, and+ /// guessing wrong returns a clean report over data that violates the rule.+ Ambiguous(String),+}++/// The PREFIX prologue for one `sh:sparql` constraint.+///+/// SHACL 5.2.1 scopes a constraint's prefix declarations: they are the ones+/// reachable from its own `sh:prefixes` values, through `owl:imports*` and+/// `sh:declare`. This used to merge EVERY `sh:declare` in the shapes graph and+/// never read `sh:prefixes` at all, which is a false-clean generator. Two+/// declaration sets binding one prefix to different namespaces emitted two+/// `PREFIX p:` lines; SPARQL takes the last, the winner was decided by store+/// row order, and the constraint pointing at the loser matched nothing and+/// reported `conforms: true` with no `skipped_constraints` entry. Two shapes+/// files that are the same RDF graph could give opposite verdicts.+///+/// So: when the constraint says which declarations it wants, use exactly those.+/// When it does not, keep the permissive whole-graph merge, because shapes in+/// the wild rely on it, but refuse to guess when that merge is ambiguous. A+/// refusal is `conforms: null` and a recorded reason, which is the failure+/// direction this module exists to keep.+fn sparql_prologue(+ shapes_store: &Store,+ constraint_term: Option<&String>,+) -> anyhow::Result<Prologue> {+ let scoped = constraint_term.map(|c| {+ format!(+ r#"+ PREFIX sh: <http://www.w3.org/ns/shacl#>+ PREFIX owl: <http://www.w3.org/2002/07/owl#>+ SELECT DISTINCT ?prefix ?namespace WHERE {{+ {c} sh:prefixes ?ont .+ ?ont owl:imports* ?src .+ ?src sh:declare ?decl .+ ?decl sh:prefix ?prefix ; sh:namespace ?namespace .+ }}+ "#+ )+ });+ let mut rows = match &scoped {+ Some(q) => query_solutions(shapes_store, q)?,+ None => Vec::new(),+ };+ // No `sh:prefixes`, or one that resolves to nothing: fall back to the whole+ // shapes graph, which is what every shapes file written against this engine+ // so far has relied on.+ if rows.is_empty() {+ rows = query_solutions(+ shapes_store,+ r#"+ PREFIX sh: <http://www.w3.org/ns/shacl#>+ SELECT DISTINCT ?prefix ?namespace WHERE {+ ?decl sh:prefix ?prefix ; sh:namespace ?namespace .+ }+ "#,+ )?;+ }++ let mut bindings: std::collections::BTreeMap<String, std::collections::BTreeSet<String>> =+ std::collections::BTreeMap::new();for row in &rows {if let (Some(prefix), Some(namespace)) = (row.get("prefix"), row.get("namespace")) {- block.push_str(&format!(- "PREFIX {}: <{}>\n",- strip_quotes(prefix),- strip_angle_brackets(&strip_quotes(namespace))- ));+ bindings+ .entry(strip_quotes(prefix))+ .or_default()+ .insert(strip_angle_brackets(&strip_quotes(namespace)));+ }+ }+ if let Some((prefix, namespaces)) = bindings.iter().find(|(_, ns)| ns.len() > 1) {+ return Ok(Prologue::Ambiguous(format!(+ "prefix {}: is declared with {} different namespaces ({}) in the declarations that \+ apply to this constraint, so the query cannot be resolved without guessing. Point the \+ constraint at one declaration set with sh:prefixes.",+ prefix,+ namespaces.len(),+ namespaces.iter().cloned().collect::<Vec<_>>().join(", ")+ )));+ }+ let mut block = String::new();+ for (prefix, namespaces) in &bindings {+ for namespace in namespaces {+ block.push_str(&format!("PREFIX {prefix}: <{namespace}>\n"));}}- Ok(block)+ Ok(Prologue::Declarations(block))}diff --git a/tests/shacl_audit_regressions_test.rs b/tests/shacl_audit_regressions_test.rsnew file mode 100644index 00000000..4767902d--- /dev/null+++ b/tests/shacl_audit_regressions_test.rs@@ -0,0 +1,265 @@+//! Defects found by an adversarial audit of the SHACL validator, 13 Sep 2026.+//!+//! All four predate the `sh:sparql` pre-binding work and were found while+//! auditing it. Each is pinned here against the answer pyshacl 0.40.1 gives on+//! the same input, which is the oracle `tools/shacl_differential.py` uses.+//!+//! 1. `strip_quotes` searched the whole string for the literal suffix markers+//! `^^` and `"@` and truncated there, so a constraint comparing against a+//! typed literal was cut mid-query and never ran.+//! 2. A blank-node node shape was spliced into SPARQL as `_:label`, which is+//! a non-distinguished variable, so it collected every property shape in+//! the file and reported conforming data as non-conforming.+//! 3. `sh:deactivated true` was honoured on node and property shapes and+//! ignored on a `sh:sparql` constraint node.+//! 4. Every `sh:declare` in the shapes graph was merged into one prologue and+//! `sh:prefixes` was never read, so a prefix bound twice resolved by store+//! row order. That is a false clean: the constraint pointing at the losing+//! binding matched nothing and the run reported `conforms: true`.++use open_ontologies::graph::GraphStore;+use open_ontologies::shacl::ShaclValidator;+use std::sync::Arc;++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()+}++const DATED: &str = r#"+ @prefix ex: <http://example.org/> .+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .+ ex:a a ex:Thing ; ex:when "2020-01-01"^^xsd:date .+ ex:b a ex:Thing ; ex:when "2030-01-01"^^xsd:date .+"#;++#[test]+fn a_constraint_comparing_against_a_typed_literal_runs() {+ // pyshacl: conforms False, one result, focus ex:a.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:sparql [ sh:message "date is in the past" ;+ sh:select """SELECT $this WHERE {+ $this <http://example.org/when> ?d .+ FILTER(?d < "2025-01-01"^^<http://www.w3.org/2001/XMLSchema#date>) }""" ] .+ "#;+ let r = report(&store(DATED), shapes);+ assert!(+ r["skipped_constraints"].is_null(),+ "the query is valid SPARQL and must not be reported as unrunnable: {r}"+ );+ assert_eq!(r["conforms"], serde_json::json!(false), "{r}");+ assert_eq!(r["violation_count"], 1, "{r}");+ assert_eq!(r["violations"][0]["focus_node"], "http://example.org/a");+}++#[test]+fn a_constraint_comparing_against_a_language_tagged_literal_runs() {+ let data = r#"+ @prefix ex: <http://example.org/> .+ ex:a a ex:Thing ; ex:label "hello"@en .+ "#;+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:sparql [ sh:message "english label" ;+ sh:select """SELECT $this WHERE {+ $this <http://example.org/label> ?l . FILTER(?l = "hello"@en) }""" ] .+ "#;+ let r = report(&store(data), shapes);+ assert!(r["skipped_constraints"].is_null(), "{r}");+ assert_eq!(r["violation_count"], 1, "{r}");+}++#[test]+fn a_pattern_containing_the_literal_suffix_markers_survives() {+ // The same helper served sh:pattern. `^^` is a plausible regex.+ let data = r#"+ @prefix ex: <http://example.org/> .+ ex:a a ex:Thing ; ex:code "x" .+ "#;+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:property [ sh:path ex:code ; sh:pattern "^[a-z]@[a-z]$" ] .+ "#;+ let r = report(&store(data), shapes);+ assert_eq!(r["conforms"], serde_json::json!(false), "the pattern must not match: {r}");+ assert_eq!(r["violation_count"], 1, "{r}");+}++#[test]+fn two_blank_node_shapes_do_not_borrow_each_others_constraints() {+ // pyshacl: conforms True. Each blank shape applies only its own property+ // shape to its own target.+ let data = r#"+ @prefix ex: <http://example.org/> .+ ex:a a ex:Alpha ; ex:name "n" .+ ex:b a ex:Beta ; ex:code "c" .+ "#;+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ [] a sh:NodeShape ; sh:targetClass ex:Alpha ; sh:property [ sh:path ex:name ; sh:minCount 1 ] .+ [] a sh:NodeShape ; sh:targetClass ex:Beta ; sh:property [ sh:path ex:code ; sh:minCount 1 ] .+ "#;+ let r = report(&store(data), shapes);+ assert_eq!(+ r["conforms"],+ serde_json::json!(true),+ "a blank node shape must not collect the other shape's constraints: {r}"+ );+ assert_eq!(r["violation_count"], 0, "{r}");+}++#[test]+fn a_blank_node_shape_still_enforces_its_own_constraint() {+ // The other direction, so the fix cannot pass by evaluating nothing.+ let data = r#"+ @prefix ex: <http://example.org/> .+ ex:a a ex:Alpha .+ "#;+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ [] a sh:NodeShape ; sh:targetClass ex:Alpha ; sh:property [ sh:path ex:name ; sh:minCount 1 ] .+ "#;+ let r = report(&store(data), shapes);+ assert_eq!(r["conforms"], serde_json::json!(false), "{r}");+ assert_eq!(r["violation_count"], 1, "{r}");+}++#[test]+fn a_deactivated_sparql_constraint_produces_no_results() {+ // SHACL 5.3: "There are no validation results if the SPARQL-based+ // constraint has true as a value for the property sh:deactivated."+ // pyshacl: conforms True.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:sparql [ sh:deactivated true ; sh:message "off" ;+ sh:select "SELECT $this WHERE { $this a <http://example.org/Thing> }" ] .+ "#;+ let r = report(&store(DATED), shapes);+ assert_eq!(r["conforms"], serde_json::json!(true), "{r}");+ assert_eq!(r["violation_count"], 0, "{r}");+ assert!(r["skipped_constraints"].is_null(), "deactivated is honoured, not skipped: {r}");+}++#[test]+fn a_live_sparql_constraint_still_fires() {+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:sparql [ sh:deactivated false ; sh:message "on" ;+ sh:select "SELECT $this WHERE { $this a <http://example.org/Thing> }" ] .+ "#;+ let r = report(&store(DATED), shapes);+ assert_eq!(r["conforms"], serde_json::json!(false), "{r}");+ assert_eq!(r["violation_count"], 2, "{r}");+}++#[test]+fn an_unimplemented_predicate_on_a_constraint_node_is_recorded() {+ // There was no complement over sh:sparql constraint nodes at all, so any+ // sh: predicate written there was invisible to the "every constraint is+ // either executed or recorded" invariant.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:sparql [ sh:message "x" ; sh:nodeKind sh:IRI ;+ sh:select "SELECT $this WHERE { $this a <http://example.org/Nothing> }" ] .+ "#;+ let r = report(&store(DATED), shapes);+ assert!(r["conforms"].is_null(), "an unevaluated constraint suppresses the verdict: {r}");+ let skipped = r["skipped_constraints"].as_array().unwrap();+ assert!(+ skipped.iter().any(|e| e["constraint"]+ .as_str()+ .unwrap_or("")+ .ends_with("nodeKind")),+ "{r}"+ );+}++const FLAGGED: &str = r#"+ @prefix ex: <http://example.org/> .+ ex:a a ex:Thing ; ex:flagged "yes" .+"#;++#[test]+fn a_prefix_bound_twice_is_refused_rather_than_guessed() {+ // Both declaration sets are in the shapes graph and the constraint names+ // neither, so which binding wins would be decided by store row order. The+ // old behaviour emitted two PREFIX lines, SPARQL took the last, and the+ // constraint pointing at the loser matched nothing and reported+ // `conforms: true` with nothing recorded. A false clean.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ @prefix other: <http://other.example/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:sparql [ sh:message "flagged" ;+ sh:select "SELECT $this WHERE { $this p:flagged ?v }" ] .+ ex:decls sh:declare [ sh:prefix "p" ; sh:namespace "http://example.org/"^^<http://www.w3.org/2001/XMLSchema#anyURI> ] .+ other:decls sh:declare [ sh:prefix "p" ; sh:namespace "http://unrelated.example/"^^<http://www.w3.org/2001/XMLSchema#anyURI> ] .+ "#;+ let r = report(&store(FLAGGED), shapes);+ assert!(+ r["conforms"].is_null(),+ "an ambiguous prologue must not resolve to a verdict: {r}"+ );+ let skipped = r["skipped_constraints"].as_array().unwrap();+ assert!(+ skipped.iter().any(|e| e["reason"].as_str().unwrap_or("").contains("different namespaces")),+ "the reason must name the ambiguity: {r}"+ );+}++#[test]+fn sh_prefixes_selects_the_declarations_that_apply() {+ // The same collision, but the constraint says which set it means. SHACL+ // 5.2.1 scopes declarations this way, and scoping resolves the ambiguity.+ // pyshacl: conforms False, one result.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ @prefix other: <http://other.example/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:sparql [ sh:message "flagged" ; sh:prefixes ex:decls ;+ sh:select "SELECT $this WHERE { $this p:flagged ?v }" ] .+ ex:decls sh:declare [ sh:prefix "p" ; sh:namespace "http://example.org/"^^<http://www.w3.org/2001/XMLSchema#anyURI> ] .+ other:decls sh:declare [ sh:prefix "p" ; sh:namespace "http://unrelated.example/"^^<http://www.w3.org/2001/XMLSchema#anyURI> ] .+ "#;+ let r = report(&store(FLAGGED), shapes);+ assert_eq!(r["conforms"], serde_json::json!(false), "{r}");+ assert_eq!(r["violation_count"], 1, "{r}");+}++#[test]+fn the_whole_graph_prologue_still_serves_shapes_that_declare_no_prefixes() {+ // Backwards compatibility: no sh:prefixes and no collision keeps working.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;+ sh:sparql [ sh:message "flagged" ;+ sh:select "SELECT $this WHERE { $this p:flagged ?v }" ] .+ ex:decls sh:declare [ sh:prefix "p" ; sh:namespace "http://example.org/"^^<http://www.w3.org/2001/XMLSchema#anyURI> ] .+ "#;+ let r = report(&store(FLAGGED), shapes);+ assert_eq!(r["conforms"], serde_json::json!(false), "{r}");+ assert_eq!(r["violation_count"], 1, "{r}");+}diff --git a/tests/shacl_source_shape_test.rs b/tests/shacl_source_shape_test.rsindex 91b2e0cf..634c457e 100644--- a/tests/shacl_source_shape_test.rs+++ b/tests/shacl_source_shape_test.rs@@ -7,8 +7,16 @@//! 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.+//! `source_shape` is the shape that CARRIES the constraint. For a constraint+//! under `sh:property` that is the property shape, not the node shape holding+//! it, and the difference is the whole point: two property shapes on one path+//! under one node shape are precisely the case the issue describes. The first+//! version of this fix named the node shape for all fifteen property-borne+//! constraint sites, which left that case as unattributable as it was before,+//! and every test here was one-rule-per-node-shape and could not see it.+//! `two_property_shapes_on_one_path_are_told_apart` is that missing test.+//!+//! The enclosing node shape is still reported, under `node_shape`.use open_ontologies::graph::GraphStore;use open_ontologies::shacl::ShaclValidator;@@ -20,7 +28,6 @@ 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 ."#;@@ -32,32 +39,29 @@ 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.+/// One named property shape per rule, so `source_shape` is meaningful rather+/// than a blank label.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:WidgetShape a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property ex:MinCountRule , ex:MaxCountRule , ex:DatatypeRule , ex:ClassRule ,+ ex:PatternRule , ex:InRule , ex:NodeKindRule , ex:HasValueRule ,+ ex:MaxInclusiveRule , ex:MinLengthRule .++ ex:MinCountRule sh:path ex:missing ; sh:minCount 1 .+ ex:MaxCountRule sh:path ex:tag ; sh:maxCount 1 .+ ex:DatatypeRule sh:path ex:size ; sh:datatype xsd:string .+ ex:ClassRule sh:path ex:part ; sh:class ex:Part .+ ex:PatternRule sh:path ex:colour ; sh:pattern "^blue$" .+ ex:InRule sh:path ex:colour ; sh:in ( "blue" "green" ) .+ ex:NodeKindRule sh:path ex:colour ; sh:nodeKind sh:IRI .+ ex:HasValueRule sh:path ex:colour ; sh:hasValue "blue" .+ ex:MaxInclusiveRule sh:path ex:size ; sh:maxInclusive 10 .+ ex:MinLengthRule 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) }" ] .@@ -81,6 +85,10 @@ fn every_violation_names_its_source_shape_and_component() {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}");+ assert!(+ v["node_shape"].as_str().unwrap_or("").starts_with("http://example.org/"),+ "the enclosing node shape is reported too: {v}"+ );let component = v["source_constraint_component"].as_str().unwrap_or_else(|| panic!("no source_constraint_component: {v}"));@@ -126,10 +134,45 @@ fn components_are_the_w3c_names() {}}+#[test]+fn two_property_shapes_on_one_path_are_told_apart() {+ // The case #131 was filed about, and the one the first fix could not+ // handle: both rules sit under ONE node shape and constrain ONE path.+ // pyshacl 0.40.1 returns ex:RuleA and ex:RuleB here.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:WidgetShape a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property ex:RuleA , ex:RuleB .+ ex:RuleA sh:path ex:colour ; sh:pattern "^blue$" ; sh:message "cell not evidenced" .+ ex:RuleB sh:path ex:colour ; sh:minLength 10 ; sh:message "cell not evidenced" .+ "#;+ let r = report(&store(), shapes);+ assert_eq!(r["violation_count"], 2, "{r}");+ let mut seen: Vec<String> = r["violations"]+ .as_array()+ .unwrap()+ .iter()+ .map(|v| v["source_shape"].as_str().unwrap().to_string())+ .collect();+ seen.sort();+ assert_eq!(+ seen,+ vec!["http://example.org/RuleA", "http://example.org/RuleB"],+ "two rules on one path under one node shape must be distinguishable: {r}"+ );+ for v in r["violations"].as_array().unwrap() {+ assert_eq!(+ v["node_shape"], "http://example.org/WidgetShape",+ "the enclosing node shape is still reported: {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.+ // Two node shapes carrying one sh:sparql each. Here the shape that carries+ // the constraint IS the node shape, so that is what source_shape names.let shapes = r#"@prefix sh: <http://www.w3.org/ns/shacl#> .@prefix ex: <http://example.org/> .@@ -151,3 +194,29 @@ fn the_shape_is_attributed_even_when_messages_are_identical() {shapes_seen.sort();assert_eq!(shapes_seen, vec!["http://example.org/RuleA", "http://example.org/RuleB"]);}++#[test]+fn a_blank_property_shape_reports_its_blank_label_not_the_node_shape() {+ // pyshacl reports the blank property shape here. A blank label is less+ // useful than an IRI and is still strictly more identity than collapsing+ // every sibling onto the node shape, which is what the first fix did.+ let shapes = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:WidgetShape a sh:NodeShape ; sh:targetClass ex:Widget ;+ sh:property [ sh:path ex:colour ; sh:pattern "^blue$" ] ;+ sh:property [ sh:path ex:colour ; sh:minLength 10 ] .+ "#;+ let r = report(&store(), shapes);+ assert_eq!(r["violation_count"], 2, "{r}");+ let seen: std::collections::HashSet<String> = r["violations"]+ .as_array()+ .unwrap()+ .iter()+ .map(|v| v["source_shape"].as_str().unwrap().to_string())+ .collect();+ assert_eq!(seen.len(), 2, "the two blank property shapes must not collapse: {r}");+ for s in &seen {+ assert!(s.starts_with("_:"), "a blank property shape reports a blank label: {s}");+ }+}