Sparql查询类的子代,孙子代.. [英] Sparql query for children, grandchildren, .. of a class

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

问题描述

我有一个用Protege构建的猫头鹰文件.什么是sparql查询,它将选择一个类的所有子类以及这些子类的所有子类,依此类推(以广度优先搜索方式"排序)?

I have an owl file I built in Protege. 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查询子类或EquivalentTo ,但是该问题及其答案包含了您在此处要求的更多信息.您实际上不能强制执行搜索策略(深度优先与深度优先),但是,如果从根到子类存在唯一的路径,则可以(按某种顺序)按子类与根的距离对其进行排序.首先,让我们获取一些示例数据:

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. 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 .

您可以使用类似的查询来获取:a的子类:

You can use a query like this to get the subclasses of :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       |
------------

结果包括:a,因为我们使用了路径rdfs:subClassOf*.这在逻辑上是正确的,因为类是其本身的子类,但是如果您不希望包含:a,则可以使用rdfs:subClassOf+,也可以使用filter( ?subclass != :a )过滤掉:a.

The results include :a because we used the path 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 ).

如果从根到子类只有一条路径,则可以计算它们之间的中间节点以确定它们的深度.如果按这种方式按深度排序,那么您将获得类似于广度优先搜索将为您带来以下结果的结果. 可以在SPARQL中获取RDF集合中元素的位置吗? 计算节点之间的路径长度?.

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? 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     |
--------------------

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

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