如何从 RDF Graph 中的给定资源构造获取整个子图? [英] How do I construct get the whole sub graph from a given resource in RDF Graph?

查看:30
本文介绍了如何从 RDF Graph 中的给定资源构造获取整个子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是RDF图,给定资源A,我需要构造所有连接到A的三元组直到最后.在这里我必须得到图表包括 B、C、D、E

If this is the RDF graph , given the resource A , I need to construct all the triples connected to A till the end . here i have to get the graph include B,C,D,E

在那之后,假设我已经得到了这个图,并且只想从起点 (:A) 开始,并通过沿着以属性 :d 上的边结束的路径获取子图.例如,如果 A1 作为起点,d 作为属性,我们将构造:

After that, suppose I've got this graph and want to go only from the starting point (:A) and get the subgraph produced by following the paths that end with an edge on property :d. For instance, if A1 is given as the starting point, and d as the property, we'd construct:

:A1 :a :B1, 
:B1 :b :S1,
:B1 :b :S2,
:S1 :d :D1,
:S2 :d :D2,

推荐答案

第一种情况

要得到整个连通图,需要使用通配符属性路径跟随大部分路径,然后用实际变量抓取最后一个链接.我在构造通配符时通常使用空的相对路径,以便使用 <>|!<> 作为通配符,但是既然你提到你的端点不喜欢它,你也可以使用您喜欢的任何绝对 IRI.例如,

The first case

To get the whole connected graph, you need to use a wildcard property path to follow most of the path, and then grab the last link with an actual variable. I usually use the empty relative path in constructing wildcards, so as to use <>|!<> as the wildcard, but since you mentioned that your endpoint doesn't like it, you can use any absolute IRI that you like, too. E.g.,

prefix x: <urn:ex:>

construct { ?s ?p ?o }
where { :A (x:|!x:)* ?s . ?s ?p ?o . }

这是可行的,因为每个属性要么是 x:,要么不是,所以 x:|!x: 匹配每个属性,然后 (x:|!x:)* 是任意长度的路径,包括长度为零的路径,这意味着 ?s 将绑定到从 :a 可达的所有内容,包括:a 本身.然后,您将获取以 ?s 为主题的三元组.当您构建所有这些三元组的图时,您会得到您正在寻找的子图.

This works because every property is either x: or not, so x:|!x: matches every property, and then (x:|!x:)* is an arbitrary length path, including paths of length zero, which means that ?s will be bound to everything reachable from :a, including :a itself. Then you're grabbing the triples where ?s is the subject. When you construct the graph of all those triples, you get the subgraph that you're looking for.

这是基于您展示的图表的示例.我对不同的边使用了不同的属性来表明它有效,但如果它们都相同,这也将有效.

Here's an example based on the graph you showed. I used different properties for different edges to show that it works, but this will work if they're all the same, too.

@prefix : <urn:ex:> .

:A :p :B, :C .
:B :q :D .
:C :r :E .

:F :s :G .
:G :t :H .

prefix x: <urn:ex:>
prefix : <urn:ex:>

construct {
  ?s ?p ?o
}
where {
  :A (x:|!x:)* ?s .
  ?s ?p ?o .
}

由于这是一个构造查询,结果是一个图表,而不是一个表格".它包含我们期望的三元组:

Since this is a construct query, the result is a graph, not a "table". It contains the triples we'd expect:

@prefix :      <urn:ex:> .

:C      :r      :E .

:B      :q      :D .

:A      :p      :B , :C .

第二种情况

如果您想确保路径以特定类型的边结束,您也可以这样做.如果您只想要从 A1 到以 d 上的边结尾的路径,您可以这样做:

The second case

If you want to ensure that the paths end in a particular kind of edge, you can do that too. If you only want the paths from A1 to those ending with edges on d, you can do:

prefix x: <urn:ex:>      #-- arbitrary, used for the property path.
prefix : <...>           #-- whatever you need for your data.

construct {
  ?s1 ?p ?o1 .           #-- internal edge in the path
  ?s2 :d ?o2 .           #-- final edge in the path
}
where {
  :A (x:|!x:)* ?s1 .     #-- start at :A and go any length into the path
  ?s1 ?p ?o1 .           #-- get the triple from within the path, but make
  ?o1 (x:|!x:)* ?s2 .    #-- sure that from ?o1 you can get to to some other
  ?s2 :d ?o2 .           #-- ?s2 that's related to an ?o2 by property :d .
}

这篇关于如何从 RDF Graph 中的给定资源构造获取整个子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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