Opera Software ASA
 
Kjetil Kjernsmo
RDF and Perl

The Semantic Web

Resource Description Framework

Serialisations

  • RDF/XML:
    <rdf:RDF
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      xmlns:foaf="http://xmlns.com/foaf/0.1/">
      <foaf:Person rdf:ID="me">
        <foaf:name>Kjetil Kjernsmo</foaf:name>
        <foaf:nick>KjetilK</foaf:nick>
      </foaf:Person>
    </rdf:RDF>
              
  • Turtle:
    @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
    <http://www.kjetil.kjernsmo.net/foaf.rdf#me>
      a         foaf:Person ;
      foaf:name "Kjetil Kjernsmo";
      foaf:nick "KjetilK" .
              

Do not think about RDF just in XML terms!

Perl code

Redland modelling

  $model->add_statement(
    new RDF::Redland::URINode('http://www.kjetil.kjernsmo.net/foaf.rdf#me',
    new RDF::Redland::URINode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
    new RDF::Redland::URINode('http://xmlns.com/foaf/0.1/Person')
  );
  $model->add_statement(
    new RDF::Redland::URINode('http://www.kjetil.kjernsmo.net/foaf.rdf#me',
    new RDF::Redland::URINode('http://xmlns.com/foaf/0.1/name'),
    new RDF::Redland::LiteralNode('Kjetil Kjernsmo')
  );
  $model->add_statement(
    new RDF::Redland::URINode('http://www.kjetil.kjernsmo.net/foaf.rdf#me',
    new RDF::Redland::URINode('http://xmlns.com/foaf/0.1/nick'),
    new RDF::Redland::LiteralNode('KjetilK')
  );
  $model->add_statement(
    new RDF::Redland::URINode('http://www.daml.org/airport/OSL',
    new RDF::Redland::URINode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
    new RDF::Redland::URINode('http://www.daml.org/2001/10/html/airport-ont#Airport')
  );
  $model->add_statement(
    new RDF::Redland::URINode('http://www.daml.org/airport/OSL',
    new RDF::Redland::URINode('http://www.daml.org/2001/10/html/airport-ont#name'),
    new RDF::Redland::LiteralNode('Gardermoen')
  );
  $model->add_statement(
    new RDF::Redland::URINode('http://www.daml.org/airport/OSL',
    new RDF::Redland::URINode('http://www.daml.org/2001/10/html/airport-ont#iataCode'),
    new RDF::Redland::LiteralNode('OSL')
  );

RDF::Helper modelling

  my $rdf = RDF::Helper->new(
      BaseInterface => 'RDF::Redland',
      BaseURI => 'http://www.kjetil.kjernsmo.net/foaf.rdf',
      Namespaces => {
        foaf => 'http://purl.org/dc/elements/1.1/',
        rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
        air => 'http://www.daml.org/2001/10/html/airport-ont#'
      }
  );
  $rdf->assert_resource('#me', 'rdf:type', 'foaf:Person');
  $rdf->assert_literal('#me', 'foaf:name', 'Kjetil Kjernsmo');
  $rdf->assert_literal('#me', 'foaf:nick', 'KjetilK');
  $rdf->assert_resource('http://www.daml.org/airport/OSL',
                        'rdf:type', 'air:Airport');
  $rdf->assert_literal('http://www.daml.org/airport/OSL',
                       'air:name', 'Gardermoen');
  $rdf->assert_literal('http://www.daml.org/airport/OSL',
                       'air:iataCode', 'OSL');

SPARQL queries

SPARQL is a query language for RDF graphs. W3C Candidate Recommendation.

  PREFIX foaf: <http://xmlns.com/foaf/0.1/>

  SELECT ?name WHERE {
    ?user  foaf:nick "kjetilk" .
    ?user  foaf:name ?name . }

Running the query is simply:

  my $query=new RDF::Redland::Query($string, undef, undef, "sparql");
  my $results=$query->execute($model);
        

You can serialise it to XML (W3C work), JSON or just work on the Perl data structure.

MetaPhoto - photo album vapourware

  1. libferris virtual file-system, will extract metadata.
  2. ... and stores it in Redland.
  3. Additional metadata can be entered in many ways.
  4. RDF::Helper as the model library.
  5. Use SPARQL to extract the data.
  6. XSLT to transform to desired formats (XHTML, Flickr API, etc)