Skip to content

Commit

Permalink
fix: Repaired the way prefixmap qualification works so the informatio…
Browse files Browse the repository at this point in the history
…n about nodes appears with qualified IRIs
  • Loading branch information
labra committed Nov 1, 2024
1 parent 028b3ee commit e0c86fa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
45 changes: 37 additions & 8 deletions prefixmap/src/prefixmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,36 @@ impl PrefixMap {
/// # Ok::<(), PrefixMapError>(())
/// ```
pub fn qualify(&self, iri: &IriS) -> String {
if let Some(qualified) = self.qualify_optional(iri) {
qualified
} else {
format!("<{iri}>")
}
}

/// Qualifies an IRI against a prefix map
/// ```
/// # use std::collections::HashMap;
/// # use prefixmap::PrefixMap;
/// # use prefixmap::PrefixMapError;
/// # use iri_s::*;
/// # use std::str::FromStr;
/// let pm = PrefixMap::from_hashmap(
/// &HashMap::from([
/// ("", "http://example.org/"),
/// ("schema", "http://schema.org/")])
/// )?;
/// let a = IriS::from_str("http://example.org/a")?;
/// assert_eq!(pm.qualify(&a), ":a");
///
/// let knows = IriS::from_str("http://schema.org/knows")?;
/// assert_eq!(pm.qualify(&knows), "schema:knows");
///
/// let other = IriS::from_str("http://other.org/foo")?;
/// assert_eq!(pm.qualify(&other), "<http://other.org/foo>");
/// # Ok::<(), PrefixMapError>(())
/// ```
pub fn qualify_optional(&self, iri: &IriS) -> Option<String> {
let mut founds: Vec<_> = self
.map
.iter()
Expand All @@ -237,22 +267,21 @@ impl PrefixMap {
Some(color) => ":".color(color),
None => ColoredString::from(":"),
};
format!("{}{}{}", prefix_colored, semicolon_colored, rest_colored)
Some(format!(
"{}{}{}",
prefix_colored, semicolon_colored, rest_colored
))
} else {
format!("<{iri}>")
None
};
if self.hyperlink {
format!(
"\u{1b}]8;;{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\",
iri.as_str(),
str
)
str.map(|s| format!("\u{1b}]8;;{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\", s.as_str(), s))
} else {
str
}
}

/// Qualifies an IRI against a prefix map
/// Qualifies an IRI against a prefix map returning the length of the qualified string
/// ```
/// # use std::collections::HashMap;
/// # use prefixmap::PrefixMap;
Expand Down
6 changes: 5 additions & 1 deletion rudof_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,11 @@ where
{
for node in node_selector.iter_node(rdf) {
let subject = node_to_subject(node, rdf)?;
writeln!(writer, "Information about node")?;
writeln!(
writer,
"Information about {}",
rdf.qualify_subject(&subject)
)?;

// Show outgoing arcs
match show_node_mode {
Expand Down
13 changes: 11 additions & 2 deletions sparql_service/src/srdf_data/rdf_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,16 @@ impl SRDFBasic for RdfData {

fn qualify_iri(&self, node: &Self::IRI) -> String {
let iri = IriS::from_str(node.as_str()).unwrap();
self.prefixmap_in_memory().qualify(&iri)
if let Some(graph) = &self.graph {
graph.prefixmap().qualify(&iri)
} else {
for e in self.endpoints.iter() {
if let Some(qualified) = e.prefixmap().qualify_optional(&iri) {
return qualified;
}
}
format!("<{node}>")
}
}

fn qualify_subject(&self, subj: &Self::Subject) -> String {
Expand All @@ -394,7 +403,7 @@ impl SRDFBasic for RdfData {
&self,
prefix: &str,
local: &str,
) -> Result<iri_s::IriS, prefixmap::PrefixMapError> {
) -> Result<IriS, prefixmap::PrefixMapError> {
if let Some(graph) = self.graph() {
let iri = graph.prefixmap().resolve_prefix_local(prefix, local)?;
Ok(iri.clone())
Expand Down
2 changes: 2 additions & 0 deletions srdf/src/srdf_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,7 @@ pub trait SRDFBasic {
fn qualify_term(&self, term: &Self::Term) -> String;

fn prefixmap(&self) -> Option<PrefixMap>;

/// Resolves a a prefix and a local name and obtains the corresponding full `IriS`
fn resolve_prefix_local(&self, prefix: &str, local: &str) -> Result<IriS, PrefixMapError>;
}

0 comments on commit e0c86fa

Please sign in to comment.