Saturday, June 23, 2012

Stardog and Spring Framework Example

Ever since the initial release of Stardog Spring support, I wanted to create a simple example application for people to learn how to use the Spring Beans and introduce semantic web technology using Stardog into traditional Spring technology stack implementations.  So I'm happy to announce today a small sample application is now available on Github, the Stardog PetStore.  Following the Spring PetClinic example, the Pet Store uses the metaphor of a store to go buy a pet - in this case, dogs!

What we'll see in the sample application is:
  • A Gradle build that references the Stardog library folder, one of the easiest ways to pull Stardog into a build
  • A basic Spring 3.1 Web MVC application, with one controller, and a couple views.
  • The usage of traditional Spring beans files, and some stereotype annotations for autowiring of the Data Access Object
  • Usage of the SnarlTemplate class to create a data access object for a POJO
  • Usage of the DataImporter capability to load some triples at initialization time

There are, of course, lots of fun things going on with Spring 3.1 with Java configurations, javax.inject annotations, environment abstraction, etc.  This sample was kept simple by following the standard 'Simple MVC Template' project available in SpringSource Toolsuite, so anyone learning Spring can follow the parallels of those samples.  Also SnarlTemplate is analogous to JdbcTemplate, so it uses the traditional Data Access Object design pattern as opposed to new rich domain models and Spring Data.  Like JdbcTemplate, this gives full control over the native APIs and lets the mappings manipulate or use as much of the underlying system as necessary.  in our case, mapping to triples, naming our properties with URIs (i.e. RDF predicates) is a good reason to have this access.  Using Stardog Empire support for RDF backed JPA and Spring Data JPA Repositories is certainly on the TODO list and a good evolutionary next step.

The prerequisite steps for this tutorial are:
  1. Obtain Stardog from and unzip somewhere, make sure license is in Stardog home
  2. Create a database called 'petstore', e.g. "stardog-admin create -n petstore"
  3. Make sure you have Gradle 1.0 build available, command line, Eclipse, etc

Let's kick things off with the build.  After checking out the project from Github, update the build.gradle file and set the location where the Stardog/lib folder exists.

The next step is to run the gradle build.  I recommend the recently released Gradle 1.0 and SpringSource Toolsuite 2.9.2, since it lets you specify the gradle folder directly.  Command line builds with Gradle also work nicely.

You'll see the Stardog Spring beans in the root application context, where we are connecting to Stardog via their SNARL protocol.  The only requisite step here is to follow the Stardog documentation and create a database called 'petstore', i.e. "stardog-admin create -n petstore."

The spring/appServlet/servlet-context.xml contains the standard Spring MVC related beans, and instructs Spring to component scan the com.example.stardog package.  From here it will find the HomeController, DogDAO and wire them togeter with the corresponding SnarlTemplate and DataSource defined above.

Looking at the HomeController, we see a run of the mill Spring controller with the following URLs:
  • "/" - the default list of dogs in the store
  • "/create" - the form for adding a new dog
  • "/delete/id" - the GET URL to remove a dog
In all cases, the controller operates on a DAO to retrieve and manipulate the Dog POJO.  The Dog POJO itself has three properties:
  • Name (String, will be referenced inside a URL for the RDF subject)
  • Wiki URL (e.g. the wikipedia page for the breed)
  • Photo URL (e.g. a wikipedia page)
The DogDAO is where the interesting intersection with Stardog and Spring happens.  Each of the usual persistence methods (list, get, add, update, remove) are backed by operations on the SnarlTemplate.  The URL properties used for RDF creation are referenced in a Constants enum. In this case I reused pieces of the FOAF vocabulary - it's not hard to imagine a linked data query helping to fill out more releated information about the subject in question.

Design note: As an aside, creating the DogDAO for pure domain object abstraction highlighted the need to enhance the SnarlTemplate with more of the equivalent methods like JdbcTemplate (e.g. queryForObject), and support Spring TX management.

If everything works out correctly for you, you'll be able to browse and see a couple of entries listed on the web page.

Default List of Dogs in the Store

