Search the web
Sign In
New User? Sign Up
jena-dev · Jena Developers
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.

Messages

  Messages Help
Advanced
RDFNode and Resource   Topic List   < Prev Topic  |  Next Topic >
Reply  | 
Re: [jena-dev] RDFNode and Resource

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



Wed May 31, 2006 11:24 am

s9268716@...
Send Email Send Email

Attachment
signature.asc
Type:
application/pgp-signature
 | 
Expand Messages Author Sort by Date

Hello, I am working on a Java class which is able to transform Jena Models into a JSON structure (using json_simple by FangYidong). Right now, I think I am...
Björn Thalheim
s9268716@...
Send Email
May 27, 2006
12:40 pm

Hi Björn, ... Interesting. I suggest you align your JSON representation with the "SPARQL results in JSON" spec at [1], it already defines a way of ...
Richard Cyganiak
cygri
Offline Send Email
May 27, 2006
2:56 pm

Hello, ... Sounds very good. Since joseki isn't documented too well, I would like to ask another question. Does there exist a way to re-transform the query...
Björn Thalheim
s9268716@...
Send Email
May 29, 2006
12:30 pm

... Note that all serialization is done by ARQ, not Joseki, so the documentation there is also relevant. I would like to ask another ... A query result that is...
Seaborne, Andy
andyseaborne
Offline Send Email
May 29, 2006
1:15 pm

Hi, ... No Offence ;-) ... Yeah, I figured that out once I read through the Joseki source code. So I came up with the following source code: public void...
Björn Thalheim
s9268716@...
Send Email
May 29, 2006
2:52 pm

... None taken - it does say that in the release notes! ... That does not reconstruct the model - the model produced by .toModel(rs) is an encoding of the...
Seaborne, Andy
andyseaborne
Offline Send Email
May 29, 2006
3:33 pm

On 29 May 2006, at 16:50, Björn Thalheim wrote: (...) ... Please do :-) Richard...
Richard Cyganiak
cygri
Offline Send Email
May 29, 2006
7:09 pm

Hi, ... 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...
Björn Thalheim
s9268716@...
Send Email
May 31, 2006
11:32 am

... Yes - see http://www.w3.org/2001/sw/DataAccess/json-sparql/ which is going to be published as a W3C Note. ARQ can serialize results and read this result...
Seaborne, Andy
andyseaborne
Offline Send Email
May 28, 2006
4:45 pm
Advanced

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help