SPARQL 查询以构建具有选择路径的子图(路径具有不同的长度) [英] SPARQL query to construct sub-graph with select paths (paths have different lengths)

查看:28
本文介绍了SPARQL 查询以构建具有选择路径的子图(路径具有不同的长度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用单个 SPARQL 查询从下图中CONSTRUCT一个包含节点 A、B、C、E、F(但没有 D、G、H)的 RDF 子图:

Is it possible to CONSTRUCT an RDF sub-graph that contains nodes A, B, C, E, F (but no D, G, H) from the following graph using a single SPARQL query:

:A :p :B .
:A :q :C .
:A :r :D .
:A :s :E . :E :t :F .
:A :u :G . :G :v :H .

我知道如何制定一个 SPARQL 查询,该查询返回 { A-p-B, A-q-C }(即两条路径,每个路径由 1 个语句组成)和一个返回 { A-s-E E-t-F }(即一条路径由 2 个语句组成).但是可以将两者合并为单个 SPARQL CONSTRUCT 查询,如果是这样,那看起来如何?

I know how to formulate a SPARQL query that returns { A-p-B, A-q-C } (i.e. two paths consisting of 1 statement each) and one that returns { A-s-E E-t-F } (i.e. one path consisting of 2 statements). But it it possible to fold both into as single SPARQL CONSTRUCT query and if so, how would that look?

推荐答案

如果您在合法的 RDF 序列化中提供数据,那么处理数据会容易得多,这样我们就可以轻松地针对它运行查询.以下是我们可以实际查询的表格形式的数据:

It's much easier to work with data if you provide it in a legal RDF serialization, so that we can easily run queries against it. Here's your data in a form that we can actually query against:

@prefix : <http://stackoverflow.com/q/20840883/1281433/> .

:A :p :B .
:A :q :C .
:A :r :D .
:A :s :E . :E :t :F .
:A :u :G . :G :v :H .

如果您只想要由节点 A、B、C、E 和 F 得出的子图,您可以简单地要求所有三元组,其中主体和客体都来自该集合.例如:

If you just want the subgraph induced by the nodes A, B, C, E, and F, you can simply ask for all triples where both the subject and object are taken from that set. E.g.:

prefix : <http://stackoverflow.com/q/20840883/1281433/>

construct {
  ?s ?p ?o
} 
where { 
    values ?s { :A :B :C :E :F }
    values ?o { :A :B :C :E :F }
    ?s ?p ?o 
}

这会产生以下结果(以您的符号、N3 和 RDF/XML):

This produces the following result (in your notation, in N3, and in RDF/XML):

A-p-B
A-q-C
A-s-E E-t-F

@prefix :      <http://stackoverflow.com/q/20840883/1281433/> .

:E      :t      :F .

:A      :p      :B ;
        :q      :C ;
        :s      :E .

<rdf:RDF
    xmlns="http://stackoverflow.com/q/20840883/1281433/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://stackoverflow.com/q/20840883/1281433/A">
    <s>
      <rdf:Description rdf:about="http://stackoverflow.com/q/20840883/1281433/E">
        <t rdf:resource="http://stackoverflow.com/q/20840883/1281433/F"/>
      </rdf:Description>
    </s>
    <q rdf:resource="http://stackoverflow.com/q/20840883/1281433/C"/>
    <p rdf:resource="http://stackoverflow.com/q/20840883/1281433/B"/>
  </rdf:Description>
</rdf:RDF>

这篇关于SPARQL 查询以构建具有选择路径的子图(路径具有不同的长度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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