Linked RP works the same way Ratpack does - you provide a single Domain Specific Language (DSL) script where you write your methods to perform some function on a URL, and it weaves those in to a Jetty container. In this case, I've added some capabilities to Ratpack to work with linked data:
- RDFBuilder from Groovy SPARQL is automatically available to the DSL script under the 'rdf' variable
- link(String endpoint) is available as a function to get an instance of the Groovy SPARQL Sparql class for performing Sparql queries.
- resolve(String uri) is a new piece of functionality that uses Groovy's HTTPBuilder DSL and Jena to retrieve a URL and read it into RDF. It should work across various RDF serialization types, and likely bomb out on HTML or anything else if you feed it an incorrect URI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set 'port', 4999 | |
// All three methods (RDFBuilder closure, construct, resolve) return | |
// Jena models and can be further processed with Groovy SPARQL's Sparql class | |
// link(String endpoint) returns the Sparql endpoint or you can instantiate new | |
// ones. Jena+ ARQ will be on the classpath | |
get("/") { | |
setHeader("Content-Type", "application/rdf+xml") | |
rdf.xml { | |
defaultNamespace "http://localhost:4999/test" | |
namespace foaf:"http://xmlns.com/foaf/0.1" | |
subject("#clarkkent") { | |
property "foaf:gender":"male" | |
property "foaf:title":"Mr" | |
property "foaf:givenname":"Clark" | |
property "foaf:family_name":"Kent" | |
} | |
};return null; // using the builder already sends output | |
// and lets you customize the serialization format | |
} | |
get("/groovy") { | |
link("http://dbpedia.org/sparql").construct(""" | |
CONSTRUCT { | |
<http://dbpedia.org/resource/Groovy_%28programming_language%29> <http://dbpedia.org/ontology/abstract> ?b | |
} wHERE { | |
<http://dbpedia.org/resource/Groovy_%28programming_language%29> <http://dbpedia.org/ontology/abstract> ?b | |
} | |
""") | |
} | |
get("/tim") { | |
resolve('http://www.w3.org/People/Berners-Lee/card') | |
} |
You can now browse to the following URLs:
- localhost:4999/
- localhost:4999/tim
- localhost:4999/groovy
Note: since Jena models being returned by those functions get automatically serialized back out - if you want to do serialization inline - return null
To get started with Linked Ratpack, you must do the following:
- Get Groovy SPARQL from Github, and build/install it with Gradle
- Get Linked Ratpack from Github, and build it
- Create simple groovy scripts, like the above gist, and run "ratpack path/to/whatever.groovy"
For me, this is one of the missing pieces in building linked data applications - an easy way to stand up little RDF servers to test walking RDF graphs hop-by-hop and perform URI de-referencing, and experimenting with generating derivative RDF sites from other RDF data sources (e.g. SPARQL Construct).
Many thanks to Justin Voss ( @ github ) for creating Ratpack in the first place, it was a solid foundation to build off of.
Enjoy!