Skip to content

Lesson: Define a custom Vocabulary (AF7)

Brian J Lowe edited this page Apr 24, 2014 · 10 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:

 require "active-fedora"
   
 class DublinCoreAsset < ActiveFedora::Rdf::Resource
     configure type: RDF::URI('http://www.mydomain.mn/metadata/ontologies/foo#SpecialAsset'), base_uri: 'info:fedora'
     property :title, predicate: RDF::DC.title
     property :creator, predicate: RDF::DC.creator
     property :contributor, predicate: RDF::DC.contributor
     property :date, predicate: RDF::DC.date
     property :subject, predicate: RDF::DC.subject
     property :relation, predicate: RDF::DC.relation
     property :height, predicate: ImageVocabulary.height
     property :width, predicate: ImageVocabulary.width
     property :color_depth, predicate: ImageVocabulary.color_depth
 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"
subject = RDF::URI('http://example.com/1234')
=> #<RDF::URI:0x3fe82995c01c URI:http://example.com/1234>
asset = DublinCoreAsset.new(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.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