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 . }";


0 件のコメント: