Sparql查询子类或EquivalentTo [英] Sparql query Subclass or EquivalentTo

查看:115
本文介绍了Sparql查询子类或EquivalentTo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询尼古丁(产品)的所有子类.
结果必须是(鼻形尼古丁,来自ni ..(4项)的口咽..如图所示) 我尝试通过rdfs:subClassOf +和owl:equivalentClass +查询,但是没有用 尝试从此示例 此处的代码相同.

I want to query all subclass of Nicotine (product).
the result must be (Nasal form nicotine, Oropharyngeal from ni ..(4 items).. see in the picture) i try to query by rdfs:subClassOf+ and owl:equivalentClass+ but didn't work try from this example the code same here.

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>           
PREFIX owl: <http://www.w3.org/2002/07/owl#>        
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>         
SELECT * WHERE 
{       
  ?s owl:equivalentClass+ ?o . # and try   ?s rdfs:subClassOf ?o    
  filter(?s=<http://snomed.info/id/323283001>)       
}

此图片来自protege 谢谢.

第一个查询很难解释和执行,因为这么大的文件有一些IRI而不是子类,而不是等效类,所以我改变了查询方式

The first query hard to explain and to do because so large file some IRI not subClass and not equivalent class, I change the way to query from this instead

<owl:Class rdf:about="http://snomed.info/id/323283001">
    <rdfs:label xml:lang="en">Nicotine (product)</rdfs:label>
    <rdfs:subClassOf>
        <owl:Class>
            <owl:intersectionOf rdf:parseType="Collection">
                <rdf:Description rdf:about="http://snomed.info/id/420383004"/>
                <rdf:Description rdf:about="http://snomed.info/id/425288007"/>
                <owl:Restriction>
                    <owl:onProperty rdf:resource="http://snomed.info/id/127489000"/>
                    <owl:someValuesFrom rdf:resource="http://snomed.info/id/68540007"/>
                </owl:Restriction>
            </owl:intersectionOf>
        </owl:Class>
    </rdfs:subClassOf>
</owl:Class>

我想查询所有id(id/420383004,id/425288007,id/127489000和id/68540007)

I want to query all id(id/420383004, id/425288007, id/127489000 and id/68540007)

来自owl:Class rdf:about ="http://snomed.info/id/323283001",请告诉我一些想法.谢谢

from owl:Class rdf:about="http://snomed.info/id/323283001" please tell me some of idea. Thank

推荐答案

首先,按顺序整理一些有关SPARQL查询的注释.第一个考虑属性路径中*+之间的差异,第二个考虑使用filtervalues.然后,我们可以看看如何从数据中查询不同类型的子类/超类关系.这里的窍门是,我们正在寻找的某些关系是我们通常会使用OWL推理程序的关系,但我们正在尝试使用SPARQL进行一些OWL推理

First, a couple of notes about your SPARQL query are in order. The first regards the difference between * and + in a property path, and the second regards the use of filter and of values. Then we can look at how to query for different types of subclass/superclass relationships from the data. The trick here is that some of the relationships that we're looking for are relationship that we would typically use an OWL reasoner for, but we're trying to do a bit of that OWL reasoning using SPARQL

请注意,在链接到的另一个问题中,属性路径使用*运算符,这表示长度为零或更大的路径.长度为零或更大的路径可能非常重要,因为如果您的形式数据中没有显式三元组

Note that in the other question that you linked to, the property path uses the * operator, which means a path of length zero or more. The path of length zero or more can be pretty important, because if you don't have an explicit triple in your data of the form

:MyClass owl:equivalentClass :MyClass

您不会获得查询的任何匹配项

you won't get any matches for a query

?myClass owl:equivalentClass+ :MyClass

但您会得到一个结果(:MyClass)

but you will get a result (:MyClass) for

?myClass owl:equivalentClass* :MyClass

实际上,即使owl:equivalentClass 是对称属性(即从a owl:equivalentClass b我们可以推断 b owl:equivalentClass a),三元组也可能只存在于数据中的一个方向,所以我们实际上需要

Actually, even though owl:equivalentClass is a symmetric property (i.e., from a owl:equivalentClass b we can infer b owl:equivalentClass a), the triple might be present in only one direction in the data, so we'd actually need

?myClass (owl:equivalentClass|^owl:equivalentClass)* :MyClass

使用values代替filter

顺便说一句,请注意,在此查询中,我直接在模式中使用了IRI;无需像原始代码中那样filter:

Using values instead of filter

As an aside, note that in this query I used the IRI in the pattern directly; there's no need to filter as in your original code:

filter(?s=<http://snomed.info/id/323283001>)

如果您确实想将变量绑定到超类,则使用values更为容易,例如

If you do want to bind a variable to the superclass, it's easier to do it with values, as in

values ?superclass { <http://snomed.info/id/323283001> }
?subclass (rdfs:subClassOf|owl:equivalentClass)* ?superclass

通过SPARQL查询进行的OWL推理

通常,类之间的关系(例如,子类和超类关系)是您需要使用OWL推理程序为您确定的事物.但是,有些足够简单,并且存在于OWL公理的RDF编码中,因此您可以使用SPARQL查询得出相同的结论.例如,如果要在基于rdfs:subClassOfowl:equivalentClass的层次结构中查找子类关系,则可以使用以下模式:

OWL Reasoning through SPARQL queries

In general, the relationships between classes (e.g., subclass and superclass relationships) are things that you'd use an OWL reasoner to determine for you. However, some are simple enough and present in the RDF encoding of the OWL axioms that you can draw the same conclusions using a SPARQL query. For instance, if you want to find subclass relationships in a hierarchy based on rdfs:subClassOf and owl:equivalentClass, you can use a pattern like this one:

?subclass (rdfs:subClassOf|owl:equivalentClass|^owl:equivalentClass)* ?superclass

现在,如下所述,您可能会遇到Protégé的SPARQL选项卡的某些问题,所以我建议您将|用法保留为二进制格式,因此在这种情况下,您应该编写: /p>

Now, as noted below, you may run into some issues with Protégé's SPARQL tab, so I'd suggest that you keep the | usage to the binary case, so you'd actually write, in this case:

?subclass (rdfs:subClassOf|(owl:equivalentClass|^owl:equivalentClass))* ?superclass

现在,您实际正在查看的数据使用了更复杂的类表达式.当您拥有

Now, the data that you're actually looking at uses more complicated class expressions. When you have an OWL axiom of the form

A subClassOf (B and C)

您实际上要说的是存在一个类 A ,并且有一个(通常是匿名的)类是交集类,并且是 B的交集 C .您没有公理

what you're actually saying is that there is a class A, and there is a (typically anonymous) class that is an intersection class, and that it is the intersection of B and C. You don't have the axioms

A subClassOf B
subClassOf C

A subClassOf B
A subClassOf C

您可以使用,尽管他们会逻辑上遵循.实际上,您遇到的情况也包括一个存在性限制,类似于

available to you, although they do logically follow. The case you've got actually includes an existential restriction as well, along the lines of

A subClassOf (B C (p 一些 D))

A subClassOf (B and C and (p some D))

重要的是要注意,这里A的超类是B,C和(p some D).特别是,A不是p或D的子类.这可以通过一个具体示例更容易看出:

It's important to note that A's superclasses here are B, C, and (p some D). In particular, A is not a subclass of p or D. This might be easier to see with a concrete example:

TiredManWearingHat subClassOf (男人 TiredPerson (戴一些帽子)

TiredManWearingHat subClassOf (Man and TiredPerson and (wearing some Hat)

一个戴着帽子的疲倦的人显然是一个男人,显然是一个疲倦的人,并且显然是戴着帽子的东西,但他肯定不是戴着衣服(甚至没有意义),他是当然不是帽子.这是一个最小的本体,它具有我们可以使用的结构:

A tired man wearing a hat is clearly a man, is clearly a tired person, and is clearly something that is wearing a hat, but he is most certainly not a wearing (which doesn't even make sense), and he's certainly not a Hat. Here's an minimal ontology that has exactly that structure that we can work with:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns="https://stackoverflow.com/q/21092246/1281433/data.owl#">
  <owl:Ontology rdf:about="https://stackoverflow.com/q/21092246/1281433/data.owl"/>
  <owl:Class rdf:about="https://stackoverflow.com/q/21092246/1281433/data.owl#C"/>
  <owl:Class rdf:about="https://stackoverflow.com/q/21092246/1281433/data.owl#B"/>
  <owl:Class rdf:about="https://stackoverflow.com/q/21092246/1281433/data.owl#A">
    <rdfs:subClassOf>
      <owl:Class>
        <owl:intersectionOf rdf:parseType="Collection">
          <owl:Class rdf:about="https://stackoverflow.com/q/21092246/1281433/data.owl#B"/>
          <owl:Class rdf:about="https://stackoverflow.com/q/21092246/1281433/data.owl#C"/>
          <owl:Restriction>
            <owl:onProperty>
              <owl:ObjectProperty rdf:about="https://stackoverflow.com/q/21092246/1281433/data.owl#p"/>
            </owl:onProperty>
            <owl:someValuesFrom>
              <owl:Class rdf:about="https://stackoverflow.com/q/21092246/1281433/data.owl#D"/>
            </owl:someValuesFrom>
          </owl:Restriction>
        </owl:intersectionOf>
      </owl:Class>
    </rdfs:subClassOf>
  </owl:Class>
</rdf:RDF>

我已经为获取OWL交集类所隐含的超类给出了答案,其中描述了如何编写查询有关交叉点类的信息,因此在此不再赘述,但我将提供一个适用于这种情况的查询.我显示的结果是我使用Jena的命令行SPARQL查询工具得到的结果.

I've already written an answer to Retrieving superclasses implied by OWL intersection classes that described how you can write a query about intersection classes, so I won't explain all of it here again, but I'll include a query that works for this case. The result I'm showing are what I got using Jena's command line SPARQL query tools.

prefix :      <https://stackoverflow.com/q/21092246/1281433/data.owl#> 
prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> 
prefix owl:   <http://www.w3.org/2002/07/owl#> 
prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

select ?superclass where {
  :A (rdfs:subClassOf|(owl:intersectionOf/rdf:rest*/rdf:first))* ?superclass .
}

--------------
| superclass |
==============
| :A         |
| _:b0       |
| :B         |
| :C         |
| _:b1       |
--------------

现在,空白节点有匿名交集类和匿名限制类.如果您不想将它们包括在结果中,则可以很容易地将它们过滤掉:

Now, the blank nodes there are the anonymous intersection class and the anonymous restriction class. If you don't want to include them in the results, you can filter them out pretty easily:

prefix :      <https://stackoverflow.com/q/21092246/1281433/data.owl#> 
prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> 
prefix owl:   <http://www.w3.org/2002/07/owl#> 
prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

select ?superclass where {
  :A (rdfs:subClassOf|(owl:intersectionOf/rdf:rest*/rdf:first))* ?superclass .
  filter(!isBlank(?superclass))
}

--------------
| superclass |
==============
| :A         |
| :B         |
| :C         |
--------------

如果您也想遵循等效的类,则也可以将^owl:equivalentClass|owl:equivalentClass添加到该属性路径中.

You can add ^owl:equivalentClass|owl:equivalentClass into that property path as well, if you want to follow equivalent classes, too.

正如我所说,上面的结果是使用Jena的命令行工具.您说过您想在Protégé中进行此操作,这使事情变得有些困难,因为它似乎带来了一些问题.我显示的最后一个查询仅在Protégé中产生A和B;由于某些原因,它不包含C:

As I said, the results above were using Jena's command line tools. You said you wanted to do this in Protégé, and that makes things a bit harder, because it seems to introduce some problems. The last query I showed only produces A and B in Protégé; it doesn't include C for some reason:

但是,对空白节点的处理要好一些(即,我们可以删除filter并获得一些有用的输出):

However, the treatment of blank nodes is a bit better (i.e., we can remove the filter and get some useful output):

不幸的是,我不确定如何获得Jena在Protégé中提供的相同输出,但是我正在通过电子邮件将其发送给他们的邮件列表.

I'm not sure how to get the same output that Jena gives in Protégé, unfortunately, but I'm emailing their mailing list about it.

这篇关于Sparql查询子类或EquivalentTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