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
convert writes OWL 2 functional syntax (to: ofn) (#332)
Fabio Rovai9e73d95126ce
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 69f6a354..ad73c3aa 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -6,6 +6,28 @@ All notable changes to Open Ontologies are documented here.### Added+- **`onto_convert`, the CLI `convert` and `batch` `convert` write OWL 2+ functional syntax (`--to ofn`)** (requested by @noahgift in #322). #311 gave+ the engine an OFN reader and no writer, so handing an ontology to the OWL+ API, ELK, HermiT, Protégé or ROBOT meant converting through another tool, and+ the reader had no round-trip test. `src/ofn_writer.rs` recognises exactly the+ image of the reader's W3C mapping (Table 1, Table 2, Section 2.3) and writes+ one axiom per line, declarations first, each group in byte order, with the+ document's own prefixes. Anonymous expressions are written inline; the only+ printed blank-node labels are anonymous individuals, labelled by RDFC-1.0+ over their blank-node components, so two runs are byte-identical. Every+ triple that maps back to no axiom (an unrecognised blank-node structure, a+ malformed `rdf:List`, `rdf:type rdf:Property`, an OWL 1 `owl:distinctMembers`,+ an `xsd:integer` cardinality the reader would retype) is named and counted in+ `constructs_not_exported` with the reason, never dropped, and a shared+ anonymous expression, which OFN can only write twice, is listed under+ `constructs_not_round_trippable`. MEASURED: `ofn::parse(write_ofn(G))` is+ RDFC-isomorphic to `G`, two parses write identical bytes, and writing the+ re-read graph gives the same bytes again, on `tests/fixtures/ofn/all_constructs.ofn`,+ 17 targeted documents, and 1,550 of 1,550 ORE 2015 pool files of up to 10 MB+ that the reader accepts (5 of the 1,555 are refused by the reader; the 363+ larger files were not run). Conversion reads the document without a store,+ because the store retypes `xsd:nonNegativeInteger` as `xsd:integer`.- **`onto_shacl` and `shacl` write a W3C `sh:ValidationReport` graph**(#318, reported by @noahgift). `report_format: "turtle" | "ntriples"`(`--report-format` on the CLI) returns the report as RDF, carryingdiff --git a/docs/tool-reference.md b/docs/tool-reference.mdindex cd633a9c..e3cb34fb 100644--- a/docs/tool-reference.md+++ b/docs/tool-reference.md@@ -104,7 +104,7 @@ it withheld.| `onto_coevolve_dependency_graph` | Build the shape→OWL-dependency map for a SHACL document. || `onto_communities` | Detect communities in the loaded entity graph and return a SKELETON per community: size, top members by degree, internal relations, and the bridges connecting it to other communities. || `onto_conservative_check` | Does adding these axioms change anything the ontology ALREADY said? Reasons base and base+extension to a fixpoint under one rule table and reports closure(base ∪ extension) minus closure(base), restricted to triples every name of which the base already used. |-| `onto_convert` | Convert an ontology file to turtle, ntriples, rdfxml, nquads or trig. |+| `onto_convert` | Convert an ontology file to turtle, ntriples, rdfxml, nquads, trig or ofn (OWL 2 functional syntax, with every unexported construct named and counted). || `onto_cq_run` | Run a batch of competency questions (CQs) against the loaded ontology (#29). || `onto_cq_verdicts_list` | List all stored verdicts for a CQ id, most-recent first. || `onto_crosswalk` | Look up clinical crosswalk mappings for a code and system (ICD10, SNOMED, MeSH). |diff --git a/src/batch.rs b/src/batch.rsindex 825bc9d0..3e106024 100644--- a/src/batch.rs+++ b/src/batch.rs@@ -860,6 +860,24 @@ impl BatchRunner {if args.len() > 1 { args[1].clone() } else { "turtle".to_string() }});let output = Self::flag_value(args, "--output");+ if crate::ofn_writer::is_ofn_format(&to) {+ return match crate::ofn_writer::convert_file(path) {+ Ok(out) => {+ let mut report = out.report_json();+ if let Some(out_path) = output {+ if let Err(e) = std::fs::write(&out_path, &out.text) {+ return json!({"error": e.to_string()});+ }+ report["path"] = json!(out_path);+ } else {+ report["content_length"] = json!(out.text.len());+ }+ report["ok"] = json!(true);+ report+ }+ Err(e) => json!({"error": e.to_string()}),+ };+ }let store = GraphStore::new();match store.load_file(path) {Ok(_) => match store.serialize(&to) {diff --git a/src/graph.rs b/src/graph.rsindex 616f0026..23683cda 100644--- a/src/graph.rs+++ b/src/graph.rs@@ -7,6 +7,9 @@ use oxigraph::model::*;use oxigraph::sparql::{QueryResults, SparqlEvaluator};use oxigraph::store::Store;+/// A document's quads and the namespace prefixes it declared, in order.+pub type ParsedDocument = (Vec<Quad>, Vec<(String, String)>);+/// How a document is serialised: one of the RDF formats Oxigraph parses, or/// OWL 2 functional syntax, which is not RDF and is read by [`crate::ofn`].#[derive(Debug, Clone, Copy, PartialEq, Eq)]@@ -327,6 +330,20 @@ impl GraphStore {self.store_parsed_quads(&quads, replace)}+ /// A file's quads, as parsed and never stored, with the namespace+ /// prefixes it declared, in the order it declared them.+ ///+ /// A store holds no prefixes, and it also normalises some literals, so a+ /// writer that must give back exactly the document's graph, abbreviated+ /// with the document's own names, reads the document here. The OWL+ /// functional-syntax writer (issue #322) is that writer.+ pub fn read_file_with_prefixes(+ path: &str,+ ) -> anyhow::Result<ParsedDocument> {+ let content = std::fs::read_to_string(path)?;+ Self::parse_document_with_prefixes(path, &content)+ }+/// Parse a whole document, whatever it is serialised as, into quads.////// Every path that reads a FILE goes through here (`load_file`,@@ -336,11 +353,22 @@ impl GraphStore {/// syntax reader existed each of those called the RDF parser directly, and/// adding a format meant finding all of them.fn parse_document(path: &str, content: &str) -> anyhow::Result<Vec<Quad>> {+ Ok(Self::parse_document_with_prefixes(path, content)?.0)+ }++ /// [`Self::parse_document`], keeping the prefixes the parser saw.+ fn parse_document_with_prefixes(+ path: &str,+ content: &str,+ ) -> anyhow::Result<ParsedDocument> {match Self::detect_source_format(path, content) {- SourceFormat::OwlFunctional => Ok(crate::ofn::parse(content)?- .into_iter()- .map(|t| t.in_graph(GraphName::DefaultGraph))- .collect()),+ SourceFormat::OwlFunctional => Ok((+ crate::ofn::parse(content)?+ .into_iter()+ .map(|t| t.in_graph(GraphName::DefaultGraph))+ .collect(),+ crate::ofn::declared_prefixes(content),+ )),SourceFormat::Rdf(format) => {// A document's own location is its default base, per RFC 3986.// Without it, any file using relative IRIs fails to parse at@@ -351,9 +379,16 @@ impl GraphStore {if let Some(base) = Self::file_base_iri(path) {parser = parser.with_base_iri(base)?;}- Ok(parser- .for_reader(Cursor::new(content.as_bytes()))- .collect::<Result<_, _>>()?)+ let mut reader = parser.for_reader(Cursor::new(content.as_bytes()));+ let mut quads = Vec::new();+ for quad in &mut reader {+ quads.push(quad?);+ }+ let prefixes = reader+ .prefixes()+ .map(|(name, iri)| (name.to_string(), iri.to_string()))+ .collect();+ Ok((quads, prefixes))}}}diff --git a/src/inputs.rs b/src/inputs.rsindex 904d15cc..b6be96b9 100644--- a/src/inputs.rs+++ b/src/inputs.rs@@ -17,7 +17,8 @@ pub struct OntoConvertInput {/// Path to the source file: any format onto_load reads, OWL functional/// syntax includedpub path: String,- /// Target format: turtle, ntriples, rdfxml, nquads, trig+ /// Target format: turtle, ntriples, rdfxml, nquads, trig, or ofn (OWL 2+ /// functional syntax, returned as JSON with what it could not export)pub to: String,/// Optional output file path (if omitted, returns content)pub output: Option<String>,diff --git a/src/lib.rs b/src/lib.rsindex 9f4066b9..3e0a1520 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -82,6 +82,9 @@ pub mod monitor;/// OWL 2 Functional-Style Syntax read into the RDF graph the W3C mapping/// assigns it, so `.ofn` files and the OFN-under-`.owl` corpora load.pub mod ofn;+/// OWL 2 functional syntax WRITTEN from an RDF graph: the inverse of [`ofn`],+/// with every triple that maps back to no axiom reported by name (issue #322).+pub mod ofn_writer;pub mod ontology;pub mod ossie;pub mod plan;diff --git a/src/main.rs b/src/main.rsindex 9602cf59..9b2c2c31 100644--- a/src/main.rs+++ b/src/main.rs@@ -386,7 +386,7 @@ enum Commands {Lint { input: String },/// Defects: check the ONTOLOGY against itself, before any data is judged by itDefects { input: String },- /// Convert between RDF formats+ /// Convert between RDF formats, or to OWL 2 functional syntax (`--to ofn`)Convert {path: String,#[arg(long)]@@ -2843,6 +2843,32 @@ async fn async_main() -> anyhow::Result<()> {.unwrap_or_else(|e| format!(r#"{{"error":"{}"}}"#, e));output_result_checked(&result, cli.pretty);}+ Commands::Convert { path, to, output } if open_ontologies::ofn_writer::is_ofn_format(&to) => {+ match open_ontologies::ofn_writer::convert_file(&path) {+ Ok(out) => {+ let mut report = out.report_json();+ if let Some(out_path) = output {+ std::fs::write(&out_path, &out.text)?;+ report["ok"] = serde_json::json!(true);+ report["path"] = serde_json::json!(out_path);+ output_json(&report, cli.pretty);+ } else {+ // The document is stdout, so the report goes to stderr,+ // and only when it has something to say: a pipe into+ // a file stays a valid OFN document, and what it lacks+ // is still printed.+ print!("{}", out.text);+ if !out.not_exported.is_empty() || !out.not_round_trippable.is_empty() {+ eprintln!("{report}");+ }+ }+ }+ Err(e) => {+ output_json(&serde_json::json!({"error": e.to_string()}), cli.pretty);+ std::process::exit(1);+ }+ }+ }Commands::Convert { path, to, output } => {let store = GraphStore::new();match store.load_file(&path) {diff --git a/src/ofn.rs b/src/ofn.rsindex 74268c61..617644b9 100644--- a/src/ofn.rs+++ b/src/ofn.rs@@ -119,7 +119,7 @@ const RDF_PLAIN_LITERAL: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#Plai/// The prefixes every OFN document may use without declaring them. The/// structural specification fixes these five, and the OWL API writes them out/// anyway, so predefining them only matters for hand-written files.-const STANDARD_PREFIXES: &[(&str, &str)] = &[+pub(crate) const STANDARD_PREFIXES: &[(&str, &str)] = &[("owl", "http://www.w3.org/2002/07/owl#"),("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),("rdfs", "http://www.w3.org/2000/01/rdf-schema#"),@@ -330,6 +330,33 @@ pub fn parse(text: &str) -> Result<Vec<Triple>, OfnError> {Ok(p.out)}+/// The `Prefix(name:=<iri>)` declarations at the head of `text`, in the order+/// written, stopping at `Ontology(` or at the first token that is not part of+/// a well-formed declaration.+///+/// [`parse`] returns triples, and a graph has no prefixes, so the names a+/// document chose are lost by the time [`crate::ofn_writer`] sees it. Reading+/// them here, with the same lexer, is what lets an OFN-to-OFN conversion keep+/// `:A` as `:A` instead of spelling every IRI in full.+pub fn declared_prefixes(text: &str) -> Vec<(String, String)> {+ let mut lx = Lexer::new(text);+ let mut out = Vec::new();+ loop {+ let Ok((Tok::Word("Prefix"), _)) = lx.next() else {+ return out;+ };+ let (Ok((Tok::Open, _)), Ok((Tok::Word(name), _)), Ok((Tok::Eq, _)), Ok((Tok::Iri(iri), _)), Ok((Tok::Close, _))) =+ (lx.next(), lx.next(), lx.next(), lx.next(), lx.next())+ else {+ return out;+ };+ match name.strip_suffix(':') {+ Some(n) if !n.contains(':') => out.push((n.to_string(), iri.to_string())),+ _ => return out,+ }+ }+}+// --------------------------------------------------------------------------- //// Tokens// --------------------------------------------------------------------------- //diff --git a/src/ofn_writer.rs b/src/ofn_writer.rsnew file mode 100644index 00000000..b52fdc97--- /dev/null+++ b/src/ofn_writer.rs@@ -0,0 +1,1754 @@+//! An RDF graph written as OWL 2 Functional-Style Syntax (OFN), issue #322.+//!+//! # What this inverts, and why only that+//!+//! [`crate::ofn`] reads OFN into the triples the W3C "OWL 2 Mapping to RDF+//! Graphs" Table 1, Table 2 and Section 2.3 assign it. This module recognises+//! exactly the image of that function and writes the axiom each piece of it+//! came from. It does not implement the full reverse mapping of Section 3,+//! which also accepts OWL 1 spellings (`owl:distinctMembers`), unqualified+//! `rdf:List` typing and other habits of other writers: each of those would+//! come back through the reader as a DIFFERENT graph, so writing it would be a+//! conversion that changes the ontology without saying so. A structure outside+//! the image is reported instead.+//!+//! The acceptance test is therefore `ofn::parse(write_ofn(G))` isomorphic to+//! `G`, measured in `tests/ofn_writer_test.rs`.+//!+//! # Nothing is dropped silently+//!+//! Every triple of the input is either consumed by an axiom in the output or+//! counted in [`OfnOutput::not_exported`] under its predicate (`rdf:type X` for+//! a type triple) with the reason the axiom it belonged to could not be+//! written. That is the rule `constructs_not_exported` states for the+//! first-order export, applied to the one writer that had no report. Two+//! further lists say where the output is NOT faithful even though nothing was+//! left out: an anonymous expression the graph shares between axioms, which+//! OFN can only write twice, and properties whose kind the graph never+//! declared.+//!+//! # Determinism+//!+//! Each axiom is rendered to a string with every anonymous class expression,+//! data range and list written inline, so their blank-node labels never reach+//! the output, and the axioms are then sorted. The only labels that are+//! printed are those of anonymous INDIVIDUALS, and those are RDFC-1.0+//! canonical labels ([`crate::canon`]) computed over the blank-node+//! components the individuals belong to, so two loads of one file, whatever+//! labels the parser handed out, give byte-identical text. A graph whose+//! components exhaust the RDFC budget (scaled with their size, see+//! `label_budget`) is refused by name, because a label chosen any other way+//! would not be canonical and the output would stop being reproducible.++use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};++use oxigraph::model::{BlankNode, GraphName, Literal, NamedNode, NamedOrBlankNode, Quad, Term, Triple};++use crate::canon;++const RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";+const RDFS: &str = "http://www.w3.org/2000/01/rdf-schema#";+const OWL: &str = "http://www.w3.org/2002/07/owl#";+const XSD: &str = "http://www.w3.org/2001/XMLSchema#";+const SWRL: &str = "http://www.w3.org/2003/11/swrl#";++const RDF_TYPE: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";+const RDF_FIRST: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#first";+const RDF_REST: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest";+const RDF_NIL: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil";+const RDF_PLAIN_LITERAL: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral";+const RDFS_DATATYPE: &str = "http://www.w3.org/2000/01/rdf-schema#Datatype";+const XSD_STRING: &str = "http://www.w3.org/2001/XMLSchema#string";+const XSD_BOOLEAN: &str = "http://www.w3.org/2001/XMLSchema#boolean";+const XSD_NON_NEGATIVE_INTEGER: &str = "http://www.w3.org/2001/XMLSchema#nonNegativeInteger";++/// Nested annotations deeper than this are refused. The reader never nests+/// more than the source did, and a graph whose `owl:Annotation` nodes point+/// at each other in a loop would otherwise recurse without end.+const MAX_ANNOTATION_DEPTH: usize = 64;++/// See [`label_budget`].+const LABEL_CALLS_PER_NODE: usize = 8;++/// The annotation properties OWL 2 builds in (Section 5.5 of the structural+/// specification). Every other IRI in a reserved namespace is vocabulary, and+/// a triple using it outside a recognised structure is reported, not written+/// as an annotation.+const BUILT_IN_ANNOTATION_PROPERTIES: &[&str] = &[+ "http://www.w3.org/2000/01/rdf-schema#label",+ "http://www.w3.org/2000/01/rdf-schema#comment",+ "http://www.w3.org/2000/01/rdf-schema#seeAlso",+ "http://www.w3.org/2000/01/rdf-schema#isDefinedBy",+ "http://www.w3.org/2002/07/owl#deprecated",+ "http://www.w3.org/2002/07/owl#versionInfo",+ "http://www.w3.org/2002/07/owl#priorVersion",+ "http://www.w3.org/2002/07/owl#backwardCompatibleWith",+ "http://www.w3.org/2002/07/owl#incompatibleWith",+];++/// Predicates whose triple is an AXIOM about its subject, never part of the+/// subject's own structure. An anonymous expression is the subject of one of+/// these whenever it is the left operand of a GCI or an n-ary chain, so they+/// are left out when a node's shape is matched against Table 1.+const AXIOM_PREDICATES: &[&str] = &[+ "http://www.w3.org/2000/01/rdf-schema#subClassOf",+ "http://www.w3.org/2000/01/rdf-schema#subPropertyOf",+ "http://www.w3.org/2000/01/rdf-schema#domain",+ "http://www.w3.org/2000/01/rdf-schema#range",+ "http://www.w3.org/2002/07/owl#equivalentClass",+ "http://www.w3.org/2002/07/owl#disjointWith",+ "http://www.w3.org/2002/07/owl#hasKey",+ "http://www.w3.org/2002/07/owl#equivalentProperty",+ "http://www.w3.org/2002/07/owl#propertyDisjointWith",+ "http://www.w3.org/2002/07/owl#propertyChainAxiom",+];++/// The property characteristics, which are `rdf:type` triples on a property+/// expression and so may sit on an `ObjectInverseOf` node.+const CHARACTERISTICS: &[&str] = &[+ "FunctionalProperty",+ "InverseFunctionalProperty",+ "ReflexiveProperty",+ "IrreflexiveProperty",+ "SymmetricProperty",+ "AsymmetricProperty",+ "TransitiveProperty",+];++/// Datatypes OWL 2 knows without a declaration (Section 4 of the structural+/// specification), besides everything in the `xsd:` namespace. Used only to+/// tell a data restriction from an object restriction, which the RDF does not+/// say when the property is undeclared.+const BUILT_IN_DATATYPES: &[&str] = &[+ "http://www.w3.org/2000/01/rdf-schema#Literal",+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral",+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString",+ "http://www.w3.org/2002/07/owl#real",+ "http://www.w3.org/2002/07/owl#rational",+];++/// One construct the output does not carry, or carries unfaithfully.+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]+pub struct NotExported {+ pub construct: String,+ pub occurrences: usize,+ pub why: String,+}++/// The document and everything a caller needs to know about what it left out.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct OfnOutput {+ pub text: String,+ pub axioms_written: usize,+ pub triples_read: usize,+ pub triples_exported: usize,+ /// Triples no axiom in `text` accounts for, by construct and reason.+ pub not_exported: Vec<NotExported>,+ /// Written, but the reader will not give back the same graph.+ pub not_round_trippable: Vec<NotExported>,+ /// The graph had no `owl:Ontology` node, so the header `Ontology(` adds+ /// one triple the input did not have.+ pub header_synthesised: bool,+ /// Assertions on a property the graph never declared, written as+ /// `AnnotationAssertion` because that is the one reading that commits the+ /// ontology to nothing. The RDF round trip is exact either way; an OWL+ /// tool reading the output sees no object or data property there.+ pub undeclared_property_assertions: usize,+}++impl OfnOutput {+ /// The report as JSON, without the text, under the names the first-order+ /// export uses for the same idea.+ pub fn report_json(&self) -> serde_json::Value {+ serde_json::json!({+ "format": "ofn",+ "axioms_written": self.axioms_written,+ "triples_read": self.triples_read,+ "triples_exported": self.triples_exported,+ "exports_fewer_triples_than_read": !self.not_exported.is_empty(),+ "constructs_not_exported": self.not_exported,+ "constructs_not_round_trippable": self.not_round_trippable,+ "header_synthesised": self.header_synthesised,+ "undeclared_property_assertions_written_as_annotations":+ self.undeclared_property_assertions,+ })+ }+}++/// Write `triples` as OFN, abbreviating IRIs with `prefixes` (plus the five+/// the specification predefines).+///+/// The only error is an RDFC budget refusal while labelling anonymous+/// individuals; see the module notes.+pub fn write_ofn(+ triples: &[Triple],+ prefixes: &[(String, String)],+) -> Result<OfnOutput, canon::CanonError> {+ let ix = Index::new(triples);+ let names = Names::new(prefixes);+ let first = Writer { ix: &ix, names: &names, labels: None }.run();+ if first.anons.is_empty() {+ return Ok(first.finish(&ix, &names));+ }+ // Label every anonymous individual canonically, then render again with+ // those labels. Rendering is a pure function of the graph and the labels,+ // so the second run makes the same decisions as the first.+ //+ // The labels are computed over every triple of the blank-node components+ // the individuals belong to, not only the triples that mention one. With+ // the narrower set, `ObjectHasValue(:diameter _:a)` under `:PM1` and the+ // same restriction under `:PM2point5` gave two individuals that looked+ // alike, because the restriction node linking each to its class was left+ // out, and RDFC swapped their labels between runs (measured on ORE file+ // ore_ont_15184, one of 141 files from 1.2 MB up that failed that way).+ // Within a component, two nodes RDFC cannot tell apart are automorphic,+ // and swapping automorphic nodes does not change the text.+ let component = blank_component(&ix.triples, &first.anons);+ let quads: Vec<Quad> = ix+ .triples+ .iter()+ .filter(|t| mentions_any(t, &component))+ .map(|t| t.clone().in_graph(GraphName::DefaultGraph))+ .collect();+ let labels: HashMap<String, String> =+ canon::canonical_labels(&quads, label_budget(&component), canon::HashAlgorithm::Sha256)?+ .into_iter()+ .collect();+ Ok(Writer { ix: &ix, names: &names, labels: Some(&labels) }+ .run()+ .finish(&ix, &names))+}++/// Whether a `to` format name asks for this writer rather than an RDF one.+pub fn is_ofn_format(name: &str) -> bool {+ matches!(name.to_ascii_lowercase().as_str(), "ofn" | "functional")+}++/// Load `path`, in any format `onto_load` reads, and write it as OFN with the+/// prefixes the document declared. This is the one function behind+/// `onto_convert`, the CLI `convert` and `batch`'s `convert`, so the three+/// cannot disagree about what was left out.+///+/// The document's triples are handed over as parsed, without passing through+/// a store: Oxigraph's store re-types an `xsd:nonNegativeInteger` literal as+/// `xsd:integer` (measured on `owl:minCardinality 2`), and every cardinality+/// that had happened to would be refused as outside Table 1.+pub fn convert_file(path: &str) -> anyhow::Result<OfnOutput> {+ let (quads, prefixes) = crate::graph::GraphStore::read_file_with_prefixes(path)?;+ let triples: Vec<Triple> = quads.into_iter().map(Triple::from).collect();+ Ok(write_ofn(&triples, &prefixes)?)+}++/// Every blank node connected to one of `seeds` through triples whose subject+/// and object are both blank nodes.+fn blank_component(triples: &[Triple], seeds: &HashSet<BlankNode>) -> HashSet<BlankNode> {+ let mut next: HashMap<&BlankNode, Vec<&BlankNode>> = HashMap::new();+ for t in triples {+ if let (NamedOrBlankNode::BlankNode(s), Term::BlankNode(o)) = (&t.subject, &t.object) {+ next.entry(s).or_default().push(o);+ next.entry(o).or_default().push(s);+ }+ }+ let mut seen: HashSet<BlankNode> = seeds.clone();+ let mut stack: Vec<&BlankNode> = seeds.iter().collect();+ while let Some(b) = stack.pop() {+ for &n in next.get(b).map(Vec::as_slice).unwrap_or(&[]) {+ if seen.insert(n.clone()) {+ stack.push(n);+ }+ }+ }+ seen+}++/// The RDFC call budget for labelling the nodes of `component`.+///+/// `canon::DEFAULT_CALL_BUDGET` (4000) is sized for one dataset of the W3C+/// suite, and an ABox with thousands of anonymous individuals that look alike+/// spends at least one `hash_n_degree_quads` call on every one of them before+/// any symmetry is involved: seven ORE 2015 files under 1 MB (4,155 to 6,800+/// anonymous individuals each) were refused at 4000. Scaling the budget with+/// the number of nodes keeps it a bound on SUPERLINEAR work, which is what+/// RDFC-1.0 section 4.4 asks to be bounded, without refusing linear work.+fn label_budget(component: &HashSet<BlankNode>) -> usize {+ canon::DEFAULT_CALL_BUDGET.max(LABEL_CALLS_PER_NODE * component.len())+}++fn mentions_any(t: &Triple, set: &HashSet<BlankNode>) -> bool {+ matches!(&t.subject, NamedOrBlankNode::BlankNode(b) if set.contains(b))+ || matches!(&t.object, Term::BlankNode(b) if set.contains(b))+}++// --------------------------------------------------------------------------- //+// Vocabulary tests+// --------------------------------------------------------------------------- //++fn local<'a>(n: &'a NamedNode, ns: &str) -> Option<&'a str> {+ n.as_str().strip_prefix(ns)+}++fn is_reserved(n: &NamedNode) -> bool {+ [RDF, RDFS, OWL, XSD, SWRL].iter().any(|ns| n.as_str().starts_with(ns))+}++fn is_annotation_predicate(p: &NamedNode) -> bool {+ !is_reserved(p) || BUILT_IN_ANNOTATION_PROPERTIES.contains(&p.as_str())+}++fn term_iri(t: &Term) -> Option<&NamedNode> {+ match t {+ Term::NamedNode(n) => Some(n),+ _ => None,+ }+}++fn term_blank(t: &Term) -> Option<&BlankNode> {+ match t {+ Term::BlankNode(b) => Some(b),+ _ => None,+ }+}++fn subject_term(s: &NamedOrBlankNode) -> Term {+ match s {+ NamedOrBlankNode::NamedNode(n) => Term::NamedNode(n.clone()),+ NamedOrBlankNode::BlankNode(b) => Term::BlankNode(b.clone()),+ }+}++fn term_subject(t: &Term) -> Option<NamedOrBlankNode> {+ match t {+ Term::NamedNode(n) => Some(n.clone().into()),+ Term::BlankNode(b) => Some(b.clone().into()),+ _ => None,+ }+}++// --------------------------------------------------------------------------- //+// The graph, indexed+// --------------------------------------------------------------------------- //++/// Declared entity kinds, from `Declaration` triples. They decide between+/// OFN keywords that share one RDF image (`SubObjectPropertyOf` and+/// `SubDataPropertyOf` are both `rdfs:subPropertyOf`), so they change what an+/// OWL tool reads but never the round-tripped graph.+#[derive(Default)]+struct Kinds {+ object: HashSet<NamedNode>,+ data: HashSet<NamedNode>,+ annotation: HashSet<NamedNode>,+ datatype: HashSet<NamedNode>,+ variable: HashSet<NamedNode>,+}++struct Index {+ triples: Vec<Triple>,+ by_subject: HashMap<NamedOrBlankNode, Vec<usize>>,+ /// `owl:Axiom` nodes by the triple they reify (Section 2.3.1).+ axiom_reifications: HashMap<Triple, Vec<BlankNode>>,+ /// `owl:Annotation` nodes by the annotation triple they reify (Table 2).+ annotation_reifications: HashMap<Triple, Vec<BlankNode>>,+ kinds: Kinds,+}++impl Index {+ fn new(input: &[Triple]) -> Self {+ let mut seen = HashSet::new();+ let triples: Vec<Triple> = input.iter().filter(|t| seen.insert(*t)).cloned().collect();+ let mut by_subject: HashMap<NamedOrBlankNode, Vec<usize>> = HashMap::new();+ for (i, t) in triples.iter().enumerate() {+ by_subject.entry(t.subject.clone()).or_default().push(i);+ }+ let mut ix = Self {+ triples,+ by_subject,+ axiom_reifications: HashMap::new(),+ annotation_reifications: HashMap::new(),+ kinds: Kinds::default(),+ };+ for t in &ix.triples {+ if t.predicate.as_str() != RDF_TYPE {+ continue;+ }+ let Some(class) = term_iri(&t.object) else { continue };+ match (&t.subject, class.as_str()) {+ (NamedOrBlankNode::NamedNode(n), c) => {+ let set = match c.strip_prefix(OWL) {+ Some("ObjectProperty") => &mut ix.kinds.object,+ Some("DatatypeProperty") => &mut ix.kinds.data,+ Some("AnnotationProperty") => &mut ix.kinds.annotation,+ _ if c == RDFS_DATATYPE => &mut ix.kinds.datatype,+ _ if c == "http://www.w3.org/2003/11/swrl#Variable" => &mut ix.kinds.variable,+ _ => continue,+ };+ set.insert(n.clone());+ }+ (NamedOrBlankNode::BlankNode(_), _) => {}+ }+ }+ let mut axioms = HashMap::<Triple, Vec<BlankNode>>::new();+ let mut annotations = HashMap::<Triple, Vec<BlankNode>>::new();+ for (subject, ids) in &ix.by_subject {+ let NamedOrBlankNode::BlankNode(x) = subject else { continue };+ let node = ix.node_of(ids);+ let target = match node.one_iri(RDF_TYPE).and_then(|c| local(c, OWL)) {+ Some("Axiom") => &mut axioms,+ Some("Annotation") => &mut annotations,+ _ => continue,+ };+ let source = node.one(&format!("{OWL}annotatedSource")).map(|(_, t)| t);+ let property = node.one(&format!("{OWL}annotatedProperty")).map(|(_, t)| t);+ let value = node.one(&format!("{OWL}annotatedTarget")).map(|(_, t)| t);+ if let (Some(s), Some(Term::NamedNode(p)), Some(o)) = (source, property, value)+ && let Some(s) = term_subject(s)+ {+ target+ .entry(Triple::new(s, p.clone(), o.clone()))+ .or_default()+ .push(x.clone());+ }+ }+ ix.axiom_reifications = axioms;+ ix.annotation_reifications = annotations;+ ix+ }++ fn ids(&self, s: &NamedOrBlankNode) -> &[usize] {+ self.by_subject.get(s).map(Vec::as_slice).unwrap_or(&[])+ }++ fn node_of(&self, ids: &[usize]) -> Node<'_> {+ let mut reserved: BTreeMap<&str, Vec<(usize, &Term)>> = BTreeMap::new();+ for &i in ids {+ let t = &self.triples[i];+ if !is_annotation_predicate(&t.predicate) && !AXIOM_PREDICATES.contains(&t.predicate.as_str()) {+ reserved.entry(t.predicate.as_str()).or_default().push((i, &t.object));+ }+ }+ Node { reserved }+ }++ fn node(&self, b: &BlankNode) -> Node<'_> {+ self.node_of(self.ids(&b.clone().into()))+ }++ fn is_datatype_iri(&self, n: &NamedNode) -> bool {+ n.as_str().starts_with(XSD)+ || BUILT_IN_DATATYPES.contains(&n.as_str())+ || self.kinds.datatype.contains(n)+ }++ /// Whether `t` reads as a data range rather than a class expression.+ fn is_data_range(&self, t: &Term) -> bool {+ match t {+ Term::NamedNode(n) => self.is_datatype_iri(n),+ Term::BlankNode(b) => self.node(b).one_iri(RDF_TYPE).map(NamedNode::as_str) == Some(RDFS_DATATYPE),+ _ => false,+ }+ }++ fn is_data_property(&self, t: &Term) -> bool {+ matches!(t, Term::NamedNode(n) if self.kinds.data.contains(n) && !self.kinds.object.contains(n))+ }++ fn is_annotation_property(&self, t: &Term) -> bool {+ matches!(t, Term::NamedNode(n) if self.kinds.annotation.contains(n))+ }+}++/// The reserved-vocabulary triples of one subject, by predicate. Annotation+/// triples are left out: on the nodes that may carry them they are read+/// separately, and on the nodes that may not they are reported.+struct Node<'a> {+ reserved: BTreeMap<&'a str, Vec<(usize, &'a Term)>>,+}++impl<'a> Node<'a> {+ fn one(&self, p: &str) -> Option<(usize, &'a Term)> {+ match self.reserved.get(p).map(Vec::as_slice) {+ Some([only]) => Some(*only),+ _ => None,+ }+ }++ fn one_iri(&self, p: &str) -> Option<&'a NamedNode> {+ self.one(p).and_then(|(_, t)| term_iri(t))+ }++ /// The reserved predicates present, besides `rdf:type`.+ fn shape(&self) -> BTreeSet<&'a str> {+ self.reserved.keys().copied().filter(|p| *p != RDF_TYPE).collect()+ }+}++// --------------------------------------------------------------------------- //+// Prefixes+// --------------------------------------------------------------------------- //++struct Names {+ /// Name to namespace, as `Prefix(...)` lines are written.+ declared: BTreeMap<String, String>,+ /// The same, longest namespace first, for abbreviation.+ by_length: Vec<(String, String)>,+}++/// A prefix name the reader's lexer reads back as one: no character that ends+/// a word, no colon, and not `_`, which would make every name a blank node.+fn safe_prefix_name(name: &str) -> bool {+ name.is_empty()+ || (name.chars().next().is_some_and(|c| c.is_ascii_alphabetic())+ && name.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.'))+ && !name.ends_with('.'))+}++/// A local name that survives the lexer and means one thing to every OFN+/// reader: the OWL API follows SPARQL's PN_LOCAL, which this is a subset of.+fn safe_local(local: &str) -> bool {+ !local.is_empty()+ && local.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.'))+ && !local.starts_with(['-', '.'])+ && !local.ends_with('.')+}++impl Names {+ fn new(prefixes: &[(String, String)]) -> Self {+ let mut declared: BTreeMap<String, String> = crate::ofn::STANDARD_PREFIXES+ .iter()+ .map(|(n, i)| ((*n).to_string(), (*i).to_string()))+ .collect();+ for (name, iri) in prefixes {+ // A later declaration of a name wins, as it does in the reader.+ if safe_prefix_name(name) && oxigraph::model::NamedNode::new(iri.as_str()).is_ok() {+ declared.insert(name.clone(), iri.clone());+ }+ }+ let mut by_length: Vec<(String, String)> =+ declared.iter().map(|(n, i)| (n.clone(), i.clone())).collect();+ by_length.sort_by(|a, b| b.1.len().cmp(&a.1.len()).then_with(|| a.0.cmp(&b.0)));+ Self { declared, by_length }+ }++ fn iri(&self, n: &NamedNode) -> String {+ let s = n.as_str();+ for (name, ns) in &self.by_length {+ if let Some(rest) = s.strip_prefix(ns.as_str())+ && safe_local(rest)+ {+ return format!("{name}:{rest}");+ }+ }+ format!("<{s}>")+ }+}++fn escape(value: &str) -> String {+ let mut out = String::with_capacity(value.len() + 2);+ out.push('"');+ for c in value.chars() {+ if matches!(c, '"' | '\\') {+ out.push('\\');+ }+ out.push(c);+ }+ out.push('"');+ out+}++// --------------------------------------------------------------------------- //+// Rendering+// --------------------------------------------------------------------------- //++type R<T> = Result<T, String>;++/// What one axiom used, committed only if the whole axiom renders.+#[derive(Default)]+struct Tx {+ used: Vec<usize>,+ /// Anonymous expressions, data ranges, lists and facets written inline.+ inline: Vec<BlankNode>,+ anons: Vec<BlankNode>,+}++#[derive(Default)]+struct Run {+ lines: BTreeSet<String>,+ header: BTreeSet<String>,+ header_line: String,+ header_synthesised: bool,+ consumed: Vec<bool>,+ why: HashMap<usize, String>,+ inline_uses: HashMap<BlankNode, usize>,+ anons: HashSet<BlankNode>,+ undeclared_property_assertions: usize,+}++struct Writer<'a> {+ ix: &'a Index,+ names: &'a Names,+ labels: Option<&'a HashMap<String, String>>,+}++impl<'a> Writer<'a> {+ fn t(&self, i: usize) -> &'a Triple {+ &self.ix.triples[i]+ }++ // ---- terminals ------------------------------------------------------ //++ fn iri(&self, n: &NamedNode) -> String {+ self.names.iri(n)+ }++ fn literal(&self, l: &Literal) -> R<String> {+ let dt = l.datatype();+ if dt.as_str() == RDF_PLAIN_LITERAL {+ // The reader turns `"x@"^^rdf:PlainLiteral` into the plain literal+ // it spells (Table 1, first two rows), so writing one would come+ // back as a different term.+ return Err("an rdf:PlainLiteral-typed literal, which the reader maps to a plain literal".into());+ }+ let v = escape(l.value());+ Ok(match l.language() {+ Some(tag) => format!("{v}@{tag}"),+ None if dt.as_str() == XSD_STRING => v,+ None => format!("{v}^^{}", self.iri(&dt.into_owned())),+ })+ }++ fn anon(&self, b: &BlankNode, tx: &mut Tx) -> String {+ tx.anons.push(b.clone());+ match self.labels.and_then(|m| m.get(b.as_str())) {+ Some(label) => format!("_:{label}"),+ None => format!("_:{}", b.as_str()),+ }+ }++ fn individual(&self, t: &Term, tx: &mut Tx) -> R<String> {+ match t {+ Term::NamedNode(n) => Ok(self.iri(n)),+ Term::BlankNode(b) => Ok(self.anon(b, tx)),+ _ => Err("a literal where an individual belongs".into()),+ }+ }++ fn annotation_value(&self, t: &Term, tx: &mut Tx) -> R<String> {+ match t {+ Term::Literal(l) => self.literal(l),+ other => self.individual(other, tx),+ }+ }++ fn property_iri(&self, t: &Term, what: &str) -> R<String> {+ term_iri(t)+ .map(|n| self.iri(n))+ .ok_or_else(|| format!("a {what} that is not an IRI"))+ }++ fn cardinality(&self, t: &Term) -> R<String> {+ // The reader writes the cardinality as the canonical decimal of a u64+ // typed xsd:nonNegativeInteger, so only that spelling comes back as+ // the same literal.+ let Term::Literal(l) = t else {+ return Err("a cardinality that is not a literal".into());+ };+ let v = l.value();+ if l.datatype().as_str() == XSD_NON_NEGATIVE_INTEGER+ && v.parse::<u64>().is_ok_and(|n| n.to_string() == v)+ {+ Ok(v.to_string())+ } else {+ Err("a cardinality that is not a canonical xsd:nonNegativeInteger".into())+ }+ }++ // ---- lists ---------------------------------------------------------- //++ /// `T(SEQ ...)` read back: the items of an `rdf:List`, each cell carrying+ /// `rdf:first` and `rdf:rest` and nothing else, or also `rdf:type+ /// swrl:AtomList` when `atom_list`, which is how the reader writes the+ /// body and head of a rule.+ fn list(&self, head: &Term, atom_list: bool, tx: &mut Tx) -> R<Vec<&'a Term>> {+ let mut items = Vec::new();+ let mut seen = HashSet::new();+ let mut cur = head.clone();+ loop {+ match &cur {+ Term::NamedNode(n) if n.as_str() == RDF_NIL => return Ok(items),+ Term::BlankNode(b) => {+ if !seen.insert(b.clone()) {+ return Err("a cyclic rdf:List".into());+ }+ let node = self.ix.node(b);+ // The cell's triples are touched whether or not it is+ // well formed, so a malformed one is reported with the+ // reason rather than as an unexplained rdf:first.+ tx.used.extend(self.ix.ids(&b.clone().into()).iter().filter(|&&i| !is_annotation_predicate(&self.t(i).predicate)));+ let mut want: BTreeSet<&str> = [RDF_FIRST, RDF_REST].into();+ if atom_list {+ want.insert(RDF_TYPE);+ }+ let have: BTreeSet<&str> = node.reserved.keys().copied().collect();+ if have != want {+ return Err("a malformed rdf:List cell".into());+ }+ if atom_list && node.one_iri(RDF_TYPE).and_then(|c| local(c, SWRL)) != Some("AtomList") {+ return Err("a SWRL atom list cell not typed swrl:AtomList".into());+ }+ let (Some((_, first)), Some((_, rest))) = (node.one(RDF_FIRST), node.one(RDF_REST)) else {+ return Err("an rdf:List cell without exactly one rdf:first and rdf:rest".into());+ };+ tx.inline.push(b.clone());+ items.push(first);+ cur = rest.clone();+ }+ _ => return Err("a malformed rdf:List: a tail that is neither rdf:nil nor a cell".into()),+ }+ }+ }++ fn list_min(&self, head: &Term, min: usize, what: &str, tx: &mut Tx) -> R<Vec<&'a Term>> {+ let items = self.list(head, false, tx)?;+ if items.len() < min {+ // The reader writes this construct only with at least `min`+ // operands (fewer is its syntax error), so a shorter list is not+ // the image of anything it reads.+ return Err(format!("{what} over fewer than {min} operand(s)"));+ }+ Ok(items)+ }++ fn join<T>(&self, items: &[T], tx: &mut Tx, mut f: impl FnMut(&Self, &T, &mut Tx) -> R<String>) -> R<String> {+ let mut parts = Vec::with_capacity(items.len());+ for it in items {+ parts.push(f(self, it, tx)?);+ }+ Ok(parts.join(" "))+ }++ // ---- expressions ---------------------------------------------------- //++ fn ope(&self, t: &Term, tx: &mut Tx) -> R<String> {+ match t {+ Term::NamedNode(n) => Ok(self.iri(n)),+ Term::BlankNode(b) => {+ let node = self.ix.node(b);+ let characteristics_only = node.reserved.get(RDF_TYPE).is_none_or(|types| {+ types.iter().all(|(_, c)| {+ term_iri(c).and_then(|c| local(c, OWL)).is_some_and(|l| CHARACTERISTICS.contains(&l))+ })+ });+ let inverse = format!("{OWL}inverseOf");+ match node.one(&inverse) {+ Some((i, Term::NamedNode(p))) if node.shape().len() == 1 && characteristics_only => {+ tx.used.push(i);+ tx.inline.push(b.clone());+ Ok(format!("ObjectInverseOf({})", self.iri(p)))+ }+ _ => Err("an anonymous object property that is not ObjectInverseOf of a named one".into()),+ }+ }+ _ => Err("a literal where an object property belongs".into()),+ }+ }++ fn ce(&self, t: &Term, tx: &mut Tx) -> R<String> {+ match t {+ Term::NamedNode(n) => Ok(self.iri(n)),+ Term::BlankNode(b) => self.class_node(b, tx),+ _ => Err("a literal where a class expression belongs".into()),+ }+ }++ fn class_node(&self, b: &BlankNode, tx: &mut Tx) -> R<String> {+ let node = self.ix.node(b);+ let Some((ti, ty)) = node.one(RDF_TYPE) else {+ return Err("an anonymous class expression without exactly one rdf:type".into());+ };+ tx.used.push(ti);+ let shape = node.shape();+ let ty = term_iri(ty).and_then(|c| local(c, OWL));+ let out = match ty {+ Some("Class") => {+ let [p] = shape.iter().copied().collect::<Vec<_>>()[..] else {+ return Err("an owl:Class node without exactly one constructor".into());+ };+ let (i, arg) = node.one(p).ok_or("a class constructor given more than once")?;+ tx.used.push(i);+ match p.strip_prefix(OWL) {+ Some(k @ ("intersectionOf" | "unionOf")) => {+ let items = self.list_min(arg, 2, k, tx)?;+ let kw = if k == "intersectionOf" { "ObjectIntersectionOf" } else { "ObjectUnionOf" };+ format!("{kw}({})", self.join(&items, tx, |w, t, tx| w.ce(t, tx))?)+ }+ Some("complementOf") => format!("ObjectComplementOf({})", self.ce(arg, tx)?),+ Some("oneOf") => {+ let items = self.list_min(arg, 1, "oneOf", tx)?;+ format!("ObjectOneOf({})", self.join(&items, tx, |w, t, tx| w.individual(t, tx))?)+ }+ _ => return Err("an owl:Class node whose constructor is not a class constructor".into()),+ }+ }+ Some("Restriction") => self.restriction(&node, &shape, tx)?,+ _ => return Err("an anonymous class expression typed neither owl:Class nor owl:Restriction".into()),+ };+ tx.inline.push(b.clone());+ Ok(out)+ }++ fn restriction(&self, node: &Node<'a>, shape: &BTreeSet<&'a str>, tx: &mut Tx) -> R<String> {+ let owl = |l: &str| format!("{OWL}{l}");+ let rest: Vec<&str> = shape+ .iter()+ .filter_map(|p| p.strip_prefix(OWL))+ .filter(|p| *p != "onProperty" && *p != "onProperties")+ .collect();+ if rest.len() + usize::from(shape.contains(owl("onProperty").as_str()))+ + usize::from(shape.contains(owl("onProperties").as_str()))+ != shape.len()+ {+ return Err("an owl:Restriction with a predicate outside the OWL vocabulary of restrictions".into());+ }+ if let Some((li, list)) = node.one(&owl("onProperties")) {+ // Table 1 writes owl:onProperties only for n-ary data+ // restrictions, n >= 2; one property gets owl:onProperty.+ let ["allValuesFrom" | "someValuesFrom"] = rest[..] else {+ return Err("an owl:onProperties restriction that is not DataSomeValuesFrom or DataAllValuesFrom".into());+ };+ if shape.contains(owl("onProperty").as_str()) {+ return Err("an owl:Restriction with both owl:onProperty and owl:onProperties".into());+ }+ tx.used.push(li);+ let props = self.list_min(list, 2, "owl:onProperties", tx)?;+ let props = self.join(&props, tx, |w, t, _| w.property_iri(t, "data property"))?;+ let (fi, filler) = node.one(&owl(rest[0])).ok_or("a restriction filler given more than once")?;+ tx.used.push(fi);+ let kw = if rest[0] == "someValuesFrom" { "DataSomeValuesFrom" } else { "DataAllValuesFrom" };+ return Ok(format!("{kw}({props} {})", self.dr(filler, tx)?));+ }+ let Some((pi, p)) = node.one(&owl("onProperty")) else {+ return Err("an owl:Restriction without exactly one owl:onProperty".into());+ };+ tx.used.push(pi);+ let take = |l: &str, tx: &mut Tx| -> R<&'a Term> {+ let (i, t) = node.one(&owl(l)).ok_or("a restriction component given more than once")?;+ tx.used.push(i);+ Ok(t)+ };+ let data_p = self.ix.is_data_property(p);+ Ok(match rest[..] {+ [k @ ("someValuesFrom" | "allValuesFrom")] => {+ let f = take(k, tx)?;+ let some = k == "someValuesFrom";+ if data_p || (term_iri(p).is_some() && self.ix.is_data_range(f)) {+ let kw = if some { "DataSomeValuesFrom" } else { "DataAllValuesFrom" };+ format!("{kw}({} {})", self.property_iri(p, "data property")?, self.dr(f, tx)?)+ } else {+ let kw = if some { "ObjectSomeValuesFrom" } else { "ObjectAllValuesFrom" };+ format!("{kw}({} {})", self.ope(p, tx)?, self.ce(f, tx)?)+ }+ }+ ["hasValue"] => match take("hasValue", tx)? {+ Term::Literal(l) => {+ format!("DataHasValue({} {})", self.property_iri(p, "data property")?, self.literal(l)?)+ }+ v => format!("ObjectHasValue({} {})", self.ope(p, tx)?, self.individual(v, tx)?),+ },+ ["hasSelf"] => match take("hasSelf", tx)? {+ Term::Literal(l) if l.value() == "true" && l.datatype().as_str() == XSD_BOOLEAN => {+ format!("ObjectHasSelf({})", self.ope(p, tx)?)+ }+ _ => return Err("an owl:hasSelf that is not \"true\"^^xsd:boolean".into()),+ },+ [k @ ("minCardinality" | "maxCardinality" | "cardinality")] => {+ let n = self.cardinality(take(k, tx)?)?;+ let kind = cardinality_kind(k);+ if data_p {+ format!("Data{kind}({n} {})", self.property_iri(p, "data property")?)+ } else {+ format!("Object{kind}({n} {})", self.ope(p, tx)?)+ }+ }+ [a, b] => {+ let (k, on) = if a.starts_with("on") { (b, a) } else { (a, b) };+ let kind = match k {+ "minQualifiedCardinality" => "MinCardinality",+ "maxQualifiedCardinality" => "MaxCardinality",+ "qualifiedCardinality" => "ExactCardinality",+ _ => return Err("an owl:Restriction whose components match no row of Table 1".into()),+ };+ let n = self.cardinality(take(k, tx)?)?;+ match on {+ "onClass" => format!("Object{kind}({n} {} {})", self.ope(p, tx)?, self.ce(take(on, tx)?, tx)?),+ "onDataRange" => format!(+ "Data{kind}({n} {} {})",+ self.property_iri(p, "data property")?,+ self.dr(take(on, tx)?, tx)?+ ),+ _ => return Err("an owl:Restriction whose components match no row of Table 1".into()),+ }+ }+ _ => return Err("an owl:Restriction whose components match no row of Table 1".into()),+ })+ }++ fn dr(&self, t: &Term, tx: &mut Tx) -> R<String> {+ match t {+ Term::NamedNode(n) => Ok(self.iri(n)),+ Term::BlankNode(b) => self.data_range_node(b, tx),+ _ => Err("a literal where a data range belongs".into()),+ }+ }++ fn data_range_node(&self, b: &BlankNode, tx: &mut Tx) -> R<String> {+ let node = self.ix.node(b);+ let Some((ti, ty)) = node.one(RDF_TYPE) else {+ return Err("an anonymous data range without exactly one rdf:type".into());+ };+ if term_iri(ty).map(NamedNode::as_str) != Some(RDFS_DATATYPE) {+ return Err("an anonymous data range not typed rdfs:Datatype".into());+ }+ tx.used.push(ti);+ let shape: Vec<&str> = node.shape().into_iter().collect();+ let take = |p: &str, tx: &mut Tx| -> R<&'a Term> {+ let (i, t) = node.one(p).ok_or("a data range component given more than once")?;+ tx.used.push(i);+ Ok(t)+ };+ let owl = |l: &str| format!("{OWL}{l}");+ let out = match shape.iter().map(|p| p.strip_prefix(OWL).unwrap_or(p)).collect::<Vec<_>>()[..] {+ [k @ ("intersectionOf" | "unionOf")] => {+ let items = self.list_min(take(&owl(k), tx)?, 2, k, tx)?;+ let kw = if k == "intersectionOf" { "DataIntersectionOf" } else { "DataUnionOf" };+ format!("{kw}({})", self.join(&items, tx, |w, t, tx| w.dr(t, tx))?)+ }+ ["datatypeComplementOf"] => format!("DataComplementOf({})", self.dr(take(&owl("datatypeComplementOf"), tx)?, tx)?),+ ["oneOf"] => {+ let items = self.list_min(take(&owl("oneOf"), tx)?, 1, "oneOf", tx)?;+ let lits = self.join(&items, tx, |w, t, _| match t {+ Term::Literal(l) => w.literal(l),+ _ => Err("a DataOneOf member that is not a literal".into()),+ })?;+ format!("DataOneOf({lits})")+ }+ ["onDatatype", "withRestrictions"] => {+ let dt = self.property_iri(take(&owl("onDatatype"), tx)?, "datatype")?;+ let facets = self.list_min(take(&owl("withRestrictions"), tx)?, 1, "owl:withRestrictions", tx)?;+ let facets = self.join(&facets, tx, |w, t, tx| w.facet(t, tx))?;+ format!("DatatypeRestriction({dt} {facets})")+ }+ _ => return Err("an rdfs:Datatype node whose components match no row of Table 1".into()),+ };+ tx.inline.push(b.clone());+ Ok(out)+ }++ fn facet(&self, t: &Term, tx: &mut Tx) -> R<String> {+ let Some(b) = term_blank(t) else {+ return Err("a facet restriction that is not a blank node".into());+ };+ let ids = self.ix.ids(&b.clone().into());+ let [i] = ids[..] else {+ return Err("a facet restriction node without exactly one triple".into());+ };+ let tr = self.t(i);+ let Term::Literal(l) = &tr.object else {+ return Err("a facet restriction whose value is not a literal".into());+ };+ tx.used.push(i);+ tx.inline.push(b.clone());+ Ok(format!("{} {}", self.iri(&tr.predicate), self.literal(l)?))+ }++ // ---- annotations ---------------------------------------------------- //++ /// Every annotation on `subject` (Table 2), nested annotations included,+ /// sorted. `TANN` writes one triple per annotation and, when that+ /// annotation is itself annotated, an `owl:Annotation` node reifying it.+ fn annotations(&self, subject: &NamedOrBlankNode, depth: usize, tx: &mut Tx) -> R<Vec<String>> {+ if depth > MAX_ANNOTATION_DEPTH {+ return Err(format!("annotations nested more than {MAX_ANNOTATION_DEPTH} deep"));+ }+ let mut out = Vec::new();+ for &i in self.ix.ids(subject) {+ if is_annotation_predicate(&self.t(i).predicate) {+ out.extend(self.annotation(i, depth, tx)?);+ }+ }+ out.sort();+ Ok(out)+ }++ /// The annotation triple `i`, once per `owl:Annotation` node that annotates+ /// it, or once bare. Two annotations that differ only in their own+ /// annotations share the triple and differ in the reifications, which is+ /// exactly what writing each of them gives back.+ fn annotation(&self, i: usize, depth: usize, tx: &mut Tx) -> R<Vec<String>> {+ let t = self.t(i);+ let body = format!("{} {}", self.iri(&t.predicate), self.annotation_value(&t.object, tx)?);+ tx.used.push(i);+ let mut out = Vec::new();+ for y in self.ix.annotation_reifications.get(t).map(Vec::as_slice).unwrap_or(&[]) {+ if let Some(nested) = self.reification(y, depth, tx) {+ out.push(format!("Annotation({} {body})", nested.join(" ")));+ }+ }+ if out.is_empty() {+ out.push(format!("Annotation({body})"));+ }+ Ok(out)+ }++ /// The annotations an `owl:Axiom` or `owl:Annotation` node carries, with+ /// its four structural triples consumed, or `None` when it carries none or+ /// does not render: the reader reifies only annotated things, so such a+ /// node is not in its image and is left to be reported.+ fn reification(&self, x: &BlankNode, depth: usize, tx: &mut Tx) -> Option<Vec<String>> {+ let node = self.ix.node(x);+ let structural = [+ RDF_TYPE.to_string(),+ format!("{OWL}annotatedSource"),+ format!("{OWL}annotatedProperty"),+ format!("{OWL}annotatedTarget"),+ ];+ let mut sub = Tx::default();+ for p in &structural {+ sub.used.push(node.one(p)?.0);+ }+ let anns = self.annotations(&x.clone().into(), depth + 1, &mut sub).ok()?;+ if anns.is_empty() {+ return None;+ }+ tx.used.extend(sub.used);+ tx.inline.extend(sub.inline);+ tx.anons.extend(sub.anons);+ Some(anns)+ }++ /// The annotation sets of the axiom whose main triple is `main`: one per+ /// `owl:Axiom` node reifying it, or a single empty set.+ fn axiom_annotations(&self, main: &Triple, tx: &mut Tx) -> Vec<Vec<String>> {+ let mut sets: Vec<Vec<String>> = self+ .ix+ .axiom_reifications+ .get(main)+ .map(Vec::as_slice)+ .unwrap_or(&[])+ .iter()+ .filter_map(|x| self.reification(x, 0, tx))+ .collect();+ sets.sort();+ if sets.is_empty() {+ sets.push(Vec::new());+ }+ sets+ }++ // ---- axioms --------------------------------------------------------- //++ fn emit(kw: &str, sets: &[Vec<String>], body: &str) -> Vec<String> {+ sets.iter()+ .map(|anns| {+ if anns.is_empty() {+ format!("{kw}({body})")+ } else {+ format!("{kw}({} {body})", anns.join(" "))+ }+ })+ .collect()+ }++ /// An axiom with a main triple: the body and the annotation sets.+ fn main(&self, i: usize, kw: &str, body: String, tx: &mut Tx) -> Vec<String> {+ let sets = self.axiom_annotations(self.t(i), tx);+ Self::emit(kw, &sets, &body)+ }++ /// The axiom triple `i` begins, if its predicate begins one. `None` means+ /// the triple is not an axiom trigger at all and is left to later passes.+ fn axiom(&self, i: usize, tx: &mut Tx) -> Option<R<Vec<String>>> {+ let t = self.t(i);+ let s = subject_term(&t.subject);+ let o = &t.object;+ let p = t.predicate.as_str();+ let ix = self.ix;+ let r = (|| -> R<Vec<String>> {+ if p == RDF_TYPE {+ return self.typed_axiom(i, tx).unwrap_or_else(|| Err(String::new()));+ }+ let (kw, body) = match (p.strip_prefix(RDFS), p.strip_prefix(OWL)) {+ (Some("subClassOf"), _) => ("SubClassOf", format!("{} {}", self.ce(&s, tx)?, self.ce(o, tx)?)),+ (_, Some("disjointWith")) => ("DisjointClasses", format!("{} {}", self.ce(&s, tx)?, self.ce(o, tx)?)),+ (_, Some("disjointUnionOf")) => {+ let c = self.property_iri(&s, "class")?;+ let items = self.list_min(o, 2, "owl:disjointUnionOf", tx)?;+ ("DisjointUnion", format!("{c} {}", self.join(&items, tx, |w, t, tx| w.ce(t, tx))?))+ }+ (Some("subPropertyOf"), _) => {+ if ix.is_annotation_property(&s) || ix.is_annotation_property(o) {+ ("SubAnnotationPropertyOf", self.iri_pair(&s, o)?)+ } else if ix.is_data_property(&s) || ix.is_data_property(o) {+ ("SubDataPropertyOf", self.iri_pair(&s, o)?)+ } else {+ ("SubObjectPropertyOf", format!("{} {}", self.ope(&s, tx)?, self.ope(o, tx)?))+ }+ }+ (_, Some("propertyChainAxiom")) => {+ let chain = self.list_min(o, 2, "ObjectPropertyChain", tx)?;+ let chain = self.join(&chain, tx, |w, t, tx| w.ope(t, tx))?;+ ("SubObjectPropertyOf", format!("ObjectPropertyChain({chain}) {}", self.ope(&s, tx)?))+ }+ (_, Some("propertyDisjointWith")) => {+ if term_iri(o).is_some() && (ix.is_data_property(&s) || ix.is_data_property(o)) {+ ("DisjointDataProperties", self.iri_pair(&s, o)?)+ } else {+ ("DisjointObjectProperties", format!("{} {}", self.ope(&s, tx)?, self.ope(o, tx)?))+ }+ }+ (Some("domain"), _) => {+ if ix.is_annotation_property(&s) {+ ("AnnotationPropertyDomain", self.iri_pair(&s, o)?)+ } else if ix.is_data_property(&s) {+ ("DataPropertyDomain", format!("{} {}", self.property_iri(&s, "data property")?, self.ce(o, tx)?))+ } else {+ ("ObjectPropertyDomain", format!("{} {}", self.ope(&s, tx)?, self.ce(o, tx)?))+ }+ }+ (Some("range"), _) => {+ if ix.is_annotation_property(&s) {+ ("AnnotationPropertyRange", self.iri_pair(&s, o)?)+ } else if ix.is_data_property(&s) || (term_iri(&s).is_some() && ix.is_data_range(o)) {+ ("DataPropertyRange", format!("{} {}", self.property_iri(&s, "data property")?, self.dr(o, tx)?))+ } else {+ ("ObjectPropertyRange", format!("{} {}", self.ope(&s, tx)?, self.ce(o, tx)?))+ }+ }+ // A blank subject of owl:inverseOf is an ObjectInverseOf+ // expression, written where it is used. Only a named subject+ // is the main triple of InverseObjectProperties.+ (_, Some("inverseOf")) if term_iri(&s).is_some() => {+ ("InverseObjectProperties", format!("{} {}", self.iri_of(&s), self.ope(o, tx)?))+ }+ (_, Some("hasKey")) => ("HasKey", format!("{} {}", self.ce(&s, tx)?, self.has_key(o, tx)?)),+ (_, Some("sameAs")) => ("SameIndividual", self.individual_pair(&s, o, tx)?),+ (_, Some("differentFrom")) => ("DifferentIndividuals", self.individual_pair(&s, o, tx)?),+ _ => return Err(String::new()),+ };+ Ok(self.main(i, kw, body, tx))+ })();+ match r {+ Err(e) if e.is_empty() => None,+ other => Some(other),+ }+ }++ fn iri_of(&self, t: &Term) -> String {+ term_iri(t).map(|n| self.iri(n)).unwrap_or_default()+ }++ fn iri_pair(&self, a: &Term, b: &Term) -> R<String> {+ Ok(format!("{} {}", self.property_iri(a, "property")?, self.property_iri(b, "property or IRI")?))+ }++ fn individual_pair(&self, a: &Term, b: &Term, tx: &mut Tx) -> R<String> {+ Ok(format!("{} {}", self.individual(a, tx)?, self.individual(b, tx)?))+ }++ /// `HasKey`'s single list, split back into its object and data parts. The+ /// reader writes the object properties first, so the split is at the+ /// first declared data property, which keeps the list order exact.+ fn has_key(&self, list: &Term, tx: &mut Tx) -> R<String> {+ let keys = self.list(list, false, tx)?;+ let split = keys.iter().position(|k| self.ix.is_data_property(k)).unwrap_or(keys.len());+ let objects = self.join(&keys[..split], tx, |w, t, tx| w.ope(t, tx))?;+ let data = self.join(&keys[split..], tx, |w, t, _| w.property_iri(t, "data property"))?;+ Ok(format!("({objects}) ({data})"))+ }++ /// The `rdf:type` axioms that pass 1 owns: property characteristics and+ /// the axioms rooted at a typed blank node (Section 2.3.3).+ fn typed_axiom(&self, i: usize, tx: &mut Tx) -> Option<R<Vec<String>>> {+ let t = self.t(i);+ let class = term_iri(&t.object)?;+ let s = subject_term(&t.subject);+ if let Some(local) = local(class, OWL) {+ let characteristic = match local {+ "FunctionalProperty" if self.ix.is_data_property(&s) => Some("FunctionalDataProperty"),+ "FunctionalProperty" => Some("FunctionalObjectProperty"),+ "InverseFunctionalProperty" => Some("InverseFunctionalObjectProperty"),+ "ReflexiveProperty" => Some("ReflexiveObjectProperty"),+ "IrreflexiveProperty" => Some("IrreflexiveObjectProperty"),+ "SymmetricProperty" => Some("SymmetricObjectProperty"),+ "AsymmetricProperty" => Some("AsymmetricObjectProperty"),+ "TransitiveProperty" => Some("TransitiveObjectProperty"),+ _ => None,+ };+ if let Some(kw) = characteristic {+ return Some((|| {+ let body = if kw == "FunctionalDataProperty" {+ self.property_iri(&s, "data property")?+ } else {+ self.ope(&s, tx)?+ };+ Ok(self.main(i, kw, body, tx))+ })());+ }+ }+ let NamedOrBlankNode::BlankNode(x) = &t.subject else {+ return None;+ };+ let kind = local(class, OWL)+ .filter(|l| matches!(*l, "AllDisjointClasses" | "AllDisjointProperties" | "AllDifferent" | "NegativePropertyAssertion"))+ .or_else(|| local(class, SWRL).filter(|l| *l == "Imp"))?;+ Some(self.rooted_axiom(i, x, kind, tx))+ }++ /// Section 2.3.3: an axiom whose translation is a typed blank node, with+ /// its annotations on that node.+ fn rooted_axiom(&self, ti: usize, x: &BlankNode, kind: &str, tx: &mut Tx) -> R<Vec<String>> {+ let node = self.ix.node(x);+ let shape: Vec<&str> = node.shape().into_iter().collect();+ let take = |p: &str, tx: &mut Tx| -> R<&'a Term> {+ let (i, t) = node.one(p).ok_or_else(|| format!("{p} given more than once"))?;+ tx.used.push(i);+ Ok(t)+ };+ let members = format!("{OWL}members");+ let (kw, body) = match kind {+ "AllDisjointClasses" | "AllDisjointProperties" | "AllDifferent" => {+ if shape != [members.as_str()] {+ return Err(format!("an owl:{kind} node with components other than owl:members"));+ }+ // The binary case is owl:disjointWith (or its kin) in Table 1,+ // so a two-member node is not the image of anything.+ let items = self.list_min(take(&members, tx)?, 3, &format!("owl:{kind}"), tx)?;+ match kind {+ "AllDisjointClasses" => ("DisjointClasses", self.join(&items, tx, |w, t, tx| w.ce(t, tx))?),+ "AllDifferent" => ("DifferentIndividuals", self.join(&items, tx, |w, t, tx| w.individual(t, tx))?),+ _ if items.iter().all(|t| term_iri(t).is_some()) && items.iter().any(|t| self.ix.is_data_property(t)) => {+ ("DisjointDataProperties", self.join(&items, tx, |w, t, _| w.property_iri(t, "data property"))?)+ }+ _ => ("DisjointObjectProperties", self.join(&items, tx, |w, t, tx| w.ope(t, tx))?),+ }+ }+ "NegativePropertyAssertion" => {+ let source = format!("{OWL}sourceIndividual");+ let prop = format!("{OWL}assertionProperty");+ let target_i = format!("{OWL}targetIndividual");+ let target_v = format!("{OWL}targetValue");+ let mut want = vec![prop.as_str(), source.as_str()];+ let target = if node.reserved.contains_key(target_v.as_str()) { &target_v } else { &target_i };+ want.push(target.as_str());+ want.sort();+ if shape != want {+ return Err("an owl:NegativePropertyAssertion whose components match no row of Table 1".into());+ }+ let a = self.individual(take(&source, tx)?, tx)?;+ let p = take(&prop, tx)?;+ let v = take(target, tx)?;+ match v {+ Term::Literal(l) if target == &target_v => {+ ("NegativeDataPropertyAssertion", format!("{} {a} {}", self.property_iri(p, "data property")?, self.literal(l)?))+ }+ v if target == &target_i => {+ ("NegativeObjectPropertyAssertion", format!("{} {a} {}", self.ope(p, tx)?, self.individual(v, tx)?))+ }+ _ => return Err("an owl:NegativePropertyAssertion whose target has the wrong kind".into()),+ }+ }+ _ => ("DLSafeRule", self.rule(&node, &shape, tx)?),+ };+ tx.used.push(ti);+ tx.inline.push(x.clone());+ let anns = self.annotations(&x.clone().into(), 0, tx)?;+ Ok(Self::emit(kw, &[anns], &body))+ }++ // ---- SWRL ----------------------------------------------------------- //++ fn rule(&self, node: &Node<'a>, shape: &[&str], tx: &mut Tx) -> R<String> {+ let body = format!("{SWRL}body");+ let head = format!("{SWRL}head");+ if shape != [body.as_str(), head.as_str()] {+ return Err("a swrl:Imp without exactly one swrl:body and swrl:head".into());+ }+ let mut parts = Vec::new();+ for (kw, p) in [("Body", &body), ("Head", &head)] {+ let (i, list) = node.one(p).ok_or("a swrl:Imp body or head given more than once")?;+ tx.used.push(i);+ let atoms = self.list(list, true, tx)?;+ parts.push(format!("{kw}({})", self.join(&atoms, tx, |w, t, tx| w.atom(t, tx))?));+ }+ Ok(parts.join(" "))+ }++ fn variable(&self, t: &Term, tx: &mut Tx) -> Option<String> {+ let n = term_iri(t)?;+ if !self.ix.kinds.variable.contains(n) {+ return None;+ }+ let ty = Triple::new(n.clone(), NamedNode::new_unchecked(RDF_TYPE), NamedNode::new_unchecked(format!("{SWRL}Variable")));+ if let Some(&i) = self.ix.ids(&n.clone().into()).iter().find(|&&i| self.t(i) == &ty) {+ tx.used.push(i);+ }+ Some(format!("Variable({})", self.iri(n)))+ }++ fn i_arg(&self, t: &Term, tx: &mut Tx) -> R<String> {+ match self.variable(t, tx) {+ Some(v) => Ok(v),+ None => self.individual(t, tx),+ }+ }++ fn d_arg(&self, t: &Term, tx: &mut Tx) -> R<String> {+ match (self.variable(t, tx), t) {+ (Some(v), _) => Ok(v),+ (None, Term::Literal(l)) => self.literal(l),+ _ => Err("a SWRL data argument that is neither a variable nor a literal".into()),+ }+ }++ fn atom(&self, t: &Term, tx: &mut Tx) -> R<String> {+ let Some(b) = term_blank(t) else {+ return Err("a SWRL atom that is not a blank node".into());+ };+ let node = self.ix.node(b);+ let Some((ti, ty)) = node.one(RDF_TYPE) else {+ return Err("a SWRL atom without exactly one rdf:type".into());+ };+ let shape: Vec<&str> = node.shape().into_iter().filter_map(|p| p.strip_prefix(SWRL)).collect();+ if shape.len() != node.shape().len() {+ return Err("a SWRL atom with a predicate outside the swrl: vocabulary".into());+ }+ let take = |l: &str, tx: &mut Tx| -> R<&'a Term> {+ let (i, t) = node.one(&format!("{SWRL}{l}")).ok_or("a SWRL atom component given more than once")?;+ tx.used.push(i);+ Ok(t)+ };+ let kind = term_iri(ty).and_then(|c| local(c, SWRL)).unwrap_or("");+ let out = match (kind, &shape[..]) {+ ("ClassAtom", ["argument1", "classPredicate"]) => {+ let c = self.ce(take("classPredicate", tx)?, tx)?;+ format!("ClassAtom({c} {})", self.i_arg(take("argument1", tx)?, tx)?)+ }+ ("DataRangeAtom", ["argument1", "dataRange"]) => {+ let d = self.dr(take("dataRange", tx)?, tx)?;+ format!("DataRangeAtom({d} {})", self.d_arg(take("argument1", tx)?, tx)?)+ }+ ("IndividualPropertyAtom", ["argument1", "argument2", "propertyPredicate"]) => {+ let p = self.ope(take("propertyPredicate", tx)?, tx)?;+ let a = self.i_arg(take("argument1", tx)?, tx)?;+ format!("ObjectPropertyAtom({p} {a} {})", self.i_arg(take("argument2", tx)?, tx)?)+ }+ ("DatavaluedPropertyAtom", ["argument1", "argument2", "propertyPredicate"]) => {+ let p = self.property_iri(take("propertyPredicate", tx)?, "data property")?;+ let a = self.i_arg(take("argument1", tx)?, tx)?;+ format!("DataPropertyAtom({p} {a} {})", self.d_arg(take("argument2", tx)?, tx)?)+ }+ ("BuiltinAtom", ["arguments", "builtin"]) => {+ let f = self.property_iri(take("builtin", tx)?, "built-in")?;+ let args = self.list(take("arguments", tx)?, false, tx)?;+ let args = self.join(&args, tx, |w, t, tx| w.d_arg(t, tx))?;+ if args.is_empty() { format!("BuiltInAtom({f})") } else { format!("BuiltInAtom({f} {args})") }+ }+ (k @ ("SameIndividualAtom" | "DifferentIndividualsAtom"), ["argument1", "argument2"]) => {+ let a = self.i_arg(take("argument1", tx)?, tx)?;+ format!("{k}({a} {})", self.i_arg(take("argument2", tx)?, tx)?)+ }+ _ => return Err("a SWRL atom whose type and arguments match no atom the reader writes".into()),+ };+ tx.used.push(ti);+ tx.inline.push(b.clone());+ Ok(out)+ }++ // ---- the n-ary chains ----------------------------------------------- //++ /// `EquivalentClasses` and `EquivalentObjectProperties` over n operands+ /// are n-1 pairwise triples (Section 2.3.2). Written back pairwise they+ /// read back the same, EXCEPT where an operand is anonymous: two axioms+ /// would write it twice and the reader would make two nodes of it. So an+ /// anonymous node with exactly one incoming and one outgoing link joins+ /// its two links into one axiom.+ fn chains(&self, predicate: &str, run: &mut Run) {+ let edges: Vec<usize> = (0..self.ix.triples.len())+ .filter(|&i| self.t(i).predicate.as_str() == predicate)+ .collect();+ let mut out: HashMap<Term, Vec<usize>> = HashMap::new();+ let mut inc: HashMap<&Term, usize> = HashMap::new();+ for &e in &edges {+ out.entry(subject_term(&self.t(e).subject)).or_default().push(e);+ *inc.entry(&self.t(e).object).or_default() += 1;+ }+ let joint = |t: &Term| {+ term_blank(t).is_some() && inc.get(t) == Some(&1) && out.get(t).is_some_and(|v| v.len() == 1)+ };+ for &start in &edges {+ if joint(&subject_term(&self.t(start).subject)) {+ continue;+ }+ let mut path = vec![start];+ let mut cur = &self.t(start).object;+ while joint(cur) {+ let next = out[cur][0];+ if path.contains(&next) {+ break;+ }+ path.push(next);+ cur = &self.t(next).object;+ }+ self.attempt(run, &path, |tx| self.chain(&path, predicate, tx));+ }+ }++ fn chain(&self, path: &[usize], predicate: &str, tx: &mut Tx) -> R<Vec<String>> {+ let mut operands: Vec<Term> = vec![subject_term(&self.t(path[0]).subject)];+ operands.extend(path.iter().map(|&e| self.t(e).object.clone()));+ let mut sets = None;+ for &e in path {+ let s = self.axiom_annotations(self.t(e), tx);+ match &sets {+ None => sets = Some(s),+ Some(prev) if *prev == s => {}+ Some(_) => return Err("the links of one n-ary axiom carry different annotations".into()),+ }+ }+ let sets = sets.unwrap_or_default();+ let (kw, body) = if predicate.ends_with("equivalentClass") {+ let (a, b) = (&operands[0], &operands[1]);+ let named_datatype = term_iri(a).is_some_and(|n| self.ix.kinds.datatype.contains(n));+ if operands.len() == 2 && term_iri(a).is_some() && (named_datatype || (term_blank(b).is_some() && self.ix.is_data_range(b))) {+ ("DatatypeDefinition", format!("{} {}", self.iri_of(a), self.dr(b, tx)?))+ } else {+ ("EquivalentClasses", self.join(&operands, tx, |w, t, tx| w.ce(t, tx))?)+ }+ } else if operands.iter().all(|t| term_iri(t).is_some()) && operands.iter().any(|t| self.ix.is_data_property(t)) {+ ("EquivalentDataProperties", self.join(&operands, tx, |w, t, _| w.property_iri(t, "data property"))?)+ } else {+ ("EquivalentObjectProperties", self.join(&operands, tx, |w, t, tx| w.ope(t, tx))?)+ };+ Ok(Self::emit(kw, &sets, &body))+ }++ // ---- passes --------------------------------------------------------- //++ /// Render one axiom transactionally: on success its triples are consumed+ /// and its lines kept; on failure nothing is consumed and every triple it+ /// touched carries the reason, so the report says why and not only what.+ fn attempt(&self, run: &mut Run, trigger: &[usize], f: impl FnOnce(&mut Tx) -> R<Vec<String>>) -> bool {+ let mut tx = Tx::default();+ tx.used.extend(trigger);+ match f(&mut tx) {+ Ok(lines) => {+ for &i in &tx.used {+ run.consumed[i] = true;+ }+ for b in tx.inline {+ *run.inline_uses.entry(b).or_default() += lines.len();+ }+ run.anons.extend(tx.anons);+ run.lines.extend(lines);+ true+ }+ // The empty reason is "this triple begins no axiom", which is not+ // a failure and must not displace a real reason.+ Err(why) if why.is_empty() => false,+ Err(why) => {+ for &i in trigger.iter().chain(&tx.used) {+ let slot = run.why.entry(i).or_insert_with(|| why.clone());+ if why < *slot {+ *slot = why.clone();+ }+ }+ false+ }+ }+ }++ fn header(&self, run: &mut Run) {+ let ontology_type = format!("{OWL}Ontology");+ let mut heads: Vec<usize> = (0..self.ix.triples.len())+ .filter(|&i| {+ let t = self.t(i);+ t.predicate.as_str() == RDF_TYPE && term_iri(&t.object).is_some_and(|c| c.as_str() == ontology_type)+ })+ .collect();+ // One header: the named ontology with the smallest IRI if there is+ // one, else an anonymous one. Any other owl:Ontology node is left in+ // the report, because OFN has one ontology per document.+ heads.sort_by_key(|&i| match &self.t(i).subject {+ NamedOrBlankNode::NamedNode(n) => (0, n.as_str().to_string()),+ NamedOrBlankNode::BlankNode(_) => (1, String::new()),+ });+ let Some(&head) = heads.first() else {+ run.header_synthesised = true;+ run.header_line = "Ontology(".into();+ return;+ };+ if heads.len() > 1 && matches!(self.t(heads[1]).subject, NamedOrBlankNode::BlankNode(_)) && matches!(self.t(head).subject, NamedOrBlankNode::BlankNode(_)) {+ // Two anonymous ontologies: which one is "the" header would depend+ // on blank-node labels, so neither is chosen.+ run.header_synthesised = true;+ run.header_line = "Ontology(".into();+ return;+ }+ run.consumed[head] = true;+ let subject = self.t(head).subject.clone();+ let mut line = "Ontology(".to_string();+ if let NamedOrBlankNode::NamedNode(o) = &subject {+ line.push_str(&self.iri(o));+ let version: Vec<usize> = self.ix.ids(&subject).iter().copied()+ .filter(|&i| self.t(i).predicate.as_str() == format!("{OWL}versionIRI") && term_iri(&self.t(i).object).is_some())+ .collect();+ if let [v] = version[..] {+ run.consumed[v] = true;+ line.push(' ');+ line.push_str(&self.iri_of(&self.t(v).object));+ }+ }+ run.header_line = line;+ for &i in self.ix.ids(&subject) {+ let t = self.t(i);+ if t.predicate.as_str() == format!("{OWL}imports") {+ if let Some(n) = term_iri(&t.object) {+ run.consumed[i] = true;+ run.header.insert(format!("Import({})", self.iri(n)));+ }+ } else if is_annotation_predicate(&t.predicate) {+ let mut tx = Tx::default();+ if let Ok(lines) = self.annotation(i, 0, &mut tx) {+ for &u in &tx.used {+ run.consumed[u] = true;+ }+ run.anons.extend(tx.anons);+ run.header.extend(lines);+ }+ }+ }+ }++ fn run(&self) -> Run {+ let n = self.ix.triples.len();+ let mut run = Run { consumed: vec![false; n], ..Run::default() };+ self.header(&mut run);++ // Pass 1: every axiom that has a trigger triple of its own. Rendering+ // here never looks at what another axiom consumed, so the outcome does+ // not depend on the order triples are visited in, and that order is+ // the one thing that follows blank-node labels.+ for p in ["equivalentClass", "equivalentProperty"] {+ self.chains(&format!("{OWL}{p}"), &mut run);+ }+ for i in 0..n {+ self.attempt(&mut run, &[i], |tx| self.axiom(i, tx).unwrap_or_else(|| Err(String::new())));+ }++ // A reification or an axiom-rooted node that did not render is still+ // not an individual: writing its leftover triples as assertions about+ // `_:x` would put half of a failed axiom into the output.+ for x in self.ix.axiom_reifications.values().chain(self.ix.annotation_reifications.values()).flatten() {+ run.inline_uses.entry(x.clone()).or_insert(0);+ }+ for t in &self.ix.triples {+ if let (NamedOrBlankNode::BlankNode(x), true) = (&t.subject, t.predicate.as_str() == RDF_TYPE) {+ let rooted = term_iri(&t.object).is_some_and(|c| {+ matches!(local(c, OWL), Some("AllDisjointClasses" | "AllDisjointProperties" | "AllDifferent" | "NegativePropertyAssertion"))+ || local(c, SWRL) == Some("Imp")+ });+ if rooted {+ run.inline_uses.entry(x.clone()).or_insert(0);+ }+ }+ }++ // Pass 2a: ClassAssertion of an ANONYMOUS class, before anything else+ // decides what a blank node is, so the class expressions it writes+ // inline are known when 2b asks.+ let inline_after_pass1: HashSet<BlankNode> = run.inline_uses.keys().cloned().collect();+ for i in 0..n {+ let t = self.t(i);+ if run.consumed[i] || t.predicate.as_str() != RDF_TYPE || term_blank(&t.object).is_none() {+ continue;+ }+ self.attempt(&mut run, &[i], |tx| {+ self.not_inline(&t.subject, &inline_after_pass1)?;+ let body = format!("{} {}", self.ce(&t.object, tx)?, self.individual(&subject_term(&t.subject), tx)?);+ Ok(self.main(i, "ClassAssertion", body, tx))+ });+ }++ // Pass 2b: declarations, named class assertions and the assertions a+ // plain triple is.+ let inline: HashSet<BlankNode> = run.inline_uses.keys().cloned().collect();+ for i in 0..n {+ if run.consumed[i] {+ continue;+ }+ let t = self.t(i);+ if t.predicate.as_str() == RDF_TYPE {+ let Some(class) = term_iri(&t.object) else { continue };+ if let (NamedOrBlankNode::NamedNode(e), Some(kind)) = (&t.subject, declaration_kind(class)) {+ self.attempt(&mut run, &[i], |tx| {+ Ok(self.main(i, "Declaration", format!("{kind}({})", self.iri(e)), tx))+ });+ continue;+ }+ if is_reserved(class) && !matches!(local(class, OWL), Some("Thing" | "Nothing")) {+ continue;+ }+ self.attempt(&mut run, &[i], |tx| {+ self.not_inline(&t.subject, &inline)?;+ let body = format!("{} {}", self.iri(class), self.individual(&subject_term(&t.subject), tx)?);+ Ok(self.main(i, "ClassAssertion", body, tx))+ });+ continue;+ }+ // A `swrl:` predicate is vocabulary inside a rule node, but OWL 2+ // does not reserve the namespace, so on an individual it is an+ // ordinary property: ORE file ore_ont_15753 asserts+ // `ObjectPropertyAssertion(swrl:argument2 _:genid71 ...)`.+ if !is_annotation_predicate(&t.predicate) && !t.predicate.as_str().starts_with(SWRL) {+ continue;+ }+ let mut undeclared = false;+ let ok = self.attempt(&mut run, &[i], |tx| {+ self.not_inline(&t.subject, &inline)?;+ if let Term::BlankNode(b) = &t.object {+ self.not_inline(&b.clone().into(), &inline)?;+ }+ let s = self.individual(&subject_term(&t.subject), tx)?;+ let p = &t.predicate;+ let declared_ap = self.ix.kinds.annotation.contains(p)+ || BUILT_IN_ANNOTATION_PROPERTIES.contains(&p.as_str());+ let (kw, body) = match &t.object {+ Term::Literal(l) if !declared_ap && self.ix.kinds.data.contains(p) => {+ ("DataPropertyAssertion", format!("{} {s} {}", self.iri(p), self.literal(l)?))+ }+ o @ (Term::NamedNode(_) | Term::BlankNode(_)) if !declared_ap && self.ix.kinds.object.contains(p) => {+ ("ObjectPropertyAssertion", format!("{} {s} {}", self.iri(p), self.individual(o, tx)?))+ }+ o => {+ undeclared = !declared_ap;+ ("AnnotationAssertion", format!("{} {s} {}", self.iri(p), self.annotation_value(o, tx)?))+ }+ };+ Ok(self.main(i, kw, body, tx))+ });+ if ok && undeclared {+ run.undeclared_property_assertions += 1;+ }+ }+ run+ }++ fn not_inline(&self, s: &NamedOrBlankNode, inline: &HashSet<BlankNode>) -> R<()> {+ match s {+ NamedOrBlankNode::BlankNode(b) if inline.contains(b) => {+ Err("a triple about an anonymous class expression, data range or list, which no OWL 2 axiom states".into())+ }+ _ => Ok(()),+ }+ }+}++fn cardinality_kind(k: &str) -> &'static str {+ match k {+ "minCardinality" => "MinCardinality",+ "maxCardinality" => "MaxCardinality",+ _ => "ExactCardinality",+ }+}++fn declaration_kind(class: &NamedNode) -> Option<&'static str> {+ if class.as_str() == RDFS_DATATYPE {+ return Some("Datatype");+ }+ match local(class, OWL)? {+ "Class" => Some("Class"),+ "ObjectProperty" => Some("ObjectProperty"),+ "DatatypeProperty" => Some("DataProperty"),+ "AnnotationProperty" => Some("AnnotationProperty"),+ "NamedIndividual" => Some("NamedIndividual"),+ _ => None,+ }+}++impl Run {+ fn finish(self, ix: &Index, names: &Names) -> OfnOutput {+ let mut not_exported: BTreeMap<(String, String), usize> = BTreeMap::new();+ let generic = "no OWL 2 axiom maps to this triple under the W3C mapping (Table 1)";+ let mut exported = 0;+ for (i, t) in ix.triples.iter().enumerate() {+ if self.consumed[i] {+ exported += 1;+ continue;+ }+ let construct = if t.predicate.as_str() == RDF_TYPE {+ match &t.object {+ Term::NamedNode(c) => format!("rdf:type {}", names.iri(c)),+ _ => "rdf:type (anonymous class)".into(),+ }+ } else {+ names.iri(&t.predicate)+ };+ let why = self.why.get(&i).filter(|w| !w.is_empty()).cloned().unwrap_or_else(|| generic.into());+ *not_exported.entry((construct, why)).or_default() += 1;+ }+ let mut unfaithful = Vec::new();+ let shared = self.inline_uses.values().filter(|&&n| n > 1).count();+ let inline: HashSet<&BlankNode> =+ self.inline_uses.iter().filter(|(_, n)| **n > 0).map(|(b, _)| b).collect();+ if shared > 0 {+ unfaithful.push(NotExported {+ construct: "anonymous expression shared by several axioms".into(),+ occurrences: shared,+ why: "OFN writes an anonymous expression inline, so a node the graph shares is written once per \+ axiom and reads back as that many nodes"+ .into(),+ });+ }+ let both = self.anons.iter().filter(|b| inline.contains(b)).count();+ if both > 0 {+ unfaithful.push(NotExported {+ construct: "blank node used both as an anonymous individual and as an anonymous expression".into(),+ occurrences: both,+ why: "OFN keeps the two apart, so the reader gives back two nodes".into(),+ });+ }+ let mut text = String::new();+ for (name, iri) in &names.declared {+ text.push_str(&format!("Prefix({name}:=<{iri}>)\n"));+ }+ text.push('\n');+ text.push_str(&self.header_line);+ text.push('\n');+ for l in &self.header {+ text.push_str(l);+ text.push('\n');+ }+ text.push('\n');+ // Declarations first, as the OWL API writes them, then every other+ // axiom; each group in byte order.+ let (declarations, axioms): (Vec<&String>, Vec<&String>) =+ self.lines.iter().partition(|l| l.starts_with("Declaration("));+ for l in declarations.into_iter().chain(axioms) {+ text.push_str(l);+ text.push('\n');+ }+ text.push_str(")\n");+ OfnOutput {+ text,+ axioms_written: self.lines.len(),+ triples_read: ix.triples.len(),+ triples_exported: exported,+ not_exported: not_exported+ .into_iter()+ .map(|((construct, why), occurrences)| NotExported { construct, occurrences, why })+ .collect(),+ not_round_trippable: unfaithful,+ header_synthesised: self.header_synthesised,+ undeclared_property_assertions: self.undeclared_property_assertions,+ }+ }+}diff --git a/src/server.rs b/src/server.rsindex 4821fdd2..7e22cfc6 100644--- a/src/server.rs+++ b/src/server.rs@@ -332,8 +332,26 @@ impl OpenOntologiesServer {}}- #[tool(name = "onto_convert", description = "Convert an ontology file to turtle, ntriples, rdfxml, nquads or trig. Reads every format onto_load reads, OWL 2 functional syntax (OFN) included, so this is also the way to turn an OFN document into RDF.")]+ #[tool(name = "onto_convert", description = "Convert an ontology file to turtle, ntriples, rdfxml, nquads, trig or ofn (OWL 2 functional syntax). Reads every format onto_load reads, OFN included, so this also turns an OFN document into RDF and back. `to: \"ofn\"` writes the inverse of the reader's W3C mapping, one axiom per line, in a byte-identical order on every run, and returns JSON: `content` (or `path` when `output` is given) plus `constructs_not_exported`, which names and counts every triple that maps back to no OWL 2 axiom (an unrecognised blank-node structure, a malformed rdf:List, a reserved-vocabulary triple outside any axiom) instead of dropping it, and `constructs_not_round_trippable` for what was written but will not read back as the same graph.")]async fn onto_convert(&self, Parameters(input): Parameters<OntoConvertInput>) -> String {+ if crate::ofn_writer::is_ofn_format(&input.to) {+ return match crate::ofn_writer::convert_file(&input.path) {+ Ok(out) => {+ let mut report = out.report_json();+ if let Some(output) = input.output {+ if let Err(e) = std::fs::write(&output, &out.text) {+ return Self::err_json(e);+ }+ report["ok"] = serde_json::json!(true);+ report["path"] = serde_json::json!(output);+ } else {+ report["content"] = serde_json::json!(out.text);+ }+ report.to_string()+ }+ Err(e) => Self::err_json(e),+ };+ }let store = GraphStore::new();match store.load_file(&input.path) {Ok(_) => {diff --git a/tests/ofn_writer_test.rs b/tests/ofn_writer_test.rsnew file mode 100644index 00000000..00c6a175--- /dev/null+++ b/tests/ofn_writer_test.rs@@ -0,0 +1,426 @@+//! The OWL 2 functional-syntax WRITER (issue #322) against its reader.+//!+//! The writer claims to be the inverse of `src/ofn.rs`, so the test of it is+//! the round trip: for an OFN document `D`, `G = ofn::parse(D)`, and+//! `ofn::parse(write_ofn(G))` must be RDFC-1.0-isomorphic to `G`. Two further+//! properties are checked on every case: a second parse of `D`, which hands+//! out different blank-node labels, writes byte-identical text, and writing+//! the re-read graph gives the same text again (the output is a fixpoint).+//!+//! `ore_pool_round_trip` runs the same three checks over the ORE 2015 corpus+//! when `OO_ORE_POOL` points at its `files` directory. The corpus is not in+//! the repository (it is git-ignored under benchmark/reasoner/corpora), so+//! that test is `#[ignore]`d and its measured result is recorded in the+//! CHANGELOG entry for #322.++mod common;++use std::collections::BTreeSet;++use open_ontologies::canon::{canonical_nquads, HashAlgorithm, DEFAULT_CALL_BUDGET};+use open_ontologies::graph::GraphStore;+use open_ontologies::ofn;+use open_ontologies::ofn_writer::{write_ofn, OfnOutput};+use oxigraph::io::{RdfFormat, RdfParser};+use oxigraph::model::{GraphName, Quad, Triple};++fn quads(triples: &[Triple]) -> Vec<Quad> {+ triples.iter().map(|t| t.clone().in_graph(GraphName::DefaultGraph)).collect()+}++/// The graph in canonical N-Quads. The in-house RDFC-1.0 is tried first; a+/// graph whose blank-node symmetry exhausts its budget is canonicalised by+/// Oxigraph's RDFC-1.0, which has no budget, and that is said in the result.+fn canonical(triples: &[Triple]) -> (String, &'static str) {+ if let Ok(c) = canonical_nquads(&quads(triples), DEFAULT_CALL_BUDGET, HashAlgorithm::Sha256) {+ return (c, "canon.rs");+ }+ let store = GraphStore::new();+ let nt: String = triples.iter().map(|t| format!("{t} .\n")).collect();+ store.load_ntriples(&nt).unwrap();+ let mut lines: Vec<String> = store+ .canonicalize_blank_nodes()+ .unwrap()+ .all_triples()+ .unwrap()+ .into_iter()+ .map(|(s, p, o)| format!("{s} {p} {o} .\n"))+ .collect();+ lines.sort();+ lines.dedup();+ (lines.concat(), "oxigraph")+}++fn diff(a: &str, b: &str) -> String {+ let a: BTreeSet<&str> = a.lines().collect();+ let b: BTreeSet<&str> = b.lines().collect();+ format!(+ "only in the original:\n {}\nonly after the round trip:\n {}",+ a.difference(&b).copied().collect::<Vec<_>>().join("\n "),+ b.difference(&a).copied().collect::<Vec<_>>().join("\n ")+ )+}++/// Why `doc` does not round-trip, or `None` when it does. Every failure mode+/// is a message rather than a panic so the corpus run can count them.+fn round_trip_failure(doc: &str) -> Option<String> {+ let prefixes = ofn::declared_prefixes(doc);+ let g = ofn::parse(doc).ok()?;+ let out = match write_ofn(&g, &prefixes) {+ Ok(o) => o,+ Err(e) => return Some(format!("writer refused: {e}")),+ };+ if !out.not_exported.is_empty() || !out.not_round_trippable.is_empty() {+ return Some(format!(+ "not exported: {:?}; not round-trippable: {:?}",+ out.not_exported, out.not_round_trippable+ ));+ }+ let back = match ofn::parse(&out.text) {+ Ok(b) => b,+ Err(e) => return Some(format!("the reader rejects the writer's output: {e}")),+ };+ let (want, _) = canonical(&g);+ let (got, _) = canonical(&back);+ if want != got {+ return Some(format!("not isomorphic\n{}", diff(&want, &got)));+ }+ let again = write_ofn(&ofn::parse(doc).unwrap(), &prefixes).unwrap();+ if again.text != out.text {+ return Some("two runs gave different bytes".into());+ }+ let fixpoint = write_ofn(&back, &ofn::declared_prefixes(&out.text)).unwrap();+ if fixpoint.text != out.text {+ return Some("writing the re-read graph gave different bytes".into());+ }+ None+}++fn assert_round_trips(doc: &str) {+ if let Some(why) = round_trip_failure(doc) {+ let out = write_ofn(&ofn::parse(doc).unwrap(), &ofn::declared_prefixes(doc)).unwrap();+ panic!("{why}\n--- written ---\n{}", out.text);+ }+}++fn document(axioms: &str) -> String {+ format!(+ "Prefix(:=<http://e.org/#>)\nPrefix(swrlb:=<http://www.w3.org/2003/11/swrlb#>)\nOntology(<http://e.org/o>\n{axioms}\n)\n"+ )+}++fn write_turtle(ttl: &str) -> OfnOutput {+ let triples: Vec<Triple> = RdfParser::from_format(RdfFormat::Turtle)+ .for_slice(ttl.as_bytes())+ .map(|q| Triple::from(q.unwrap()))+ .collect();+ write_ofn(&triples, &[("".into(), "http://e.org/#".into())]).unwrap()+}++// --------------------------------------------------------------------------- //+// Round trips+// --------------------------------------------------------------------------- //++#[test]+fn the_all_constructs_fixture_round_trips_exactly_and_deterministically() {+ let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/ofn/all_constructs.ofn");+ assert_round_trips(&std::fs::read_to_string(path).unwrap());+}++/// The structures the fixture does not reach and the writer has a rule for:+/// an anonymous operand in the middle of an annotated n-ary chain (written+/// pairwise it would be two nodes), nested annotation reifications, two axioms+/// that differ only in their annotations, annotated axioms rooted at a blank+/// node, an anonymous ontology, and every SWRL atom.+#[test]+fn the_structures_the_fixture_does_not_reach_round_trip() {+ for axioms in [+ "EquivalentClasses(Annotation(:n \"x\") :A ObjectSomeValuesFrom(:r :B) ObjectIntersectionOf(:C :D) :E)",+ "EquivalentObjectProperties(:p ObjectInverseOf(:q) :s)",+ "SubClassOf(Annotation(Annotation(Annotation(:n \"3\") :n \"2\") :n \"1\") :A ObjectUnionOf(:B :C))",+ "SubClassOf(Annotation(:n \"one\") :A ObjectSomeValuesFrom(:r :B))\nSubClassOf(Annotation(:n \"two\") :A ObjectSomeValuesFrom(:r :B))",+ "DifferentIndividuals(Annotation(:n \"d\") :a _:b :c)\nNegativeDataPropertyAssertion(Annotation(:n \"x\") :age _:b \"3\"^^xsd:integer)",+ "SameIndividual(:a _:x :b)\nClassAssertion(ObjectOneOf(_:x :c) _:y)\nAnnotationAssertion(:n _:y _:x)",+ "Declaration(DataProperty(:dp))\nSubClassOf(:A DataSomeValuesFrom(:dp :dp2 xsd:integer))\nSubClassOf(:A DataMinCardinality(0 :dp))",+ "Declaration(DataProperty(:age))\nHasKey(Annotation(:n \"k\") ObjectSomeValuesFrom(:r :B) (:r ObjectInverseOf(:s)) (:age))",+ "DLSafeRule(Annotation(:n \"rule\") Body(ClassAtom(:A Variable(:x)) ObjectPropertyAtom(:r Variable(:x) Variable(:y)) DataPropertyAtom(:age Variable(:x) Variable(:v)) BuiltInAtom(swrlb:greaterThan Variable(:v) \"18\"^^xsd:integer) DataRangeAtom(xsd:integer Variable(:v))) Head(SameIndividualAtom(Variable(:x) :i) DifferentIndividualsAtom(Variable(:y) _:z) ClassAtom(ObjectComplementOf(:B) Variable(:y))))",+ "DLSafeRule(Body() Head(BuiltInAtom(swrlb:now)))",+ "Declaration(AnnotationProperty(:n))\nAnnotationAssertion(Annotation(:n \"on an annotation assertion\") rdfs:comment :A \"text\"@en)",+ "Declaration(Class(owl:Thing))\nSubClassOf(:A owl:Nothing)\nClassAssertion(owl:Thing :i)",+ "DisjointUnion(Annotation(:n \"u\") :U ObjectSomeValuesFrom(:r :A) :B)",+ "SubObjectPropertyOf(ObjectPropertyChain(ObjectInverseOf(:r) :s) ObjectInverseOf(:t))",+ "DataPropertyAssertion(:age :i \"multi\nline\")\nAnnotationAssertion(rdfs:label <http://e.org/weird%20iri> \"x\")",+ ] {+ assert_round_trips(&document(axioms));+ }+ // Six anonymous individuals that look alike unless the class each hangs+ // under is part of what labels them (ORE file ore_ont_15184 has this+ // shape): with the wrong labelling the 720 permutations make two runs+ // agree by luck once in 720.+ let alike: String = (0..6)+ .map(|i| format!("SubClassOf(:C{i} ObjectHasValue(:d _:a{i}))\nAnnotationAssertion(:n _:a{i} \"v\")\n"))+ .collect();+ assert_round_trips(&document(&alike));+ assert_round_trips("Ontology(\nSubClassOf(<http://e.org/A> <http://e.org/B>)\n)");+}++#[test]+fn output_is_one_axiom_per_line_with_declared_prefixes_and_declarations_first() {+ let doc = document("SubClassOf(:B :A)\nDeclaration(Class(:A))\nAnnotationAssertion(rdfs:label :A \"a\")");+ let out = write_ofn(&ofn::parse(&doc).unwrap(), &ofn::declared_prefixes(&doc)).unwrap();+ let body: Vec<&str> = out.text.lines().filter(|l| !l.is_empty()).collect();+ assert!(body.contains(&"Prefix(:=<http://e.org/#>)"), "{}", out.text);+ assert!(body.contains(&"Prefix(owl:=<http://www.w3.org/2002/07/owl#>)"), "{}", out.text);+ let start = body.iter().position(|l| *l == "Ontology(:o").or(body.iter().position(|l| *l == "Ontology(<http://e.org/o>"));+ let start = start.unwrap_or_else(|| panic!("no header in\n{}", out.text));+ assert_eq!(+ &body[start + 1..],+ &[+ "Declaration(Class(:A))",+ "AnnotationAssertion(rdfs:label :A \"a\")",+ "SubClassOf(:B :A)",+ ")"+ ],+ "{}",+ out.text+ );+ assert_eq!(out.axioms_written, 3);+ assert_eq!(out.triples_exported, out.triples_read);+}++// --------------------------------------------------------------------------- //+// What does not map back+// --------------------------------------------------------------------------- //++const PLANTED: &str = "@prefix : <http://e.org/#> .+@prefix owl: <http://www.w3.org/2002/07/owl#> .+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+<http://e.org/o> a owl:Ontology .+:Good a owl:Class .+:Good rdfs:subClassOf :Better .+# A restriction with no filler: no row of Table 1.+:Planted rdfs:subClassOf [ a owl:Restriction ; owl:onProperty :p ] .+# A union whose list cell has no rdf:rest.+:Broken owl:equivalentClass [ a owl:Class ; owl:unionOf _:cell ] .+_:cell rdf:first :X .+# RDFS vocabulary with no OWL 2 axiom behind it.+:p a rdf:Property .+";++fn report_of(out: &OfnOutput) -> Vec<(String, usize)> {+ out.not_exported.iter().map(|n| (n.construct.clone(), n.occurrences)).collect()+}++#[test]+fn a_planted_unmappable_structure_is_reported_by_name_and_count_and_not_written() {+ let out = write_turtle(PLANTED);+ let report = report_of(&out);+ for want in [+ ("rdfs:subClassOf", 1),+ ("rdf:type owl:Restriction", 1),+ ("owl:onProperty", 1),+ ("owl:equivalentClass", 1),+ ("rdf:type owl:Class", 1),+ ("owl:unionOf", 1),+ ("rdf:first", 1),+ ("rdf:type rdf:Property", 1),+ ] {+ assert!(+ report.contains(&(want.0.to_string(), want.1)),+ "{want:?} missing from {:#?}",+ out.not_exported+ );+ }+ assert_eq!(report.iter().map(|(_, n)| n).sum::<usize>(), 8, "{:#?}", out.not_exported);+ // Every triple is accounted for: written or reported.+ assert_eq!(out.triples_exported + 8, out.triples_read);+ let why = |c: &str| out.not_exported.iter().find(|n| n.construct == c).unwrap().why.clone();+ assert!(why("owl:onProperty").contains("Table 1"), "{}", why("owl:onProperty"));+ assert!(why("rdf:first").contains("rdf:List"), "{}", why("rdf:first"));++ // And none of it is in the document.+ for absent in [":Planted", ":Broken", ":X", "ObjectUnionOf", "rdf:Property", ":p"] {+ assert!(!out.text.contains(absent), "`{absent}` was written:\n{}", out.text);+ }+ assert!(out.text.contains("SubClassOf(:Good :Better)"), "{}", out.text);+ assert!(out.text.contains("Declaration(Class(:Good))"), "{}", out.text);+ // What was written reads back as exactly the triples counted as exported.+ assert_eq!(ofn::parse(&out.text).unwrap().len(), out.triples_exported);+ let json = out.report_json();+ assert_eq!(json["exports_fewer_triples_than_read"], true);+ assert_eq!(json["constructs_not_exported"].as_array().unwrap().len(), out.not_exported.len());+}++#[test]+fn spellings_other_writers_use_are_reported_because_the_reader_would_change_them() {+ let out = write_turtle(+ "@prefix : <http://e.org/#> .+@prefix owl: <http://www.w3.org/2002/07/owl#> .+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+<http://e.org/o> a owl:Ontology .+:A rdfs:subClassOf [ a owl:Restriction ; owl:onProperty :r ; owl:cardinality 1 ] .+[] a owl:AllDifferent ; owl:distinctMembers ( :a :b :c ) .+[] a owl:AllDisjointClasses ; owl:members ( :C :D ) .+:B owl:equivalentClass [ a owl:Class ; owl:oneOf [ a rdf:List ; rdf:first :a ; rdf:rest rdf:nil ] ] .+",+ );+ let why: BTreeSet<String> = out.not_exported.iter().map(|n| n.why.clone()).collect();+ assert!(why.iter().any(|w| w.contains("canonical xsd:nonNegativeInteger")), "{why:#?}");+ assert!(why.iter().any(|w| w.contains("fewer than 3")), "{why:#?}");+ assert!(why.iter().any(|w| w.contains("malformed rdf:List cell")), "{why:#?}");+ assert!(report_of(&out).iter().any(|(c, _)| c == "owl:distinctMembers"), "{:#?}", out.not_exported);+ assert_eq!(out.axioms_written, 0, "{}", out.text);+}++#[test]+fn a_shared_anonymous_expression_is_written_but_flagged_as_not_round_trippable() {+ let out = write_turtle(+ "@prefix : <http://e.org/#> .+@prefix owl: <http://www.w3.org/2002/07/owl#> .+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+<http://e.org/o> a owl:Ontology .+:A rdfs:subClassOf _:r . :B rdfs:subClassOf _:r .+_:r a owl:Restriction ; owl:onProperty :p ; owl:someValuesFrom :C .+",+ );+ assert!(out.not_exported.is_empty(), "{:#?}", out.not_exported);+ assert_eq!(out.not_round_trippable.len(), 1, "{:#?}", out.not_round_trippable);+ assert_eq!(out.not_round_trippable[0].occurrences, 1);+}++#[test]+fn undeclared_properties_are_written_as_annotations_and_counted() {+ let out = write_turtle(+ "@prefix : <http://e.org/#> .+@prefix owl: <http://www.w3.org/2002/07/owl#> .+<http://e.org/o> a owl:Ontology .+:p a owl:ObjectProperty .+:a :p :b . :a :q :c . :a :q \"lit\" .+",+ );+ assert!(out.text.contains("ObjectPropertyAssertion(:p :a :b)"), "{}", out.text);+ assert!(out.text.contains("AnnotationAssertion(:q :a :c)"), "{}", out.text);+ assert_eq!(out.undeclared_property_assertions, 2);+}++#[test]+fn a_graph_without_an_ontology_header_says_the_header_was_added() {+ let out = write_turtle("@prefix : <http://e.org/#> .\n:A <http://www.w3.org/2000/01/rdf-schema#subClassOf> :B .\n");+ assert!(out.header_synthesised);+ assert!(out.text.contains("SubClassOf(:A :B)"), "{}", out.text);+}++/// Anonymous individuals are the only blank nodes whose labels are printed,+/// and they are RDFC-1.0 labels, so two Turtle parses (different labels from+/// the parser) give the same bytes.+#[test]+fn anonymous_individual_labels_are_canonical_across_loads() {+ let ttl = "@prefix : <http://e.org/#> .+@prefix owl: <http://www.w3.org/2002/07/owl#> .+<http://e.org/o> a owl:Ontology .+:n a owl:AnnotationProperty .+_:x a :A ; :n _:y . _:y a :B ; :n \"y\" . _:z :n _:x .+";+ let a = write_turtle(ttl);+ let b = write_turtle(ttl);+ assert_eq!(a.text, b.text);+ assert!(a.text.contains("_:c14n"), "{}", a.text);+ assert!(a.not_exported.is_empty(), "{:#?}", a.not_exported);+}++// --------------------------------------------------------------------------- //+// The CLI and its report+// --------------------------------------------------------------------------- //++#[test]+fn the_cli_convert_command_writes_ofn_and_reports_what_it_left_out() {+ let _gate = common::exec_gate();+ let tmp = tempfile::tempdir().unwrap();+ let fixture = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/ofn/all_constructs.ofn");+ let run = |args: &[&str]| {+ std::process::Command::new(env!("CARGO_BIN_EXE_open-ontologies"))+ .args(["--no-connect", "--data-dir"])+ .arg(tmp.path())+ .args(args)+ .output()+ .unwrap()+ };+ let stdout = run(&["convert", fixture, "--to", "ofn"]);+ assert!(stdout.status.success(), "{}", String::from_utf8_lossy(&stdout.stderr));+ let text = String::from_utf8(stdout.stdout).unwrap();+ assert!(stdout.stderr.is_empty(), "a complete export printed a report: {}", String::from_utf8_lossy(&stdout.stderr));+ let original = ofn::parse(&std::fs::read_to_string(fixture).unwrap()).unwrap();+ assert_eq!(canonical(&ofn::parse(&text).unwrap()).0, canonical(&original).0);++ let planted = tmp.path().join("planted.ttl");+ std::fs::write(&planted, PLANTED).unwrap();+ let out_path = tmp.path().join("planted.ofn");+ let result = run(&["convert", planted.to_str().unwrap(), "--to", "ofn", "--output", out_path.to_str().unwrap(), "--pretty"]);+ assert!(result.status.success(), "{}", String::from_utf8_lossy(&result.stdout));+ let report: serde_json::Value = serde_json::from_slice(&result.stdout).unwrap();+ let names: Vec<&str> = report["constructs_not_exported"]+ .as_array()+ .unwrap()+ .iter()+ .map(|c| c["construct"].as_str().unwrap())+ .collect();+ assert!(names.contains(&"rdf:type rdf:Property"), "{report:#}");+ assert!(std::fs::read_to_string(&out_path).unwrap().contains("SubClassOf(:Good :Better)"));++ // Without --output the document is stdout and the report goes to stderr.+ let piped = run(&["convert", planted.to_str().unwrap(), "--to", "ofn"]);+ assert!(ofn::parse(&String::from_utf8(piped.stdout).unwrap()).is_ok());+ let err: serde_json::Value = serde_json::from_slice(&piped.stderr).unwrap();+ assert_eq!(err["exports_fewer_triples_than_read"], true);+}++// --------------------------------------------------------------------------- //+// The ORE 2015 corpus+// --------------------------------------------------------------------------- //++/// `OO_ORE_POOL=<.../pool_sample/files> cargo test --test ofn_writer_test --+/// --ignored ore_pool_round_trip --nocapture`. `OO_ORE_LIMIT` caps the number+/// of files (smallest first) and `OO_ORE_MAX_BYTES` their size.+#[test]+#[ignore = "needs the ORE 2015 corpus, which is not in the repository"]+fn ore_pool_round_trip() {+ let Ok(dir) = std::env::var("OO_ORE_POOL") else {+ panic!("set OO_ORE_POOL to the ORE 2015 pool_sample/files directory");+ };+ let limit: usize = std::env::var("OO_ORE_LIMIT").ok().and_then(|v| v.parse().ok()).unwrap_or(usize::MAX);+ let max_bytes: u64 = std::env::var("OO_ORE_MAX_BYTES").ok().and_then(|v| v.parse().ok()).unwrap_or(u64::MAX);+ let mut files: Vec<(u64, std::path::PathBuf)> = std::fs::read_dir(&dir)+ .unwrap()+ .map(|e| e.unwrap().path())+ .map(|p| (std::fs::metadata(&p).unwrap().len(), p))+ .filter(|(n, _)| *n <= max_bytes)+ .collect();+ files.sort();+ files.truncate(limit);+ let (mut handled, mut exact, mut refused_by_reader) = (0, 0, 0);+ let mut failures: Vec<(String, String)> = Vec::new();+ for (_, path) in &files {+ let doc = std::fs::read_to_string(path).unwrap();+ if ofn::parse(&doc).is_err() {+ refused_by_reader += 1;+ continue;+ }+ handled += 1;+ match round_trip_failure(&doc) {+ None => exact += 1,+ Some(why) => failures.push((path.file_name().unwrap().to_string_lossy().into(), why)),+ }+ }+ println!(+ "ORE round trip: {exact}/{handled} exact; {refused_by_reader} of {} files refused by the reader",+ files.len()+ );+ for (f, why) in &failures {+ let short: String = why.chars().take(600).collect();+ println!(" {f}: {short}");+ }+ assert!(failures.is_empty(), "{} file(s) did not round-trip", failures.len());+}