比较 SPARQL 图 [英] Comparing SPARQL graphs

查看:51
本文介绍了比较 SPARQL 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用 SPARQL 比较两个 RDF 图?如果我有图表 :a 和 :b,我想找到 :a 出现在 :b 中的所有时间.我可以查询 :a 的所有主语、谓词和宾语,然后以编程方式构建一个模式查询,该查询将匹配 :b 中的 :a 模式.有没有办法在 SPARQL 中构建 :a 模式查询,而无需编程构建?

How does one compare two RDF graphs with SPARQL? If I have graphs :a and :b, I want to find all the times :a appears in :b. I can query for all of :a's subjects, predicates, and objects, then programmatically build a pattern query that will match :a's pattern in :b. Is there a way that builds an :a pattern query all in SPARQL, with no programmatic construction?

推荐答案

我设置了一个带有两个命名图的 Jena Fuseki 端点,http://ahttp://b,我们将其称为 A 和 B.A 包含一个三元组,B 包含两个.A,(被视为)一组三元组,是 B 的子集,以下查询证实了这一点:

I set up a Jena Fuseki endpoint with two named graphs, http://a and http://b, which we'll call A and B. A contains one triple, and B contains two. A, (viewed) as a set of triples, is a subset of B, which the following query confirms:

select * where { 
  graph ?g { ?s ?p ?o }
}

-----------------------------------------------------------
| s            | p            | o            | g          |
===========================================================
| <urn:uuid:b> | <urn:uuid:p> | <urn:uuid:b> | <http://b> |
| <urn:uuid:a> | <urn:uuid:p> | <urn:uuid:b> | <http://b> |
| <urn:uuid:a> | <urn:uuid:p> | <urn:uuid:b> | <http://a> |
-----------------------------------------------------------

现在,我们可以要求出现在一个中而不出现在另一个中的三元组.要在 B 中查询不在 A 中的三元组,我们可以使用以下查询:

Now, we can ask for triples that appear in one and not in the other. To ask for triple in B that are not in A, we can use this query:

select * where { 
  graph <http://a> { ?s ?p ?o }
  FILTER NOT EXISTS { graph <http://b> { ?s ?p ?o } }
}

-------------
| s | p | o |
=============
-------------

我们也可以要求出现在 B 中但不在 A 中的三元组.我们期望并收到一个三元组.

We can also ask for triples that appear in B, but not in A. We expect and receive one triple.

select * where { 
  graph <http://b> { ?s ?p ?o }
  FILTER NOT EXISTS { graph <http://a> { ?s ?p ?o } }
}

----------------------------------------------
| s            | p            | o            |
==============================================
| <urn:uuid:b> | <urn:uuid:p> | <urn:uuid:b> |
----------------------------------------------

一般来说,如果 X 不包含不在 Y 中的三元组,则 X 是 Y 的子集.使用上述查询,我​​们可以找到这样的三元组,它们在一个中而不在另一个中.

In general, if X contains no triples that are not also in Y, then X is a subset of Y. Using queries like the above, we can find such triples that are in one and not in another.

如果我们不关心特定的三元组,我们可以使用 ASK 查询来检查是否存在任何三元组,而无需找出它们是什么.例如,

If we don't care about the particular triples, we can use an ASK query to check whether any exist, without finding out what they are. For instance,

ask { 
  graph <http://a> { ?s ?p ?o }
  NOT EXISTS { graph <http://b> { ?s ?p ?o } }
}

no

因为没有这样的三元组.然而,由于我们试图询问 A 是否是 B 的子图,这表明它们不是三元组,因此我们需要在此处反转真值.所以我们使用:

because there are no such triples. However, since we're trying to ask whether A is a subgraph of B, which is indicated by their being no triples, we need to invert the truth value here. So we use:

ask { 
  NOT EXISTS {
    graph <http://a> { ?s ?p ?o }
    NOT EXISTS { graph <http://b> { ?s ?p ?o } }
  }
}

yes

同样,如果我们问 B 是否是 A 的子图,我们得到 no:

Similarly, if we ask whether B is a subgraph of A, we get no:

ask { 
  NOT EXISTS {
    graph <http://b> { ?s ?p ?o }
    NOT EXISTS { graph <http://a> { ?s ?p ?o } }
  }
}

no

这篇关于比较 SPARQL 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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