SPARQL Group-Concat not working

760 views Asked by At

I've got an RDF graph element looking like this using Android & Jena:

<ns0:CalendarItem rdf:about="http://my.url.com/ontologies/mash-up#CalendarItem-uh0cjvnuehhvfjpgppiggmpjac">
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-2de52a85-3107-4d82-a361-cca11afabdc8" />
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-3b0e0333-5693-43e7-bb6e-af06f7df9b12" />
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-431885fb-1f56-4253-a757-3039dfaceabd" />
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-db957016-5cba-41d4-b2b2-5ae2a472822c" />
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-fe05d441-4e09-419c-99af-504d93177574" />
   <ns0:description rdf:datatype="&xsd;string">First Entry</ns0:description>
   <ns0:endDate rdf:datatype="&xsd;dateTime">2015-06-10T12:30:00Z</ns0:endDate>
   <ns0:name rdf:datatype="&xsd;string">Meeting Everyone</ns0:name>
   <ns0:startDate rdf:datatype="&xsd;dateTime">2015-06-10T11:30:00Z</ns0:startDate> 
</ns0:CalendarItem>

I want all the attendees to be listed in some kind of group/array. I tried using this query:

"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
                "PREFIX ns0: <http://my.url.com/ontologies/mash-up#> "+
                "PREFIX ns1: <http://xmlns.com/foaf/0.1/> "+
                "SELECT ?object (group_concat(distinct ?attendee) as ?attendees) " +
                "WHERE { " +
                "?object rdf:type ns0:CalendarItem . " +
                "OPTIONAL { ?object ns0:attendee ?attendee } " +
                "} group by ?object";

I always get an exception like this:

.../AndroidLoggerFactory﹕ Logger name 'com.hp.hpl.jena.sparql.lib.SystemUtils' exceeds maximum length of 23 characters, using 'c*.h*.h*.j*.s*.l*.Syst*' instead.


I/System.out﹕ ERROR
.../System.err﹕ com.hp.hpl.jena.query.QueryParseException: Encountered" <VAR1> "?attendee "" at line 1, column 200.
...W/System.err﹕ Was expecting one of:
...W/System.err﹕ "(" ...
...W/System.err﹕ <NIL> ...

I just can't seem to get this working after searching everywhere. Thanks in advance.

1

There are 1 answers

2
Joshua Taylor On BEST ANSWER

It's always helpful if you can show the query and the data complete and on their own, not just snippets and embedded in code. Here's some complete data that we can test with:

<rdf:RDF
  xmlns:ns0="http://my.url.com/ontologies/mash-up#"
  xmlns:ns1="http://xmlns.com/foaf/0.1/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <ns0:CalendarItem rdf:about="http://my.url.com/ontologies/mash-up#CalendarItem-uh0cjvnuehhvfjpgppiggmpjac">
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-2de52a85-3107-4d82-a361-cca11afabdc8" />
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-3b0e0333-5693-43e7-bb6e-af06f7df9b12" />
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-431885fb-1f56-4253-a757-3039dfaceabd" />
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-db957016-5cba-41d4-b2b2-5ae2a472822c" />
   <ns0:attendee rdf:resource="http://my.url.com/ontologies/mash-up#Person-fe05d441-4e09-419c-99af-504d93177574" />
   <ns0:description rdf:datatype="http://www.w3.org/2001/XMLSchema#string">First Entry</ns0:description>
   <ns0:endDate rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2015-06-10T12:30:00Z</ns0:endDate>
   <ns0:name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Meeting Everyone</ns0:name>
   <ns0:startDate rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2015-06-10T11:30:00Z</ns0:startDate> 
</ns0:CalendarItem>
</rdf:RDF>

And here's your query, not embedded in Java code:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ns0: <http://my.url.com/ontologies/mash-up#>
PREFIX ns1: <http://xmlns.com/foaf/0.1/>

SELECT ?object (group_concat(distinct ?attendee) as ?attendees)
WHERE {
  ?object rdf:type ns0:CalendarItem .
  OPTIONAL { ?object ns0:attendee ?attendee }
}
group by ?object

Sparql.org's query validator says that's a legal query. (There was briefly another answer stating that you need to add a separator argument for group_concat, but that's not true. 18.5.1.7 GroupConcat says that 'If the "separator" scalar argument is absent from GROUP_CONCAT then it is taken to be the "space" character, unicode codepoint U+0020.') In fact, when I run that with Jena locally (using the command line sparql tool), I get these results, just like you'd expect:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| object                                      | attendees                                                                                                                                                                                                                                                                                                                                                                                                              |
========================================================================================================================================================================================================================================================================================================================================================================================================================================================================
| ns0:CalendarItem-uh0cjvnuehhvfjpgppiggmpjac | "http://my.url.com/ontologies/mash-up#Person-db957016-5cba-41d4-b2b2-5ae2a472822c http://my.url.com/ontologies/mash-up#Person-fe05d441-4e09-419c-99af-504d93177574 http://my.url.com/ontologies/mash-up#Person-2de52a85-3107-4d82-a361-cca11afabdc8 http://my.url.com/ontologies/mash-up#Person-3b0e0333-5693-43e7-bb6e-af06f7df9b12 http://my.url.com/ontologies/mash-up#Person-431885fb-1f56-4253-a757-3039dfaceabd" |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

That doesn't really explain the problem though. Are you perhaps using a very old version of Jena that only supports the first SPARQL standard, and not SPARQL 1.1?