[英]Sparql, how to merge different results
I am trying to create a SPARQL query to find all the people that Jim knows, then to find which people the knowers of Jim know and then the same thing like a chain. 我试图创建一个SPARQL查询,以查找Jim认识的所有人员,然后查找Jim的认识者认识的人员,然后找到类似链的东西。
For example I have that: 例如,我有:
Jim knows Clare and Antoine
Clare knows Jim and David
Antoine knows David and Clare
David knows Clare
So the results are then: 因此结果如下:
result1: Clare, Antoine
result2: Jim, David, David, Clare
result3: Clare, David, Clare, Clare, Jim, David
More or less I have made something like a tree. 我或多或少地做了类似树的东西。
What I want is to merge result1
, result2
and result3
into result4
. 我想要的是将result1
, result2
和result3
合并到result4
。 So the result4
will be: 因此, result4
将是:
result4: clare, antoine, jim, david, david, clare, clare, david, clare, clare, jim, david.
and then to use DISTINCT
to remove the duplicates. 然后使用DISTINCT
删除重复项。 How can I achieve this please? 请问我该如何实现?
SELECT ?Result1 ?Result2 ?Result3
WHERE{
{
base:Knows dc:Names _:BN1 .
_:BN1 dc:FName "Jim";
dc:KnownFName ?Result1 .
}
.
{
base:Knows dc:Names _:BN2 .
_:BN2 dc:FName ?Result1;
dc:KnownFName ?Result2 .
}
.
{
base:Knows dc:Names _:BN3 .
_:BN3 dc:FName ?Result2;
dc:KnownFName ?Result3 .
}
}
Let's say your example is represented as follows: 假设您的示例表示如下:
<http://example.com/person/1> <http://xmlns.com/foaf/0.1/knows> <http://example.com/person/2> .
<http://example.com/person/1> <http://xmlns.com/foaf/0.1/knows> <http://example.com/person/3> .
<http://example.com/person/2> <http://xmlns.com/foaf/0.1/knows> <http://example.com/person/1> .
<http://example.com/person/2> <http://xmlns.com/foaf/0.1/knows> <http://example.com/person/4> .
<http://example.com/person/3> <http://xmlns.com/foaf/0.1/knows> <http://example.com/person/4> .
<http://example.com/person/3> <http://xmlns.com/foaf/0.1/knows> <http://example.com/person/2> .
<http://example.com/person/4> <http://xmlns.com/foaf/0.1/knows> <http://example.com/person/2> .
<http://example.com/person/1> <http://www.w3.org/2000/01/rdf-schema#label> "Jim" .
<http://example.com/person/2> <http://www.w3.org/2000/01/rdf-schema#label> "Clare" .
<http://example.com/person/3> <http://www.w3.org/2000/01/rdf-schema#label> "Antoine" .
<http://example.com/person/4> <http://www.w3.org/2000/01/rdf-schema#label> "David" .
Then you can use SPARQL's UNION feature to achieve what you want by merging the result of three separate queries. 然后,您可以使用SPARQL的UNION功能,通过合并三个独立查询的结果来实现所需的功能。 It can be expressed more succinctly using SPARQL property paths: 可以使用SPARQL属性路径更简洁地表示它:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT * WHERE {
{ SELECT * WHERE {
<http://example.com/person/1> foaf:knows/rdfs:label ?knowsName .
} }
UNION
{ SELECT * WHERE {
<http://example.com/person/1> foaf:knows/foaf:knows/rdfs:label ?knowsName .
} }
UNION
{ SELECT * WHERE {
<http://example.com/person/1> foaf:knows/foaf:knows/foaf:knows/rdfs:label ?knowsName .
} }
}
You can also get the full transitive closure of the knows
predicate via a single property path expression: 您还可以通过单个属性路径表达式来获得knows
谓词的完全可传递闭包:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * WHERE {
<http://example.com/person/1> foaf:knows+/rdfs:label ?knowsName .
}
...if your triplestore supports SPARQL 1.1. ...如果您的Triplestore支持SPARQL 1.1。 Otherwise you would have to use reasoning or repeated queries to get the full closure. 否则,您将不得不使用推理或重复查询来获得完全关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.