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
fix(graph): resolve local RDF document bases consistently
Rudy Celekli2c082e8f1df3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3d864085..bf3df615 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -46,6 +46,11 @@ All notable changes to Open Ontologies are documented here.### Fixed+- Validate relative RDF references against the local document base, matching+ loading, conversion, and linting. Encode local file URLs so RDF files under paths with+ spaces, percent signs, or fragment characters resolve relative references+ correctly; explicit Turtle and XML bases retain priority.+- **The embedding model is loaded once per process rather than once per HTTPsession** (#262). The HTTP arm hands the same server constructor to`StreamableHttpService::new` as a per-session factory, so everything thatdiff --git a/src/graph.rs b/src/graph.rsindex 8581bf72..79fbea51 100644--- a/src/graph.rs+++ b/src/graph.rs@@ -291,13 +291,11 @@ impl GraphStore {// it, any file using relative IRIs fails to parse at all, which is// most published RDF/XML: LUBM's generated data would not load a// single triple before this.- let base = std::fs::canonicalize(path)- .ok()- .and_then(|abs| abs.to_str().map(|s| format!("file://{s}")));+ let base = Self::file_base_iri(path);// All or nothing: see load_turtle (issue #93).let mut parser = RdfParser::from_format(format);- if let Some(p) = base.as_ref().and_then(|b| parser.clone().with_base_iri(b).ok()) {- parser = p;+ if let Some(base) = base {+ parser = parser.with_base_iri(base)?;}let quads: Vec<_> = parser.for_reader(reader)@@ -344,17 +342,20 @@ impl GraphStore {pub fn content_as_turtle(path_hint: &str, content: String) -> anyhow::Result<String> {let format = Self::detect_format_sniffed(path_hint, &content);if format == RdfFormat::Turtle {+ if let Some(base) = Self::file_base_iri(path_hint) {+ // Supply the document base without rewriting its Turtle. A+ // later @base in the source still overrides this default.+ return Ok(format!("@base <{base}> .\n{content}"));+ }return Ok(content);}let store = Self::new();{let inner = &store.store;- let base = std::fs::canonicalize(path_hint)- .ok()- .and_then(|abs| abs.to_str().map(|s| format!("file://{s}")));+ let base = Self::file_base_iri(path_hint);let mut parser = RdfParser::from_format(format);- if let Some(p) = base.as_ref().and_then(|b| parser.clone().with_base_iri(b).ok()) {- parser = p;+ if let Some(base) = base {+ parser = parser.with_base_iri(base)?;}let quads: Vec<_> = parser.for_reader(Cursor::new(content.as_bytes()))@@ -370,8 +371,19 @@ impl GraphStore {let content = std::fs::read_to_string(path)?;let format = Self::detect_format_sniffed(path, &content);let reader = Cursor::new(content.as_bytes());- let parser = RdfParser::from_format(format).for_reader(reader);- Self::count_parsed(parser)+ let mut parser = RdfParser::from_format(format);+ if let Some(base) = Self::file_base_iri(path) {+ parser = parser.with_base_iri(base)?;+ }+ Self::count_parsed(parser.for_reader(reader))+ }++ /// A local document's default base is its encoded file URL. Building this+ /// by concatenation treats filename characters such as spaces, `#` and `%`+ /// as URI syntax instead of part of the path.+ fn file_base_iri(path: &str) -> Option<String> {+ let absolute = std::fs::canonicalize(path).ok()?;+ reqwest::Url::from_file_path(absolute).ok().map(Into::into)}/// Count what a parser produced, distinguishing statements from triples.diff --git a/tests/rdf_document_base_test.rs b/tests/rdf_document_base_test.rsnew file mode 100644index 00000000..a5fac0d4--- /dev/null+++ b/tests/rdf_document_base_test.rs@@ -0,0 +1,173 @@+mod common;++use open_ontologies::graph::GraphStore;+use open_ontologies::ontology::OntologyService;++const RELATIVE_TTL: &str = "<#Order> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .";+const RELATIVE_XML: &str = r##"<?xml version="1.0"?>+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:owl="http://www.w3.org/2002/07/owl#">+<owl:Class rdf:about="#Order"/>+</rdf:RDF>"##;+const ABSOLUTE_TTL: &str = "<https://example.org/Order> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .";++#[test]+fn validation_accepts_relative_iris_in_turtle_and_rdfxml() {+ let tmp = tempfile::tempdir().unwrap();+ for (name, content) in [("orders.ttl", RELATIVE_TTL), ("orders.rdf", RELATIVE_XML)] {+ let path = tmp.path().join(name);+ std::fs::write(&path, content).unwrap();+ let path = path.to_str().unwrap();+ let counts = GraphStore::validate_file(path).unwrap();+ assert_eq!(counts.triples, 1);+ assert_eq!(counts.statements, 1);+ let report: serde_json::Value =+ serde_json::from_str(&OntologyService::validate_file(path).unwrap()).unwrap();+ assert_eq!(report["valid"], true);+ assert_eq!(report["triple_count"], 1);+ }+}++#[test]+fn encoded_paths_resolve_the_entire_filename_before_the_fragment() {+ let tmp = tempfile::tempdir().unwrap();+ let dir = tmp.path().join("Order Ontology % # café");+ std::fs::create_dir(&dir).unwrap();+ for (name, content) in [("orders.ttl", RELATIVE_TTL), ("orders.rdf", RELATIVE_XML)] {+ let path = dir.join(name);+ std::fs::write(&path, content).unwrap();+ let path_str = path.to_str().unwrap();+ let store = GraphStore::new();+ assert_eq!(store.load_file(path_str).unwrap(), 1);+ let serialized = store.serialize("ntriples").unwrap();+ assert!(serialized.contains("Order%20Ontology%20%25%20%23%20caf%C3%A9/"));+ assert!(+ serialized.contains(&format!("{name}#Order>")),+ "{serialized}"+ );+ assert_eq!(GraphStore::validate_file(path_str).unwrap().triples, 1);+ let normalized = GraphStore::read_as_turtle(path_str).unwrap();+ let parsed = GraphStore::new();+ parsed.load_turtle(&normalized, None).unwrap();+ assert_eq!(parsed.serialize("ntriples").unwrap(), serialized);+ }+}++#[test]+fn explicit_document_bases_override_the_local_file_base() {+ let tmp = tempfile::tempdir().unwrap();+ let turtle = format!("@base <https://example.org/base/> .\n{RELATIVE_TTL}");+ let xml = RELATIVE_XML.replace(+ "<rdf:RDF ",+ "<rdf:RDF xml:base=\"https://example.org/base/\" ",+ );+ for (name, content) in [("explicit.ttl", turtle), ("explicit.rdf", xml)] {+ let path = tmp.path().join(name);+ std::fs::write(&path, content).unwrap();+ let path = path.to_str().unwrap();+ let store = GraphStore::new();+ store.load_file(path).unwrap();+ assert!(+ store+ .serialize("ntriples")+ .unwrap()+ .starts_with("<https://example.org/base/#Order>")+ );+ assert_eq!(GraphStore::validate_file(path).unwrap().triples, 1);+ let normalized = GraphStore::read_as_turtle(path).unwrap();+ let normalized_store = GraphStore::new();+ normalized_store.load_turtle(&normalized, None).unwrap();+ assert_eq!(+ normalized_store.serialize("ntriples").unwrap(),+ store.serialize("ntriples").unwrap()+ );+ }+}++#[test]+fn absolute_iris_and_invalid_document_results_are_preserved() {+ let tmp = tempfile::tempdir().unwrap();+ let path = tmp.path().join("absolute.ttl");+ std::fs::write(&path, ABSOLUTE_TTL).unwrap();+ assert_eq!(+ GraphStore::validate_file(path.to_str().unwrap())+ .unwrap()+ .triples,+ 1+ );+ let store = GraphStore::new();+ store.load_file(path.to_str().unwrap()).unwrap();+ assert!(+ store+ .serialize("ntriples")+ .unwrap()+ .starts_with("<https://example.org/Order>")+ );+ std::fs::write(&path, "this is not RDF").unwrap();+ assert!(GraphStore::validate_file(path.to_str().unwrap()).is_err());+ let report: serde_json::Value =+ serde_json::from_str(&OntologyService::validate_file(path.to_str().unwrap()).unwrap())+ .unwrap();+ assert_eq!(report["valid"], false);+ assert!(!report["errors"].as_array().unwrap().is_empty());+}++#[test]+fn cli_validation_and_conversion_accept_a_normal_folder_with_spaces() {+ let _gate = common::exec_gate();+ let tmp = tempfile::tempdir().unwrap();+ let dir = tmp.path().join("Order Ontology");+ std::fs::create_dir(&dir).unwrap();+ for (name, content) in [("orders.ttl", RELATIVE_TTL), ("orders.rdf", RELATIVE_XML)] {+ let path = dir.join(name);+ std::fs::write(&path, content).unwrap();+ for command in ["validate", "convert", "lint"] {+ let mut process = std::process::Command::new(env!("CARGO_BIN_EXE_open-ontologies"));+ process.args(["--no-connect", "--data-dir"]).arg(tmp.path());+ process.arg(command).arg(&path);+ if command == "convert" {+ process.args(["--to", "ntriples"]);+ }+ let result = process.output().unwrap();+ assert!(+ result.status.success(),+ "{}",+ String::from_utf8_lossy(&result.stdout)+ );+ let stdout = String::from_utf8(result.stdout).unwrap();+ if command == "validate" {+ let report: serde_json::Value = serde_json::from_str(&stdout).unwrap();+ assert_eq!(report["ok"], true);+ assert_eq!(report["triples"], 1);+ } else if command == "lint" {+ let report: serde_json::Value = serde_json::from_str(&stdout).unwrap();+ assert_eq!(report["issue_count"], 2);+ } else {+ assert!(stdout.contains("Order%20Ontology/"));+ assert!(stdout.contains(&format!("{name}#Order>")));+ }+ }+ }+}++#[test]+fn turtle_source_and_comments_are_preserved_and_inline_input_is_unchanged() {+ let tmp = tempfile::tempdir().unwrap();+ let path = tmp.path().join("orders.ttl");+ let content = format!("# A comment\n{RELATIVE_TTL}\n");+ std::fs::write(&path, &content).unwrap();+ let normalized = GraphStore::read_as_turtle(path.to_str().unwrap()).unwrap();+ assert!(normalized.starts_with("@base <file://"));+ assert!(normalized.ends_with(&content));+ assert_eq!(+ GraphStore::content_as_turtle("-", content.clone()).unwrap(),+ content+ );+ assert_eq!(+ GraphStore::content_as_turtle(+ tmp.path().join("missing.ttl").to_str().unwrap(),+ content.clone()+ )+ .unwrap(),+ content+ );+}