これ('RDF用クエリ言語SPARQL/8.2.3 FROMとFROM NAMEDの結合')をやってみた.
やってみた0.環境
前回環境を参考1.データファイル
data_8.2.1_1.ttl# Default graph (stored at http://example.org/dft.ttl)
@prefix dc: <http://purl.org/dc/elements/1.1/> .
<http://example.org/bob> dc:publisher "Bob Hacker" .
<http://example.org/alice> dc:publisher "Alice Hacker" .
data_8.2.1_2.ttl# Named graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
data_8.2.1_3.ttl# Named graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .
各ファイルの'#'行はコメントなのでこれでナニカを定義しているわけではない.2.コード
Test_8_2_3.javapackage sample;
import org.openjena.atlas.lib.StrUtils;
import com.hp.hpl.jena.query.DataSource;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
public class Test_8_2_3 {
public static void main(String[] args) {
Model model1 = ModelFactory.createDefaultModel();
FileManager.get().readModel(model1, "data_8.2.3_1.ttl");
Model model2 = ModelFactory.createDefaultModel();
FileManager.get().readModel(model2, "data_8.2.3_2.ttl");
Model model3 = ModelFactory.createDefaultModel();
FileManager.get().readModel(model3, "data_8.2.3_3.ttl");
DataSource dataSource= DatasetFactory.create();
dataSource.setDefaultModel(model1);
dataSource.addNamedModel("http://example.org/bob", model2);
dataSource.addNamedModel("http://example.org/alice", model3);
System.out.println(dataSource.asDatasetGraph().toString());
System.out.println();
String queryString = StrUtils.strjoin("\n",
"PREFIX foaf: <http://xmlns.com/foaf/0.1/>",
"PREFIX dc: <http://purl.org/dc/elements/1.1/>",
"",
"SELECT ?who ?g ?mbox",
"FROM <http://example.org/dft.ttl>",
"FROM NAMED <http://example.org/alice>",
"FROM NAMED <http://example.org/bob>",
"WHERE",
"{",
" ?g dc:publisher ?who .",
" GRAPH ?g { ?x foaf:mbox ?mbox }",
"}");
Query query = QueryFactory.create(queryString);
QueryExecution qExec = QueryExecutionFactory.create(query, dataSource);
ResultSetFormatter.out(System.out, qExec.execSelect(), query);
qExec.close();
}
}
('addNamedModelの第一引数の値で関連づけている)3.実行(dataset
(graph
(triple <http://example.org/alice> <http://purl.org/dc/elements/1.1/publisher> "Alice Hacker")
(triple <http://example.org/bob> <http://purl.org/dc/elements/1.1/publisher> "Bob Hacker")
)
(graph <http://example.org/bob>
(triple _:-335a33de:130a7151814:-7fff <http://xmlns.com/foaf/0.1/mbox> <mailto:bob@oldcorp.example.org>)
(triple _:-335a33de:130a7151814:-7fff <http://xmlns.com/foaf/0.1/name> "Bob")
)
(graph <http://example.org/alice>
(triple _:-335a33de:130a7151814:-7ffe <http://xmlns.com/foaf/0.1/mbox> <mailto:alice@work.example.org>)
(triple _:-335a33de:130a7151814:-7ffe <http://xmlns.com/foaf/0.1/name> "Alice")
))
----------------------------------------------------------------------------------
| who | g | mbox |
==================================================================================
| "Alice Hacker" | <http://example.org/alice> | <mailto:alice@work.example.org> |
| "Bob Hacker" | <http://example.org/bob> | <mailto:bob@oldcorp.example.org> |
----------------------------------------------------------------------------------
0 件のコメント:
コメントを投稿