Rdf 多语句查询SPARQL 1.1属性路径Virtuoso 7.2.X

Rdf 多语句查询SPARQL 1.1属性路径Virtuoso 7.2.X,rdf,sparql,virtuoso,Rdf,Sparql,Virtuoso,示例RDF数据集具有按owl#Class分组的条目owl#namedivisividual,以及名为IsMemberOf的自定义关系。当我试图获得按类型划分的结果列表时,效果很好,但当我添加一种方法来获得相应的IsMemberOf时,我没有得到预期的结果 下面是我给Virtuoso的三个SPARQL 1.1查询(下面是示例数据集): 问题1 sparql select * from <test> where { #If I uncomment the next line I don'

示例RDF数据集具有按
owl#Class
分组的条目
owl#namedivisividual
,以及名为
IsMemberOf
的自定义关系。当我试图获得按类型划分的结果列表时,效果很好,但当我添加一种方法来获得相应的
IsMemberOf
时,我没有得到预期的结果

下面是我给Virtuoso的三个SPARQL 1.1查询(下面是示例数据集):

问题1

sparql select * from <test>
where {
#If I uncomment the next line I don't get the proper results
#  ?s <IsMemberOf> ?member_of.
  ?s rdfs:label ?name.
  {
    ?s rdf:type/rdfs:subClassOf* <livingthings>.
  } UNION {
    ?s a <rock>.
  }
};
最后,这里是我使用的数据

sparql create silent graph <test>;
sparql insert into graph <test> {
  <nonlivingthings> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <nonlivingthings> <http://www.w3.org/2000/01/rdf-schema#label> "Non-living things".
  <rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <rock> <http://www.w3.org/2000/01/rdf-schema#label> "Rock".
  <rock> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <nonlivingthings>.
  <rocky_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <rocky_the_rock> <IsMemberOf> <group_a>.
  <rocky_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <rock>.
  <rocky_the_rock> <http://www.w3.org/2000/01/rdf-schema#label> "Rocky the rock".
  <ralf_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <ralf_the_rock> <IsMemberOf> <group_a>.
  <ralf_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <rock>.
  <ralf_the_rock> <http://www.w3.org/2000/01/rdf-schema#label> "Ralf the rock".  
  <livingthings> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <livingthings> <http://www.w3.org/2000/01/rdf-schema#label> "Living things".
  <mammal> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <mammal> <http://www.w3.org/2000/01/rdf-schema#label> "Mammal".
  <mammal> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <livingthings>.
  <reptile> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <reptile> <http://www.w3.org/2000/01/rdf-schema#label> "Reptile".
  <reptile> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <livingthings>.
  <dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <dog> <http://www.w3.org/2000/01/rdf-schema#label> "Dog".
  <dog> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
  <cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <cat> <http://www.w3.org/2000/01/rdf-schema#label> "Cat".
  <cat> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
  <elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <elephant> <http://www.w3.org/2000/01/rdf-schema#label> "Elephant".
  <elephant> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
  <snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <snake> <http://www.w3.org/2000/01/rdf-schema#label> "Snake".
  <snake> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <reptile>.
  <snakey_the_snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <snakey_the_snake> <IsMemberOf> <group_a>.
  <snakey_the_snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <snake>.
  <snakey_the_snake> <http://www.w3.org/2000/01/rdf-schema#label> "Snakey the snake".
  <doggy_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <doggy_the_dog> <IsMemberOf> <group_a>.
  <doggy_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <dog>.
  <doggy_the_dog> <http://www.w3.org/2000/01/rdf-schema#label> "Doggy the dog".
  <fido_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <fido_the_dog> <IsMemberOf> <group_b>.
  <fido_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <dog>.
  <fido_the_dog> <http://www.w3.org/2000/01/rdf-schema#label> "Fido the dog".
  <katy_the_cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <katy_the_cat> <IsMemberOf> <group_b>.
  <katy_the_cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <cat>.
  <katy_the_cat> <http://www.w3.org/2000/01/rdf-schema#label> "Katy the cat".
  <eli_the_elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <eli_the_elephant> <IsMemberOf> <group_c>.
  <eli_the_elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <elephant>.
  <eli_the_elephant> <http://www.w3.org/2000/01/rdf-schema#label> "Eli the elephant".
};

