Skip to content

Lesson: define a custom vocabulary

Adam Wead edited this page Feb 24, 2014 · 8 revisions

A custom vocabulary is simply a namespace and a set of terms. We encode these into a Ruby class so they are useable by the RDF.rb gem.

We'll start out by defining a vocabulary about images.

Create a lib directory called rdf_vocabularies

mkdir lib/rdf_vocabularies

Then create a file in rdf_vocabularies called image.rb. We'll give it the namespace "http://projecthydra.org/images#". We'll give it three terms: "height", "width", and "color_depth".

class ImageVocabulary < RDF::Vocabulary("http://projecthydra.org/images#")
  property :height
  property :width
  property :color_depth
end

Restart your console session and you can now you can see that each of these properties resolves to a URI:

require "./lib/rdf_vocabularies/image"
=> true
> ImageVocabulary.height
=> #<RDF::URI:0x3fe828f1dee8 URI:http://projecthydra.org/images#height>

Let's add these properties to our class:

class DublinCoreAsset
  include ActiveFedora::RdfObject 
  map_predicates do |map|
    map.title(in: RDF::DC)
    map.creator(in: RDF::DC)
    map.contributor(in: RDF::DC)
    map.date(in: RDF::DC)
    map.subject(in: RDF::DC)
    map.relation(in: RDF::DC)
    map.height(in: ImageVocabulary)
    map.width(in: ImageVocabulary)
    map.color_depth(in: ImageVocabulary)
  end
end

Restart the console session and require the new files. Then when we instantiate the DublinCoreAsset, we can assign values to the properties:

require "./lib/rdf_vocabularies/image"
require "./lib/rdf_types/dublin_core_asset"
graph = RDF::Graph.new
=> #<RDF::Graph:0x3fe8298e01c4(default)>
subject = RDF::URI('http://example.com/1234')
=> #<RDF::URI:0x3fe82995c01c URI:http://example.com/1234>
asset = DublinCoreAsset.new(graph, subject)
=> #<DublinCoreAsset:0x007fd0532c9658 @graph=#<RDF::Graph:0x3fe8298e01c4(default)>, @rdf_subject=#<RDF::URI:0x3fe82995c01c URI:http://example.com/1234>>
>> asset.title="A picture of a waterfall"
=> "A picture of a waterfall"
>> asset.height = "1028px"
=> "1028px"
>> asset.width = "800px"
=> "800px"
>> asset.color_depth = "32bit"
=> "32bit"
>> puts asset.graph.dump(:ntriples)
<http://example.com/1234> <http://purl.org/dc/terms/title> "A picture of a waterfall" .
<http://example.com/1234> <http://projecthydra.org/images#height> "1028px" .
<http://example.com/1234> <http://projecthydra.org/images#width> "800px" .
<http://example.com/1234> <http://projecthydra.org/images#color_depth> "32bit" .
=> nil

Next Step

Go on to Lesson: Define a Complex Network of Related RDF Types or return to the Tame your RDF Metadata with ActiveFedora landing page.

Clone this wiki locally