Sparql,如何合并不同的结果

时间:2022-11-08 19:01:45

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. So the result4 will be:

我想要的是将result1, result2和result3合并为result4。所以结果是:

result4: clare, antoine, jim, david, david, clare, clare, david, clare, clare, jim, david.

and then to use DISTINCT to remove the duplicates. How can I achieve this please?

然后使用DISTINCT来删除副本。请问我怎样才能做到这一点?

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 .
    }
}

1 个解决方案

#1


4  

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. It can be expressed more succinctly using SPARQL property paths:

然后您可以使用SPARQL的UNION特性来通过合并三个独立查询的结果来实现您想要的结果。可以使用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:

您还可以通过单个属性路径表达式获得know谓词的完整传递闭包:

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. Otherwise you would have to use reasoning or repeated queries to get the full closure.

…如果您的triplestore支持SPARQL 1.1。否则,您将不得不使用推理或重复查询来获得完整的闭包。

#1


4  

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. It can be expressed more succinctly using SPARQL property paths:

然后您可以使用SPARQL的UNION特性来通过合并三个独立查询的结果来实现您想要的结果。可以使用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:

您还可以通过单个属性路径表达式获得know谓词的完整传递闭包:

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. Otherwise you would have to use reasoning or repeated queries to get the full closure.

…如果您的triplestore支持SPARQL 1.1。否则,您将不得不使用推理或重复查询来获得完整的闭包。