Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Meet Jena, a Semantic Web Platform for Java : Page 4

Tools for developing semantically aware applications are rapidly growing more Java friendly. Take a closer look at Jena, an open source toolkit for processing and working with semantic web data.

File-Backed and Database Models
The Model API has operations to read a document from a URL (including a file URL), and to write it again to an output stream. Serializing a model to and from a file is straightforward. This serialization, however, means that ensuring a model is saved is the client's responsibility. You could argue that doing so undermines a clean separation of concerns. Jena provides a simple persistence solution for models that save their state in a file. This next code example shows how to create models that automatically save their state when closed. Each time the program runs, a new timestamp value is added to the persistent model state:

public static final String DATA_DIR = "./data"; public static final String NS = "http://devx.com/examples/jena#"; public static final String EX3 = "example3"; public void run() { Model m = getModel(); // add a new timestamp Literal now = m.createTypedLiteral( Calendar.getInstance() ); m.getResource( NS + EX3 ).addProperty( DC.date, now ); // as a test, write the model out in Turtle format m.write( System.out, "N3" ); // closing the model is the cue to save the persistent state m.close(); } /** Answer a model backed by a persistent file store */ protected Model getModel() { ModelMaker maker = ModelFactory.createFileModelMaker( DATA_DIR ); // get the existing model with that name if we can Model m = maker.getModel( EX3 ); // if we didn't find it, create a new one if (m == null) { m = maker.createModel( EX3 ); m.withDefaultMappings( PrefixMapping.Standard ); } return m; }

Saving a model to a file is a very lightweight form of persistence, but it can be very useful in some situations. More often, however, when the design calls for a persistent model it's because the application has very large volumes of data, or requires transactional support. Then a database is needed usually.

Jena ships with support for a wide range of standard database engines, including MySQL, PostgresQL, SQL Server, Oracle, and Derby. "Support" here means that Jena's database adapters use standard JDBC drivers to manage those database engines as triple stores. In particular, it means that Jena will create and manage its own table layout in the database, rather than use existing tables. There are ways of treating normal relational tables as triple sources, but the core relational model support in Jena only manages Jena-specific tables. These tables can store any number of models, as long as each model has a distinct name.

The Jena adapters support transactions if the JDBC driver supports them, and they hide the variations of SQL syntax in the different databases from the Model interface. When a Jena application calls, for example, listStatements() on a database-backed model, Jena will construct the appropriate SQL query, execute it against the database engine, and translate the ResultSet into an iterator over Statement objects.

There is no requirement for a database to be pre-initialized. If a Jena RDBModel is connected to a database that doesn't have the Jena-specific table layout, Jena will auto-initialize the database tables there and then. Of course, in a production environment it might be advisable to include an explicit initialization step in the automated build/test/deploy scripts as fits the organization's local policies.

As a basic illustration, here is a Jena RDBModel connected to a MySQL database:

@Override public void run() { ModelMaker maker = getModelMaker(); Model m; if (!maker.hasModel( MODEL_NAME )) { // we have not loaded this model yet; do so now and read some content System.out.println( "Loading model content - one time only" ); m = maker.createModel( MODEL_NAME ); FileManager.get().readModel( m, HelloWorld.FOAF_FILE ); } else { m = maker.getModel( MODEL_NAME ); } listPeople( m ); } /** Answer a ModelMaker for connecting to RDB models */ protected ModelMaker getModelMaker() { try { Class.forName( "com.mysql.jdbc.Driver" ); } catch (Exception e) {e.printStackTrace();} IDBConnection conn = new DBConnection( DB_URL, DB_USER, DB_PW, DB_TYPE ); return ModelFactory.createModelRDBMaker( conn ); }

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.