I need to keep a couple of [Jena] Models (OntModels, specifically) synchronized across a socket, and I'd like to do this one change at a time (for various reasons -- one being that each Statement added or removed from the OntModels is also adapting a JESS rule base.). I am able to listen to the add/remove events on the OntModels and then create simple event instances that wrap the added / removed Statements along with a ChangeType that indicates that the Statement was added or removed, but serializing the Statement has proven to be a problem.

Unfortunately, all of the JENA serialization documentation that I've found relates to serializing an entire model to xml / rdf / n3 / etc. Since statements are simply triples of Strings (at one level, anyway) it seems like it should be trivial to serialize the data at the Statement level. However, [Jena] doesn't seem to provide an API for creating Statements with plain strings that "does the right thing". Problems arise with typed literals. eg:

This wouldn't be that much of a problem, since the literal can still be parsed out with a regex, but I've been unable to create a Statement with the proper literal. The obvious approach (ModelCon.createStatement(Resource, Property, String)) generates an untyped string literal with the full value of the String passed in.

Does anyone know how I can reliably serialize (and deserialize, of course) individual Jena Statements?

4 Answers
4

I would serialize the changes out in N-TRIPLES format. Jena has built-in N-TRIPLES serializer and parser, but the N-TRIPLES syntax is (deliberately) very simple so it would be easy to generate manually in your code.

However, it might be even easier to keep a plain mem model around to hold the changes, have the event handlers write changes into that model, then serialize that model over the wire according to your synchronization schedule. Likewise, at the far end I would read the updates from the sync channel into a temporary mem model, then yourOntModel.add( changesModel ) should add in the updates very straightforwardly.

The N-triples syntax is esentially what I was using (and ended up using) the problem was in detecting literals during deserialization. (You can see my approach here now). Using models for serialization is probably the most robust though.
–
rcreswickJan 14 '09 at 0:12

I'm surprised you had issues serialising individual statements using JENA, but perhaps if you created a graph according to the changeset schema and serialised the graph you'd have more luck? Alternately, add the statement to a new graph and serialise a graph of one tripple.

This may be the best approach -- if the need arises I'll probably consider keeping a model around just for serialization / deserialization purposes. (each change gets pushed to the model, the model is serialised, and then cleared for the next set of changes.)
–
rcreswickJan 14 '09 at 0:10