W3C home > Mailing lists > Public > public-rdf-ruby@w3.org > April 2016

Re: How to define the prefix for a created vocabulary?

From: Gregg Kellogg <gregg@greggkellogg.net>
Date: Wed, 20 Apr 2016 13:20:30 -0700
Cc: "public-rdf-ruby@w3.org" <public-rdf-ruby@w3.org>
Message-Id: <04D3365C-FF5D-4A6A-9434-D3A8D04E6B21@greggkellogg.net>
To: Ian Dickinson <i.j.dickinson@gmail.com>
> On Apr 20, 2016, at 2:15 AM, Ian Dickinson <i.j.dickinson@gmail.com> wrote:
> 
> The .qname() method on URI is quite handy, but it relies on knowing
> the __prefix__ for a vocabulary. For vocabs that I define in my code,
> the constructor takes the base URI of the vocabulary, but I can't see
> any way to define the __prefix__. Am I missing something?

Hi Ian,

No, you’ve got it right. The vocabulary Prefix comes from the class name of the defined vocabulary. Also, note that URI#pname is more commonly used, and a vocabulary term can be found from RDF::Vocabulary.find_term, which can do some sanity checking too.

To make sure the prefix is defined, create a Vocabulary subclass such as RDF::OWL, which will inherit this behavior. You can also create it dynamically such as is done in RDF::Vocabulary.from_graph: https://github.com/ruby-rdf/rdf/blob/develop/lib/rdf/vocabulary.rb#L276-L280.

> For reference, my use case is that I'm creating constants in my code
> to interact with a remote RDF store (i.e. I'm not loading files of
> triples into my Ruby code, which is how I think the prefix usually
> gets set on a vocabulary):
> 
> COMMON = RDF::Vocabulary.new( "http://foo.bar.com/common/" )
> MAN = COMMON.man
> FANFARE = COMMON.fanfare

The problem is that you need to create a sub-class of RDF::Vocabulary. In Vocabulary.from_graph, it’s done like the following:

    COMMON = Class.new(RDF::Vocabulary.create("http://foo.bar.com/common/“))

Then you can do COMMON.man.pname, or COMMON.man.qname. You can also do RDF::Vocabulary.find_term("http://foo.bar.com/common/man").pname.

> This all works very nicely. Later, I'd like to create a slug for the
> resources I'm using, which basically comes down to the local name of
> the resource:
> 
> def slug( uri_resource )
>  uri_resource.qname.second
> end
> 
> Only at the moment, FANFARE.qname returns nil because the common vocab
> doesn't have a __prefix__

It should work as I described. Basically, you need to sub-class RDF::Vocabulary to get it so that RDF::Vocabulary.each will enumerate it.

PR for improved documentation appreciated.

Gregg

> Thanks,
> Ian
> 
Received on Wednesday, 20 April 2016 20:21:00 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 17:02:18 UTC