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フォルダにあるの全部.

0 件のコメント: