Warning:
This wiki has been archived and is now read-only.

RDF-API

From RDFa Working Group Wiki
Jump to: navigation, search

This page is the beginnings of a survey of APIs for various RDF toolkits/etc.

APIs

For each toolkit, provide a brief description of the data model (e.g. triple based / resource based) and programming model (object oriented / functional / etc) followed by the names of CRUD (create, read, update, delete) parts of the API.

RDF::Trine / RDF::Query

Perl object-oriented low-level RDF framework. This seems to be the RDF toolkit which Perl developers are standardising around. RDF::Trine provides in-memory and SQL-backed models (a model is a quad store), and parsers and serialisers to read and write data. RDF::Query provides SPARQL and RDQL query parsing and execution for RDF::Trine models (and for a few other RDF toolkits).

Create

 # Create a new model ($store is a storage backend)
 $model = RDF::Trine::Model->new($store);
 
 # Read some RDF data into it
 $parser = RDF::Trine::Parser->new($syntax);
 $parser->parse_into_model($baseuri, $data, $model);
 
 # Or from a URL
 RDF::Trine::Parser->parse_url_into_model($url, $model);
 
 # Add a statement to a model
 $subject   = RDF::Trine::Node::Blank->new;
 $predicate = RDF::Trine::Node::Resource->new("http://example.com/name");
 $object    = RDF::Trine::Node::Literal->new("Fred", "en", undef); # third param is datatype
 $statement = RDF::Trine::Statement->new($subject, $predicate, $object);
 $model->add_statement($statement);
 
 # Alternative method of loading data, using a hashref (pointer to an associative array).
 # The hashref needs to be built along RDF/JSON principles.
 $model->add_hashref($data);

Read

 # Get a list of all triples (RDF::Trine::Statement objects)
 $iterator = $model->get_statements();
 while ($st = $iterator->next)
 {
   ($subject, $predicate, $object) = ($st->subject, $st->predicate, $st->object);
 }
 
 # Get a list of all triples with a particular predicate
 $predicate = RDF::Trine::Node::Resource->new("http://example.com/name");
 $iterator  = $model->get_statements(undef, $predicate, undef);
 
 # Get an RDF/JSON-like hashref:
 $data = $model->as_hashref;

Using SPARQL:

 $query  = RDF::Query->new("SELECT ?foo WHERE ...");
 $result = $query->execute($model);
 while ($row = $result->next)
 {
   $foo = $row->{'foo'};
 }
 
 $query  = RDF::Query->new("CONSTRUCT ...");
 $result = $query->execute($model);
 while ($statement = $result->next)
 {
   # ...
 }

Update

 # No method of in-place editing of triples.

Delete

 # Remove a particular statement from a model
 $subject   = RDF::Trine::Node::Blank->new;
 $predicate = RDF::Trine::Node::Resource->new("http://example.com/name");
 $object    = RDF::Trine::Node::Literal->new("Fred", "en", undef); # third param is datatype
 $statement = RDF::Trine::Statement->new($subject, $predicate, $object);
 $model->remove_statement($statement);
 
 # Remove all triples with a particular predicate
 $predicate = RDF::Trine::Node::Resource->new("http://example.com/name");
 $model->remove_statements(undef, $predicate, undef);

RDFLib

Python low-level RDF framework; one of the major Python environment for RDF; in contrast to others (eg, Python binding to Redland, to AllegroGraph, etc), it is natively Python all the way up. It provides an (incomplete) SPARQL query. It is triple based, but also has a 'context' possibility, ie, it implements some sort of a named graph. It parses RDF/XML, n3/turtle, ntriples, and can serialize graphs in these formats.

Create

import rdflib
# there are a number of special Graph types, ie, for memory store, interfaced to a database, etc
# default is in memory store
graph = rdflib.Graph()
# parse can get files, uris, "file like objects" in Python terminology, ie, strings via StringIO
graph.parse(file)
graph.parse(uri)
# default is RDF/XML, but can also be n3 and, actually, RDFa (eventually...)
graph.parse(uri, format='n3')
# adding a statement
# creation of resources
subject   = rdflib.URIRef('http://...')
subject2  = rdflib.BNode()
predicate = rdflib.URIRef('http://...')
# datatype could be added
object    = rdflib.Literal('sometext', lang='en')
graph.add((subject,predicate,object))

Read

# This will return all the
for (s,p,o) in graph.triples((None,None,None)) :
   ...
# None can be replaced with a real resource
# eg, get all triples with 'object' as an object
object    = rdflib.URIRef('http://...')
for (s,p,o) in graph.triples((None,None,object)) :
   ...
# There are also shortcuts for the triple above
for (s,p) in graph.subject_predicate(object) :
   ...

Update

No method for explicit update.

Delete

subject   = rdflib.URIRef('http://...')
predicate = rdflib.URIRef('http://...')
object    = rdflib.Literal('sometext', lang='en')
graph.remove((subject,predicate,object))