Tuesday, June 28, 2011

Gradle Build to create POM

Any Java/java-compatible language compile these days is going to want to create a POM file - POM's to install locally, POM's to upload remotely, a POM for all seasons.

For Groovy SPARQL, I wanted to create a simple POM, so folks could add dependencies to their project and start using it.  I'm using the latest and greatest Gradle to build the project, and wanted to simply add 'apply plugin' to get a POM created.  Unfortunately that errored out with a missing group Id.  This wasn't surprising, since it wasn't clear if gradle was going to somehow extract a groupId automatically or if you would specify it.

The Gradle docs have the example, but it's down at example 36.8, and since it doesn't show groupId, it didn't jump out as obvious as this was the correct way to do it.  So here it is, a quick gist on how to get a simple gradle artifact into POM format and installed.

This created a ./build/poms/default-pom.xml with correct groupId, artifactId, and dependencies (properly scoped).

Sunday, June 26, 2011

Announcing Groovy SPARQL

As part of my on-going weekend hobby of looking at Groovy and the Semantic web technology stack, I've created a quick port of Groovy SQL support style operations into the land of SPARQL.  So, just like you can do Sql.eachRow(closure), you can now do Sparql.eachRow(closure)

Below is a Gist that shows the features of the relatively small API.  Note that this was also a dive into some Groovy 1.8 and Gradle, and was coded up this weekend inbetween BBQs using SpringSource Toolsuite 2.7.0M2 for the Groovy 1.8/Gradle support.

Other features to come include:
  • Fluent DSL, leveraging Groovy 1.8 features 
  • Pure Java "Templates" for Jena/SPARQL similar to JdbcTemplate/jmsTemplate in Spring
  • Object marshalling and GORM / Spring Data support
  • Sparql / RDF Builder -- still deciding if this is necessary or not, or if it'll fall naturally into the DSL
  • Grails plug-in for the above
  • Testing with triples stores Jena TDB, Stardog, and AllegroGraph being the first three
The code is up on Github here




Enjoy!

Sunday, June 12, 2011

Spring Controller with Date Object Bindings

UPDATE (28 Dec 2011): The new Spring 3.1 release includes a new DateFormatter, which provides this flexibility for passing in date fields in URLs.  Below is the original post for those still interested in the WebDataBinder API. 

I guess this is turning into a series on time handling, but I wanted to share how to use the data binders in Spring to pass around date classes.  After all, you may want to have URLs that end in some representation of a "day" to page through time-sensitive data, or filter your data based on time, etc.

For example, if you wanted to have a URL parameter of the form "yyyy-MM-dd", i.e. the XML Schema format, and marshal it into a java.util.Date, how would you go about doing this?

Well, Spring provides:
  • an InitBinder to initialize WebDataBinders on a controller - i.e. setup a the binding mechanism on a Spring controller automatically
  • WebDataBinder to register custom property editors to convert certain types.  In this case the type is java.util.Date, and the property editor is also provided by Spring - CustomDate Editor
With this, you can now use a date class with:
  • @RequestParam(value="date", required=false) Date date
  • @RequestMapping(value="/date/{date}", method=RequestMethod.GET) ...
  • @PathVariable("date") Date date

Here's the code:



Enjoy!

Saturday, June 4, 2011

Jena and DateTime

Before you start storing timestamp literals in your triple store, consider the value of typed literals.  Typed literals can be easily converted to their native language type, in this case - java.util.Calendar.  Furthermore, SPARQL lets you do things like compare and filter on date values. 

Here is a quick JUnit test to illustrate using Jena to create/read triples with dateTime typed literals, as well as using ARQ to query for dateTime literals.



Some notes on the SPARQL (not shown above):
- A dateTime literal can be used in SORT expressions, e.g. ORDER BY ASC(?date)
- A dateTime literal can be used in FILTER expressions with comparisons such as FILTER ( ?date >= "$someDateTime"^^xsd:dateTime
- Note that in SPARQL, you explicitly type the literal. You should define a PREFIX with xsd: so you can use the short hand

Using xsd:dateTime is a good choice because the API translates to java.util.Calendar, of which you can easily do things like get your hands on a java.util.Date, automatically handle time zones, etc.

Friday, June 3, 2011

Two ways to Query Linked Data with ARQ

ARQ provides two ways to query Linked Data, i.e. remote SPARQL Endpoints.  The first one us to use the "sparqlService" method on the QueryExecutionFactory. 

E.g.: 

QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

The other way can be done with a QueryExecutionFactory that is also attached to a Jena Model, however you specifcy the "service" in the SPARQL directly.

E.g.:

QueryExecution qe =  QueryExecutionFactory.create(query,model);

Sparql:
SELECT ?s ?p WHERE {
    SERVICE <http://dbpedia.org/sparql> {
        ?s ?p <http://en.wikipedia.org/wiki/Sparql>
    }
 }

Enjoy traversing the linked data!