ラベル OpenRDF_Sesame の投稿を表示しています。 すべての投稿を表示
ラベル OpenRDF_Sesame の投稿を表示しています。 すべての投稿を表示

2012年10月2日火曜日

“SPARQL query with OPTIONAL to two remote SPARQL endpoints”をやってみる(やっただけ)

参考: 2.2 SPARQL query with OPTIONAL to two remote SPARQL endpoints
参考をopenrdf-sesame 2.6.9 + tomcat (6.0.35 (今回IP:192.168.0.33) / 7.0.30 (今回IP:192.168.0.18) )で行う.
(注意: tomcat-7.0.30 では localhost:8080/openrdf-sesame にアクセスすると404を返すが,localhost:8080/openrdf-workbench へアクセスするとCurrent Selections/Sesame serverの項に表記されるので,応答はしているようである.)
1. 前々回を参考に一台目(今回IP:192.168.0.33)にpeopleというMemory storeを作成し,下記(参考より)を登録する.
  @prefix foaf:  <http://xmlns.com/foaf/0.1/> .
  @prefix : <http://example.org/> .
  
  :people15  foaf:name     "Alice" .
  :people16  foaf:name     "Bob" .
  :people17  foaf:name     "Charles" .
  :people17  foaf:interest     <http://www.w3.org/2001/sw/rdb2rdf/> .

2. 同じく前々回を参考にニ台目(今回IP:192.168.0.18)にpeople2というMemory storeを作成し,下記(参考より)を登録する.
  @prefix foaf:  <http://xmlns.com/foaf/0.1/> .
  @prefix : <http://example.org/> .
  
  :people15  foaf:knows    :people18 . 
  :people18  foaf:name     "Mike" .
  :people17  foaf:knows    :people19 . 
  :people19  foaf:name     "Daisy" .

3. 一台目もしくは二台目のworkbenchより下記(参考より)のSPARQL Queryを送信する(どちらからでも問題はない).
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?name ?interest ?known
WHERE
{
  SERVICE <http://192.168.0.33:8080/openrdf-sesame/repositories/people> { 
    ?person foaf:name ?name .  
    OPTIONAL { 
      ?person foaf:interest ?interest .
      SERVICE <http://192.168.0.18:8080/openrdf-sesame/repositories/people2> { 
        ?person foaf:knows ?known . } }
  }    
}

赤字の部分は参考では'people'になっていたが(2012/10/2現在), 参考内のQuery Resultと合わせるには'name'にする必要がある.
4. Query Resultを確認する.

基本は以下のQuery
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE
{
  SERVICE <http://192.168.0.33:8080/openrdf-sesame/repositories/people> { 
    ?person foaf:name ?name .  
  }    
}

一台目(192.168.0.33)でなら以下でもよい
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE
{
  ?person foaf:name ?name .      
}


上のQueryの場合の結果

2012年9月28日金曜日

別のRDFS推論を試す(三段論法?) / openrdf-sesame 2.6.9のjar

前々回の参考サイトにある別のRDFS推論(3段論法的?)を試す.
RDFS推論 以下のN3ファイルから
@prefix kb:  <http://knowledgebooks.com/ontology#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/#> .

foaf:Person rdfs:subClassOf foaf:Agent .
kb:KnowledgeEngineer rdfs:subClassOf foaf:Person .

<http://www.markwatson.com/index.rdf> a kb:KnowledgeEngineer .
次のSPARQL Queryを実行して
PREFIX foaf:  <http://xmlns.com/foaf/0.1/#>
   SELECT ?subject ?predicate WHERE { ?subject ?predicate foaf:Agent . }
foaf:Agentとして以下を得る.
 http://xmlns.com/foaf/0.1/#Person http://www.w3.org/2000/01/rdf-schema#subClassOf
 http://xmlns.com/foaf/0.1/#Agent http://www.w3.org/2000/01/rdf-schema#subClassOf
 http://knowledgebooks.com/ontology#KnowledgeEngineer http://www.w3.org/2000/01/rdf-schema#subClassOf
 http://www.markwatson.com/index.rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#type
下記のような関係を見つけることになる.
コード
ファイル読み込みではなく,途中トリプルを一つ一つ入れて(赤い部分)みた.
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.repository.sail.SailRepositoryConnection;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParseException;
import org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer;
import org.openrdf.sail.memory.MemoryStore;

public class Sesame_Test {

