Hi,
Richard Cyganiak wrote:
>>I do not know if that works, but I'm going to test it. If anybody is
>>interested in the results, I can post them on the list.
>
> Please do :-)
As we haven't expected any differently, Andys Code does work.
My test code has shown, that the model which is read in from the
sparql-json file is equivalent to to original model. I tested that on
three different models testing equvalence by hand using a dotty file
representing the model.
My code works now as follows:
public void exportToJSONFile(Model model, String pathtofile) {
FileOutputStream fos;
try {
fos = new FileOutputStream(pathtofile);
} catch (Exception e) {
e.printStackTrace();
return;
}
// get Model as ResultSet by SELECT * WHERE { ?s ?p ?o }
Query q = QueryFactory.create("SELECT * WHERE { ?s ?p ?o }");
QueryExecution qexec = QueryExecutionFactory.create(q, model);
ResultSet rs = qexec.execSelect();
// Take ResultSet and serialize it into JSON Output Stream
ResultSetFormatter.outputAsJSON(fos, rs);
try {
fos.flush();
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public Model importFromJSONFile(String pathtofile) {
Model m = ModelFactory.createDefaultModel();
FileInputStream fis;
try {
fis = new FileInputStream(pathtofile);
} catch (Exception e) {
e.printStackTrace();
return;
}
try {
ResultSet results = ResultSetFactory.fromJSON(fis);
for (; results.hasNext();) {
QuerySolution soln = results.nextSolution();
Resource s = soln.getResource("s");
// Turn resources into properties because they have URIs
Property p = m.createProperty(soln.getResource("p").getURI());
RDFNode o = soln.get("o");
m.add(s, p, o);
}
} finally { // nothing
}
return m;
}
This works quite fine and should be a good possibility to serialize Jena
models.
Ciao,
Björn
--
He is now rising from affluence to poverty.
-- Mark Twain
--
Important! Please recognize my new GPG Public Key!
Björn Thalheim
gpg fingerprint: 2F22 AAEB 1818 1548 EC78 1AE8 9D2E FCB4 0980 28CC
download key: wget http://www.ifsr.de/~bjoern/gpg/public_key.asc
See also: http://www.ifsr.de/~bjoern/gpg/key.html