Example12.java: 名前付きグラフを伴うSPARQL

この例では、名前付きグラフを伴う問合せを示します。これには、名前付きグラフのURIとそのパブリッシャに関する情報を持つデフォルト・グラフが伴います。この問合せはグラフ名とそのパブリッシャを検索し、名前付きグラフのそれぞれでfoaf:mbox述語を使用してメールボックスの値を検索します。

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.sparql.core.*;
import com.hp.hpl.jena.query.*;
import oracle.rdf.kv.client.jena.*;

public class Example12
{
  
  public static void main(String[] args) throws Exception
  {
  
String szStoreName  = args[0];
String szHostName   = args[1];
String szHostPort   = args[2];
    
// Create Oracle NoSQL connection 
OracleNoSqlConnection conn 
       = OracleNoSqlConnection.createInstance(szStoreName,
                                              szHostName, 
                                              szHostPort);
    
// Create Oracle NoSQL graph and dataset 
OracleGraphNoSql graph = new OracleGraphNoSql(conn);
DatasetGraphNoSql datasetGraph = DatasetGraphNoSql.createFrom(graph);

// Close graph, as it is no longer needed
graph.close(); 
    
// Clear dataset
datasetGraph.clearRepository();
    
// add data to the default graph
datasetGraph.add(new Quad(
        Quad.defaultGraphIRI, // specifies default graph
        Node.createURI("http://example.org/bob"),
        Node.createURI("http://purl.org/dc/elements/1.1/publisher"),
        Node.createLiteral("Bob Hacker")));
    
datasetGraph.add(new Quad(
        Quad.defaultGraphIRI, // specifies default graph
        Node.createURI("http://example.org/alice"),
        Node.createURI("http://purl.org/dc/elements/1.1/publisher"),
        Node.createLiteral("alice Hacker")));
    
// add data to the bob named graph
datasetGraph.add(new Quad(
        Node.createURI("http://example.org/bob"), // graph name
        Node.createURI("urn:bob"),
        Node.createURI("http://xmlns.com/foaf/0.1/name"),
        Node.createLiteral("Bob")));
    
datasetGraph.add(new Quad(
        Node.createURI("http://example.org/bob"), // graph name
        Node.createURI("urn:bob"),
        Node.createURI("http://xmlns.com/foaf/0.1/mbox"),
        Node.createURI("mailto:bob@example")));

// add data to the alice named graph
datasetGraph.add(new Quad(
        Node.createURI("http://example.org/alice"), // graph name
        Node.createURI("urn:alice"),
        Node.createURI("http://xmlns.com/foaf/0.1/name"),
        Node.createLiteral("Alice")));
    
datasetGraph.add(new Quad(
        Node.createURI("http://example.org/alice"), // graph name
        Node.createURI("urn:alice"),
        Node.createURI("http://xmlns.com/foaf/0.1/mbox"),
        Node.createURI("mailto:alice@example")));
    
Dataset ds = DatasetImpl.wrap(datasetGraph);
    
String szQuery = " PREFIX foaf: <http://xmlns.com/foaf/0.1/>"    +
" PREFIX dc: <http://purl.org/dc/elements/1.1/> "    +
" SELECT ?who ?graph ?mbox "                         +
" FROM NAMED <http://example.org/alice>"            +
" FROM NAMED <http://example.org/bob>"              +
" WHERE "                                            +
" { "                                                +
" ?graph dc:publisher ?who . "                       +
" GRAPH ?graph { ?x foaf:mbox ?mbox } "              +
" } ";
    
Query query = QueryFactory.create(szQuery);
QueryExecution qexec = QueryExecutionFactory.create(query, ds);
    
try {
      ResultSet results = qexec.execSelect();
      ResultSetFormatter.out(System.out, results, query);
    }
    
finally {
      qexec.close();
    }
    
ds.close();
conn.dispose();  
  }
}

この例をコンパイルして実行するコマンドと、想定されるJavaコマンドの出力は次のとおりです。

javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example12.java

javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example12 <store_name> \
<host_name> <host_port> 

----------------------------------------------------------------------
| who            | graph                      | mbox                 |
======================================================================
| "Bob Hacker"   | <http://example.org/bob>   | <mailto:bob@example> |
|"alice Hacker"  | <http://example.org/alice> | <mailto:alice@example>|
---------------------------------------------------------------------