 /**
  * @param args
  * @throws RepositoryException
  * @throws IOException
  * @throws RDFParseException
  * @throws QueryEvaluationException
  * @throws MalformedQueryException
  */
 public static void main(String[] args) throws RepositoryException,
   RDFParseException, IOException, MalformedQueryException,
   QueryEvaluationException {
  MemoryStore ms = new MemoryStore();
  ForwardChainingRDFSInferencer fci = new ForwardChainingRDFSInferencer(
    ms);
  SailRepository myRepository = new SailRepository(fci);
  myRepository.initialize();
  SailRepositoryConnection con = myRepository.getConnection();
  
  if (true) {
   {
    Resource s = new URIImpl("http://xmlns.com/foaf/0.1/#Person");
    URI p = new URIImpl(
      "http://www.w3.org/2000/01/rdf-schema#subClassOf");
    Value o = new URIImpl("http://xmlns.com/foaf/0.1/#Agent");
    con.add(s, p, o);
   }
   {
    Resource s = new URIImpl(
      "http://knowledgebooks.com/ontology#KnowledgeEngineer");
    URI p = new URIImpl(
      "http://www.w3.org/2000/01/rdf-schema#subClassOf");
    Value o = new URIImpl("http://xmlns.com/foaf/0.1/#Person");
    con.add(s, p, o);
   }
   {
    Resource s = new URIImpl("http://www.markwatson.com/index.rdf");
    URI p = new URIImpl(
      "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
    // The 'a' in Notation3 format means the
    // 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' in URI;
    Value o = new URIImpl(
      "http://knowledgebooks.com/ontology#KnowledgeEngineer");
    con.add(s, p, o);
   }
  } else {
    File file = new File("rdf_files/class_example.n3");
    con.add(file, null, RDFFormat.N3);
  }
  
  String sparql_query = "PREFIX foaf:  <http://xmlns.com/foaf/0.1/#>";
  sparql_query += " SELECT ?subject ?predicate WHERE { ?subject ?predicate foaf:Agent . }";
  TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL,
    sparql_query);
  TupleQueryResult result = tupleQuery.evaluate();

  try {
   List<String> bindingNames = result.getBindingNames();
   while (result.hasNext()) {
    BindingSet bindingSet = result.next();
    int size2 = bindingSet.size();
    for (int i = 0; i < size2; i++) {
     System.out.print('\t' + bindingSet.getValue(
       bindingNames.get(i)).stringValue());
    }
    System.out.print('\n');
   }
  } finally {
   result.close();
  }
 }
}
jarは前々回を参照してもらうか,めんどくさければopenrdf-sesame-2.6.9-sdk.zip展開後のlibフォルダにあるの全部.

openrdf-sesameに付いてくるopenrdf-workbenchからRDFS推論をするまで

前回エントリーの内容を前々回エントリーで作成したopenrdf-sesame serverとworkbenchから試みる.また最後にJavaを用いてSesame ServerへSPARQL Queryで問い合わせることを試みる.
1. とりあえずSesame Serverが動作しているか確認
http://localhost:8080/openrdf-sesame
にアクセスして動作してみる
2. とりあえずWorkbenchにアクセスする
http://localhost:8080/openrdf-workbench
にアクセスして動作してみる
3. Repositoryを作る(とりあえず実行できる程度の内容)
a. 左メニューよりRepositories/New repositoryを選択
b. 作成するRepositoryのTypeを指定(Typeとして'Memory store with RDF Schema inferencing' RDFS推論を行うため!)
c. 'Create'ボタンにてRepositoryを作成
d. できあがったRepository詳細の表示(Current Selectionsに指定されていることを確認)

ちなみにここの'Summary/Repository Location/Location'のURLがSPARQL QueryのEndpointになる
4. ファイルアップロードによりRDF data(news.n3)を追加する
a. 左メニューよりModify/Addを選択
b. 'Base URI:'にある'use base URI as context identifier'のチェックを外す,'RDF Data File:'にある'Select the file containing the RDF data you wish to upload'をセレクト, 'ファイル選択'にてnews.n3(前回エントリー参照)を選択,'Upload'ボタンにてファイルをアップロード
c. 結果画面としてSummaryのページの表示を確認
d. 左メニューの'Explore/Export'にてアップロードされた内容が入っていることを確認(可能)
5. SPARQL Queryを実行する
a. 左メニューより'Explore/Query'を選択
b. 'Query:'にクエリを記述,'Execute'ボタンにて実行('Include inferred statements'のチェックは外さない)
PREFIX kb:   SELECT ?subject ?object WHERE { ?subject kb:containsPlace ?object . }

