使用 sparql 生成带有子图的图 [英] Generate Graph with sub-Graphs using sparql

查看:54
本文介绍了使用 sparql 生成带有子图的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 RDF 存储中有以下情况:

I have the following situation in my RDF-store:

命名图:A

  S1 P1 O1

命名图:B

  S2 P2 O1

命名图:C

  S3 P3 O1

我现在想定义一个 SPARQL 查询,它查找 O1 在三元组中的所有图,并将这 3 个图 + 三元组存储在一个名为 A+B+C 的新图中

I now want to define a SPARQL Query which finds all Graphs where O1 is in the tripples, and stores those 3 Graphs + the tripples in a new Graph with the name A+B+C

新命名图:A+B+C

Named Graph: A
   S1 P1 O1
Named Graph: B
   S2 P2 O1
Named Graph: C
   S3 P3 O1

使用构造或插入到我只发现了如何用三元组构建图形,bzw 不知道如何构建包含图形的图形?

With Construct or insert Into i only found out how to build a graph out of tripples, bzw not how to build a graph which contains graphs?

这可能吗,如果是的话,如何?

Is this possible, if yes how?

给你加油

推荐答案

您无法完全按照自己的意愿行事,因为子图无法按照您描述的方式显式存储在彼此之间.但是,您可以将所有三元组放入一个图形中,这可能就足够了:

You can't do exactly what you want because sub-graphs can't be explicitly stored within each other in the way you seem to be describing. However you can put all the triples into a single graph which may prove sufficient:

PREFIX : <http://example.org/>
INSERT
{
  # Output triples in the desired graph
  GRAPH ?name { ?s ?p ?o }
}
WHERE
{
  # Use a sub-query to find all relevant graphs and concatenate their names together
  {
    SELECT (GROUP_CONCAT(STR(?g)) AS ?strName) (URI(?strName) AS ?name)
    WHERE
    {
       GRAPH ?g { ?s ?p :o1 }
    }
    GROUP BY ?g
  }
  # Join the name to all the relevant triples
  GRAPH ?g 
  {
    ?s ?p :o1 .
    ?s ?p ?o .
  }
}

所以这有点hacky,但应该可以大致满足您的要求.它使用子查询来查找所有具有相关三元组的图,并将它们的名称连接在一起并将其转换为 URI.请注意,这很可能会创建一个非常无效的 URI,这可能意味着查询的后续步骤将不起作用.

So this is a little hacky but should do roughly what you want. It uses a sub-query to find all the graphs that have the relevant triples and concatenates their name together and turns it into a URI. Note that this may well create a very invalid URI which may mean that the later steps of the query will not work.

然后将该图名称与包含具有所需主题的三元组的图中的所有三元组连接在一起.

It then joins that graph name together with all the triples from the graphs which contain triples with the desired subject.

最后,它使用新创建的名称将这些输出到图形中,这取决于商店,这可能不起作用,因为您可能会创建一个非法的图形名称.

Finally it outputs these into the graph with the newly created name, depending on the store this is the bit that might not work as you are liable to have created an illegal graph name.

这可能不完全符合您的要求,但希望能为您指明正确的方向.

This may not do exactly what you want but hopefully will point you in the right direction.

这篇关于使用 sparql 生成带有子图的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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