3

RDF ファイルがあり、そこから情報を抽出してファイルに書き込む必要があります。私はそれが基本的にどのように機能するかを理解しましたが、私はこれにこだわっています:

String queryString = "select ?person ?children where { ?person ?hasChildren ?children}";
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = tupleQuery.evaluate();     
while (result.hasNext()) {
    BindingSet bindingSet = result.next();
    Value p1 = bindingSet.getValue("person");
    Value p2 = bindingSet.getValue("child");
    println(p1 + " has children " + p2 +"");
}
result.close();

私が得る出力は次のようなものです:

http://example.org/people/person1 has children http://example.org/people/child1
http://example.org/people/person1 has children http://example.org/people/child2

この形式ですべての人をオブジェクトとともにリストする方法がわかりません。

person1 has children child1 and child2

これはどのように行うことができますか?

4

1 に答える 1

3

SPARQL の について説明しているこの回答がgroup_concat役立つ場合があります。

SPARQL では、クエリ ソリューションの結果セットがある場合、group1 つ以上の変数に対して、これらの変数が共通しているソリューションをマージできます。たとえば、データを考えてみましょう

@prefix : <http://example.org/people/>.

:person1 :hasChild :child1, :child2, :child3 .
:person2 :hasChild :child4, :child5 .
:person3 :hasChild :child6 .

その上で次のクエリを実行すると

prefix : <http://example.org/people/>

select ?person ?child where { 
  ?person :hasChild ?child .
}

次のような結果が得られます。

$ arq --data data.n3 --query query.sparql
----------------------
| person   | child   |
======================
| :person3 | :child6 |
| :person2 | :child5 |
| :person2 | :child4 |
| :person1 | :child3 |
| :person1 | :child2 |
| :person1 | :child1 |
----------------------

質問にあるように結果を反復すると、現在取得しているタイプの出力が生成されます。私たちがやりたいことは、実際に次のような結果を得ることです。

$ arq --data data.n3 --query query.sparql
----------------------------------------
| person   | child                     |
========================================
| :person3 | :child6                   |
| :person2 | :child4, :child5          |
| :person1 | :child1, :child2, :child3 |
----------------------------------------

それがまさに私たちにgroup_byできることです。次のようなクエリ:

prefix : <http://example.org/people/>

select ?person (group_concat(?child;separator=' and ') as ?children) where { 
  ?person :hasChild ?child .
}
group by ?person

?childrenが生成されます (結果の変数はではなくであることに注意してください。これは、新しい変数 を作成する?childために使用したためです):group_concat(...) as ?children?children

$ arq --data data.n3 --query query.sparql
---------------------------------------------------------------------------------------------------------------------------
| person   | children                                                                                                     |
===========================================================================================================================
| :person3 | "http://example.org/people/child6"                                                                           |
| :person1 | "http://example.org/people/child3 and http://example.org/people/child2 and http://example.org/people/child1" |
| :person2 | "http://example.org/people/child5 and http://example.org/people/child4"                                      |
---------------------------------------------------------------------------------------------------------------------------

このようなクエリを使用して結果を反復処理し、結果を印刷すると、必要な出力が得られます。人や子供から先頭を取り除きたい場合はhttp://example.org/people/、もう少し文字列処理が必要です。たとえば、STRAFTERを使用してプレフィックスを削除するとhttp://example.org/people/、次のようなクエリを使用できます。

prefix : <http://example.org/people/>

select
 (strafter(str(?personX),"http://example.org/people/") as ?person)
 (group_concat(strafter(str(?child),"http://example.org/people/");separator=' and ') as ?children)
where { 
  ?personX :hasChild ?child .
}
group by ?personX

次のような結果を得るには:

$ arq --data data.n3 --query query.sparql
----------------------------------------------
| person    | children                       |
==============================================
| "person3" | "child6"                       |
| "person2" | "child5 and child4"            |
| "person1" | "child3 and child2 and child1" |
----------------------------------------------

印刷を行うと、次のような結果が得られます

person3 has children child6
person2 has children child5 and child4
person1 has children child3 and child2 and child1
于 2013-08-28T13:28:56.263 に答える