openRDF.org Welcome Guest   | Login   
  Search  
  Index  | Recent Threads  | Who's Online  | User List  | Search  | Help  | RSS feeds

Forum has been closed down
This forum has been closed down due to extensive spamming activities. Please use the mailing list instead.


Quick Go »
Thread Status: Normal
Total posts in this thread: 7
[Add To My Favorites] [Watch this Thread]
Author
Previous Thread This topic has been viewed 1873 times and has 6 replies Next Thread
Apr 27, 2008 12:18:36 PM

bblfish
Sesame Addict
Member's Avatar

France
Joined: Sep 13, 2004
Posts: 102
Status: Offline
HOWTO remove CONSTRUCT query duplicates?

Hi I have a CONSTRUCT query and it writes a lot of triples out in duplicate to the output. What do I have to do to generate a more compact graph? Do I have to put all the results into their own context and then serialise that context/graph to output? If so it would be good if the graph could take a RDFWriter as intput.

The code that I am using to make the query is the following.

        InputStream sprklfile = FoafPublisher.class.getResourceAsStream("resources/publishfoaf.sparql");
StringBuffer query = new StringBuffer();
try {
BufferedReader bin = new BufferedReader(new InputStreamReader(sprklfile, "UTF-8"));
String line;
while ((line = bin.readLine()) != null) {
query.append(line); query.append("\n");
}
} catch (IOException e) {
}
System.out.println("query="+query);
try {
GraphQuery gquery = null;
gquery = rep.prepareGraphQuery(QueryLanguage.SPARQL, query.toString(),saveTo.toURI().toString());
Value vid = smapr.findKnownMappedValueFor(id);
gquery.setBinding("id",vid);
System.out.println("gquery="+gquery);
final TurtleWriter turtleWriter = new TurtleWriter(new FileOutputStream(saveTo));
gquery.evaluate(turtleWriter);
turtleWriter.endRDF();
} catch (RepositoryException e) {
e.printStackTrace(); //todo: decide what exception to throw
} catch (MalformedQueryException e) {
e.printStackTrace(); //todo: decide what exception to throw
} catch (RDFHandlerException e) {
e.printStackTrace(); //todo: decide what exception to throw
} catch (FileNotFoundException e) {
e.printStackTrace(); //todo: decide what exception to throw
} catch (QueryEvaluationException e) {
e.printStackTrace(); //todo: decide what exception to throw
} catch (Exception e) {
e.printStackTrace();
}


The SPARQL query is the following

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

CONSTRUCT {
?id ?rel ?obj;
foaf:knows ?friend .
?friend ?frel ?fobj .
} WHERE {
?id ?rel ?obj .
OPTIONAL {
?id foaf:knows ?friend .
?friend ?frel ?fobj .
}
}

Show Printable Version of Post        Hidden to Guest    http://bblfish.net [Link] Report threatening or abusive post: please login first  Go to top 
Apr 29, 2008 7:15:01 PM

james
OpenRDF.org Consultant
Member's Avatar

Canada
Joined: Jul 21, 2005
Posts: 650
Status: Offline
Re: HOWTO remove CONSTRUCT query duplicates?

You can use the RDFInserter to insert the results of a graph query into an enforced context.
----------------------------------------
Sesame Consultant
http://leighnet.ca
http://jamesrdf.blogspot.com
Show Printable Version of Post        Hidden to Guest    http://leighnet.ca [Link] Report threatening or abusive post: please login first  Go to top 
May 3, 2008 4:37:53 PM

bblfish
Sesame Addict
Member's Avatar

France
Joined: Sep 13, 2004
Posts: 102
Status: Offline
Re: HOWTO remove CONSTRUCT query duplicates?

Thanks for the hint. Here is the code.


InputStream sprklfile = FoafPublisher.class.getResourceAsStream("resources/publishfoaf.sparql");
StringBuffer query = new StringBuffer();
try {
BufferedReader bin = new BufferedReader(new InputStreamReader(sprklfile, "UTF-8"));
String line;
while ((line = bin.readLine()) != null) {
query.append(line);
query.append("\n");
}
} catch (IOException e) {
}

//we want to write this file out, so no need to save it (or perhaps not?)
SesameMapper outmap = (SesameMapper) MapperManager.getMapperForGraph(new SesameMemorySailInit());
RepositoryConnection outrep = outmap.rep();

