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.
rdfs: subPropertyOf
and rdfs: subClassOf
.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);
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);
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 ...
Recommended Posts