Ruby semweb 2011-12-06
Upcoming SlideShare
Loading in...5
×
 

Ruby semweb 2011-12-06

on

  • 2,878 views

Slides for December 6 2011 Lotico Sanfrancisco Semantic Web Meetup

Slides for December 6 2011 Lotico Sanfrancisco Semantic Web Meetup

Statistics

Views

Total Views
2,878
Views on SlideShare
2,872
Embed Views
6

Actions

Likes
2
Downloads
22
Comments
1

3 Embeds 6

http://www.twylah.com 3
http://paper.li 2
http://a0.twimg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Ruby semweb 2011-12-06 Ruby semweb 2011-12-06 Presentation Transcript

  • Linked Open Data for Rubyists What the Semantic Web brings to Ruby Gregg Kellogg gregg@greggkellogg.net Twitter: @gkellogg G+: greggkellogg
  • Why RDF for Ruby? ✤ Major creative force in Web 2.0 ✤ Rich eco-system (Gems/Rails/…) ✤ Fair support for XML (Nokogiri, anyway) ✤ Great environment for prototyping and getting stuff done. ✤ RDF is relatively virgin territory in Ruby6 Dec 2012 Ruby SemWeb 2
  • Ruby RDF ❖ RDF.rb/linkeddata ❖ Storage ❖ Readers/Writers ➡ Cassandra ➡ RDF/XML ➡ SQL(RDF::DO) ➡ N3/Turtle/NTriples ➡ MongoDB ➡ NQuads/TriX ➡ Redstore ➡ RDFa/Microdata ➡ Sesame ➡ JSON-LD/RDF-JSON ➡ AlegroGraph ➡ Raptor bridge ➡ Talis ➡ 4store ❖ Query ➡ SPARQL/SSE ➡ SPARQL::Client6 Dec 2012 Ruby SemWeb 3
  • Core Classes ❖ RDF::Term ❖ Serializations ➡ RDF::Literal ➡ RDF::Format ➡ XSD subtypes ➡ RDF::Reader ➡ RDF::Resource ➡ RDF::Writer ➡ RDF::Node ❖ Storage ➡ RDF::URI ➡ RDF::Repository ➡ RDF::List ❖ Query ➡ RDF::Graph ➡ RDF::Query ❖ RDF::Statement ➡ RDF::Query::Pattern ❖ RDF::Vocabulary ➡ RDF::Query::Solution ➡ With definitions for common ➡ RDF::Query::Variable vocabularies6 Dec 2012 Ruby SemWeb 4
  • Simple Graph Manipulationrequire rdfinclude RDFg = Graph.newg << Statement.new( RDF::URI.new("https://github.com/gkellogg/rdf"), RDF::URI.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), RDF::URI.new("http://usefulinc.com/ns/doap#GitRepository"))# Using common vocabulariesproj = Node.newg << Statement.new(proj, RDF.type, DOAP.Project)g << Statement.new(proj, DOAP.repository, RDF::URI.new("https://github.com/gkellogg/rdf"))puts g.dump(:ntriples)6 Dec 2012 Ruby SemWeb 5
  • Serializing with Writers ✤ Graphs can be serialized with available Writersrequire rdf/ntriplesrequire rdf/turtleputs NTriples::Writer.buffer {|writer| writer << g}# Also, you can include other formatsTurtle::Writer.buffer {|writer| writer << g}# Use Graph.dump or Writer.open to save to a fileputs g.dump(:ttl, :standard_prefixes => true)Turtle::Writer.open(example2.ttl) {|w| w << g}puts File.read(example2.ttl)6 Dec 2012 Ruby SemWeb 6
  • RDFa Serialization with Haml ✤ RDFa writer uses Haml templates write a graph to HTML ➡ Define your own templates ➡ Other examples from Structured Data Linter: ➡ http://linter.structured-data.org ➡ https://github.com/structured-data/linter/blob/master/lib/rdf/linter/ rdfa_template.rb ✤ In principle, this can be used to write any XML- based format by defining an appropriate template ✤ More information in RDFa gem6 Dec 2012 Ruby SemWeb 7
  • Finding Formats ✤ Find a format for reading or writingrequire rdf/rdfarequire rdf/rdfxmlWriter.for(:ttl)Writer.for(:content_type => "text/html")Reader.for(example2.ttl)# List available formatsRDF::Format.to_a.map(&:to_sym)# Open a URL and use format detection to find a writerputs Graph.load(http://greggkellogg.net/foaf). dump(:ttl, :base_uri => http://greggkellogg.net/foaf, :standard_prefixes => true)f = "http://greggkellogg.net/github-lod/doap.ttl"Turtle::Reader.open(f) do |reader| reader.each {|st| puts st.inspect}end6 Dec 2012 Ruby SemWeb 8
  • BGP Query supportf = "http://greggkellogg.net/github-lod/doap.ttl"doap = Graph.load(f) ✤ Query with# using RDF::Queryquery = Query.new( RDF::Query :person => { RDF.type => FOAF.Person, FOAF.name => :name, FOAF.mbox => :email, })query.execute(doap).each do |soln| puts "name: #{soln.name}, email: #{soln[:email]}"end; nil# using Query::Patternquery = Query.new do pattern [:project, DOAP.developer, :person] pattern [:person, FOAF.name, :name]endquery.execute(doap).each do |soln| puts "project: #{soln.project} name: #{soln.name}"end; nil6 Dec 2012 Ruby SemWeb 9
  • SPARQLrequire sparql ❖ SPARQL gemf = "./dumps/github-lod.nt" executes locallydoap = Graph.load(f) for againstquery = SPARQL.parse(%q( RDF::Queryable PREFIX doap: <http://usefulinc.com/ns/doap#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> object. SELECT ?repo ?name ❖ SPARQL::Client WHERE { gem executes [ a doap:Project; doap:name ?repo; against a remote doap:developer [ a foaf:Person; repository foaf:name ?name ] ➡ Best for querying ] large datasets. } ORDER BY DESC(?repo) LIMIT 20))query.execute(doap).each do |soln| puts "project: #{soln.repo} name: #{soln.name}"end; nil 10
  • RDF Behavior ✤ Classes can behave like RDF ➡ RDF::Countable – #empty?, #count, #size ➡ RDF::Durable ➡ RDF::Enumerable – must implement #each – #statements, #each, #triples, #quads, ... ➡ RDF::Writable – must implement #(insert/delete/each)_statement – #load, #insert, #<<, #update, #delete ➡ RDF::Queryable – must implement #each ➡ should implement #query_pattern & #query_execute – #query, #first, #first_(subject,predicate,object) – RDF::TypeCheck – raises TypeError on illegal comparison6 Dec 2012 Ruby SemWeb 11
  • ActiveRecord with RDF ✤ Import #RDF::Enumerable and implement #eachrequire github-api-clientclass GitHub::User include RDF::Enumerable def each u = RDF::URI("http://github.com/#{login}") yield RDF::Statement.new(u, RDF::FOAF.name, name) yield RDF::Statement.new(u, RDF::mbox, RDF::URI("mailto:#{email}")) unless email.nil? endendu = GitHub::User.get(gkellogg)puts u.dump(:ttl, :standard_prefixes => true)6 Dec 2012 Ruby SemWeb 12
  • Other Resources ✤ Spira ➡ Get Ruby classes from RDF datastores ✤ Distiller ➡ Transform between RDF formats, including RDFa generation ➡ http://rdf.greggkellogg.net ✤ Documentation ➡ Comprehensive documentation of Ruby LinkedData related gems available at http://rdf.greggkellogg.net/yard/index.html ✤ GitHub LOD Demo ➡ Examples used in this presentation, along with a demo Sinatra application illustrating Ruby RDF usage is available: ➡ GitHub project: http://github.com/gkellogg/github-lod (Public Domain) ➡ Running demo: http://greggkellogg.net/github-lod6 Dec 2012 Ruby SemWeb 13