c. 結果の確認
番外編: JavaでEndpointへQueryを投げてみる
ライブラリは前回エントリーの状態にさらに,lib/commons-httpclient-3.1.jar, lib/commons-codec-1.4.jarを加える.またcommons-logging.jarも加えるのだがこれはopenrdf-sesame-*-sdk.zipに入っていないのでサイトから取得する.
import java.io.IOException;
import java.util.List;

import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sparql.SPARQLRepository;
import org.openrdf.rio.RDFParseException;

public class Sesame_Test {

 public static void main(String[] args) throws RepositoryException,
   MalformedQueryException, QueryEvaluationException,
   RDFParseException, IOException {

  String queryEndpointUrl = "http://localhost:8080/openrdf-sesame/repositories/memory-rdfs";
  SPARQLRepository myRepository = new SPARQLRepository(queryEndpointUrl);
  myRepository.initialize();
  RepositoryConnection con = myRepository.getConnection();

  String sparql_query = "PREFIX kb: <http://knowledgebooks.com/ontology#> SELECT ?subject ?object WHERE { ?subject kb:containsPlace ?object . }";

  TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL,
    sparql_query);
  //tupleQuery.setIncludeInferred(true); //this property's default value is true.

  TupleQueryResult result = tupleQuery.evaluate();

  try {
   List<String> bindingNames = result.getBindingNames();
   while (result.hasNext()) {
    BindingSet bindingSet = result.next();
    for (int i = 0; i < bindingSet.size(); i++) {
     String val = bindingSet.getValue(bindingNames.get(i))
       .stringValue();
     System.out.print('\t' + val);
    }
    System.out.print('\n');
   }
  } finally {
   result.close();
  }
 }

}

2012年9月27日木曜日

openrdf-sesae 2.6.9のjarを用いてRDFS推論の例を真似してみた(だけ).

参考 Java/JRuby開発者のためのセマンティックWeb入門
参考サイトのRDFS推論の例の実行をopenrdf-sesame 2.6.9のjarで試みる
news.n3を読み込み次のsparql queryによって結果を得る
sparql query
PREFIX kb:  <http://knowlegebooks.com/ontology/#> SELECT ?subject ?object WHERE { ?subject kb:containsPlace ?object . }
news.n3 (参考サイトのサンプルソース内ファイル.以下は参照サイトのリスト5にある同ファイルの最初の数行)
@prefix kb: <http://knowledgebooks.com/ontology#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

kb:containsCity rdfs:subPropertyOf kb:containsPlace .
kb:containsCountry rdfs:subPropertyOf kb:containsPlace .
kb:containsState rdfs:subPropertyOf kb:containsPlace .

<http://news.yahoo.com/s/nm/20080616/ts_nm/usa_flooding_dc_16 /> kb:containsCity "Burlington" , "Denver" , "St. Paul" , "Chicago" , "Quincy" , "CHICAGO" , "Iowa City" ;
  kb:containsRegion "U.S. Midwest" , "Midwest" ;
  kb:containsCountry "United States" , "Japan" ;
  kb:containsState "Minnesota" , "Illinois" , "Mississippi" , "Iowa" ;
  kb:containsOrganization "National Guard" , "U.S. Department of Agriculture" , "White House" , "Chicago Board of Trade" , "Department of Transportation" ;
  kb:containsPerson "Dena Gray-Fisher" , "Donald Miller" , "Glenn Hollander" , "Rich Feltes" , "George W. Bush" ;
  kb:containsIndustryTerm "food inflation" , "food" , "finance ministers" , "oil" .

上記部分だけではあるが,図示すると以下の2つの図のようになる.
(ただし,
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX kb: <http://knowledgebooks.com/ontology#>
)


( predicateとしてkb:containsPlaceというURIは,このnews.n3ファイルにはない.)

やったこと
1. Eclipse内にJava Project('Sesame_Test')を作成.
2. openrdf-sesame-2.6.9/libをImport.
3. lib/sesame-query*.jar, lib/sesame-repository-*.jar, lib/sesame-rio-*.jar, lib/sesame-sail-*.jar, lib/sesame-util-*.jar, lib/slf4j-api-*.jarをBuild Pathに追加.
4. 参照サイトのサンプルソース内'rdf_files/news.n3'をImport.
5. Sample_Test.javaクラスを作成.
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.repository.sail.SailRepositoryConnection;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParseException;
import org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer;
import org.openrdf.sail.memory.MemoryStore;