The list of dogs gets loaded from a dogs.n3 file, found in src/main/resources.

Lessons Learned from this sample:
  1.  Enterprise grade applications built with Semtech is certainly possible, and with proper encapsulation the RDF and SPARQL can live in the data layer, leaving the business logic the usual bundle of POJO joy
  2. Stardog provides a rich development experience, friendly to Java developers
  3. Gradle saves a tremendeous amount of time by being able to interweave in file system trees along side Maven style dependencies
  4. There is an opportunity for balancing the encapsulation of persistence information (i.e. predicate URIs), business logic, and semantic web exposure (i.e. generating RDFa, RDF representations of POJO resources or REST services)
 My objective is to maintain this example project as a showcase of Semantic web capability and add to it:
  • Linked Data de-reference (e.g. using Spring Jena to query DBPedia)
  • Spring Content Negotiators to expose an RDF resource or RESTful RDF service maybe with JSON-LD
  • Add Empire and a JPA sytle example so SnarlTemplate and JPA can be compared and contrasted
  • Add a few more relationships and triples to showcase the power of SPARQL


  1. PetStore Sample on Github
  2. Stardog - Download and obtain license here
  3. Stardog Docs - Includes chapters on Stardog Spring
  4. Clark & Parsia - creators of Stardog
  5. Spring 3.1 Documentation
  6. Gradle and SpringSource Toolsuite Tutorial


  1. very good infromation and thanks for sharing this keepitup.
    JAVA Online Tutorials

  2. Really something grate in This Article thanks for sharing this. We are providing Java courses training online. After reading this slightly am changed my way of introduction about my training to people. And also refer my website for JAVA Training and solutions of JAVA applications. Please Visit Us @ JAVA training courses online

  3. thanks,You can find material related to Spring Framework by following below link.Spring Framework Online Training in USA

  4. This information which you provided is very much useful for us.It was very interesting and useful for JAVA online training.We also Spring online training in USA

  5.'s very nice post..really i like this post..we are giving spring online training ..if any one want to learn plz once visit the link..

  6. nice video about servicenow well done thanks for valuable information awful piece of information, I had come to know about your blog from my friend vimal, mumbai,i have read atleast 3 posts of yours by now, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a million once again, Regardsdevops training in hyderabad

  7. very nice article.Thanks for sharing the post...!
    OBIEE Online Training

  8. very nice article.Thanks for sharing the post...! Java Spring Online Training

  9. Nice explanation with snapshots, On SAP Modules SAP TOOLS

  10. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

  11. Excellent Blog very imperative good content, this article is useful to beginners and real time
    employees.Thank u for sharing...
    Hadoop Training in Hyderabad
    JAVA Training in Hyderabad

  12. Hi, your article really nice. That is so logical and clearly explained. Keep it up! I will follow up your site for the future post.
    Java Online Training
    Java Online Training in India

  13. Stardog and Spring Frame work withholds the specific set of things, While I was searching for a good institute in Pune for pmp certification then I found certain projects which enriched with certain modules where they are been examined through different Frameworks Anywayz Thank you for providing the information

  14. Each department of CAD have specific programmes which, while completed could provide you with a recognisable qualification that could assist you get a job in anything design enterprise which you would really like.

    AutoCAD training in Noida

    AutoCAD training institute in Noida

    Best AutoCAD training institute in Noida

  15. An amazing web journal I visit this blog, it's unbelievably wonderful. Oddly, in this blog's content made without a doubt and reasonable. The substance of data is informative.
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training
    oracle Fusion Technical online training

  16. An astounding web diary I visit this blog, it's inconceivably magnificent. Strangely, in this current blog's substance made point of fact and sensible. The substance of information is instructive.
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training
    oracle Fusion Technical online training

  17. Great Post,really it was very helpful for us.
    Thanks a lot for sharing!
    I found this blog to be very useful!!
    JAVA training in Bangalore

  18. An astounding web diary I visit this blog, it's inconceivably magnificent. Strangely, in this current blog's substance made point of fact and sensible. The substance of information is instructive.
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training