类下的所有结果都被忽略。这是因为Virtuoso中的错误还是我的SPARQL查询的形成方式?

很难说到底是什么问题。您显示的查询本身不是合法的SPARQL(例如,在SPARQL中,您可以
select…
,而不是
SPARQL select…;
),但这可能是您正在使用的Virtuoso界面。另一个可能的问题是,
和类似项是相对的IRI,它们的解决方式在数据加载和查询处理等方面可能有所不同,因此不一定是一个地方的
实际引用的IRI与另一个地方的
引用的IRI相同。你真的应该使用绝对虹膜。我不知道这是否是问题所在

也就是说,我获取了您的数据,并根据http://example.org/因此它们都是绝对的,而且看起来查询的工作方式与您希望的一样。例如,使用Jena运行以下查询就可以了

base
前缀rdfs:
选择*where{
?s?委员会成员。
?s rdfs:标签?名称。
?s a/rdfs:子类*。
}
-------------------------------------------------------
|s |成员|姓名|
=======================================================
|| |“狗狗”|
|| |“大象伊莱”|
|| |“Fido the dog”|
|| |“猫凯蒂”|
|| |“捉住蛇”|
-------------------------------------------------------
以下是最新数据:


A.
“大象”;
.
A.
“捕蛇”;
.
A.
“猫”;
.
A.
“哺乳动物”;
.
A.
“猫凯蒂”;
.
A.
“狗菲多”;
.
A.
“狗”;
.
A.
“大象伊莱”;
.
A.
“爬行动物”;
.
A.
“非生物”。
A.
“生物”。
A.
“狗狗”;
.
A.
“岩石”;
.
A.
“岩石”;
.
A.
“岩石拉尔夫”;
.
A.
“蛇”;
.

当与属性路径混合时,Virtuoso似乎在查询中处理用户定义的关系很奇怪

以下是解决这一问题的具体方法

问题1答案

sparql select * from <test>
where {
  ?s rdfs:label ?name.
  {
    ?s <IsMemberOf>{1} ?member_of.
    ?s rdf:type/rdfs:subClassOf* <livingthings>.
  } UNION {
    ?s <IsMemberOf>{1} ?member_of.
    ?s a <rock>.
  }
};
sparql选择*from
在哪里{
?s rdfs:标签?名称。
{
?s{1}的成员。
?s rdf:类型/rdf:子类*。
}联合{
?s{1}的成员。
这是一个好主意。
}
};
问题2答案

sparql select * from <test>
where {
    ?s <IsMemberOf>{1} ?member_of.
    ?s rdfs:label ?name.
    ?s rdf:type/rdfs:subClassOf* <livingthings>.
};
sparql选择*from
在哪里{
?s{1}的成员。
?s rdfs:标签?名称。
?s rdf:类型/rdf:子类*。
};
将显式属性路径应用于用户定义的关系,例如。的
?s{1}?成员_,可以得到预期的结果


在查询一中,
?s{1}成员_行应用于每个联合元素内的每个术语。如果不是,则结果不符合要求。这确实解决了问题,但有点荒谬。这就是为什么我将把这个问题留待几天,看看是否有人能提供合乎逻辑的解释,来解释为什么事情是这样的。如果只是一个bug,我会通过他们的邮件列表与他们联系。

