XQuery/Graphing from RDF

Motivation

edit

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

edit

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

edit

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: <http://www.cems.uwe.ac.uk/rdffold/vocab/>
PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

 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

edit

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

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://www.w3.org/2005/sparql-results#" version="1.0">
    <xsl:output method="text"/>
    <xsl:variable name="nl">
        <xsl:text>
</xsl:text>
    </xsl:variable>
    <xsl:template match="/s:sparql/s:results">
        <xsl:text>digraph</xsl:text>
        <xsl:text>{ rankdir=LR;</xsl:text>
        <xsl:apply-templates select="*"/>
        <xsl:text>}</xsl:text>
        <xsl:value-of select="$nl"/>
    </xsl:template>
    <xsl:template match="s:result">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="s:binding[@name='from']/s:literal"/>
        <xsl:text>" -&gt; "</xsl:text>
        <xsl:value-of select="s:binding[@name='to']/s:literal"/>
        <xsl:text>" [label="</xsl:text>
        <xsl:value-of select="s:binding[@name='label']/s:literal"/>
        <xsl:text>"];</xsl:text>
        <xsl:value-of select="$nl"/>
    </xsl:template>
</xsl:stylesheet>

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: <http://www.cems.uwe.ac.uk/rdffold/vocab/>
PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

 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

edit

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

edit

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

edit

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.