针对两个图表运行sparql查询? [英] run a sparql query against two graphs?

查看:204
本文介绍了针对两个图表运行sparql查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  SELECT?b?c WHERE {GRAPH< HTTP:// AliceIRI> {
< http://local.virt/foo>这将告诉我AliceIRI中的所有三元组,其中foo是一个三元组,其中foo学科。如果我想查看两个不同的图表,是我做UNION的唯一选择:

  SELECT?b?c WHERE {{GRAPH< http:// AliceIRI> {
< http://local.virt/foo> ?b?c}}
UNION
{GRAPH< http:// BobIRI> {
< http://local.virt/foo>或者是否有一些速记可以让我更方便地写出这些内容? ,像这样:

  SELECT?b?c WHERE {GRAPH< http:// AliceIRI> +< http:// BobIRI> {
< http://local.virt/foo> ?b?c}

顺便提一下,我在Virtuoso 6.01.3127上。



更新1



澄清一下,我真的很想运行:

  SELECT?b?c WHERE {GRAPH< http:// AliceIRI> +< http:// BobIRI> {
< http://local.virt/foo> ?b?c。
< http://local.virt/bar> ?b?c}}

并匹配?b ?c ,这样< http://local.virt/foo> ?b?c 位于< http:// AliceIRI> < http://local.virt /栏> ?b?c 位于< http:// BobIRI> 中。只需在< http:// AliceIRI> (单独)和< http:// BobIRI>

并进一步澄清:我已经意识到,如果foos全部属于Alice并且所有酒吧都属于Bob,然后我可以写出:

  SELECT?b?c WHERE {
GRAPH< http:// AliceIRI> {
< http://local.virt/foo> ?b?c}。
GRAPH< http:// BobIRI> {
< http://local.virt/bar> (这实际上是我的应用程序所需要的) - 但是在至少对于学术兴趣来说,是否存在一种语法上好的方式来针对图的联合运行查询(而不是针对多个图,然后结合结果)的问题仍然存在。

解决方案



  SELECT?b?c 
FROM NAMED< http:// AliceIRI>
FROM NAMED< http:// BobIri>
WHERE
{
GRAPH?g {< http://local.virt/foo> ?b?c}
}

FROM NAMED 子句可用于设置在使用 GRAPH?var 子句时查询的命名图形。



重要的是要注意,在语义上,这个查询和你上面写的是一样的,所以由 GRAPH 子句表示的模式分别与每个命名图匹配, / p>

编辑

要回答评论中的额外问题,您使用 FROM 而不是 FROM NAMED ,这会将查询的默认图形设置为 FROM 子句并且不要求您使用 GRAPH eg

  SELECT?b?c 
FROM< http:// AliceIRI>
FROM< http:// BobIri>
WHERE
{
< http://local.virt/foo> ?b?c
}

这包括您希望允许匹配的三元组三元模式的不同部分来自您已命名的任何图表。



请注意,某些三重商店可能允许您设置它们以便默认图形自动视为所有命名图的合并。


A typical SPARQL query that specifies a graph might look like this:

SELECT ?b ?c WHERE { GRAPH <http://AliceIRI> {
<http://local.virt/foo> ?b ?c}}

This will tell me all the triples in AliceIRI where "foo" is a subject. What if I want to look in two different graphs, is my only option to do a UNION:

SELECT ?b ?c WHERE {{ GRAPH <http://AliceIRI> {
<http://local.virt/foo> ?b ?c}}
UNION
{ GRAPH <http://BobIRI> {
<http://local.virt/foo> ?b ?c}}}

Or is there some shorthand that would allow me to write this more conveniently, something like this:

SELECT ?b ?c WHERE { GRAPH <http://AliceIRI> + <http://BobIRI> {
<http://local.virt/foo> ?b ?c}

BTW I'm on Virtuoso 6.01.3127.

Update 1

To clarify, I'd really like to be able to run:

SELECT ?b ?c WHERE { GRAPH <http://AliceIRI> + <http://BobIRI> {
<http://local.virt/foo> ?b ?c .
<http://local.virt/bar> ?b ?c}}

and have this match ?b and ?c such that <http://local.virt/foo> ?b ?c is in <http://AliceIRI> and <http://local.virt/bar> ?b ?c is in <http://BobIRI>. Simply taking the union of matches in <http://AliceIRI> (alone) and <http://BobIRI> (alone) won't accomplish this.

And to clarify further: I've realized that if the foos all belonged to Alice and the bars all belonged to Bob, then I could write

SELECT ?b ?c WHERE { 
GRAPH <http://AliceIRI> {
<http://local.virt/foo> ?b ?c } .
GRAPH <http://BobIRI> {
<http://local.virt/bar> ?b ?c}}

(which is actually what I needed for my application) -- but at least for "academic interest" the question of whether there is a syntactically nice way to run a query against a union of graphs (as opposed to against multiple graphs and then union the results) still stands.

解决方案

Yes

SELECT ?b ?c
FROM NAMED <http://AliceIRI>
FROM NAMED <http://BobIri>
WHERE
{
  GRAPH ?g { <http://local.virt/foo> ?b ?c }
}

FROM NAMED clauses may be used to set named graphs that are queried when you use a GRAPH ?var clause.

It is important to note that semantically this query is identical to what you wrote above so the pattern expressed by the GRAPH clause is matched against each named graph separately and unioned together

Edit

To answer the extra question in your comment yes you can, if you use FROM instead of FROM NAMED this sets the default graph for the query to be the merge of all graphs in FROM clauses and does not require you to use GRAPH e.g.

SELECT ?b ?c
FROM <http://AliceIRI>
FROM <http://BobIri>
WHERE
{
  <http://local.virt/foo> ?b ?c
}

This covers the case where you want to allow triples that match different parts of the triple pattern come from any of the graphs you have named.

Note that some triple stores may allow you to set them up so that the default graph is automatically treated as the merge of all named graphs.

这篇关于针对两个图表运行sparql查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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