"I have a mind like a steel... uh... thingy." Patrick Logan's weblog.

Search This Blog

Saturday, April 02, 2011

Using SPARQL endpoints and 4store RDF databases from Common Lisp

Jeni Tennison posted an interesting article using the 4store for RDF databases from Ruby. This was an experiment to see how close one can get to meeting a challenge from Richard Pope. The challenge puts forth criteria for a set of easy-to-use "linked data" programming tools.

I have duplicated the essence of the ruby/4store demonstration, using common lisp. Mainly because I like programming in various lisp dialects, I want to explore using 4store, and I recently decided to pick up common lisp again after, for all intents and purposes, a 20 year hiatus from that particular dialect.

The source and sample data is on github at https://github.com/patrickdlogan/sbcl-4store

These are just some examples that a reusable package could be based on. The code in workspace.lisp includes instructions for installing 4store on ubuntu, installing Steel Bank Common Lisp, and using the quicklisp system (think ruby gems) for finding and installing useful libraries.

Several functions are defined showing quickly how to load, query, and process RDF data. Two primary functions and their results are listed here:

The function extract-rdfs-classes performs a SPARQL "select" query to select all of the RDFS classes from the sample data. Note: these are not 'object-oriented classes', rather an RDFS class is more like the identifier of a logical "set" of members which are themselves identifiers.

The SPARQL query is:

prefix rdf: 
prefix rdfs: 
select distinct ?type 
where { 
  ?x a ?type .
} 
order by ?type
And running it from the lisp REPL:
* (extract-rdfs-classes)

("http://purl.org/linked-data/cube#DataSet"
"http://purl.org/linked-data/cube#DataStructureDefinition"
"http://purl.org/linked-data/cube#Observation"
"http://purl.org/net/opmv/ns#Artifact" "http://purl.org/net/opmv/ns#Process"
"http://purl.org/net/opmv/types/google-refine#OperationDescription"
"http://purl.org/net/opmv/types/google-refine#Process"
"http://purl.org/net/opmv/types/google-refine#Project"
"http://rdfs.org/ns/void#Dataset"
"http://reference.data.gov.uk/def/central-government/AssistantParliamentaryCounsel"
"http://reference.data.gov.uk/def/central-government/CivilServicePost"
"http://reference.data.gov.uk/def/central-government/Department"
"http://reference.data.gov.uk/def/central-government/DeputyDirector"
...
The function extract-persons performs a SPARQL "construct" query to construct a graph of FOAF Person instances along with their FOAF names and other triples having the instance as the subject.

The SPARQL query is:

prefix foaf: 
construct {
  ?person 
    a foaf:Person ;
    foaf:name ?name ;
    ?prop ?value .
} where { 
  ?person a foaf:Person ;
  foaf:name ?name ;
  ?prop ?value .                        
}
And running it from the lisp REPL:
* (extract-persons)

(("http://source.data.gov.uk/data/reference/organogram-co/2010-10-31#person189"
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
  "http://xmlns.com/foaf/0.1/Person")
 ("http://source.data.gov.uk/data/reference/organogram-co/2010-10-31#person189"
  "http://xmlns.com/foaf/0.1/name" #<"Philip Davies"@en>)
 ("http://source.data.gov.uk/data/reference/organogram-co/2010-10-31#person189"
  "http://reference.data.gov.uk/def/central-government/holdsPost"
  "http://reference.data.gov.uk/id/department/co/post/190")
 ("http://source.data.gov.uk/data/reference/organogram-co/2010-10-31#person189"
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
  "http://xmlns.com/foaf/0.1/Person")
 ("http://source.data.gov.uk/data/reference/organogram-co/2010-10-31#person189"
  "http://xmlns.com/foaf/0.1/name" #<"Philip Davies"@en>)
 ("http://source.data.gov.uk/data/reference/organogram-co/2010-10-31#person189"
  "http://xmlns.com/foaf/0.1/mbox"
  "mailto:philip.j.davies@cabinet-office.x.gsi.gov.uk")
 ("http://source.data.gov.uk/data/reference/organogram-co/2010-10-31#person189"
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
  "http://xmlns.com/foaf/0.1/Person")
...

No comments:

Blog Archive

About Me

Portland, Oregon, United States
I'm usually writing from my favorite location on the planet, the pacific northwest of the u.s. I write for myself only and unless otherwise specified my posts here should not be taken as representing an official position of my employer. Contact me at my gee mail account, username patrickdlogan.