Library Version 12.1.3.0

Chapter 7. Inference on an RDF Graph
Prev |  | Next

## Chapter 7. Inference on an RDF Graph

The RDF Graph feature supports RDF Schema (RDFS) and Web Ontology Language (OWL) inference through Apache Jena OntModel APIs. It also has the ability to support other memory-based third party reasoners, such as Pellet and TrOWL.

## Use Jena OntModel APIs

Apache Jena provides a set of Java APIs including `Reasoner`, `ReasonerFactory`, `InfModel`, `OntModelSpec`, OntModel and more. Refer to http://jena.apache.org/documentation/inference/index.html for details. The following example describes how to use `OWL_MEM_RULE_INF` to build an OntologyModel on top of an OracleModelNoSql instance. The inference results are added to an in-memory 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();
      }
    }

For the above example, one can find the following triples from the output. The inference produces a correct classification of individual 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

One can of course create an InferredNamedGraphNoSql object and add the contents from the OntModel into it. Furher details on storing inference triples using InferredNamedGraphNoSql class can be found in Example1c.java: Create an inferred graph and add/delete triples and Example1d.java: Create an inferred graph and add/delete triples.

Prev |  | Next
Chapter 6. Update an RDF Graph | Home | Use SPARQL Construct