Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use PrefixMap? #113

Closed
KonradHoeffner opened this issue Mar 22, 2022 · 5 comments
Closed

How to use PrefixMap? #113

KonradHoeffner opened this issue Mar 22, 2022 · 5 comments

Comments

@KonradHoeffner
Copy link
Contributor

I want to shorten URIs shown to users using a prefix map, but following https://docs.rs/sophia/latest/sophia/prefix/trait.PrefixMap.html I do not understand how to construct and fill one. Can you give an example on how it is supposed to be used?

@pchampin
Copy link
Owner

How to build a PrefixMap

The trait PrefixMap is implemented by any slice of (prefix, IRI) pair. For example, a Vec of (PrefixBox, IriBox) would do the trick.

How to use a PrefixMap to abbreviate IRIs

The easiest way is to use the get_prefixed_pair method of PrefixMap. It takes any term, and returns a pair (prefix, suffix) if that term is an IRI and can be abbreviated using one of the prefixes in the map. Otheriwse, it returns None (meaning that you should display the IRI in full).

Hope this helps.

@KonradHoeffner
Copy link
Contributor Author

Thanks for the quick reply! As a Rust beginner, I'm not quite sure what to do but I got as far as:

let mut prefixes: Vec<(PrefixBox,IriBox)> = Vec::new(); 
let pBox: PrefixBox = Prefix::new("http://hitontology.eu/ontology/").unwrap().boxed();

However that causes: thread 'main' panicked at 'called Result::unwrap()on anErr value: InvalidPrefix("http://hitontology.eu/ontology/")', src/main.rs:17:74
What is the error cause there?

@KonradHoeffner
Copy link
Contributor Author

KonradHoeffner commented Mar 22, 2022

Update

I think I got it now:

let mut prefixes: Vec<(PrefixBox,IriBox)> = Vec::new(); 
let hito_prefix: PrefixBox = Prefix::new("hito").unwrap().boxed();                              
let hito_iri: IriBox = Iri::new("http://hitontology.eu/ontology/").unwrap().boxed();
prefixes.push((hito_prefix,hito_iri));

Could this be added as an example to the documentation? (using ? instead of unwrap())

A convenience method to read a prefix map from a turtle file would also be nice and a method to add a prefix map entry such as add(&str prefix,&str iri).

@pchampin
Copy link
Owner

Yes, the lack of documentation is well recognized. My efforts are now focused on a big refactorying of the library. Any new documentation is at risk of being quickly outdated, so unfortunately, it is not my priority at the moment.

About being able to get the prefix map out of a parsed turtle file: I agree. There is an issue about it Rio, the parser library on which Sophia is relying.

@KonradHoeffner
Copy link
Contributor Author

Ah OK, then I will just write a quick convenience method and adapt to the refactoring later for sophia 0.8, thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants