SPARQL Group-Concat无法正常工作

时间:2015-06-10 12:07:16

标签: rdf sparql jena

我有一个使用Android& amp;的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>

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

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

在搜索到处之后,我似乎无法使其正常工作。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

如果您可以自己显示查询和数据,而不仅仅是片段和嵌入代码,那么它总是有用的。这是我们可以测试的一些完整数据:

<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说&#39;如果&#34; ;分隔符&#34;标量参数在GROUP_CONCAT中不存在,然后它被视为&#34;空格&#34;字符,unicode代码点U + 0020。&#39;)实际上,当我在本地运行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?