- Optional syntax in Groovy 1.8 for more fluid DSLs
- GPars, aka the Groovy Parallelizer for asynchronous output hooks
- Usage of the BuilderSupport class
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).
This file contains hidden or 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
@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> | |
**/ | |
Enjoy!
No comments:
Post a Comment