【问题标题】:Multiple statements query SPARQL 1.1 property-paths Virtuoso 7.2.X多条语句查询 SPARQL 1.1 属性路径 Virtuoso 7.2.X
【发布时间】:2015-07-22 14:09:28
【问题描述】:

示例 RDF 数据集具有按 owl#Class 分组的条目 owl#NamedIndividual 和名为 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 &lt;IsMemberOf&gt; ?member_of. 时只得到以下不完整的结果?

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

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

【问题讨论】:

  • 值得标记 Virtuoso 的 SPASQL -- SPARQL-in-SQL;用于 ODBC、JDBC、ADO.NET、OLE DB 和其他基于 SQL 的连接——查询本身。开头的 sparql 和结尾的 ; 会让那些只希望看到 SPARQL 的人感到困惑。你也可以去掉这些,然后通过 SPARQL 接口完成你在此处所做的一切——http://virtuoso-host:port/sparql/

标签: rdf sparql virtuoso


【解决方案1】:

很难确切地说出这里的问题是什么。您显示的查询本身不是合法的 SPARQL(例如,在 SPARQL 中您执行的是 select ...,而不是 sparql select ...;),但这可能是您正在使用的 Virtuoso 接口。另一个可能的问题是&lt;IsMemberOf&gt;和类似的都是相对的IRI,它们的解析方式可能在数据加载和查询处理等方面有所不同,所以不一定是&lt;IsMemberOf&gt;在一个地方实际上是指&lt;IsMemberOf&gt; 在另一个中引用的 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> .

【讨论】:

  • 是的,“sparql select”而不是“sparl”是 Virtuoso 特定的工件。数据使用相对 IRI,因为我想要更短的东西,我应该使用前缀。感谢您在 Jena 中测试数据和查询,它帮助我确认 SPARQL 1.1 的正确使用。这个问题是 Virtuoso 特有的,我已经找到了解决方案。
【解决方案2】:

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 &lt;IsMemberOf&gt;{1} ?member_of.,可以得到预期的结果。

在查询一中,?s &lt;IsMemberOf&gt;{1} ?member_of. 行应用于每个联合元素内的每个术语。如果不是,则结果不如预期。它确实解决了这个问题,但它有点荒谬。这就是为什么我将这个问题搁置几天,看看是否有人能提供合乎逻辑的解释,解释为什么 Virtuoso 会这样。如果这只是一个错误,我会通过他们的邮件列表与他们联系。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
相关资源
最近更新 更多