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

时间:2015-07-22 14:09:28

标签: rdf sparql virtuoso

示例RDF数据集包含按owl#NamedIndividual分组的条目owl#Class和名为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>.
  }
};

查询2

sparql select * from <test>
where {
#If I uncomment the next line I get no results at all
#   ?s <IsMemberOf> ?member_of.
    ?s rdfs:label ?name.
    ?s rdf:type/rdfs:subClassOf* <livingthings>.
};

查询3

sparql select * from <test>
where {
  ?s <IsMemberOf> ?member_of.
  ?s rdfs:label ?name.
  ?s rdf:type <dog>.
#If I replace the previous line with the next line I get no results at all
# ?s rdf:type/rdfs:subClassOf* <livingthings>.
};

这是数据的分组方式:

livingthings
->mammal
--->dog
----->doggy_the_dog
----->fido_the_dog
--->cat
---->katy_the_cat
--->elephant
----->eli_the_elephant
->reptile
--->snake
----->snakey_the_snake
nonlivingthings
->rock
--->rocky_the_rock
--->ralf_the_rock

group_a
->rocky_the_rock
->ralf_the_rock
->snakey_the_snake
->doggy_the_dog
group_b
->fido_the_dog
->katy_the_cat
group_c
->eli_the_elephant

这里是我使用的数据

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 <IsMemberOf> ?member_of.时,我只会得到以下不完整的结果?

s               member_of   name
rocky_the_rock  group_a     Rocky the rock
ralf_the_rock   group_a     Ralf the rock

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

2 个答案:

答案 0 :(得分:1)

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

以下是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>.
  }
};

查询2回答

sparql select * from <test>
where {
    ?s <IsMemberOf>{1} ?member_of.
    ?s rdfs:label ?name.
    ?s rdf:type/rdfs:subClassOf* <livingthings>.
};

为用户定义的关系应用显式属性路径,因此?s <IsMemberOf>{1} ?member_of.会给出预期的结果。

在查询一中,行?s <IsMemberOf>{1} ?member_of.应用于每个union元素内的每个术语。如果没有,则结果不符合要求。它确实解决了这个问题,但它有点荒谬。这就是为什么我会把这个问题保持开放几天,看看是否有人可以提供合理的解释为什么在Virtuoso中的事情是这样的。如果这只是一个错误,我会通过他们的邮件列表与他们联系。

答案 1 :(得分:0)

很难确切地说出这里的问题是什么。您显示的查询本身不是合法的SPARQL(例如,您在SPARQL中执行select ...,而不是sparql select ...;),但这可能是您正在使用的Virtuoso界面。另一个可能的问题是<IsMemberOf>和类似的是相对IRI,它们如何被解析可能在数据加载和查询处理等之间有所不同,因此不一定是<IsMemberOf>在一个place实际上是指<IsMemberOf>在另一个中引用的相同IRI。你真的应该使用绝对IRI。我不知道这是不是问题。

那就是说,我拿走了你的数据,并针对 http://example.org/ 解决了IRI,这样它们都是绝对的,看起来查询的工作方式与你的方式相同想要他们。例如,使用Jena运行以下查询就可以了。

base <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select * where {
  ?s <IsMemberOf> ?member_of.
  ?s rdfs:label ?name.
  ?s a/rdfs:subClassOf* <livingthings>.
}
-------------------------------------------------------
| s                  | member_of | name               |
=======================================================
| <doggy_the_dog>    | <group_a> | "Doggy the dog"    |
| <eli_the_elephant> | <group_c> | "Eli the elephant" |
| <fido_the_dog>     | <group_b> | "Fido the dog"     |
| <katy_the_cat>     | <group_b> | "Katy the cat"     |
| <snakey_the_snake> | <group_a> | "Snakey the snake" |
-------------------------------------------------------

以下是更新后的数据:

<http://example.org/elephant>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Elephant" ;
        <http://www.w3.org/2000/01/rdf-schema#subClassOf>
                <http://example.org/mammal> .

<http://example.org/snakey_the_snake>
        a       <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/snake> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Snakey the snake" ;
        <http://example.org/IsMemberOf>
                <http://example.org/group_a> .

<http://example.org/cat>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Cat" ;
        <http://www.w3.org/2000/01/rdf-schema#subClassOf>
                <http://example.org/mammal> .

<http://example.org/mammal>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Mammal" ;
        <http://www.w3.org/2000/01/rdf-schema#subClassOf>
                <http://example.org/livingthings> .

<http://example.org/katy_the_cat>
        a       <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/cat> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Katy the cat" ;
        <http://example.org/IsMemberOf>
                <http://example.org/group_b> .

<http://example.org/fido_the_dog>
        a       <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/dog> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Fido the dog" ;
        <http://example.org/IsMemberOf>
                <http://example.org/group_b> .

<http://example.org/dog>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Dog" ;
        <http://www.w3.org/2000/01/rdf-schema#subClassOf>
                <http://example.org/mammal> .

<http://example.org/eli_the_elephant>
        a       <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/elephant> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Eli the elephant" ;
        <http://example.org/IsMemberOf>
                <http://example.org/group_c> .

<http://example.org/reptile>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Reptile" ;
        <http://www.w3.org/2000/01/rdf-schema#subClassOf>
                <http://example.org/livingthings> .

<http://example.org/nonlivingthings>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Non-living things" .

<http://example.org/livingthings>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Living things" .

<http://example.org/doggy_the_dog>
        a       <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/dog> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Doggy the dog" ;
        <http://example.org/IsMemberOf>
                <http://example.org/group_a> .

<http://example.org/rocky_the_rock>
        a       <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/rock> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Rocky the rock" ;
        <http://example.org/IsMemberOf>
                <http://example.org/group_a> .

<http://example.org/rock>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Rock" ;
        <http://www.w3.org/2000/01/rdf-schema#subClassOf>
                <http://example.org/nonlivingthings> .

<http://example.org/ralf_the_rock>
        a       <http://www.w3.org/2002/07/owl#NamedIndividual> , <http://example.org/rock> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Ralf the rock" ;
        <http://example.org/IsMemberOf>
                <http://example.org/group_a> .

<http://example.org/snake>
        a       <http://www.w3.org/2002/07/owl#Class> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Snake" ;
        <http://www.w3.org/2000/01/rdf-schema#subClassOf>
                <http://example.org/reptile> .