[JAVA] About Apache Inference API

Introduction

Following the Ontology API in Previous article, this is an explanation of the Inference API. Jena is characterized by the handling of models by the Ontology API and the provision of Inference, Reasoning in separate modules. Reasoners that can be handled by Jena are as follows. Other than that, Pellet is famous and has been featured in papers, but it seems that it was absorbed by Stardog's products.

  1. ** Transitive reasoner **: Accept only Transive / Reflexive properties. Specifically, rdfs: subPropertyOf and rdfs: subClassOf.
  2. ** RDFS rule reasoner **: A configurable subset containing RDFS.
  3. ** OWL, OWL Mini, OWL Micro Reasoners **: As the name implies. For example, a subset of OWL.
  4. ** Generic rule reasoner **: A rule-based Reasoner that users can implement themselves. I don't usually use it.

Reasoner It is a component called Reasoner that makes the inferences. The basic flow is to use an inference model (InfModel) using Reasoner to perform inference operations.

Reasoners are usually generated via factory methods. There seems to be a pre-prepared Reasoner, but if you are using the Ontology API, the pre-generated OntModelSpec seems to fit the appropriate Reasoner. For example, when inferring a simple RDF model using RDFS, ModelFactory.createRDFSModel is used. It looks like the following.


String NS = "urn:x-hp-jena:eg/";

//Make a suitable model
Model rdfsExample = ModelFactory.createDefaultModel();
Property p = rdfsExample.createProperty(NS, "p");
Property q = rdfsExample.createProperty(NS, "q");
rdfsExample.add(p, org.apache.jena.vocabulary.RDFS.subPropertyOf, q);
rdfsExample.createResource(NS+"a").addProperty(p, "foo");

//Inf Model by Reasoner using RDFS
InfModel inf = ModelFactory.createRDFSModel(rdfsExample);

In the above, the predicate rdfs: subPropertyOf is described in the property p, and the target is q. Then inference works and you can get the contents of p (ie foo) by referencing the property q.

Resource a = inf.getResource(NS+"a");
System.out.println("Statement: " + a.getProperty(q));

In addition to the above method, if you want to make various settings for Reasoner in advance, you can also create InfModel by dividing Reasoner as follows.

Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().create(null);
InfModel inf = ModelFactory.createInfModel(reasoner, rdfsExample);

Various Reasoners

The RDFS Reasoner(RDFSRuleReasoner) An inferencer included by default in Jena that supports RDFS. There are three conformance levels, which can be specified at the time of setting. In the example below, it is specified by ReasonerVocabulary.PROPsetRDFSLevel.

Resource config = ModelFactory.createDefaultModel()
                  .createResource()
                  .addProperty(ReasonerVocabulary.PROPsetRDFSLevel, "simple");
Reasoner reasoner = RDFSRuleReasonerFactory.theInstance()Create(config);
  1. ** Full **: Includes all axioms, rules, etc.
  2. ** Default **: Excludes high-load checks. I'm not sure, but it seems that there are no two rules, "everything is a resource" and "everything used as a property is one" (rdf1, rdfs4a, rdfs4b), and this seems to be almost no problem in practice.
  3. ** Simple **: Includes only transitive relations such as subPropertyOf and subClassOf, and range descriptions such as domain and range, and does not include axioms. It seems to be the most convenient, but it is not the default due to incomplete implementation.

The OWL Reasoner Jena provides an implementation of OWL / Lite, which is a subset of OWL / full, and currently it is possible to specify two modes, default and Faster / Smaller, in the settings. I'm not aiming for a complete implementation of OWL / Full, so I asked for another engine such as Pellet, Racer, FaCT. thing. The usage is as follows. The ontology (schema) described in OWL is read, bound to the reasoner, and then the InfModel is created.

Model schema = FileManager.get().loadModel("file:data/owlDemoSchema.owl");
Model data = FileManager.get().loadModel("file:data/owlDemoData.rdf");
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(schema);
InfModel infmodel = ModelFactory.createInfModel(reasoner, data);

That's all for today. With this much information, I wonder if I can make an app that uses ontology inference for the time being ...

reference

Recommended Posts

About Apache Inference API
About Apache Jena Ontology API
Chew about API
Talk about uploading files using slack API with Apache HttpPost
[Introduction to Java] About Stream API
About Gradle's compile, api, implementation, etc.
About =
[Java Siler] About type inference by var
Articles to learn more about Stream API
Build REST API with Apache2 + Passenger + Sinatra.