XQuery/Graphing from RDF

Motivation
RDF graphs are very complex if all triples are graphed. Usually what is needed is a projection of that graph to show a specific aspect. In this example we use a SPARQL query to extract derived triples from a triple store and transform these using XSLT to the dot format for processing by Graphviz.

Triple Store
The source data is a collection of facts about a university department, the modules it provides and the staff involved. The triples are held in a store provided by the Talis Data Incubator http://dataincubator.org/.

View
The view of this data we want to build is a graph of the 'reviews' relationship between staff. A member of staff X reviews another member Y when a module has a Module Leader of Y and an Internal Moderator of X.

The SPARQL query to construct this relationship is : PREFIX p:  PREFIX rdfs: 

select ?from ?to ?label where { ?run p:Internal_Moderator ?im. ?im rdfs:label ?from. ?run p:Module_Leader ?ml. ?ml rdfs:label ?to. ?run p:module ?mod. ?mod p:shortCode ?label. }

This query constructs a new set of triples, one for each moderation relationship. The parts of the triple are named from, to and label so that the transformation to dot can be generalized. The result is returned in SPARQL XML result format.

Transformation
The transformation to dot is carried out by a generic XSLT:

The XQuery script to extract these triples and return the dot file is :

declare variable $service := "http://api.talis.com/stores/cwallace-dev2/services/sparql";

declare variable $query := " PREFIX p:  PREFIX rdfs: 

select ?from ?to ?label where { ?run p:Internal_Moderator ?im. ?im rdfs:label ?from. ?run p:Module_Leader ?ml. ?ml rdfs:label ?to. ?run p:module ?mod. ?mod p:shortCode ?label. } ";

let $sparql := concat($service,"?query=",encode-for-uri($query) ) let $result := doc($sparql) let $serialize := util:declare-option("exist:serialize","method=text media-type=text/text") let $xsl := doc("/db/apps/rdf/assets/sparqlResult2dot.xsl") return transform:transform($result,$xsl,)

Transformation to a Graph
To transform to a graph, we use a service provided by the University of the West of England. This accepts a URL of a dot file, passes the file to a local installation of Graphviz and returns the result as either PNG, JPG or SVG.

http://www.cems.uwe.ac.uk/~cjwallac/apps/services/dot2media-v2.php?output=png&url=http://kitwallace.co.uk/rdf/xquery/reviewers.xq

Summary
We have implemented a small pipeline of transformations: tripleStore ->(SPARQL) -> SPARQL Result -> (XSLT) -> dot -> (graphviz) -> SVG

The XSLT is generic so that any SPARQL query return containing from, to and label triples can be graphed.

Development
The dot file is currently pulled by the PHP script because in general it is too large to be passed as a parameter. An eXist module should be available to interface to a local version of Graphviz in place of this lashup.