是的,“sparql select”而不是“sparl”是特定于大师的工件。数据使用相对虹膜,因为我想要更短的东西,我应该使用更多的前缀。感谢您在Jena中测试数据和查询,它帮助我确认了SPARQL 1.1的正确使用。这个问题是针对Virtuoso的,我已经找到了一个解决方案;用于ODBC、JDBC、ADO.NET、OLEDB和其他基于SQL的连接——例如查询。前面的
sparql
和后面的
会让那些只希望看到SPARQL的人感到困惑。您还可以剥离这些内容,并通过SPARQL接口完成这里所做的一切--
http://virtuoso-host:port/sparql/
如果这种情况在当前的Virtuoso(开源和/或商业)7.2.1中仍然存在,并且如果您尚未报告,请在、、或中报告。
sparql create silent graph <test>;
sparql insert into graph <test> {
  <nonlivingthings> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <nonlivingthings> <http://www.w3.org/2000/01/rdf-schema#label> "Non-living things".
  <rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <rock> <http://www.w3.org/2000/01/rdf-schema#label> "Rock".
  <rock> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <nonlivingthings>.
  <rocky_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <rocky_the_rock> <IsMemberOf> <group_a>.
  <rocky_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <rock>.
  <rocky_the_rock> <http://www.w3.org/2000/01/rdf-schema#label> "Rocky the rock".
  <ralf_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <ralf_the_rock> <IsMemberOf> <group_a>.
  <ralf_the_rock> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <rock>.
  <ralf_the_rock> <http://www.w3.org/2000/01/rdf-schema#label> "Ralf the rock".  
  <livingthings> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <livingthings> <http://www.w3.org/2000/01/rdf-schema#label> "Living things".
  <mammal> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <mammal> <http://www.w3.org/2000/01/rdf-schema#label> "Mammal".
  <mammal> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <livingthings>.
  <reptile> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <reptile> <http://www.w3.org/2000/01/rdf-schema#label> "Reptile".
  <reptile> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <livingthings>.
  <dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <dog> <http://www.w3.org/2000/01/rdf-schema#label> "Dog".
  <dog> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
  <cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <cat> <http://www.w3.org/2000/01/rdf-schema#label> "Cat".
  <cat> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
  <elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <elephant> <http://www.w3.org/2000/01/rdf-schema#label> "Elephant".
  <elephant> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <mammal>.
  <snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>.
  <snake> <http://www.w3.org/2000/01/rdf-schema#label> "Snake".
  <snake> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <reptile>.
  <snakey_the_snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <snakey_the_snake> <IsMemberOf> <group_a>.
  <snakey_the_snake> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <snake>.
  <snakey_the_snake> <http://www.w3.org/2000/01/rdf-schema#label> "Snakey the snake".
  <doggy_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <doggy_the_dog> <IsMemberOf> <group_a>.
  <doggy_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <dog>.
  <doggy_the_dog> <http://www.w3.org/2000/01/rdf-schema#label> "Doggy the dog".
  <fido_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <fido_the_dog> <IsMemberOf> <group_b>.
  <fido_the_dog> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <dog>.
  <fido_the_dog> <http://www.w3.org/2000/01/rdf-schema#label> "Fido the dog".
  <katy_the_cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <katy_the_cat> <IsMemberOf> <group_b>.
  <katy_the_cat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <cat>.
  <katy_the_cat> <http://www.w3.org/2000/01/rdf-schema#label> "Katy the cat".
  <eli_the_elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual>.
  <eli_the_elephant> <IsMemberOf> <group_c>.
  <eli_the_elephant> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <elephant>.
  <eli_the_elephant> <http://www.w3.org/2000/01/rdf-schema#label> "Eli the elephant".
};
s               member_of   name
rocky_the_rock  group_a     Rocky the rock
ralf_the_rock   group_a     Ralf the rock
sparql select * from <test>
where {
  ?s rdfs:label ?name.
  {
    ?s <IsMemberOf>{1} ?member_of.
    ?s rdf:type/rdfs:subClassOf* <livingthings>.
  } UNION {
    ?s <IsMemberOf>{1} ?member_of.
    ?s a <rock>.
  }
};
sparql select * from <test>
where {
    ?s <IsMemberOf>{1} ?member_of.
    ?s rdfs:label ?name.
    ?s rdf:type/rdfs:subClassOf* <livingthings>.
};