Skip to content

A simple Ruby DSL for creating and working with RDF graphs as regular Ruby objects

License

Notifications You must be signed in to change notification settings

rsinger/RDFObjects

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RDFObjects are intended to simplify working with RDF data by providing a (more) Ruby-like interface to resources (thanks to OpenStruct).

There is a Google Group at http://groups.google.com/group/rdfobjects to discuss problems, enhancements, changes, announcements, etc.

Installation:
  The gem is currently hosted at http://gemcutter.org/gems/rdfobjects so you'll either need the gemcutter gem (and run "gem tumble") or add gemcutter.org to your sources manually (gem sources -a http://gemcutter.org).
  
  $ sudo gem install rdfobjects

Requirements:
  * Nokogiri (the idea is for more options in the future)
  * curies
  * json (or json_pure)
  
Usage:
  >> require 'rdf_objects'
  
  >> include RDFObject
  
  >> Curie.add_prefixes! :skos=>"http://www.w3.org/2004/02/skos/core#"
  
  >> resource = Resource.new('http://id.loc.gov/authorities/sh2002000569#concept')
  >> resource.describe
  >> resource.skos
  
  => {"inScheme"=>[#<RDFObject::Resource uri="http://id.loc.gov/authorities#topicalTerms">, #<RDFObject::Resource uri="http://id.loc.gov/authorities#conceptScheme">], "broader"=>[#<RDFObject::Resource skos={"prefLabel"=>"Semantic networks (Information theory)"}, uri="http://id.loc.gov/authorities/sh92004914#concept">, #<RDFObject::Resource skos={"prefLabel"=>"World Wide Web"}, uri="http://id.loc.gov/authorities/sh95000541#concept">, #<RDFObject::Resource skos={"prefLabel"=>"Semantic integration (Computer systems)"}, uri="http://id.loc.gov/authorities/sh2004000479#concept">], "closeMatch"=>#<RDFObject::Resource uri="http://stitch.cs.vu.nl/vocabularies/rameau/ark:/12148/cb14521343b">, "prefLabel"=>"Semantic Web"}
  
  >> resource["[skos:prefLabel]"]
  
  => "Semantic Web"
  
  >> resource.skos["prefLabel"]
  
  => "Semantic Web"
  
  >> resource["http://www.w3.org/2004/02/skos/core#prefLabel"]
  
  => "Semantic Web"
  (etc.)
  
  >> resource.skos["broader"].first.skos["prefLabel"]
  
  => "Semantic networks (Information theory)"
  
  Unnecessary, but helpful, way to define typed literals
  >> source = Literal.new("Library of Congress Authorities", {:language=>"en"})
  
  And assert them
  
  >> resource.assert("http://purl.org/dc/terms/source", source)
  
  => ["Work cat.: 2002070545: The Semantic Web--ISWC 20002, 2002.", "ASTI on FirstSearch, May 6, 2002: in titles (semantic Web)", "Engr. index online, May 6, 2002 (identifier: Semantic Web)", "Library of Congress Authorities"]
  
  >> resource["http://purl.org/dc/terms/source"].last.language
  
  => "en"
  
  To relate a resource to another URI you can use #.relate - it will accept full uri strings, safe curies or other RDFObject::Resource objects
  
  >> resource.relate("[skos:closeMatch]", "http://dbpedia.org/resource/Category:Semantic_Web")
  
  => [#<RDFObject::Resource uri="http://stitch.cs.vu.nl/vocabularies/rameau/ark:/12148/cb14521343b">, #<RDFObject::Resource uri="http://dbpedia.org/resource/Category:Semantic_Web">]
  
  These relationships are actually replaced with RDFObject::ResourceReference objects (to prevent massive recursion errors).  To access the actual Resource object, use:
  
  >> resource["[skos:closeMatch]"].first.resource
  
  => #<RDFObject::Resource uri="http://stitch.cs.vu.nl/vocabularies/rameau/ark:/12148/cb14521343b">
  
  In order to be sure you are always asserting against the same object, use RDFObject::Collection objects to manage your Resources.
  
  >> collection = Collection.new
  
  => {}
  
  >> r1 = collection.find_or_create('http://ex.org/ex/1234')
  
  => #<RDFObject::Resource uri="http://ex.org/ex/1234">
  
  >> r1.object_id
  
  => 8996290
  
  >> r2 = collection.find_or_create('http://ex.org/ex/1234')
  
  => #<RDFObject::Resource uri="http://ex.org/ex/1234">  
  
  >> r2.object_id
  
  => 8996290  
  
  So relationships and assertions are always applied to the same objects.
  
  You can delete a single resource:
  
  >> collection.remove(r1)
  
  >> collection
  => {}
  
  There is also a convenience method on RDFObject::Collection to help find particular resources by predicate:
  
  >> resources_with_types = collection.find_by_predicate("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
  
  And to find resources by predicate and object:
  
  >> resources_i_want = collection.find_by_predicate_and_object("http://www.w3.org/2004/02/skos/core#closeMatch", "http://stitch.cs.vu.nl/vocabularies/rameau/ark:/12148/cb145537755")
  
  Both these return RDFObject::Collection objects with your matches.  Changes to resources in these collections also affect the resource in original collection.
  
  There are also parsers for ntriples, rdf/xml, RSS 1.0, RDFa, and JSON.  Parsers return a RDFObject::Collection with all of the resources discovered in the RDF source.
  
  >> resources = Parser.parse(open('lcsh.nt').read)
  
  >> resources.values.first
  
  => #<RDFObject::Resource n0={"altLabel"=>"Lichen ruber planus", "inScheme"=>[#<RDFObject::Resource uri="http://id.loc.gov/authorities#conceptScheme">, #<RDFObject::Resource uri="http://id.loc.gov/authorities#topicalTerms">], "prefLabel"=>"Lichen planus"}, n1={"sameAs"=>#<RDFObject::Resource uri="info:lc/authorities/sh85076767">}, uri="http://id.loc.gov/authorities/sh85076767#concept", n2={"modified"=>#<DateTime: 211644344801/86400,-1/6,2299161>}, rdf={"type"=>#<RDFObject::Resource uri="http://www.w3.org/2004/02/skos/core#Concept">}>
  
  There are serializers for RDF/XML and ntriples.  Both resources and collections can be serialized.
  
  >> resource.to_xml
  
  => "<rdf:RDF xmlns:n0="http://www.w3.org/2004/02/skos/core#" xmlns:n1="http://www.w3.org/2002/07/owl#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:n2="http://purl.org/dc/terms/"><rdf:Description rdf:about="http://id.loc.gov/authorities/sh85076767#concept"><n0:altLabel>Lichen ruber planus</n0:altLabel><n0:inScheme><rdf:Description rdf:about="http://id.loc.gov/authorities#conceptScheme"></rdf:Description></n0:inScheme><n0:inScheme><rdf:Description rdf:about="http://id.loc.gov/authorities#topicalTerms"></rdf:Description></n0:inScheme><n0:prefLabel>Lichen planus</n0:prefLabel><n1:sameAs><rdf:Description rdf:about="info:lc/authorities/sh85076767"></rdf:Description></n1:sameAs><n2:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">1994-08-22T15:46:41-04:00</n2:modified><rdf:type><rdf:Description rdf:about="http://www.w3.org/2004/02/skos/core#Concept"></rdf:Description></rdf:type></rdf:Description></rdf:RDF>"
  
  >> collection.to_xml
  
  >> resource.to_ntriples
  
  => "<http://id.loc.gov/authorities/sh85076767#concept> <http://www.w3.org/2004/02/skos/core#altLabel> "Lichen ruber planus"@en.\n<http://id.loc.gov/authorities/sh85076767#concept> <http://www.w3.org/2004/02/skos/core#inScheme> <http://id.loc.gov/authorities#conceptScheme>.\n<http://id.loc.gov/authorities/sh85076767#concept> <http://www.w3.org/2004/02/skos/core#inScheme> <http://id.loc.gov/authorities#topicalTerms>.\n<http://id.loc.gov/authorities/sh85076767#concept> <http://www.w3.org/2004/02/skos/core#prefLabel> "Lichen planus"@en.\n<http://id.loc.gov/authorities/sh85076767#concept> <http://www.w3.org/2002/07/owl#sameAs> <info:lc/authorities/sh85076767>.\n<http://id.loc.gov/authorities/sh85076767#concept> <http://purl.org/dc/terms/modified> "1994-08-22T15:46:41-04:00"^^<http://www.w3.org/2001/XMLSchema#dateTime>.\n<http://id.loc.gov/authorities/sh85076767#concept> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept>.\n"
  
  >> collection.to_ntriples
  
  etc.
  

About

A simple Ruby DSL for creating and working with RDF graphs as regular Ruby objects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages