Category Archives: SPARQL

http://www.w3.org/TR/rdf-sparql-query/

Introducing SPO(G)

SPO(G) is not a new syntax, nor a format or a protocol. It is, however, a syntactic profile and a convention.

<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  <head>
    <variable name="g"/>
    <variable name="s"/>
    <variable name="p"/>
    <variable name="o"/>
  </head>
  <results>
    ...
  </results>
</sparql>

I have previously written about exchange of named RDF graphs through the use of quads-in-zips, and while that approach works just fine, it needs to be implemented on both sides of the exchange.

This is also true for SPO(G), but with SPARQL implementations being widespread, the export side is already in place all around the web, and the import side is quite easily implemented — I have sent an implementation of SPARQLXMLResultLoader for ARC to bengee, and while he is also busy working on a streaming serialiser, it seems likely it’ll be a part of a coming release of ARC.

As can be seen from the example, SPO(G) is simply a constrained SPARQL Query Results XML Format: It needs to have three or four variables, s, p, and o must be present, with g being optional (making it YARS). For all results, all variables must be bound.

SPO(G) isn’t as compact as quads-in-zips, but there’s no reason for it not to be compressed during exchange, either through a manual process or via the usual gzip-encoding on-the-fly.

I should perhaps write it up properly, but I think I’d rather go off and implement it for Redland.

SPARQL Endpoint

SPARQL

As I hinted to in my post about Named Graph Exchange, and later picked up by Danny, it is possible to create a SPARQL endpoint using ARC with only six lines of PHP — or rather was, as Benjamin Nowack has now announced a suggested update, that makes it possible in only fourthree lines:

include_once('path/to/arc/ARC2.php');
$ep = ARC2::getStoreEndpoint(array(...));
$ep->go();

The same announcement, the ARC Release Notes and Change Log, and Benjamins post about his cool data wiki also mentions the new ARC plugin system, that I helped take off.

My plugin submission is essentially a SPARQL client, making it possible to use ARC for accessing remote SPARQL endpoints: ARC2::RemoteEndpointPlugin

The plugin works, in that it supports the read-only query types SELECT, CONSTRUCT, DESCRIBE, and ASK, but not yet the ones that write, since the ARC class that does HTTP only speaks GET at the moment.

The plugin homepage doubles as its documentation — it contains an example of how to use the plugin: Basically just like with a regular ARC2::Store, only with a simpler configuration — only a SPARQL endpoint URL is needed.

The homepage is also a Bazaar repository, and as such provides an Atom feed with updates, and — of course — a DOAP file, that I hope will one day play a role as the authoritative source of information shown on the ARC plugins page, with automatic updates.

Fitting, also, that this surfaces on the very day that SPARQL becomes a recommendation. It has been quite a journey, and fun to be a (small) part of, starting way back when Squish and RDQL were state of the art, and later with The Gargonza Experiment — perhaps it is now time to retire my partial SPARQL Rewriter and resurrect Sparqlette

Authorization by Codepiction

Over the last fours years and counting, I have been providing access to a triplestore with description of my photos through a facetted interface. Through those years I have received several requests for excluding people from the searchable interface, as they showed up quite prominently on Google.

It turned out to make the interface much less useful for me — and possibly for others as well — so not too long ago I ended up simply excluding photos depicting people — using SPARQL, of course:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?Photo 
WHERE { ?Photo foaf:depicts ?Person .
        ?Person a foaf:Person }

That, of course, didn’t really improve things from my personal perspective, so watching others experiment with OpenID and social networking, I decided to take the same route — with a twist.

I didn’t really want to maintain or discover a social network for this use, but then it dawned on me: My photos actually represent a social network, as codepiction describes relations between people even better than explicitly stated ones.

With that in mind, I got the most recent version of the PHP OpenID Library up and running, and now use SPARQL to extend the non-person subset created above with photos of codepictees for users logged in with their OpenID — assuming they match with the descriptions in the store:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?Photo 
WHERE { ?Photo foaf:depicts ?Friend .
        ?OtherPhoto foaf:depicts ?Friend .
        ?OtherPhoto foaf:depicts ?User .
        ?User ?ifp ?OpenID .
        ?ifp a owl:InverseFunctionalProperty }

Note that since I don’t restrict ?User to be distinct from ?Friend (and ?Photo from ?OtherPhoto), the query also returns photos depicting the user. The query also doesn’t explicitly look for foaf:openid, but rather any inverse functional property, and since I’ve used foaf:mbox/foaf:mbox_sha1sum quite a lot in the descriptions of the photos’ depictees, I have added the option to verify your e-mail address — it is not enough to trust what’s returned from an OpenID provider, as there is no validation, and I wouldn’t want everyone to create OpenID identities with my e-mail address.

All in all, you can now use OpenID to log into my facetted interface and — if you are recognized — get access to additional photos. Try it!

Quite slick use of codepiction, if I may say so myself…

NB: I cheated. Parts of the backend still use an old RDQL engine, so some of this was actually not done in SPARQL. Also, I supplemented the above codepiction query with queries regarding albums with codepiction and photographer information (mostly useful for myself, of course), meaning you will actually see more photos than you might expect.

Planet Timeline

Following up on the other day’s post on SPARQL and the Simile Timeline, here is another way of putting items on a timeline.

I’ve created a SPARQL template for Planet Planet, you can see it in action on my personal planet.

The template makes (optional) use of identifying icons for each feed, which are configured in the Planet config.ini (don’t forget to add the SPARQL template to the template_files configuration option as well):

[http://www.wasab.dk/morten/blog/feed/rdf]
name = Binary Relations
timeline_icon = dull-red-circle.png 

Once set up, the generated SPARQL results (example) can be run through the SPARQL Timeline Service (example).

Integrating the timeline into the default Planet HTML isn’t hard, I’ve created a tweaked version of examples/basic/index.html.tmpl from the latest nightly — see it in action.

Getting this running on a working Planet installation should be as simple as downloading the two templates to the right location, and updating the Planet configuration file (default dull-blue icons will be used if not specificed per feed).

Update: Note that the planet-sparql.srx file needs to be returned by the web server with an XML mime type, preferably application/sparql-results+xml. If you’re unable to configure your server to make it do that, try renaming the template and file to use .xml instead of .srx as the file extension — that should make it return with application/xml. Thanks to Edward Summers for pointing this out on the Planet developer list.