SPARQL Group-Concat 不工作

SPARQL Group-Concat not working

我使用 Android & Jena 得到了一个看起来像这样的 RDF 图元素:

<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>

我希望所有与会者都列在某种 group/array 中。我尝试使用此查询:

"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";

我总是遇到这样的异常:

.../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> ...

我到处搜索后似乎无法正常工作。 提前致谢。

如果您能独立完整地显示查询和数据,而不只是代码片段和嵌入代码,那总是很有帮助的。以下是我们可以用来测试的一些完整数据:

<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>

这是您的查询,未嵌入 Java 代码中:

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 的查询验证器说这是一个合法的查询。 (有另一个简短的回答说你需要为 group_concat 添加一个分隔符参数,但事实并非如此。18.5.1.7 GroupConcat 说 'If the "separator" scalar argument is absent from GROUP_CONCAT then it is taken to be the "space" character, unicode codepoint U+0020.')事实上,当我 运行 在本地使用 Jena(使用命令行 sparql 工具)时,我得到了这些结果,就像你期望的那样:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 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" |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

但这并不能真正解释问题。您是否正在使用仅支持第一个 SPARQL 标准而不支持 SPARQL 1.1 的非常旧版本的 Jena?