• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Linked Data on Rails
 

Linked Data on Rails

on

  • 6,635 views

Introduction to Linked Data for Ruby and Rails developers

Introduction to Linked Data for Ruby and Rails developers

Statistics

Views

Total Views
6,635
Views on SlideShare
6,487
Embed Views
148

Actions

Likes
12
Downloads
127
Comments
0

5 Embeds 148

http://edotprintstacktrace.tumblr.com 120
http://www.slideshare.net 23
http://www.google.com 2
http://www.soso.com 2
http://safe.tumblr.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
  • <br />
  • I want to start by looking at a data mashup. <br /> It’s not pretty, but it’s doing some interesting things under the covers. <br /> <br /> With this mashup, you provide a place, e.g. London, and it will list the programmes on the BBC that feature artists coming from that place. <br /> <br /> e.g. ... <br /> <br /> Nice for a world music show, or if you’re building a travel website and want to recommend music for people wanting to travel to a location... <br />
  • So this mashup involves: <br /> programme information from BBC Programmes <br /> artist information from BBC Music <br /> and from Wikipedia where each artist was born or was formed <br /> <br /> How would you go about it in the traditional Web 2.0 API world? <br /> <br /> BBC Programmes would provide an API, BBC Music would have it’s API - most likely designed from scratch and with it’s own proprietary XML schema or format. <br /> <br /> Hopefully someone would have made a nice Ruby gem that abstracts the API so you can call it from a script. <br /> <br /> With Wikipedia again you’ll need an API and some code around it. <br /> but the artist born/form data isn’t readily available. But what happens with Wikipedia? You’d probably have to screen scrape each artist page and work out where the artist was born, then fetch all that data and store it in a database and query that... <br />
  • And thinking of all of those Web 2.0 sites, each implementing their own API - that’s a lot of Ruby libraries for wrapping around... <br />
  • what if there was a standard way of publishing and querying data on the web? <br /> <br /> It turns out there is! <br />
  • And it’s been the plan all along. <br /> <br /> This diagram is from Tim Berners-Lee original proposal for the web. <br /> At the time he was thinking about typed links: <br /> Tim Berners Lee - wrote - this docment - describes - a proposal (“Mesh”) <br /> <br /> “This document was an attempt to persuade CERN management that a global hypertext system was in CERN's interests. Note that the only name I had for it at this time was \"Mesh\" -- I decided on \"World Wide Web\" when writing the code in 1990.” <br /> <br />
  • And in this diagram, which is based on a talk by Tim Berners Lee from 1994, there’s the idea of having documents on the web (the blue blobs), and how they relate to real objects: people, houses, relationships... <br /> <br /> http://www.w3.org/Talks/WWW94Tim/ <br /> <br />
  • The mechanism for exposing, sharing, and connecting data on the web is called Linked Data. <br /> <br /> And it’s based on four rules. <br />
  • The first rule of linked data is: Use URIs as names for things <br /> <br /> Any resource you want to talk about: people, places, programming languages, proteins - it gets a URI <br />
  • The second rule of linked data is: Use HTTP URIs so that people can look up those names <br /> <br /> And this is important, because... <br />
  • The third rule of Linked Data is: When someone looks up a URI, provide useful information <br /> <br /> So whenever there’s a URI for a resource you want to know more about, you can use HTTP to fetch data about that resource. <br />
  • The fourth rule of Linked Data is: Include links to other URIs, so that they can discover more things <br /> <br /> And this is what makes linked data so powerful, as you can start using links to traverse across different datasets. <br /> <br />
  • Linked Data uses RDF, the resource description framework, to model data. <br /> <br /> So when you want to fetch some information about a resource, it is returned in RDF. <br />
  • With RDF, you make statements about resources using subject-predicate-object expressions. <br /> <br /> For example, if you want to say that “Ruby was designed by Matz”. <br /> <br /> The subject is “Ruby”, the predicate is “was designed by”, and the “object” is “Matz”. <br /> <br /> And in RDF we’re using URIs. <br /> So there’s a URI for Ruby <br /> <br /> And a URI for Matz. <br /> <br /> And here is the URI for the property indicating something’s designer. <br /> <br /> <br /> <br /> <br />
  • And using these triple expressions, you build up a graph of data... <br /> <br />
  • And I really want to stress that RDF is a model - there are different serialisations. <br /> <br /> Here are 2 triples expressed in the ntriples format: it’s the most basic format. <br /> You simply get subject - predicate - object - new line, subject - predicate - object - new line <br /> <br /> Here is the most established RDF format: RDF/XML - unfortunately this is the format most people are exposed to when learning about RDF. <br /> It’s horrible to read, it’s ugly - it’s just not user friendly at all! <br /> <br /> Luckily, there’s turtle: it let’s you write RDF graphs in a compact text format. <br /> In turtle, you can see what those 2 triples are saying: <br /> The resource here Ruby, has a type Language, and it’s designer is Matz. <br /> <br /> I like to think of turtle versus RDF/XML in the same way you get YAML and XML - it’s just so much nicer to use! <br />
  • I want to show you what happens when you fetch RDF <br /> But first I want to introduce DBpedia, as the examples I’ve been showing uses DBpedia URIs. <br /> <br /> <br />
  • The DBpedia project is extracting structured data from Wikipedia, and making it available as Linked Data. <br /> <br /> It’s mostly based on extracting data from InfoBoxes on the Wikipedia articles. <br /> <br /> And it uses the Wikipedia article title in the URI for the RDF resource. <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • I want to quickly introduce the concept of RDF vocabularies. <br /> For those familiar with microformats, these are roughly equivalent to microformat vocabularies like hcard, haudio and so on. <br /> <br /> Some of the most popular ones are: <br /> * FOAF - friend of a friend, allows you to talk about people and their relationships, who knows who - a social graph <br /> * DOAP - description of a project, let’s you talk about projects and who works on them <br /> * Music Ontology - defines the domain of music: artists, composers, works, tracks and so on <br /> * Programmes Ontology - defined at the BBC, describes programmes and series and broadcasts and so on <br /> <br /> In RDF there are two ways to define a vocabulary. <br /> Originally, there was RDF Schema which let’s you define classes and properties, and restrictions (e.g. a “born” property can only be applied to a “person” <br /> <br /> The Web Ontology Language OWL expands on this, so you have more control on restrictions. e.g. can say that a project must have at least one contributor <br /> <br /> It also defines a really valuable property: owl:sameAs, so you can say this thing in this data set here is the same thing as that data set over there - really useful for mashups. <br />
  • Reddy - an RDF/XML parser based on libxml. <br /> <br /> Redland bindings - crossplatform RDF parsing library <br /> <br /> ActiveRDF, as the name suggests, is like ActiveRecord for RDF. <br /> <br />
  • <br />
  • <br />
  • you can write stuff back <br /> <br /> if you add a write enabled adapter, you can store data <br /> <br /> it’s like a openstruct on the web <br /> <br /> and it’s a perfect fit for ruby - try doing that with java - it looks horrendous! <br /> <br />
  • Based on the Friends of a Friend (FOAF) vocabulary. <br /> Describes people and their relationships... <br /> <br /> <br /> <br />
  • takes the foaf profile and builds his homepage <br /> <br /> uses linked data: e.g. it links to his friend’s foaf and fetches information about them (e.g. their homepage). Then when someone updates their homepage in their foaf file it can be picked up automatically here. <br /> <br /> look at interests: linked to dbpedia concepts, so could fetch more data about the interest on the page <br />
  • <br />
  • One of the interesting things happening in the Linked Data world is the Linking Open Data initiative. <br /> <br /> A number of data set owners have been publishing their information as Linked Data, including: <br /> * DBPedia <br /> * BBC <br /> * Freebase <br /> * Reuters with Open Calais <br /> * Lots of the academic scientific community: eprints, pubmed <br /> <br /> Many linked data geeks have been writing wrappers around normal APIs: <br /> * last.fm <br /> * myspace <br /> * musicbrainz <br /> <br /> And these datasets, and how they link together, have been documented on what is called the Linked Data cloud. And it’s been really exciting to see this cloud grow over the last couple of years. <br />
  • I’d like to see more Ruby and Rails applications on the Linked Data cloud! <br /> <br /> So here’s a quick guide on the simplest possible technique to publish RDF from Rails. <br /> It’s not quite automagical, but it gets the job done - and it’s the same technique we’re using on BBC Music and BBC Programmes. <br /> <br /> I’m taking this example from the UK Companies app I was involved in for Rewired state - I’m going to take you through the git change set when I added an RDF view to this application. <br /> <br /> The app displays information about companies registered in the UK. <br /> <br />
  • The first step is to embrace REST - one subject per resource. <br /> <br /> But as REST is already second nature to most Rails developers, I won’t go into this much more. <br />
  • Now to dig in to some Rails code - first of all we add the RDF mime type. <br /> <br />
  • In the controller action we want to add RDF to, we need to add a format.rdf. <br />
  • We then need an application.rdf.erb. <br /> <br /> As RDF/XML is the most widely supported RDF serialisation, I based the RDF view on it. <br /> <br /> It’s pretty straight forward, it just sets up the XML namespaces for the vocabularies I want to use. <br />
  • Here’s the actual RDF erb for the show action for a company. <br /> It’s pretty simple to implement. <br /> <br /> The tricky bit is working out the classes/properties to describe your data. <br /> <br /> But to build this view, I simply started off with some other RDF example. I think I took the RDF/XML off BBC programmes and then hacking around with it. I went to linked data search engines like DBPedia and searched for some companies to see what kind of vocabulary was used to describe them. <br /> <br /> And that was enough to get going - I put the RDF out there, and once I tweeted about it within a few hours I’d received some feedback about certain aspects of the RDF. Some of those comments I took onboard, and then fed this back into the RDF view. <br /> <br /> It reminds me of when I was learning HTML - you simply look around for stuff you like, view source, copy/paste and then hack around until it looks right. <br />
  • <br />
  • <br /> <br />

Linked Data on Rails Linked Data on Rails Presentation Transcript

  • Linked Data on Rails Patrick Sinclair LRUG 8th June 2009
  • http://dbtune.org/openhacklondon/London
  • BBC Music BBC Programmes Wikipedia
  • BBC Music BBC Programmes API Wikipedia
  • BBC Music BBC Programmes API API Wikipedia
  • BBC Music BBC Programmes API API Code Wikipedia
  • BBC Music BBC Programmes API Code API Code Wikipedia
  • BBC Music BBC Programmes API Code API Code Wikipedia API
  • BBC Music BBC Programmes API Code API Code Wikipedia API Code
  • BBC Music BBC Programmes API Code API Code Wikipedia ? API Code
  • what if there was a standard way of publishing and querying data on the web?
  • Information Management: A Proposal Tim Berners-Lee, CERN March 1989, May 1990 - http://www.w3.org/History/1989/proposal.html
  • http://www.flickr.com/photos/danbri/2415237566/
  • Linked Data http://linkeddata.org
  • 1. Use URIs as names for things
  • 2. Use HTTP URIs so that people can look up those names
  • 3. When someone looks up a URI, provide useful information
  • 4. Include links to other URIs, so that they can discover more things
  • RDF Resource Description Framework
  • RDF subject - predicate - object
  • RDF subject - predicate - object Ruby was designed by Matz
  • RDF subject - predicate - object Ruby was designed by Matz Ruby was designed by Matz
  • RDF subject - predicate - object Ruby was designed by Matz Ruby was designed by Matz http://dbpedia.org/resource/Ruby_(programming_language)
  • RDF subject - predicate - object Ruby was designed by Matz Ruby was designed by Matz http://dbpedia.org/resource/Ruby_(programming_language) http://dbpedia.org/resource/Yukihiro_Matsumoto
  • RDF subject - predicate - object Ruby was designed by Matz Ruby was designed by Matz http://dbpedia.org/resource/Ruby_(programming_language) http://dbpedia.org/property/designer http://dbpedia.org/resource/Yukihiro_Matsumoto
  • prefix dbprop: <http://dbpedia.org/property/> http://dbpedia.org/resource/Yukihiro_Matsumoto dbprop:designer http://dbpedia.org/resource/Ruby_(programming_language)
  • prefix dbprop: <http://dbpedia.org/property/> “Yukihiro Matsumoto” dbprop:name http://dbpedia.org/resource/Yukihiro_Matsumoto dbprop:designer http://dbpedia.org/resource/Ruby_(programming_language)
  • prefix dbprop: <http://dbpedia.org/property/> “Yukihiro Matsumoto” dbprop:name http://dbpedia.org/resource/Yukihiro_Matsumoto dbprop:designer dbprop:nickname “Matz” http://dbpedia.org/resource/Ruby_(programming_language)
  • prefix dbprop: <http://dbpedia.org/property/> http://dbpedia.org/resource/Japan “Yukihiro Matsumoto” dbprop:nationality dbprop:name http://dbpedia.org/resource/Yukihiro_Matsumoto dbprop:designer dbprop:nickname “Matz” http://dbpedia.org/resource/Ruby_(programming_language)
  • prefix dbprop: <http://dbpedia.org/property/> prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> 35.683334 139.766663 geo:lat geo:long http://dbpedia.org/resource/Japan “Yukihiro Matsumoto” dbprop:nationality dbprop:name http://dbpedia.org/resource/Yukihiro_Matsumoto dbprop:designer dbprop:nickname “Matz” http://dbpedia.org/resource/Ruby_(programming_language)
  • <http://dbpedia.org/resource/Ruby_(programming_language)> <http://dbpedia.org/property/designer> <http://dbpedia.org/resource/Yukihiro_Matsumoto> . <http://dbpedia.org/resource/Ruby_(programming_language)> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Language> . ntriples
  • <http://dbpedia.org/resource/Ruby_(programming_language)> <http://dbpedia.org/property/designer> <http://dbpedia.org/resource/Yukihiro_Matsumoto> . <http://dbpedia.org/resource/Ruby_(programming_language)> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Language> . ntriples <rdf:RDF xmlns:log=quot;http://www.w3.org/2000/10/swap/log#quot; xmlns:rdf=quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#quot;> <Language xmlns=quot;http://umbel.org/umbel/sc/quot; rdf:about=quot;http://dbpedia.org/resource/Ruby_(programming_language)quot;> <designer xmlns=quot;http://dbpedia.org/property/quot; rdf:resource=quot;http://dbpedia.org/resource/Yukihiro_Matsumotoquot;/> </Language> </rdf:RDF> RDF/XML
  • <http://dbpedia.org/resource/Ruby_(programming_language)> <http://dbpedia.org/property/designer> <http://dbpedia.org/resource/Yukihiro_Matsumoto> . <http://dbpedia.org/resource/Ruby_(programming_language)> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Language> . ntriples <rdf:RDF xmlns:log=quot;http://www.w3.org/2000/10/swap/log#quot; xmlns:rdf=quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#quot;> <Language xmlns=quot;http://umbel.org/umbel/sc/quot; rdf:about=quot;http://dbpedia.org/resource/Ruby_(programming_language)quot;> <designer xmlns=quot;http://dbpedia.org/property/quot; rdf:resource=quot;http://dbpedia.org/resource/Yukihiro_Matsumotoquot;/> </Language> </rdf:RDF> RDF/XML @prefix dbprop: <http://dbpedia.org/property/> . <http://dbpedia.org/resource/Ruby_(programming_language)> a <http://umbel.org/umbel/sc/Language>; dbprop:designer <http://dbpedia.org/resource/Yukihiro_Matsumoto> . turtle
  • http://dbpedia.org
  • http://en.wikipedia.org/wiki/Ruby_(programming_language) http://dbpedia.org/resource/Ruby_(programming_language)
  • RDF grabbing time curl --include --location http://dbpedia.org/resource/Yukihiro_Matsumoto => html representation curl --include --location --header quot;Accept: application/rdf+xmlquot; http://dbpedia.org/resource/Yukihiro_Matsumoto => rdf representation rapper http://dbpedia.org/resource/Yukihiro_Matsumoto => returns triples e.g. <http://dbpedia.org/resource/Ruby_(programming_language)> <http://dbpedia.org/property/designer> <http://dbpedia.org/resource/Yukihiro_Matsumoto> . rapper http://dbpedia.org/property/designer => returns data about a property rapper -oturtle http://dbpedia.org/property/designer => returns data formatted in turtle
  • curl --include --location http://dbpedia.org/resource/Yukihiro_Matsumoto => redirect to http://dbpedia.org/page/Yukihiro_Matsumoto => html representation
  • curl --include --location --header quot;Accept: application/rdf+xmlquot; http://dbpedia.org/resource/Yukihiro_Matsumoto => rdf representation
  • rapper http://dbpedia.org/resource/Yukihiro_Matsumoto => returns triples e.g. <http://dbpedia.org/resource/Ruby_(programming_language)> <http://dbpedia.org/property/designer> <http://dbpedia.org/resource/Yukihiro_Matsumoto> .
  • rapper -oturtle http://dbpedia.org/property/designer => returns data formatted in turtle @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . <http://dbpedia.org/property/designer> a rdf:Property ; rdfs:label quot;designerquot; .
  • RDF Vocabularies • FOAF, DOAP, Music Ontology, Programmes Ontology... • RDF Schema (RDFS) • Describe classes, properties, some restrictions • Web Ontology Language (OWL) • More control over restrictions • owl:sameAs
  • Ruby and RDF • Reddy - http://github.com/tommorris/reddy • Redland bindings - http://librdf.org • ActiveRDF - http://activerdf.org • Like ActiveRecord for RDF data
  • require 'rubygems' require 'active_rdf' # add DBpedia SPARQL endpoint as a datasource adapter = ConnectionPool.add_data_source(:type => :sparql, :url => quot;http://dbpedia.org/sparqlquot;, :engine => :virtuoso, :results => :sparql_xml) adapter.enabled = true # we register a short-hand notation for the namespaces Namespace.register(:dbprop, 'http://dbpedia.org/property/') Namespace.register(:dbowl, 'http://dbpedia.org/ontology/')
  • matz = RDFS::Resource.new(quot;http://dbpedia.org/resource/Yukihiro_Matsumotoquot;) => #<RDFS::Resource:0x1b91a58 @predicates={}, @uri=quot;http://dbpedia.org/ resource/Yukihiro_Matsumotoquot;> matz.dbprop::nickname => quot;Matzquot; matz.dbprop::nationality => #<RDFS::Resource:0x1b91a58 @predicates={}, @uri=quot;http://dbpedia.org/ resource/Japanquot;> japan = matz.dbprop::nationality => #<RDFS::Resource:0x1b91a58 @predicates={}, @uri=quot;http://dbpedia.org/ resource/Japanquot;> tokyo = japan.dbowl::capital => #<RDFS::Resource:0x1b91a58 @predicates={}, @uri=quot;http://dbpedia.org/ resource/Tokyoquot;> tokyo.dbprop::name => quot;Tokyoquot; tokyo.dbowl::populationTotal => 12790000
  • adapter2 = ConnectionPool.add_data_source :type => :rdflite adapter2.enabled = true Namespace.register(:mystuff, 'http://example.org/test/') => MYSTUFF tokyo.mystuff::rating = 5 => 5 tokyo.mystuff::rating => 5 tokyo.mystuff::is_in = japan => #<RDFS::Resource:0x1b91a58 @predicates={}, @uri=quot;http:// dbpedia.org/resource/Japanquot;>
  • njh’s homepage builder http://www.aelius.com/njh/
  • @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix : <http://xmlns.com/foaf/0.1/> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix dc: <http://purl.org/dc/elements/1.1/> . <http://www.aelius.com/njh/foaf.rdf> a :PersonalProfileDocument ; rdfs:label quot;Nicholas J Humfrey's FOAF Filequot; ; :maker <http://www.aelius.com/njh#me> ; :primaryTopic <http://www.aelius.com/njh#me> . <http://www.aelius.com/njh#me> a :Person ; dc:description quot;I am a Software Engineer, working for BBC Audio and Music in London.quot;@en ; :homepage <http://www.aelius.com/njh/> ; :based_near <http://dbpedia.org/resource/London> ; :knows <http://danbri.org/foaf.rdf#danbri>, <http://foafbuilder.qdos.com/people/ whomwah.com/foaf.rdf#me>, <http://id.ecs.soton.ac.uk/person/9639>; :currentProject <http://www.aelius.com/njh#project-captivednsd>, <http://www.aelius.com/ njh#project-dvbshout>; :interest <http://dbpedia.org/resource/C_(programming_language)>, <http://dbpedia.org/ resource/Cycling>, <http://dbpedia.org/resource/Debian>; owl:sameAs <http://dbtune.org/last-fm/njh>, <http://www.bbc.co.uk/users/njh#me>, <http:// qdos.com/user/89295d77e0e390538aac662e3f3cae03>;
  • and it’s all in ruby, check it out!
  • Publishing RDF in Rails http://bit.ly/rYPWS http://github.com/robmckinnon/ukcompanies/commit/c3aa368e7aa50e221a983a79faa8ca45bff8902a
  • Embrace REST!
  • Add the mime type http://bit.ly/rYPWS http://github.com/robmckinnon/ukcompanies/commit/c3aa368e7aa50e221a983a79faa8ca45bff8902a
  • respond block http://bit.ly/rYPWS http://github.com/robmckinnon/ukcompanies/commit/c3aa368e7aa50e221a983a79faa8ca45bff8902a
  • application.rdf.erb http://bit.ly/rYPWS http://github.com/robmckinnon/ukcompanies/commit/c3aa368e7aa50e221a983a79faa8ca45bff8902a
  • application.rdf.erb http://bit.ly/rYPWS http://github.com/robmckinnon/ukcompanies/commit/c3aa368e7aa50e221a983a79faa8ca45bff8902a
  • There’s plenty more! • Schemas (RDFS) and Ontologies (OWL) • SPARQL • Query language for Linked Data • Triple stores • Store RDF data, no need to design schema up front • RDFa • Embed RDF into HTML markup • Supported by Google and Yahoo