Skip to content

Lesson: Use RDF to Represent Flat Metadata like Dublin Core (AF6)

Mark Bussey edited this page Mar 11, 2015 · 1 revision

This lesson is known to work with active-fedora version 6.x.
Please update this wiki to reflect any other versions that have been tested.

Goals

  • Define a Ruby Class whose instances Create/Manipulate RDF Nodes with RDF assertions in them

Explanation

The real awesomeness of RDF comes when you start building heterogeneous graphs/networks of linked nodes. However, as you will see, even with a homogeneous network where all of the nodes are the same RDF Type, you can build rich networks of linked data. For this example, we will create nodes with Dublin Core metadata assertions in them.

Steps

Step: Define an "RdfNode" Class for Nodes that use Dublin Core RDF metadata

Note: We call these classes "RDF Types". Within a code base, we prefer to put the their definitions into a directory called rdf_types that lives either in the lib directory or in app/models.

Create a directory called rdf_types and

mkdir lib
mkdir lib/rdf_types

Then create a file called lib/rdf_types/dublin_core_asset.rb and put the following code into it

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)
  end
end

Step: In console Create a Node and inspect its graph

On the command line, run bundle console

Within the console, require the class you've just defined.

require "./lib/rdf_types/dublin_core_asset"
 => true 

Now create a new DublinCoreAsset node and inspect its graph. Note: Currently, the ActiveFedora::RDFObject module doesn't automatically initialize an empty graph for you, so you have to create one and pass it into DublinCoreAsset.new

graph = RDF::Graph.new
subject = RDF::URI('http://example.com/1234')
asset = DublinCoreAsset.new(graph, subject)
 => #<DublinCoreAsset:0x007fbc128f9be0 @graph=#<RDF::Graph:0x3fde0947b388(default)>, @rdf_subject=#<RDF::URI:0x3fd66da75d7c URI:http://example.com/1234>>
puts graph.dump(:ntriples)

 => nil

We haven't added any assertions into the graph yet, so we got an empty string when we dumped it. Now let's add some assertions.

Step: Assert association with a literal as the Subject -- Put String Values into your Graph

Add an assertion that the title of your asset is "My title"

asset.title
 => [] 
asset.title = "My title"
 => "My title" 
asset.title
 => ["My title"] 

Now look at the graph again

puts graph.dump(:ntriples)
<http://example.com/1234> <http://purl.org/dc/terms/title> "My title" .
 => nil 

Now there's an assertion that our node (identified by <http://example.com/1234> in this example) has a dc:title that is the String literal "My title".

Add some more assertions and then look at the graph again.

asset.creator = "Sally Creator"
 => "Sally Creator" 
asset.contributor = "Billy Contributor"
 => "Billy Contributor" 
asset.contributor << "Sgt Labdirector"
 => ["Sgt Labdirector"] 
asset.subject = ["repositories", "small data"]
 => ["repositories", "small data"] 
puts asset.graph.dump(:ntriples)
<http://example.com/1234> <http://purl.org/dc/terms/title> "My title" .
<http://example.com/1234> <http://purl.org/dc/terms/creator> "Sally Creator" .
<http://example.com/1234> <http://purl.org/dc/terms/contributor> "Billy Contributor" .
<http://example.com/1234> <http://purl.org/dc/terms/contributor> "Sgt Labdirector" .
<http://example.com/1234> <http://purl.org/dc/terms/subject> "repositories" .
<http://example.com/1234> <http://purl.org/dc/terms/subject> "small data" .
 => nil 

Step: Assert association with a Node as the Subject -- Create a Relationship between two Nodes

asset2 = DublinCoreAsset.new(RDF::Graph.new)
 => #<DublinCoreAsset:0x007fbc128c3248 @graph=#<RDF::Graph:0x3fde09461b54(default)>, @rdf_subject=#<RDF::Node:0x3fde09461910(_:g70222870878480)>>
asset.relation = asset2
 => #<DublinCoreAsset:0x007fc494fcd130 @graph=#<RDF::Graph:0x3fe24a7e6960(default)>, @rdf_subject=#<RDF::Node:0x3fe24a7e6884(_:g70222870878480)>> 
asset2.rdf_subject.id
 => "g70222870878480" 
puts asset.graph.dump(:ntriples)
_:g70222870990300 <http://purl.org/dc/terms/title> "My title" .
_:g70222870990300 <http://purl.org/dc/terms/creator> "Sally Creator" .
_:g70222870990300 <http://purl.org/dc/terms/contributor> "Billy Contributor" .
_:g70222870990300 <http://purl.org/dc/terms/contributor> "Sgt Labdirector" .
_:g70222870990300 <http://purl.org/dc/terms/subject> "repositories" .
_:g70222870990300 <http://purl.org/dc/terms/subject> "small data" .
_:g70222870990300 <http://purl.org/dc/terms/relation> _:g70222870878480 .
 => nil 
asset.relation.first == asset2.rdf_subject
 => true

Step: Assert the RDF type in your nodes

Often, you will want to add an RDF type assertion into your nodes. To do this, simply call rdf_type within your Class definition and pass in the URI you want to assert into. For example, if you want your DublinCoreAssets to assert that they conform to the 'http://www.mydomain.mn/metadata/ontologies/foo#SpecialAsset' type, you would update your class definition like this:

require "active-fedora"
class DublinCoreAsset
  include ActiveFedora::RdfObject
  rdf_type 'http://www.mydomain.mn/metadata/ontologies/foo#SpecialAsset'
  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)
  end
end

Now restart the console and create an asset

require "./lib/rdf_types/dublin_core_asset"
asset = DublinCoreAsset.new(RDF::Graph.new)
 => #<DublinCoreAsset:0x007faa3ba06580 @graph=#<RDF::Graph:0x3fd51dcd4c18(default)>, @rdf_subject=#<RDF::Node:0x3fd51dd0ddc4(_:g70184560811460)>> 
asset.graph.dump(:ntriples)
 => "_:g70184560811460 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.mydomain.mn/metadata/ontologies/foo#SpecialAsset> .\n"

As you can see, even before we add any assertions our node has an rdf:type assertion pointing to the URI we specified.

Next Step

Go on to Lesson: Define a custom Vocabulary or return to the Tame your RDF Metadata with ActiveFedora landing page.

Clone this wiki locally