如何在任意长度的sparql路径中使用Union/or? [英] how to use Union/or in sparql path with arbitrary length?
问题描述
我正在使用以下查询从DBPedia本体中查找具有城市域(或城市超类)或国家范围(或国家超类)的所有属性.当我使用固定长度的路径没有问题,但是当我输入 * 来定义任意长度的路径时,会出现以下错误:
I'm using below query to find all properties with domain of city (or superclasses of city) or range of country (or superclasses of country) from DBPedia ontology. when I use path with fixed length there is no problem but when I put * to define paths with arbitrary length, I get this error:
Virtuoso 37000错误SP031:SPARQL编译器:变量 '_ :: trans_subj_6_4'用于查询的子表达式,但不使用 分配
Virtuoso 37000 Error SP031: SPARQL compiler: Variable '_::trans_subj_6_4' is used in subexpressions of the query but not assigned
我的SPARQL:
define sql:signal-void-variables 1
define input:default-graph-uri <http://dbpedia.org>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX res: <http://dbpedia.org/resource/>
PREFIX owl:<http://www.w3.org/2002/07/owl#>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
select ?property where{
{ ?property rdfs:domain/^rdfs:subClassOf* dbo:City }
UNION
{ ?property rdfs:range/^rdfs:subClassOf* dbo:Country }
}
另外,当我输入任何数字而不是 * 时,也会出现相同的错误.我正在使用 Virtuoso 作为DBPedia SPARQL端点.
Also when I put any number instead of *, I get same error. I'm using Virtuoso as DBPedia SPARQL endpoint.
推荐答案
使用VALUES代替UNION(如果可以)
与实际的SPARQL查询相比,Virtuoso给您的错误更多地是关于它对属性路径和 union 的实现.查询的SPARQL部分看起来正确. (我无法与Virtuoso特定的定义交谈.)
Use VALUES instead of UNION (when you can)
The error Virtuoso is giving you is more about its implementation of property paths and union than the actual SPARQL query. The SPARQL part of the query looks correct. (I can't speak to the Virtuoso specific defines.)
在原始SPARQL标准中需要 union 的许多地方,您现在可以使用 values 来指定变量可以具有的特定值.它通常会导致更具可读性的查询(至少在我看来),并且某些端点(例如Virtuoso)似乎可以更好地处理它.
In many places that required union in the original SPARQL standard, you can now use values to specify the particular values that variables can have. It typically leads to more readable queries (at least, in my opinion), and some endpoints, such as Virtuoso, seem to handle it better.
使用值(并使用端点的Web界面使用的 dbpedia-owl 前缀),您将查询以下内容,Virtuoso将返回您的内容寻找:
Using values (and using the dbpedia-owl prefix that the web interface to the endpoint uses), you query becomes the following, and Virtuoso returns what you're looking for:
select ?property where {
values (?p ?v) { (rdfs:domain dbpedia-owl:City)
(rdfs:range dbpedia-owl:Country) }
?property ?p ?class .
?class ^rdfs:subClassOf* ?v .
}