How to do reasoning/inference based on Jena

Just realize how to do real reasoning by Jena.

 

Before I just used Jena to query the statments expressed explicitly in ontology. That works, but is so simple, and cannot implement the real reasoning. It just works as a dictionary where no entailment knowledge can be infered.

 

I realize this problem when I set some instances as Symmetric and Transitive. At that time, the old code cannot make infer. I thought it could be, but in fact, not.

 

Now, I had a look and add reasoner into the code, and it can make a rough reasoning now!

(Reasoner reasoner = ReasonerRegistry.getOWLReasoner(); I just use this to create a reasoner, no further restriction. Althugh it is still a rough reasoning which returns all the entailments, a little too big and make the search space too big. Need more improvement.)

 

This article from http://www.ibm.com/developerworks/xml/library/j-jena/ helps most. I copy the last paragraph I used as follows:

 

=========================================

 

Inference with Jena

Given an ontology and a model, Jena's inference engine can derive additional statements that the model doesn't express explicitly. Jena provides several Reasoner types to work with different types of ontology. Because you want to use the OWL ontology with the WordNet model, an OWLReasoner is needed.

The following example shows how to apply your OWL WordNet ontology to the WordNet model itself to create an inference model. I'm actually going to use a subset of the WordNet model here, containing only those nouns beneath "plant life" in the hyponym hierarchy. The reason for using only a subset is that the inference model needs to be held in memory, and the WordNet model is too large for an in-memory model to be practical. The code I used to extract the plants model from the full WordNet model is included in the article source, and is named ExtractPlants.java (see Resources).

First, I'll get an OWLReasoner from the ReasonerRegistry. ReasonerRegistry.getOWLReasoner() returns an OWL reasoner in its standard configuration, which is fine for this simple case. The next step is to bind the reasoner to the WordNet ontology. This operation returns a reasoner ready to apply the ontology's rules. Next I'll use the bound reasoner to create an InfModel from the WordNet model.

Having created an inference model from the original data and the OWL ontology, it can be treated just like any other Model instance. Therefore, as Listing 16 shows, the Java code and RDQL query used with a regular Jena model by FindHypernym.java can be re-applied to the inference model without any changes:


Listing 16. Creating and querying an inference model

// Get a reference to the WordNet plants model
ModelMaker maker = ModelFactory.createModelRDBMaker(connection);
Model model = maker.openModel("wordnet-plants",true);

// Create an OWL reasoner 
Reasoner owlReasoner = ReasonerRegistry.getOWLReasoner();

// Bind the reasoner to the WordNet ontology model
Reasoner wnReasoner = owlReasoner.bindSchema(wnOntology);

// Use the reasoner to create an inference model
InfModel infModel = ModelFactory.createInfModel(wnReasoner, model);

// Set the inference model as the source of the query
query.setSource(infModel);

// Execute the query as normal
QueryEngine qe = new QueryEngine(query);
QueryResults results = qe.exec(initialBinding);

The full listing is available in the article source, named FindInferredHypernyms.java. Listing 17 shows what happens when the inference model is queried for hypernyms of "wisteria":


Listing 17. Running the example FindInferredHypernyms program

$ java FindInferredHypernyms wisteria

Hypernyms found for 'wisteria':

vine:   weak-stemmed plant that derives support from climbing, twining, or creeping along a surface
tracheophyte:   green plant having a vascular system: ferns, gymnosperms, angiosperms
vascular plant: green plant having a vascular system: ferns, gymnosperms, angiosperms
plant life:     a living organism lacking the power of locomotion
flora:  a living organism lacking the power of locomotion
plant:  a living organism lacking the power of locomotion

The information contained in the OWL ontology has allowed Jena to infer that "wisteria" has hypernyms right up through the model.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章