String baseURI = null;
try {
final RDFInserter inserter = new RDFInserter(outrep);
GraphQuery gquery = null;
// baseURI;
gquery = rep.prepareGraphQuery(QueryLanguage.SPARQL, query.toString(), baseURI);
Value vid = smapr.findKnownMappedValueFor(id);
gquery.setBinding("id", vid);
System.out.println("gquery=" + gquery);
gquery.evaluate(inserter);
inserter.endRDF();
} catch (RepositoryException _GEN_UCE_NAME_1) {
_GEN_UCE_NAME_1.printStackTrace();
} catch (MalformedQueryException _GEN_UCE_NAME_3) {
_GEN_UCE_NAME_3.printStackTrace();
} catch (QueryEvaluationException _GEN_UCE_NAME_5) {
_GEN_UCE_NAME_5.printStackTrace();
} catch (RDFHandlerException _GEN_UCE_NAME_7) {
_GEN_UCE_NAME_7.printStackTrace();
}



try {
final RDFWriter writer;
if (saveTo != null) {
writer = new TurtleWriter(new FileOutputStream(saveTo));
baseURI = saveTo.toURI().toString();
} else if (selectedFile != null ) {
writer = new TurtleWriter(selectedFile.getContent().getOutputStream());
baseURI = selectedFile.getURL().toExternalForm();
} else {
System.out.println("WARNING: no outputstream to write to!");
return null;
}
outrep.export(writer);
} catch (RepositoryException e) {
e.printStackTrace(); //todo: decide what exception to throw
} catch (RDFHandlerException e) {
e.printStackTrace(); //todo: decide what exception to throw
} catch (FileNotFoundException e) {
e.printStackTrace(); //todo: decide what exception to throw
} catch (Exception e) {
e.printStackTrace();
}

Show Printable Version of Post        Hidden to Guest    http://bblfish.net [Link] Report threatening or abusive post: please login first  Go to top 
May 7, 2008 7:36:17 PM

bblfish
Sesame Addict
Member's Avatar

France
Joined: Sep 13, 2004
Posts: 102
Status: Offline
Re: HOWTO remove CONSTRUCT query duplicates?

Mhh it still contains a lot of duplicates. But this is probably something related to the serialiser. See the output from the So(m)mer Address Book Java Web Start link.
Show Printable Version of Post        Hidden to Guest    http://bblfish.net [Link] Report threatening or abusive post: please login first  Go to top 
Jun 4, 2008 12:54:56 PM

bblfish
Sesame Addict
Member's Avatar

France
Joined: Sep 13, 2004
Posts: 102
Status: Offline
Re: HOWTO remove CONSTRUCT query duplicates?

Ok there was a bug in my code, as I was serialising the whole repository, not just the context I thought I was.

Still it would be nice to have a more beautiful Turtle serialisation. I think on the whole rdf should be published as Turtle as much as possible. When developers see XML they automatically will grab for their DOM/SAX/... toolset. When that does not work they will give up without understanding. So it would be much better to output something that will force them to look at rdf tools such as sesame for parsing. I even suggest one add a comment in serialisations such as

# serialised with Sesame at http://openrdf.org/ . Learn about Turtle serialisation format at ...

so people can get the tools they need when they want to do something with it.
But this means that one should try as much as possible to produce human readable turtle. A few simple tricks would make that possible.

Here is the request for enhancement:

http://www.openrdf.org/issues/browse/RIO-63

(That request for enhancement also points to the code that shows how I use CONSTRUCT queries to publish foaf. Could be useful to other projects....)
----------------------------------------
[Edit 1 times, last edit by bblfish at Jun 4, 2008 12:56:50 PM]
Show Printable Version of Post        Hidden to Guest    http://bblfish.net [Link] Report threatening or abusive post: please login first  Go to top 
Jun 4, 2008 2:22:15 PM

jeen
Sesame Addict
Member's Avatar

The Netherlands
Joined: Jan 23, 2004
Posts: 1091
Status: Offline
Re: HOWTO remove CONSTRUCT query duplicates?

I am wondering, could you perhaps achieve this effect by using an ORDER BY clause in your query? For this to work you'd have to remove the temporary repository (outrep) again and just directly push your query result into an RDFWriter... But that might work.
----------------------------------------
Researcher at AFSG - Wageningen UR
Show Printable Version of Post     [Link] Report threatening or abusive post: please login first  Go to top 
Jun 5, 2008 12:18:01 PM

bblfish
Sesame Addict
Member's Avatar

France
Joined: Sep 13, 2004
Posts: 102
Status: Offline
Re: HOWTO remove CONSTRUCT query duplicates?

Order by won't work with CONSTRUCT queries, as stated in the SPARQL spec at the end of section 9.1:
 
Using ORDER BY on a solution sequence for a CONSTRUCT or DESCRIBE query has no direct effect because only SELECT returns a sequence of results.

----------------------------------------
[Edit 1 times, last edit by bblfish at Jun 5, 2008 12:19:08 PM]
Show Printable Version of Post        Hidden to Guest    http://bblfish.net [Link] Report threatening or abusive post: please login first  Go to top 
[Show Printable Version of Thread]