简体   繁体   English

Sparql 查询一个班级的孩子、孙子、..

[英]Sparql query for children, grandchildren, .. of a class

I have an owl file I built in Protege.我有一个在 Protege 中构建的 owl 文件。 What is the sparql query which will select all the subclasses of a class and all the subclasses of those subclasses, so on and so on (Breadth First Search Sort of Manner)?什么是 sparql 查询,它将选择一个类的所有子类和这些子类的所有子类,依此类推(广度优先搜索方式)?

This might be answered by Sparql query Subclass or EquivalentTo , but that question and its answer contain a lot more information that what you're asking for here.这可能由Sparql query Subclass 或 EquivalentTo回答,但该问题及其答案包含更多您在此处要求的信息。 You can't really enforce the search strategy (depth first vs. depth first), but you can (sort of) order subclasses by their distance from the root, if there's a unique path from the root to the subclass.您不能真正强制执行搜索策略(深度优先与深度优先),但是如果从根到子类有唯一的路径,您可以(某种程度上)根据子类与根的距离对子类进行排序。 First, let's get some sample data:首先,让我们获取一些示例数据:

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix : <https://stackoverflow.com/q/23094361/1281433/>.

#            a
#          /   \
#         b     c
#        / \   / \
#       d   e f   g 

:b rdfs:subClassOf :a .
:c rdfs:subClassOf :a .

:d rdfs:subClassOf :b .
:e rdfs:subClassOf :b .

:f rdfs:subClassOf :c .
:g rdfs:subClassOf :c .

You can use a query like this to get the subclasses of :a :您可以使用这样的查询来获取:a的子类:

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix : <https://stackoverflow.com/q/23094361/1281433/>

select ?subclass where {
  ?subclass rdfs:subClassOf* :a
}
------------
| subclass |
============
| :a       |
| :c       |
| :g       |
| :f       |
| :b       |
| :e       |
| :d       |
------------

The results include :a because we used the path rdfs:subClassOf* .结果包括:a因为我们使用了路径rdfs:subClassOf* This is logically correct, since a class is a subclass of itself, but if you don't want :a included, you could use rdfs:subClassOf+ , or you could filter out :a with filter( ?subclass != :a ) .这在逻辑上是正确的,因为一个类是它自己的一个子类,但是如果你不想包含:a ,你可以使用rdfs:subClassOf+ ,或者你可以用filter( ?subclass != :a )过滤掉:a

In the case that there's a single path from the root to the subclass, you can count the intermediate nodes between them to determine their depth.如果从根到子类只有一条路径,您可以计算它们之间的中间节点以确定它们的深度。 If you order by depth in this way, then you'll get your results in something like what a breadth first search would give you the following.如果您以这种方式按深度排序,那么您将得到类似于广度优先搜索会为您提供以下结果的结果。 This technique is described in more detail in Is it possible to get the position of an element in an RDF Collection in SPARQL?是否可以在 SPARQL 中获取 RDF 集合中元素的位置中更详细地描述了此技术 and Calculate length of path between nodes?计算节点之间的路径长度? . .

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix : <https://stackoverflow.com/q/23094361/1281433/>

select ?subclass (count(?intermediate)-1 as ?depth) where {
  ?subclass rdfs:subClassOf* ?intermediate .
  ?intermediate rdfs:subClassOf* :a .
}
group by ?subclass
order by ?depth
--------------------
| subclass | depth |
====================
| :a       | 0     |
| :b       | 1     |
| :c       | 1     |
| :d       | 2     |
| :e       | 2     |
| :f       | 2     |
| :g       | 2     |
--------------------

""SELECT ?x WHERE { ?x rdfs:subClassOf* ?y. ""SELECT ?x WHERE { ?x rdfs:subClassOf* ?y. } }

Use the subClassof* for fetch all the subclasses and their subclasses too.使用 subClassof* 也可以获取所有子类及其子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM