Wednesday, July 13, 2011

Groovy SPARQL 0.2 Available

Version 0.2 of Groovy SPARQL is now available.  This minor release includes a Groovy DSL for RDF, now you can build RDF and then query it.  The Groovy DSL is fairly flexible and takes advantage of a number of Groovy features including:
  1. Optional syntax in Groovy 1.8 for more fluid DSLs
  2. GPars, aka the Groovy Parallelizer for asynchronous output hooks
  3. Usage of the BuilderSupport class
 Per previous posts on the blog, if you want to use it in Grails / GroovyConsole / other apps, I recommend downloading, doing the Gradle build, install into your local Maven repo and then you can include it easily enough in whatever build environment you are using.

Here is a GIST showing the RDFBuilder DSL in action, with comments noting all of the 'features' available so far.  This is still a work in progress, and the more I attempt to use it to build FOAF and other vocabularies, I'm sure I'll be shaking some bugs out (not the least of which is the wonderful world of URI fragments).

@Grab('org.codehaus.groovy.sparql:groovy-sparql:0.2')
import groovy.sparql.*
/**
Can be constructed with:
- Outputstream
- PrintWriter
- Model (all statements built will be copied into the input model
via model.add(List<Statement>)
Can register other output hooks
builder.registerOutputHook(closure)
If more than one output hook is registered, they're executed in parallel via GPars
First closure in the builder is the output type, here are the format types (mapped to Jena):
[xml:"RDF/XML", xmlabbrev:"RDF/XML-ABBREV", ntriple:"N-TRIPLE", n3:"N3", turtle:"TURTLE"]
All other root closure names are ignored, and you just get the model returned
Vocab in the DSL is:
defaultNamespace "namespace"
namespace prefix:"namespace"
subject("uri or fragment) {
property "predicate":object"
property "predicate" {
subject("uri or fragment") {
....
}
}
}
You can nest subjects and properties per above, see the JUnit test for example
**/
def builder = new RDFBuilder(System.out)
// model is a Jena Model
def model = builder.n3 {
defaultNamespace "http://www.example.org"
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"
}
}
/**
As N3
<http://www.example.org#clarkkent>
<http://xmlns.com/foaf/0.1/family_name>
"Kent" ;
<http://xmlns.com/foaf/0.1/gender>
"male" ;
<http://xmlns.com/foaf/0.1/givenname>
"Clark" ;
<http://xmlns.com/foaf/0.1/title>
"Mr" .
**/
model = builder.xml {
defaultNamespace "http://www.example.org"
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"
}
}
/**
As XML
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://xmlns.com/foaf/0.1/" >
<rdf:Description rdf:about="http://www.example.org#clarkkent">
<j.0:family_name>Kent</j.0:family_name>
<j.0:givenname>Clark</j.0:givenname>
<j.0:title>Mr</j.0:title>
<j.0:gender>male</j.0:gender>
</rdf:Description>
</rdf:RDF>
**/
view raw RDFDSL.groovy hosted with ❤ by GitHub


Enjoy!

No comments:

Post a Comment