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:
I can create the statement:
<http://someuri/myont#foo> <http://someuri/myont#weight> "50.7"^^www.w3.org/2001/XMLSchema#double
but the string version that I can get out looks like this:
"http://someuri/myont#foo" "http://someuri/myont#weight" "50.7^^www.w3.org/2001/XMLSchema#double"
(note the absence of a " before the ^^)
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?
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.Ian
Not an area I've looked at in great depth, but I recalled Talis were doing some research and was able to follow breadcrumbs to the relevant vocabulary called "Changeset".
http://vocab.org/changeset/schema
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.
The solution I ended up with is below. I ended up using a reg-ex approach because of time constraints (I didn't see the other suggestions on this question until very recently)
This is probably not the best approach, but it seems to work well (and I've vetted it with a test suite that exercise the use cases I need to deal with at this point).
The
createStatement(...)
method is in an OntUtilities helper class.Maybe you should try to replace the String parameter of createStatement by Model.createLiteral(String)...