This commit is contained in:
Alex Wied
2026-06-15 19:10:55 -04:00
parent d6b54137e1
commit 6df00eb5a3
4 changed files with 280 additions and 97 deletions
+57 -8
View File
@@ -8,6 +8,8 @@ use oxigraph::model::{
};
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
use std::collections::HashMap;
use iced::widget::sensor::Key;
use tracing::debug;
const PREFIXES: &[(&str, &str)] = &[
("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
@@ -192,6 +194,42 @@ SELECT DISTINCT ?class ?subject ?label ?comment ?read_only {
results
}
fn fields(dataset: &Dataset) -> HashMap<NamedNode, IndexField> {
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/>
SELECT DISTINCT ?subject ?key ?label {
?subject a gl:IndexField ;
rdfs:value ?key ;
rdfs:label ?label .
FILTER(langMATCHES(LANG(?label), "en") || !hasLANG(?label))
}"#,
)
.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);
let key = solution.get("key").and_then(term_to_string);
let label = solution.get("label").and_then(term_to_string);
if let Some(subject) = subject && let Some(key) = key {
let field = IndexField {
key: key.to_owned(),
label: label.map(String::from),
};
results.insert(subject.to_owned(), field);
}
}
}
results
}
pub fn build(&mut self) -> error::Result<Ontology> {
let prefixes = PREFIXES
.iter()
@@ -211,12 +249,14 @@ SELECT DISTINCT ?class ?subject ?label ?comment ?read_only {
let iri_info = Self::iri_information(&mut dataset);
// Full-text search index field names
let mut field_map = HashMap::new();
let fields = Self::fields(&dataset);
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::Literal(literal) = quad.object
&& let TermRef::NamedNode(field) = quad.object
{
field_map.insert(subject.into_owned(), literal.value().to_string());
indexed_by.insert(subject.into_owned(), field.into_owned());
}
}
@@ -237,7 +277,8 @@ SELECT DISTINCT ?class ?subject ?label ?comment ?read_only {
dataset,
prefixes,
iri_info,
field_map,
fields,
indexed_by,
catalog_ids,
})
}
@@ -251,11 +292,18 @@ pub struct IriInformation {
pub read_only: bool,
}
#[derive(Clone, Debug)]
pub struct IndexField {
pub key: String,
pub label: Option<String>,
}
pub struct Ontology {
dataset: Dataset,
prefixes: HashMap<String, String>,
iri_info: HashMap<NamedNode, IriInformation>,
field_map: HashMap<NamedNode, String>,
fields: HashMap<NamedNode, IndexField>,
indexed_by: HashMap<NamedNode, NamedNode>,
catalog_ids: HashMap<NamedNode, u64>,
}
@@ -324,8 +372,9 @@ impl Ontology {
.map(|base| NamedNode::new_unchecked(format!("{base}{name}")))
}
pub fn field_name(&self, property: &NamedNode) -> Option<&String> {
self.field_map.get(property)
pub fn field_for_property(&self, property: &NamedNode) -> Option<&IndexField> {
self.indexed_by.get(property)
.and_then(|node| self.fields.get(node))
}
pub fn catalog_id(&self, class: &NamedNode) -> Option<u64> {
@@ -435,4 +484,4 @@ SELECT ?class {{
None
}
}
}
}