It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical? Alan Perlis, Epigrams in Programming

Stardog Spring

Introduction

Spring is a platform to build and run enterprise applications in Java. Stardog's Spring support makes life easier for enterprise developers who need to work with Semantic Web technology—including RDF, SPARQL, and OWL—by way of Stardog.

The Spring for Stardog source code is available on Github. A more featureful version of will be available in Stardog Enterprise Edition.

Building Spring for Stardog

To build Spring for Stardog, you need a relase of Stardog; we use Gradle to build Stardog for Spring. Then,

Overview

Spring for Stardog 0.0.3 provides a set of capabilities for rapidly building Stardog-backed applications with the Spring Framework. As with many other parts of Spring, Stardog's Spring integration uses the template design pattern for abstracting standard boilerplate away from application developers.

At the lowest level, Spring for Stardog includes

  1. DataSouce and DataSourceFactoryBean for managing Stardog connections
  2. SnarlTemplate for transaction- and connection-pool safe Stardog programming
  3. DataImporter for easy bootstrapping of input data into Stardog

Future releases of Spring for Stardog will address other common enterprise capabilities: Spring Batch, Spring Data, etc.

Use

There are three Beans to add to a Spring application context:

DataSourceFactoryBean is a Spring FactoryBean that configures and produces a DataSource. All of the Stardog ConnectionConfiguration and ConnectionPoolConfig methods are also property names of the DataSourceFactoryBean—for example, "to", "url", "createIfNotPresent".

DataSource is a Spring for Stardog class, similar to javax.sql.DataSource, that can be used to retrieve a Connection from the ConnectionPool. This additional abstraction serves as place to add Spring-specific capabilities (e.g. spring-tx support in the future) without directly requiring Spring in Stardog.

SnarlTemplate provides a template abstraction over much of Stardog's native API, SNARL, and follows the same approach of other Spring template, i.e., JdbcTemplate, JmsTemplate, and so on.

The key methods on SnarlTemplate include the following:

query(String sparqlQuery, Map args, RowMapper)

query() executes the SELECT query with provided argument list, and invokes the mapper for result rows.

doWithAdder(AdderCallback)

doWithAdder() is a transaction- and connection-pool safe adder call.

doWithGetter(String subject, String predicate, GetterCallback)

doWithGetter() is the connection pool boilerplate method for the Getter interface, including the programmatic filters.

doWithRemover(RemoverCallback)

doWithRemover() As above, the remover method that is transaction and pool safe.

execute(ConnectionCallback)

execute() lets you work with a connection directly; again, transaction and pool safe.

construct(String constructSparql, Map args, GraphMapper)

construct() executes a SPARQL CONSTRUCT query with provided argument list, and invokes the GraphMapper for the result set.

DataImporter is a new class that automates the loading of RDF files into Stardog at initialization time.

It uses the Spring Resource API, so files can be loaded anywhere that is resolvable by the Resource API: classpath, file, url, etc. It has a single load method for further run-time loading and can load a list of files at initialization time. The list assumes a uniform set of file formats, so if there are many different types of files to load with different RDF formats, there would be different DataImporter beans configured in Spring.

Here's a sample applicationContext:

Examples

query() with SELECT queries

doWithGetter

doWithAdder

doWithRemover

construct()

Notes

Comments

blog comments powered by Disqus