第7章 RDFグラフの推論

目次

Jena OntModel APIの使用方法
SPARQLコンストラクトの使用
外部推論システムとJena APIとの併用

RDFグラフ機能では、Apache Jena OntModel APIを通じてRDFスキーマ(RDFS)とWeb Ontology Language (OWL)の推論がサポートされています。PelletやTrOWLなどメモリーベースのサード・パーティ製推論システムをサポートする機能もあります。

Jena OntModel APIの使用方法

Apache Jenaには、ReasonerReasonerFactoryInfModelOntModelSpec、OntModelなど各種のJava APIが用意されています。詳細は、http://jena.apache.org/documentation/inference/index.htmlを参照してください。次の例では、OWL_MEM_RULE_INFを使用してOracleModelNoSqlインスタンス上にOntologyModelを構築する方法について説明します。推論結果はインメモリーのJena Modelに追加されます。

import java.io.PrintStream;
import java.util.Iterator;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec; 
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.graph.*; 
import oracle.rdf.kv.client.jena.*;

public class ExampleOntModel
{

  public static void main(String[] szArgs) throws Exception
  {

PrintStream psOut = System.out;

psOut.println("start");
String szStoreName = szArgs[0];
String szHostName  = szArgs[1];
String szHostPort  = szArgs[2];

// Create a connection to the Oracle NoSQL Database
OracleNoSqlConnection conn
                  = OracleNoSqlConnection.createInstance(szStoreName,
                                                         szHostName, 
                                                         szHostPort);

// Create an OracleGraphNoSql object to handle the default graph
// and use it to create a Jena Model object.
Node graphNode = Node.createURI("http://example.org/graph1");
OracleGraphNoSql graph = new OracleNamedGraphNoSql(graphNode, conn);
Model model = 
      OracleModelNoSql.createOracleModelNoSql(graphNode, conn);

// Clear model
model.removeAll();

Node sub = Node.createURI("http://sub/a");
Node pred = Node.createURI("http://pred/a");
Node obj = Node.createURI("http://obj/a");

// Add few axioms

Triple triple = Triple.create(sub, pred, obj);
graph.add(triple);

graph.add(Triple.create(pred, 
    Node.createURI("http://www.w3.org/2000/01/rdf-schema#domain"),
    Node.createURI("http://C")));

graph.add(Triple.create(pred, 
   Node.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
   Node.createURI("http://www.w3.org/2002/07/owl#ObjectProperty")));

{
      // read it out
      Iterator it = GraphUtil.findAll(graph);
      
      while (it.hasNext()) {
        psOut.println("triple " + it.next().toString());
      }
    }

// Create an OntModel instance
OntModel om = 
      ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF,
                                       model);

Model modelInMem = ModelFactory.createDefaultModel();
modelInMem.add(om);

    {
      Iterator it = GraphUtil.findAll(modelInMem.getGraph());
      while (it.hasNext()) {
                psOut.println("triple from OntModel " + 
                it.next().toString());
      }
    }

model.close();
conn.close();
  }
}

上の例では、出力から次のトリプルが見つかります。推論によって、個々のhttp://sub/aが正しく分類されます。

triple from OntModel http://sub/a @owl:sameAs http://sub/a
triple from OntModel http://sub/a @rdf:type rdfs:Resource
triple from OntModel http://sub/a @rdf:type owl:Thing
triple from OntModel http://sub/a @rdf:type http://C
triple from OntModel http://sub/a @http://pred/a http://obj/a

もちろん、InferredNamedGraphNoSqlオブジェクトを作成して、OntModelからそこに内容を追加することもできます。InferredNamedGraphNoSqlクラスを使用して推論トリプルを格納する方法については、「Example1c.java: 推論されたグラフの作成とトリプルの追加/削除」「Example1d.java: 推論されたグラフの作成とトリプルの追加/削除」を参照してください。