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: lint reads any RDF serialisation; batch accepts vocab-check

Fabio Rovaia7e612cec767
diff --git a/src/batch.rs b/src/batch.rs
index f606ead4..18949c4a 100644
--- a/src/batch.rs
+++ b/src/batch.rs
@@ -99,7 +99,10 @@ impl BatchRunner {
"lint" => self.exec_lint(&cmd.args),
"reason" => self.exec_reason(&cmd.args),
"shacl" => self.exec_shacl(&cmd.args),
- "vocab_check" => self.exec_vocab_check(&cmd.args),
+ // The CLI subcommand is spelled with a hyphen and this arm accepted
+ // only the underscore, so every documented invocation was rejected
+ // as an unknown batch command while the implementation sat unused.
+ "vocab-check" | "vocab_check" => self.exec_vocab_check(&cmd.args),
"diff" => self.exec_diff(&cmd.args),
"convert" => self.exec_convert(&cmd.args),
"enforce" => self.exec_enforce(&cmd.args),
@@ -223,7 +226,7 @@ impl BatchRunner {
Some(p) => p,
None => return json!({"error": "lint requires a file path"}),
};
- match std::fs::read_to_string(input) {
+ match crate::graph::GraphStore::read_as_turtle(input) {
Ok(content) => {
let result = OntologyService::lint_with_feedback(&content, Some(&self.db))
.unwrap_or_else(|e| format!(r#"{{"error":"{}"}}"#, e));
@@ -259,14 +262,25 @@ impl BatchRunner {
}
}
+ /// Closed-world vocabulary check against the loaded ontology.
+ ///
+ /// This function existed and was never reachable: no arm of the dispatch
+ /// match called it, so `batch` rejected `vocab-check` as an unknown command.
+ /// That mattered more than an ordinary gap, because the check needs a loaded
+ /// ontology, `load` does not persist in the default memory storage mode, and
+ /// `batch` is the only way to load and then operate in one process. The
+ /// command was therefore unreachable in its intended mode.
fn exec_vocab_check(&self, args: &[String]) -> Value {
- let data_path = match args.first() {
+ let data_path = match args.iter().find(|a| !a.starts_with("--")) {
Some(p) => p,
- None => return json!({"error": "vocab_check requires a data file path"}),
+ None => return json!({"error": "vocab-check requires a data file path"}),
};
- match std::fs::read_to_string(data_path) {
+ let extra: Vec<String> = Self::flag_value(args, "--namespaces")
+ .map(|v| v.split(',').map(|s| s.trim().to_string()).collect())
+ .unwrap_or_default();
+ match crate::graph::GraphStore::read_as_turtle(data_path) {
Ok(data) => {
- let result = crate::vocab_check::check_data_vocab(&self.graph, &data, &[])
+ let result = crate::vocab_check::check_data_vocab(&self.graph, &data, &extra)
.unwrap_or_else(|e| format!(r#"{{"error":"{}"}}"#, e));
serde_json::from_str(&result).unwrap_or(json!({"raw": result}))
}
diff --git a/src/graph.rs b/src/graph.rs
index c99b55ac..545baf49 100644
--- a/src/graph.rs
+++ b/src/graph.rs
@@ -199,6 +199,45 @@ impl GraphStore {
Self::count_parsed(parser)
}
+ /// Read a file and hand back Turtle, whatever it was serialised as.
+ ///
+ /// `validate` has always sniffed the format; `lint` read the bytes and
+ /// handed them to a Turtle parser, so it failed on every RDF/XML document
+ /// with a parse error from the wrong parser. Since OBO Foundry publishes
+ /// RDF/XML, that made `lint` unusable on 62 of the 64 real ontologies in a
+ /// census of the most-declared vocabularies in the public metabolomics
+ /// record. Both commands now share this.
+ pub fn read_as_turtle(path: &str) -> anyhow::Result<String> {
+ let content = std::fs::read_to_string(path)?;
+ Self::content_as_turtle(path, content)
+ }
+
+ /// As `read_as_turtle`, for content already in hand (stdin, for instance).
+ 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 {
+ return Ok(content);
+ }
+ let store = Self::new();
+ {
+ let inner = store.store.lock().unwrap();
+ let base = std::fs::canonicalize(path_hint)
+ .ok()
+ .and_then(|abs| abs.to_str().map(|s| format!("file://{s}")));
+ 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;
+ }
+ let quads: Vec<_> = parser
+ .for_reader(Cursor::new(content.as_bytes()))
+ .collect::<Result<_, _>>()?;
+ for quad in &quads {
+ inner.insert(quad)?;
+ }
+ }
+ store.serialize("turtle")
+ }
+
pub fn validate_file(path: &str) -> anyhow::Result<ValidationCounts> {
let content = std::fs::read_to_string(path)?;
let format = Self::detect_format_sniffed(path, &content);
diff --git a/src/main.rs b/src/main.rs
index 8a2ff229..1ec9d464 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1880,13 +1880,18 @@ async fn async_main() -> anyhow::Result<()> {
Commands::Lint { input } => {
use open_ontologies::ontology::OntologyService;
let (db, _graph) = setup(&cli.data_dir)?;
- let content = if input == "-" {
+ let raw = if input == "-" {
let mut buf = String::new();
std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)?;
buf
} else {
std::fs::read_to_string(&input)?
};
+ // lint reasons over Turtle. Sniff the serialisation first, exactly as
+ // validate does, so an RDF/XML document is converted rather than
+ // handed to a Turtle parser that can only fail on it.
+ let content = GraphStore::content_as_turtle(&input, raw)
+ .unwrap_or_else(|e| format!("# could not read as RDF: {e}\n"));
let result = OntologyService::lint_with_feedback(&content, Some(&db))
.unwrap_or_else(|e| format!(r#"{{"error":"{}"}}"#, e));
output_result(&result, cli.pretty);