Files
tools/publish/src/rdf/ontology.rs
T

390 lines
13 KiB
Rust
Raw Normal View History

2026-06-08 19:33:49 -04:00
use crate::error;
2026-06-17 20:50:26 -04:00
use crate::rdf::vocab::{gl, owl, rda};
2026-06-08 19:33:49 -04:00
use oxigraph::io::{RdfFormat, RdfParser};
use oxigraph::model::vocab::{rdf, rdfs, xsd};
2026-06-23 21:55:52 -04:00
use oxigraph::model::{Dataset, GraphName, LiteralRef, NamedNode, NamedNodeRef, NamedOrBlankNode, NamedOrBlankNodeRef, Quad, Term, TermRef, Triple, TripleRef};
2026-06-08 19:33:49 -04:00
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
2026-06-17 20:50:26 -04:00
use std::collections::{HashMap, HashSet};
use std::fmt::Display;
2026-06-23 21:55:52 -04:00
use std::path::{Path, PathBuf};
use oxigraph::store::Store;
use crate::rdf::materialize;
2026-06-08 19:33:49 -04:00
const PREFIXES: &[(&str, &str)] = &[
("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
("rdfs", "http://www.w3.org/2000/01/rdf-schema#"),
("owl", "http://www.w3.org/2002/07/owl#"),
("xsd", "http://www.w3.org/2001/XMLSchema#"),
("ldp", "http://www.w3.org/ns/ldp#"),
("dc", "http://purl.org/dc/elements/1.1/"),
("posix", "http://www.w3.org/ns/posix/stat#"),
(
"ebucore",
"http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#",
),
("premis", "http://www.loc.gov/premis/rdf/v1#"),
("premis3", "http://www.loc.gov/premis/rdf/v3/"),
("dcterms", "http://purl.org/dc/terms/"),
("fedora", "http://fedora.info/definitions/v4/repository#"),
("rdaa", "http://rdaregistry.info/Elements/a/"),
2026-06-23 21:55:52 -04:00
("rdaad", "http://rdaregistry.info/Elements/a/datatype/"),
2026-06-08 19:33:49 -04:00
("rdac", "http://rdaregistry.info/Elements/c/"),
("rdae", "http://rdaregistry.info/Elements/e/"),
("rdai", "http://rdaregistry.info/Elements/i/"),
("rdam", "http://rdaregistry.info/Elements/m/"),
("rdan", "http://rdaregistry.info/Elements/n/"),
("rdap", "http://rdaregistry.info/Elements/p/"),
("rdat", "http://rdaregistry.info/Elements/t/"),
("rdaw", "http://rdaregistry.info/Elements/w/"),
("rdax", "http://rdaregistry.info/Elements/x/"),
("schema", "https://schema.org/"),
("quill", "http://fedora.quill.lan/rest/"),
("gl", "https://graphofliberty.org/2026/04/ont/"),
];
2026-06-23 21:55:52 -04:00
pub struct OntologyBuilder {
path: Option<PathBuf>,
2026-06-08 19:33:49 -04:00
}
2026-06-23 21:55:52 -04:00
impl OntologyBuilder {
pub fn with_path(mut self, path: impl AsRef<Path>) -> Self {
let path = path.as_ref().to_owned();
self.path = Some(path);
2026-06-08 19:33:49 -04:00
self
}
2026-06-23 21:55:52 -04:00
pub fn build(self) -> error::Result<Ontology> {
let store = if let Some(path) = self.path {
Store::open(path)
} else {
Store::new()
}?;
2026-06-08 19:33:49 -04:00
2026-06-23 21:55:52 -04:00
let prefixes = PREFIXES
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect::<HashMap<String, String>>();
2026-06-08 19:33:49 -04:00
2026-06-23 21:55:52 -04:00
let store = materialize::same_as(store)?;
//materialize::super_classes(&mut dataset);
// Full-text search index field names
//let fields = Self::fields(&dataset);
/*let mut entities: HashMap<NamedNode, Entity> = HashMap::new();
let entity_iris = Self::subclass_of(&dataset, gl::ENTITY)
.chain(Self::subclass_of(&dataset, rda::ENTITY))
.chain([
rdf::PROPERTY.into_owned(),
rdfs::CLASS.into_owned(),
]);
for iri in entity_iris {
let subject = iri.as_ref().into();
let label = dataset.quads_for_pattern(Some(subject), Some(rdfs::LABEL), None, None)
.filter_map(|quad| term_to_string(&quad.object.into_owned()).map(String::from))
.next();
let catalog_id = dataset.quads_for_pattern(Some(subject), Some(gl::CATALOG_ID), None, None)
.filter_map(|quad| term_to_u64(&quad.object.into_owned()))
.next();
if let Some(catalog_id) = catalog_id {
let mut properties = HashSet::new();
for quad in dataset.quads_for_pattern(Some(subject), Some(gl::ASSOCIATED_PROPERTY), None, None) {
if let NamedOrBlankNodeRef::NamedNode(subject) = quad.subject
&& let TermRef::NamedNode(property) = quad.object {
properties.insert(property.into_owned());
2026-06-08 19:33:49 -04:00
}
}
2026-06-23 21:55:52 -04:00
entities.insert(iri, Entity {
label: label.unwrap_or(catalog_id.to_string()),
catalog_id,
properties,
});
2026-06-09 15:40:28 -04:00
}
}
2026-06-23 21:55:52 -04:00
let mut indexed_by = HashMap::new();
for quad in dataset.quads_for_pattern(None, Some(gl::INDEXED_BY_FIELD), None, None) {
if let NamedOrBlankNodeRef::NamedNode(subject) = quad.subject
&& let TermRef::NamedNode(field) = quad.object
{
indexed_by.insert(subject.into_owned(), field.into_owned());
}
}*/
Ok(Ontology {
store,
prefixes,
fields: HashMap::new(),
entities: HashMap::from_iter([(rdf::PROPERTY.into_owned(), Entity {
label: "property".to_string(),
catalog_id: 0,
properties: HashSet::new(),
})]),
indexed_by: HashMap::new(),
})
2026-06-09 15:40:28 -04:00
}
2026-06-23 21:55:52 -04:00
/*fn fields(dataset: &Dataset) -> HashMap<NamedNode, IndexField> {
2026-06-15 19:10:55 -04:00
let query = SparqlEvaluator::new()
.parse_query(
r#"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX gl: <https://graphofliberty.org/2026/04/ont/>
2026-06-17 20:50:26 -04:00
SELECT DISTINCT ?subject ?name ?label {
2026-06-15 19:10:55 -04:00
?subject a gl:IndexField ;
2026-06-17 20:50:26 -04:00
gl:fieldName ?name ;
gl:fieldLabel ?label .
FILTER (langMATCHES(LANG(?label), "en") || !hasLANG(?label))
2026-06-15 19:10:55 -04:00
}"#,
)
.expect("Unable to parse field query");
let mut results = HashMap::new();
if let QueryResults::Solutions(solutions) =
query.on_queryable_dataset(dataset).execute().unwrap()
{
for solution in solutions.filter_map(Result::ok) {
let subject = solution.get("subject").and_then(term_to_named_node);
2026-06-17 20:50:26 -04:00
let name = solution.get("name").and_then(term_to_string);
2026-06-15 19:10:55 -04:00
let label = solution.get("label").and_then(term_to_string);
2026-06-17 20:50:26 -04:00
if let Some(subject) = subject && let Some(name) = name {
2026-06-15 19:10:55 -04:00
let field = IndexField {
2026-06-17 20:50:26 -04:00
name: name.to_string(),
label: label.map(|l| l.to_string()),
2026-06-15 19:10:55 -04:00
};
results.insert(subject.to_owned(), field);
}
}
}
results
}
2026-06-17 20:50:26 -04:00
fn subclass_of(dataset: &Dataset, class: NamedNodeRef<'_>) -> impl Iterator<Item = NamedNode> {
let query = SparqlEvaluator::new()
.parse_query(
format!(
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?class {{
?class (rdfs:subClassOf|^rdfs:subClassOf)* {class} .
}}"
)
.as_str(),
)
.expect("Unable to parse subclass_of query");
if let QueryResults::Solutions(solutions) =
query.on_queryable_dataset(dataset).execute().unwrap()
{
solutions.filter_map(Result::ok).filter_map(|solution| {
if let Some(Term::NamedNode(class)) = solution.get("class") {
Some(class.clone())
} else {
None
}
})
} else {
unreachable!()
}
2026-06-23 21:55:52 -04:00
}*/
2026-06-08 19:33:49 -04:00
}
2026-06-17 20:50:26 -04:00
#[derive(Clone, Debug)]
pub struct LabeledIri {
pub iri: NamedNode,
pub label: String,
}
impl PartialEq for LabeledIri {
fn eq(&self, other: &Self) -> bool {
self.iri == other.iri
}
}
impl Display for LabeledIri {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.label.clone())
}
}
2026-06-09 19:25:47 -04:00
#[derive(Clone, Debug)]
pub struct IriInformation {
2026-06-09 20:09:16 -04:00
pub type_: NamedNode,
2026-06-09 19:25:47 -04:00
pub label: Option<String>,
pub comment: Option<String>,
pub read_only: bool,
2026-06-09 15:40:28 -04:00
}
2026-06-17 20:50:26 -04:00
#[derive(Clone, Debug)]
pub struct Entity {
pub label: String,
pub catalog_id: u64,
pub properties: HashSet<NamedNode>,
}
2026-06-15 19:10:55 -04:00
#[derive(Clone, Debug)]
pub struct IndexField {
2026-06-17 20:50:26 -04:00
pub name: String,
2026-06-15 19:10:55 -04:00
pub label: Option<String>,
}
2026-06-08 19:33:49 -04:00
pub struct Ontology {
2026-06-23 21:55:52 -04:00
store: Store,
2026-06-08 19:33:49 -04:00
prefixes: HashMap<String, String>,
2026-06-17 20:50:26 -04:00
// Resource (Property or Class) -> Rust Type
2026-06-23 21:55:52 -04:00
//iri_info: HashMap<NamedNode, IriInformation>,
2026-06-17 20:50:26 -04:00
// NamedIndividual of class IndexField -> Rust Type
2026-06-15 19:10:55 -04:00
fields: HashMap<NamedNode, IndexField>,
2026-06-17 20:50:26 -04:00
// NamedIndividual of class Entity -> Rust Type
entities: HashMap<NamedNode, Entity>,
// Property -> NamedIndividual of class IndexField
2026-06-15 19:10:55 -04:00
indexed_by: HashMap<NamedNode, NamedNode>,
2026-06-08 19:33:49 -04:00
}
impl Ontology {
2026-06-23 21:55:52 -04:00
pub fn builder() -> OntologyBuilder {
2026-06-08 19:33:49 -04:00
OntologyBuilder {
2026-06-23 21:55:52 -04:00
path: None,
2026-06-08 19:33:49 -04:00
}
}
2026-06-23 21:55:52 -04:00
/*pub fn info(&self, node: NamedNodeRef<'_>) -> Option<&IriInformation> {
2026-06-09 17:20:46 -04:00
let node = node.into_owned();
2026-06-09 19:25:47 -04:00
self.iri_info.get(&node)
2026-06-23 21:55:52 -04:00
}*/
2026-06-08 19:33:49 -04:00
pub fn abbreviate(&self, node: NamedNodeRef<'_>) -> String {
for (prefix_name, prefix_iri) in &self.prefixes {
if let Some(local_name) = node.as_str().strip_prefix(prefix_iri) {
return if local_name.is_empty() {
format!("{prefix_name}:")
} else {
format!("{prefix_name}:{local_name}")
};
}
}
node.as_str().to_string()
}
pub fn expand(&self, prefixed_iri: &str) -> Option<NamedNode> {
let (prefix, name) = prefixed_iri.split_once(':')?;
self.prefixes
.get(prefix)
.map(|base| NamedNode::new_unchecked(format!("{base}{name}")))
}
2026-06-15 19:10:55 -04:00
pub fn field_for_property(&self, property: &NamedNode) -> Option<&IndexField> {
self.indexed_by.get(property)
.and_then(|node| self.fields.get(node))
2026-06-08 19:33:49 -04:00
}
2026-06-17 20:50:26 -04:00
pub fn fields_for_class(&self, class: &NamedNode) -> Vec<&IndexField> {
self.entities.get(class)
.and_then(|entity| {
entity.properties.iter()
.map(|property| self.field_for_property(property))
.filter(Option::is_some)
.collect()
}).unwrap_or(Vec::new())
2026-06-08 19:33:49 -04:00
}
2026-06-17 20:50:26 -04:00
pub fn catalog_id(&self, class: &NamedNode) -> Option<u64> {
self.entities
.get(class)
.map(|entity| entity.catalog_id)
}
pub fn labeled_entity(&self, class: &NamedNode) -> Option<LabeledIri> {
self.entities.get(class)
.map(|entity| LabeledIri {
iri: class.to_owned(),
label: entity.label.to_owned(),
})
}
pub fn labeled_entities(&self) -> impl Iterator<Item = LabeledIri> {
self.entities.iter()
.map(|(iri, entity)| LabeledIri {
iri: iri.to_owned(),
label: entity.label.to_owned(),
})
}
2026-06-23 21:55:52 -04:00
pub fn datatypes(&self) -> impl Iterator<Item = NamedNode> {
self.store
2026-06-08 19:33:49 -04:00
.quads_for_pattern(
None,
Some(rdf::TYPE),
Some(TermRef::NamedNode(rdfs::DATATYPE)),
None,
)
2026-06-23 21:55:52 -04:00
.filter_map(Result::ok)
2026-06-08 19:33:49 -04:00
.filter_map(|quad| match quad.subject {
2026-06-23 21:55:52 -04:00
NamedOrBlankNode::NamedNode(subject) => Some(subject),
2026-06-08 19:33:49 -04:00
_ => None,
})
}
2026-06-10 14:16:25 -04:00
pub fn is_read_only<'a>(&self, triple: impl Into<TripleRef<'a>>) -> bool {
2026-06-23 21:55:52 -04:00
let triple = triple.into();
let true_term = TermRef::Literal(LiteralRef::new_typed_literal("true", xsd::BOOLEAN));
let subject = match (triple.predicate, triple.object) {
(rdf::TYPE, TermRef::NamedNode(class)) => class,
(predicate, _) => predicate,
};
self.store
.quads_for_pattern(Some(NamedOrBlankNodeRef::NamedNode(subject)), Some(gl::READ_ONLY), Some(true_term), None)
.filter_map(Result::ok)
.count() >= 1
2026-06-10 14:16:25 -04:00
}
2026-06-23 21:55:52 -04:00
/*pub fn for_each_annotated_(&self) -> impl Iterator<Item = TripleRef<'_>> {
self.store
.quads_for_pattern()
}*/
2026-06-08 19:33:49 -04:00
pub fn template_triples<'a>(
&'a self,
class: NamedNodeRef<'a>,
subject: NamedNodeRef<'_>,
) -> Option<impl Iterator<Item = Triple>> {
if let Some(quad) = self
2026-06-23 21:55:52 -04:00
.store
2026-06-08 19:33:49 -04:00
.quads_for_pattern(
Some(NamedOrBlankNodeRef::NamedNode(class)),
2026-06-09 15:40:28 -04:00
Some(gl::TEMPLATE),
2026-06-08 19:33:49 -04:00
None,
None,
)
2026-06-23 21:55:52 -04:00
.filter_map(Result::ok)
2026-06-08 19:33:49 -04:00
.next()
{
2026-06-23 21:55:52 -04:00
if let Term::BlankNode(blank_node) = quad.object {
2026-06-08 19:33:49 -04:00
let iter = self
2026-06-23 21:55:52 -04:00
.store
.quads_for_pattern(Some(NamedOrBlankNodeRef::BlankNode(blank_node.as_ref())), None, None, None)
.filter_map(Result::ok)
2026-06-08 19:33:49 -04:00
.map(Triple::from)
.map(move |mut triple| {
triple.subject = NamedOrBlankNode::NamedNode(subject.into_owned());
triple
});
Some(iter)
} else {
None
}
} else {
None
}
}
2026-06-15 19:10:55 -04:00
}