SPARQLSelect[pattern]
pattern にマッチする部分グラフ内の値に相当する変数の連想のリストを返す,RDFStoreに適用可能なクエリ演算子である.
SPARQLSelect[patternvars]
変数 vars についての値のみを返す.
SPARQLSelect
SPARQLSelect[pattern]
pattern にマッチする部分グラフ内の値に相当する変数の連想のリストを返す,RDFStoreに適用可能なクエリ演算子である.
SPARQLSelect[patternvars]
変数 vars についての値のみを返す.
詳細とオプション
- SPARQLExecuteでSPARQLSelectを使ってSPARQL Endpointのクエリを行うことができる.
- SPARQLSelect[pattern]を実行した結果は連想{<|var1val1,1,…|>,…}のリストである.ただし,variは pattern に現れるSPARQLVariableで各 vali,jはマッチする部分グラフの対応する位置に現れる値である.
- SPARQLSelect[pattern]の pattern は以下の任意の形式でよい.
-
RDFTriple[s,p,o] トリプルパターン {RDFTriple[…],RDFTriple[…],…} 基本的なグラフパターン {patt1,patt2,…} グラフパターンのグループ化 SPARQLFilter[expr] expr で周囲のパターンの解にフィルタをかける patt/;expr expr で解にフィルタをかける SPARQLOptional[patt] 任意のパターン patt1|patt2|... 和集合パターン Except[patt] patt と互換の解を削除する SPARQLPropertyPath[…] 特性パスのパターン varexpr 式 expr と変数"var"をバインドする SPARQLValues[vars, values] 変数の値を指定する SPARQLSelect[patt] サブクエリ SPARQLGraph[g,patt] patt と名前が g のグラフをマッチさせる SPARQLService[url,patt] マッチする patt を url にあるSPARQL Endpointと連携させる - トリプルパターンRDFTriple[s,p,o]のSPARQLVariableは任意の式を表す.
- 次は,使用可能なオプションである.
-
"Distinct" False 重複する解が削除されるかどうか "Limit" Infinity 返す解の最大数 "Offset" 0 飛ばす解の数 "OrderBy" None それによって解を並べる式 "Reduced" False 重複する解を削除してもよいかどうか
例題
すべて開く すべて閉じる例 (2)
Needs["GraphStore`"]schema[s_] := URL["http://schema.org/" <> s];
ex[s_] := URL["http://example.org/" <> s];fruitGraph = RDFStore[{
RDFTriple[ex["banana"], schema["color"], "yellow"],
RDFTriple[ex["strawberry"], schema["color"], "red"],
RDFTriple[ex["strawberry"], ex["shape"], ex["round"]]
}];fruitGraph//SPARQLSelect[RDFTriple[SPARQLVariable["fruit"], schema["color"], SPARQLVariable["color"]]]fruitGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["fruit"], schema["color"], SPARQLVariable["color"]],
RDFTriple[SPARQLVariable["fruit"], ex["shape"], SPARQLVariable["shape"]]
}]Needs["GraphStore`"]Wikidataでカルシウム(Q706)を含む(P527)化学式(P274)と薬品の和名(Q11173)を求める:
rdfs[s_] := URL["http://www.w3.org/2000/01/rdf-schema#" <> s];
wd[s_] := URL["http://www.wikidata.org/entity/" <> s];
wdt[s_] := URL["http://www.wikidata.org/prop/direct/" <> s];SPARQLExecute[
"https://query.wikidata.org/sparql",
SPARQLSelect[
{
RDFTriple[SPARQLVariable["chemical"], wdt["P31"], wd["Q11173"]],
RDFTriple[SPARQLVariable["chemical"], wdt["P274"], SPARQLVariable["formula"]],
RDFTriple[SPARQLVariable["chemical"], wdt["P527"], wd["Q706"]],
RDFTriple[SPARQLVariable["chemical"], rdfs["label"], SPARQLVariable["label"]] /; SPARQLEvaluation["lang"][SPARQLVariable["label"]] == "ja"
} -> {"formula", "name" -> SPARQLEvaluation["str"][SPARQLVariable["label"]]},
"Limit" -> 5
]
]スコープ (9)
トリプルパターン (1)
Needs["GraphStore`"]ex[s_] := URL["http://example.org/" <> s];exampleGraph = RDFStore[{
RDFTriple[ex["s1"], ex["p"], 4],
RDFTriple[ex["s2"], ex["p"], 8],
RDFTriple[ex["s3"], ex["p"], 8]
}];exampleGraph//SPARQLSelect[RDFTriple[ex["s1"], ex["p"], SPARQLVariable["x"]]]exampleGraph//SPARQLSelect[RDFTriple[SPARQLVariable["x"], ex["p"], 8]]exampleGraph//SPARQLAsk[RDFTriple[ex["s3"], ex["p"], 8]]基本的なグラフパターン (1)
Needs["GraphStore`"]dct[s_] := URL["http://purl.org/dc/terms/" <> s];
ex[s_] := URL["http://example.org/" <> s];articlesGraph = RDFStore[{
RDFTriple[ex["article1"], dct["title"], "some title"],
RDFTriple[ex["article1"], dct["creator"], "Jenny"],
RDFTriple[ex["article1"], dct["creator"], "Oliver"],
RDFTriple[ex["article1"], dct["created"], 2018],
RDFTriple[ex["article2"], dct["title"], "other title"],
RDFTriple[ex["article2"], dct["creator"], "Jenny"],
RDFTriple[ex["article2"], dct["created"], 2017]
}];基本的なグラフパターンはトリプルパターンのリストである.すべての記事の"title"と"created"を取り出す:
articlesGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["a"], dct["title"], SPARQLVariable["title"]],
RDFTriple[SPARQLVariable["a"], dct["created"], SPARQLVariable["created"]]
} -> {"title", "created"}]変数とリテラルは任意の場所に現れることができる.2018年に著された記事の著者を求める:
articlesGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["a"], dct["created"], 2018],
RDFTriple[SPARQLVariable["a"], dct["creator"], SPARQLVariable["author"]]
} -> "author"]基本的なグラフにおける異なるトリプルパターンは,グラフ中の異なるトリプルと一致する必要はない.以下には"art1"と"art2"を同じ記事とバインドする解が含まれている:
articlesGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["art1"], dct["creator"], SPARQLVariable["auth"]],
RDFTriple[SPARQLVariable["art2"], dct["creator"], SPARQLVariable["auth"]]
}]//Take[#, 3]&複数の記事を公開している著者を見付けるために,"art1"と"art2"は異ならなければならないとすることで解にフィルタをかける:
articlesGraph//SPARQLSelect[
{
RDFTriple[SPARQLVariable["art1"], dct["creator"], SPARQLVariable["auth"]],
RDFTriple[SPARQLVariable["art2"], dct["creator"], SPARQLVariable["auth"]]
} /; SPARQLVariable["art1"] ≠ SPARQLVariable["art2"] -> "auth",
"Distinct" -> True
]フィルタ (1)
Needs["GraphStore`"]ex[s_] := URL["http://example.org/" <> s];bookGraph = RDFStore[{
RDFTriple[ex["faust1"], ex["yearPublished"], 1808],
RDFTriple[ex["faust2"], ex["yearPublished"], 1832],
RDFTriple[ex["RomeoAndJuliet"], ex["yearPublished"], 1597]
}];bookGraph//SPARQLSelect[RDFTriple[SPARQLVariable["book"], ex["yearPublished"], SPARQLVariable["year"]] /; SPARQLVariable["year"] < 1820]任意 (1)
Needs["GraphStore`"]foaf[s_] := URL["http://xmlns.com/foaf/0.1/" <> s];
ex[s_] := URL["http://example.org/" <> s];personGraph = RDFStore[{
RDFTriple[ex["alice"], foaf["nick"], "Alice"],
RDFTriple[ex["alice"], foaf["mbox"], URL["mailto:alice@example.org"]],
RDFTriple[ex["bob"], foaf["nick"], "Bob"]
}];personGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["p"], foaf["nick"], SPARQLVariable["name"]],
SPARQLOptional[RDFTriple[SPARQLVariable["p"], foaf["mbox"], SPARQLVariable["mail"]]]
} -> {"name", "mail"}]SPARQLOptionalラッパーが省略されていると,"Bob"についての解が見付からなくなる:
personGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["p"], foaf["nick"], SPARQLVariable["name"]],
RDFTriple[SPARQLVariable["p"], foaf["mbox"], SPARQLVariable["mail"]]
} -> {"name", "mail"}]和集合 (1)
Needs["GraphStore`"]rdfs[s_] := URL["http://www.w3.org/2000/01/rdf-schema#" <> s];
dct[s_] := URL["http://purl.org/dc/terms/" <> s];
ex[s_] := URL["http://example.org/" <> s];thingsGraph = RDFStore[{
RDFTriple[ex["earth"], rdfs["label"], "Earth"],
RDFTriple[ex["rubiksCube"], rdfs["label"], "Rubik's Cube"],
RDFTriple[ex["guide"], dct["title"], "The Hitchhiker's Guide"]
}];thingsGraph//SPARQLSelect[RDFTriple[SPARQLVariable["thing"], rdfs["label"], SPARQLVariable["name"]] | RDFTriple[SPARQLVariable["thing"], dct["title"], SPARQLVariable["name"]] -> "name"]thingsGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["thing"], {rdfs["label"] | dct["title"]}, SPARQLVariable["name"]] -> "name"]否定 (1)
Needs["GraphStore`"]foaf[s_] := URL["http://xmlns.com/foaf/0.1/" <> s];
ex[s_] := URL["http://example.org/" <> s];personGraph = RDFStore[{
RDFTriple[ex["alice"], foaf["nick"], "Alice"],
RDFTriple[ex["alice"], foaf["mbox"], URL["mailto:alice@example.org"]],
RDFTriple[ex["bob"], foaf["nick"], "Bob"]
}];personGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["person"], foaf["nick"], SPARQLVariable["name"]],
Except[RDFTriple[SPARQLVariable["person"], foaf["mbox"], SPARQLVariable["mbox"]]]
}]この場合,NOT EXISTSに基づいたクエリで同じ結果が得られる:
personGraph//SPARQLSelect[RDFTriple[SPARQLVariable["person"], foaf["nick"], SPARQLVariable["name"]] /; !SPARQLEvaluation["exists"][RDFTriple[SPARQLVariable["person"], foaf["mbox"], SPARQLVariable["mbox"]]]]特性パス (1)
Needs["GraphStore`"]schema[s_] := URL["http://schema.org/" <> s];
ex[s_] := URL["http://example.org/" <> s];familyGraph = RDFStore[{
RDFTriple[ex["August"], schema["parent"], ex["Johann"]],
RDFTriple[ex["Alma"], schema["parent"], ex["August"]],
RDFTriple[ex["Wolfgang"], schema["parent"], ex["August"]],
RDFTriple[ex["Walther"], schema["parent"], ex["August"]]
}];familyGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["child"], {schema["parent"]..}, ex["Johann"]]]familyGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["child"], {schema["parent"], schema["parent"]}, ex["Johann"]]]割当て (1)
Needs["GraphStore`"]schema[s_] := URL["http://schema.org/" <> s];
ex[s_] := URL["http://example.org/" <> s];productGraph = RDFStore[{
RDFTriple[ex["pizza"], schema["price"], 10],
RDFTriple[ex["pizza"], ex["commission"], .20],
RDFTriple[ex["pasta"], schema["price"], 8],
RDFTriple[ex["pasta"], ex["commission"], .25],
RDFTriple[ex["friedRice"], schema["price"], 10],
RDFTriple[ex["friedRice"], ex["commission"], .20]
}];productGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]],
RDFTriple[SPARQLVariable["food"], ex["commission"], SPARQLVariable["c"]],
"totalPrice" -> SPARQLVariable["price"](1 + SPARQLVariable["c"])
} -> {"food", "totalPrice"}]productGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]],
RDFTriple[SPARQLVariable["food"], ex["commission"], SPARQLVariable["c"]]
} -> {"food", "totalPrice" -> SPARQLVariable["price"](1 + SPARQLVariable["c"])}]productGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]],
RDFTriple[SPARQLVariable["food"], ex["commission"], SPARQLVariable["c"]],
"totalPrice" -> SPARQLVariable["price"](1 + SPARQLVariable["c"])
} /; SPARQLVariable["totalPrice"] < 11 -> {"food", "totalPrice"}]productGraph//SPARQLSelect[{
SPARQLValues["food", {ex["pizza"], ex["pasta"]}],
RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]]
}]集約 (1)
Needs["GraphStore`"]salaryGraph = ImportString["
base <http://example.org/>
<bob> <department> <physics>; <salary> 3000 .
<anna> <department> <physics>; <salary> 2000 .
<tom> <department> <math>; <salary> 5000 .
<helga> <department> <it>; <salary> 3000 .
<carl> <department> <it>; <salary> 1000 .
", "Turtle"];ex[s_] := URL["http://example.org/" <> s];salaryGraph//SPARQLQuery[
SPARQLSelect[RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]]] /* SPARQLAggregate["avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]]
]salaryGraph//SPARQLQuery[
SPARQLSelect[{
RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]],
RDFTriple[SPARQLVariable["person"], ex["department"], SPARQLVariable["dept"]]
}] /* SPARQLAggregate[
{
"dept",
"avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]
},
SPARQLVariable["dept"]
]
]salaryGraph//SPARQLQuery[
SPARQLSelect[{
RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]],
RDFTriple[SPARQLVariable["person"], ex["department"], SPARQLVariable["dept"]]
}] /* SPARQLAggregate[
{
"dept",
"avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]
},
SPARQLVariable["dept"],
SPARQLEvaluation["sum"][SPARQLVariable["salary"]] > 4000
]
]salaryGraph//SPARQLQuery[
SPARQLSelect[{
RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]],
RDFTriple[SPARQLVariable["person"], ex["department"], SPARQLVariable["dept"]]
}] /* SPARQLAggregate[
{
"dept",
"avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]
},
SPARQLVariable["dept"],
SPARQLEvaluation["sum"][SPARQLVariable["salary"]] > 4000,
SPARQLEvaluation["avg"][SPARQLVariable["salary"]] -> "Descending"
]
]おもしろい例題 (1)
Needs["GraphStore`"]Wikidataを使ってクジラ(Q42196)の分類ツリー(P171)のGraphを作る:
wikibase[s_] := URL["http://wikiba.se/ontology#" <> s];
bd[s_] := URL["http://www.bigdata.com/rdf#" <> s];
wd[s_] := URL["http://www.wikidata.org/entity/" <> s];
wdt[s_] := URL["http://www.wikidata.org/prop/direct/" <> s];SPARQLExecute[
"https://query.wikidata.org/sparql",
SPARQLSelect[{
SPARQLPropertyPath[wd["Q42196"], {wdt["P171"]...}, SPARQLVariable["taxon"]],
SPARQLOptional[RDFTriple[SPARQLVariable["taxon"], wdt["P171"], SPARQLVariable["parentTaxon"]]],
SPARQLService[wikibase["label"], RDFTriple[bd["serviceParam"], wikibase["language"], "en"]]
} -> {"taxon", "taxonLabel", "parentTaxon"}]
]//Function[whaleTaxons,
Graph[
Labeled[#taxon, First[#taxonLabel]]& /@ whaleTaxons,
If[KeyExistsQ[#, "parentTaxon"], #taxon -> #["parentTaxon"], Nothing]& /@ whaleTaxons
]
]関連するガイド
テキスト
Wolfram Research (2019), SPARQLSelect, Wolfram言語関数, https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html.
CMS
Wolfram Language. 2019. "SPARQLSelect." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html.
APA
Wolfram Language. (2019). SPARQLSelect. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html
BibTeX
@misc{reference.wolfram_2026_sparqlselect, author="Wolfram Research", title="{SPARQLSelect}", year="2019", howpublished="\url{https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html}", note=[Accessed: 11-August-2026]}
BibLaTeX
@online{reference.wolfram_2026_sparqlselect, organization={Wolfram Research}, title={SPARQLSelect}, year={2019}, url={https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html}, note=[Accessed: 11-August-2026]}