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(diff): compare canonical blank node identities

Rudy Celekli69c9d5f820e9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d864085..9c190bff 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -46,6 +46,11 @@ All notable changes to Open Ontologies are documented here.
### Fixed
+- Compare canonical blank node identities in ontology diffs, so unchanged
+ anonymous restrictions do not appear as added and removed triples. Anonymous
+ structure remains visible when it changes; canonical IDs can shift under edits.
+
+
- **The embedding model is loaded once per process rather than once per HTTP
session** (#262). The HTTP arm hands the same server constructor to
`StreamableHttpService::new` as a per-session factory, so everything that
diff --git a/src/ontology.rs b/src/ontology.rs
index e1f33ee9..0da4b3bf 100644
--- a/src/ontology.rs
+++ b/src/ontology.rs
@@ -61,29 +61,25 @@ impl OntologyService {
/// Diff two ontologies. Returns added/removed triples.
pub fn diff(old_content: &str, new_content: &str) -> anyhow::Result<String> {
- let old_store = Store::new()?;
- let new_store = Store::new()?;
-
- let old_reader = Cursor::new(old_content.as_bytes());
- for quad in RdfParser::from_format(RdfFormat::Turtle).for_reader(old_reader) {
- old_store.insert(&quad?)?;
- }
-
- let new_reader = Cursor::new(new_content.as_bytes());
- for quad in RdfParser::from_format(RdfFormat::Turtle).for_reader(new_reader) {
- new_store.insert(&quad?)?;
- }
+ let old_raw = GraphStore::new();
+ let new_raw = GraphStore::new();
+ old_raw.load_turtle(old_content, None)?;
+ new_raw.load_turtle(new_content, None)?;
+ // Parse-local blank node IDs are not identity across snapshots.
+ // Preserve anonymous structure using the same RDFC 1.0 helper as drift.
+ // Canonical IDs can shift under edits; this is not a minimal edit script.
+ let old_store = old_raw.canonicalize_blank_nodes()?;
+ let new_store = new_raw.canonicalize_blank_nodes()?;
let old_triples: HashSet<String> = old_store
- .iter()
- .filter_map(|q| q.ok())
- .map(|q| format!("{} {} {}", q.subject, q.predicate, q.object))
+ .all_triples()?
+ .into_iter()
+ .map(|(s, p, o)| format!("{s} {p} {o}"))
.collect();
-
let new_triples: HashSet<String> = new_store
- .iter()
- .filter_map(|q| q.ok())
- .map(|q| format!("{} {} {}", q.subject, q.predicate, q.object))
+ .all_triples()?
+ .into_iter()
+ .map(|(s, p, o)| format!("{s} {p} {o}"))
.collect();
let added: Vec<&String> = new_triples.difference(&old_triples).collect();
diff --git a/tests/ontology_diff_blank_node_test.rs b/tests/ontology_diff_blank_node_test.rs
new file mode 100644
index 00000000..8b184540
--- /dev/null
+++ b/tests/ontology_diff_blank_node_test.rs
@@ -0,0 +1,98 @@
+mod common;
+
+use open_ontologies::ontology::OntologyService;
+
+const ANONYMOUS: &str = r#"
+@prefix ex: <https://example.org/> .
+@prefix owl: <http://www.w3.org/2002/07/owl#> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+ex:Order a owl:Class;
+ rdfs:subClassOf [ a owl:Restriction; owl:onProperty ex:quantity;
+ owl:someValuesFrom <http://www.w3.org/2001/XMLSchema#integer> ] .
+"#;
+const EXPLICIT_REORDERED: &str = r#"
+@prefix ex: <https://example.org/> .
+@prefix owl: <http://www.w3.org/2002/07/owl#> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+_:restriction owl:someValuesFrom <http://www.w3.org/2001/XMLSchema#integer>;
+ owl:onProperty ex:quantity; a owl:Restriction .
+ex:Order rdfs:subClassOf _:restriction; a owl:Class .
+"#;
+
+fn report(old: &str, new: &str) -> serde_json::Value {
+ serde_json::from_str(&OntologyService::diff(old, new).unwrap()).unwrap()
+}
+
+fn assert_no_changes(value: &serde_json::Value) {
+ assert_eq!(value["added"], 0);
+ assert_eq!(value["removed"], 0);
+ assert_eq!(value["added_triples"], serde_json::json!([]));
+ assert_eq!(value["removed_triples"], serde_json::json!([]));
+}
+
+#[test]
+fn anonymous_restrictions_and_equivalent_reordered_nodes_have_no_diff() {
+ assert_no_changes(&report(ANONYMOUS, ANONYMOUS));
+ assert_no_changes(&report(ANONYMOUS, EXPLICIT_REORDERED));
+ assert_no_changes(&report(EXPLICIT_REORDERED, ANONYMOUS));
+}
+
+#[test]
+fn anonymous_property_changes_remain_visible() {
+ let changed = ANONYMOUS.replace("owl:onProperty ex:quantity", "owl:onProperty ex:price");
+ let result = report(ANONYMOUS, &changed);
+ assert_eq!(result["added"], 1);
+ assert_eq!(result["removed"], 1);
+ let added = result["added_triples"][0].as_str().unwrap();
+ let removed = result["removed_triples"][0].as_str().unwrap();
+ assert!(added.starts_with("_:c14n"));
+ assert!(removed.starts_with("_:c14n"));
+ assert!(added.ends_with("<https://example.org/price>"));
+ assert!(removed.ends_with("<https://example.org/quantity>"));
+}
+
+#[test]
+fn named_triple_changes_keep_existing_diff_counts_and_spelling() {
+ let old =
+ "<https://example.org/Order> <http://www.w3.org/2000/01/rdf-schema#label> \"Order\" .";
+ let new = old.replace("\"Order\"", "\"Purchase order\"");
+ assert_no_changes(&report(old, old));
+ let result = report(old, &new);
+ assert_eq!(result["added"], 1);
+ assert_eq!(result["removed"], 1);
+ assert_eq!(
+ result["added_triples"],
+ serde_json::json!([new.trim_end_matches(" .")])
+ );
+ assert_eq!(
+ result["removed_triples"],
+ serde_json::json!([old.trim_end_matches(" .")])
+ );
+}
+
+#[test]
+fn cli_diff_same_file_and_equivalent_anonymous_snapshot_report_no_changes() {
+ let _gate = common::exec_gate();
+ let tmp = tempfile::tempdir().unwrap();
+ let old = tmp.path().join("orders.ttl");
+ let equivalent = tmp.path().join("reordered.ttl");
+ std::fs::write(&old, ANONYMOUS).unwrap();
+ std::fs::write(&equivalent, EXPLICIT_REORDERED).unwrap();
+ for new in [&old, &equivalent] {
+ let output = std::process::Command::new(env!("CARGO_BIN_EXE_open-ontologies"))
+ .args(["--no-connect", "--data-dir"])
+ .arg(tmp.path())
+ .arg("diff")
+ .arg(&old)
+ .arg(new)
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "{}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let result: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
+ assert_no_changes(&result);
+ }
+}