public class Sesame_Test {

 private static SailRepository myRepository;
 private static SailRepositoryConnection con;

 /**
  * @param args
  * @throws RepositoryException
  * @throws IOException
  * @throws RDFParseException
  * @throws QueryEvaluationException
  * @throws MalformedQueryException
  */
 public static void main(String[] args) throws RepositoryException,
   RDFParseException, IOException, MalformedQueryException,
   QueryEvaluationException {
  MemoryStore ms = new MemoryStore();
  ForwardChainingRDFSInferencer fci = new ForwardChainingRDFSInferencer(
    ms);
  myRepository = new SailRepository(fci);
  myRepository.initialize();
  System.out.println(myRepository);
  con = myRepository.getConnection();

  File file = new File("rdf_files/news.n3");
  con.add(file, null, RDFFormat.N3); //the news.n3 file doesn't require the 'baseURI'. (?)

  String sparql_query = "PREFIX kb: <http://knowledgebooks.com/ontology#> SELECT ?subject ?object WHERE { ?subject kb:containsPlace \"Japan\" . }";

  TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL,
    sparql_query);
  TupleQueryResult result = tupleQuery.evaluate();
  
  try {
   List<String> bindingNames = result.getBindingNames();
   while (result.hasNext()) {
    BindingSet bindingSet = result.next();
    int size2 = bindingSet.size();
    for (int i = 0; i < size2; i++) {
     String val = bindingSet.getValue(bindingNames.get(i))
       .stringValue();
     System.out.print('\t');
     System.out.print(val);
    }
    System.out.print('\n');
   }
  } finally {
   result.close();
  }
 }

}

ここまでのプロジェクトの状態(Package Explorer)


結果(最初の数行)
 http://news.yahoo.com/s/nm/20080616/ts_nm/usa_flooding_dc_16 / Burlington
 http://news.yahoo.com/s/nm/20080616/ts_nm/usa_flooding_dc_16 / Denver
 http://news.yahoo.com/s/nm/20080616/ts_nm/usa_flooding_dc_16 / St. Paul
 http://news.yahoo.com/s/nm/20080616/ts_nm/usa_flooding_dc_16 / Chicago
 http://news.yahoo.com/s/nm/20080616/ts_nm/usa_flooding_dc_16 / Quincy
…

news.n3でなく,rdfs.ntとnews.ntをそれぞれ読み込んむ場合は上記コードの赤色部分を以下のように変更する.
(rdfs.nt, news.nt内では'knowledgebooks.com/ontology'に関するURI先頭が'http://'でなく'http:://'となっているのでsparql queryも変更)
  File rdfs_file = new File("rdf_files/rdfs.nt");
  con.add(rdfs_file, null, RDFFormat.NTRIPLES); //the rdfs.nt file doesn't require the 'baseURI'. (?)
  File news_file = new File("rdf_files/news.nt"); 
  con.add(news_file, null, RDFFormat.NTRIPLES); //the news.nt file doesn't require the 'baseURI'. (?)

  String sparql_query = "PREFIX kb: <http:://knowledgebooks.com/ontology#> SELECT ?subject ?object WHERE { ?subject kb:containsPlace ?object . }";


openrdf-sesame 2.6.9 's war ( tomcat 6.0.35 / mac os 10.8.2 )

$ unzip openrdf-sesame-2.6.9-sdk.zip
$ unzip apache-tomcat-6.0.35.zip
$ chmod a+x /path/to/apache-tomcat-6.0.35/bin/*.sh
$ cp -p /path/to/openrdf-sesame-2.6.9/war/*.war /path/to/apache-tomcat-6.0.35/webapps/
$ /path/to/apache-tomcat-6.0.35/bin/startup.sh
$ open http://localhost:8080/open-sesame
if you can access the page, you can find following folder.
/Users/[user]/Library/Application Support/Aduna/OpenRDF Sesame/
if you found following folder.
/Users/[user]/Library/Application Support/Aduna/OpenRDF/
you edit the file ( /path/to/apache-tomcat-6.0.35/webapps/open-sesame/WEB-INF/openrdf-http-server-servlet.xml ). the 'applicationId','longName' properties which are in the 'adunaAppConfig' bean element are modified from 'OpenRDF Sesame' to 'OpenRDF_Sesame'. and restart the server.
$ open http://localhost:8080/openrdf-workbench