Jena OntModel APIを使用したオントロジ・モデルの構築

この例では、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();
  }
}

この例をコンパイルして実行するコマンドと、想定される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 Example17.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 Example17 <store_name> \
<host_name> <host_port>

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