* exception
*/
@Test
public void shouldUseReasoner() throws Exception {
// Create our ontology manager in the usual way.
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ont = loadPizza(manager);
// We need to create an instance of OWLReasoner. An OWLReasoner provides
// the basic query functionality that we need, for example the ability
// obtain the subclasses of a class etc. To do this we use a reasoner
// factory. Create a reasoner factory. In this case, we will use HermiT,
// but we could also use FaCT++ (http://code.google.com/p/factplusplus/)
// or Pellet(http://clarkparsia.com/pellet) Note that (as of 03 Feb
// 2010) FaCT++ and Pellet OWL API 3.0.0 compatible libraries are
// expected to be available in the near future). For now, we'll use
// HermiT HermiT can be downloaded from http://hermit-reasoner.com Make
// sure you get the HermiT library and add it to your class path. You
// can then instantiate the HermiT reasoner factory: Comment out the
// first line below and uncomment the second line below to instantiate
// the HermiT reasoner factory. You'll also need to import the
// org.semanticweb.HermiT.Reasoner package.
OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
// OWLReasonerFactory reasonerFactory = new Reasoner.ReasonerFactory();
// We'll now create an instance of an OWLReasoner (the implementation
// being provided by HermiT as we're using the HermiT reasoner factory).
// The are two categories of reasoner, Buffering and NonBuffering. In
// our case, we'll create the buffering reasoner, which is the default
// kind of reasoner. We'll also attach a progress monitor to the
// reasoner. To do this we set up a configuration that knows about a
// progress monitor. Create a console progress monitor. This will print
// the reasoner progress out to the console.
// ConsoleProgressMonitor progressMonitor = new
// ConsoleProgressMonitor();
// Specify the progress monitor via a configuration. We could also
// specify other setup parameters in the configuration, and different
// reasoners may accept their own defined parameters this way.
// OWLReasonerConfiguration config = new SimpleConfiguration(
// progressMonitor);
// Create a reasoner that will reason over our ontology and its imports
// closure. Pass in the configuration.
// OWLReasoner reasoner = reasonerFactory.createReasoner(ont, config);
OWLReasoner reasoner = reasonerFactory.createReasoner(ont);
// Ask the reasoner to do all the necessary work now
reasoner.precomputeInferences();
// We can determine if the ontology is actually consistent (in this
// case, it should be).
boolean consistent = reasoner.isConsistent();
// System.out.println("Consistent: " + consistent);
// We can easily get a list of unsatisfiable classes. (A class is
// unsatisfiable if it can't possibly have any instances). Note that the
// getUnsatisfiableClasses method is really just a convenience method
// for obtaining the classes that are equivalent to owl:Nothing. In our
// case there should be just one unsatisfiable class - "mad_cow" We ask
// the reasoner for the unsatisfiable classes, which returns the bottom
// node in the class hierarchy (an unsatisfiable class is a subclass of
// every class).
Node<OWLClass> bottomNode = reasoner.getUnsatisfiableClasses();
// This node contains owl:Nothing and all the classes that are
// equivalent to owl:Nothing - i.e. the unsatisfiable classes. We just
// want to print out the unsatisfiable classes excluding owl:Nothing,
// and we can used a convenience method on the node to get these
Set<OWLClass> unsatisfiable = bottomNode.getEntitiesMinusBottom();
if (!unsatisfiable.isEmpty()) {
// System.out.println("The following classes are unsatisfiable: ");
for (OWLClass cls : unsatisfiable) {
// System.out.println(" " + cls);
}
} else {
// System.out.println("There are no unsatisfiable classes");
}
// Now we want to query the reasoner for all descendants of vegetarian.
// Vegetarians are defined in the ontology to be animals that don't eat
// animals or parts of animals.
OWLDataFactory fac = manager.getOWLDataFactory();
// Get a reference to the vegetarian class so that we can as the
// reasoner about it. The full IRI of this class happens to be:
// <http://owl.man.ac.uk/2005/07/sssw/people#vegetarian>
OWLClass vegPizza = fac.getOWLClass(IRI
.create("http://owl.man.ac.uk/2005/07/sssw/people#vegetarian"));