3

I have the following situation in my RDF-store:

Named Graph: A

  S1 P1 O1

Named Graph: B

  S2 P2 O1

Named Graph: C

  S3 P3 O1

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

NEW Named Graph: A+B+C

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

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?

Tank you

4

1 回答 1

4

你不能完全按照你的意愿去做,因为子图不能像你描述的那样显式地存储在彼此之间。但是,您可以将所有三元组放入一个图形中,这可能就足够了:

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,这可能意味着查询的后续步骤将不起作用。

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

最后,它使用新创建的名称将这些输出到图形中,具体取决于存储,这可能无法正常工作,因为您可能创建了非法的图形名称。

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

于 2015-02-08T02:03:43.317 回答