Jena Notes and the Happy Cat Fiasco
this should probably be under a separate JENA sub-heading... change later....
Jena with a reasoner
First, go to the Jena inference support page and read. Now, here are the gotchas: 1) Reasoning happens again in Jena whenever a change is made to the OntModel; 2) Reasoning doesn't actually happen until the OntModel is queried (e.g. listRDFTypes is called) or you use the OntModel.prepare() method; 3) Reasoning is often very slow and very memory intensive; 4) Adding instances to your OntModel ~can render it unsatisfiable! - depending on how you define them; 5) If you need a bunch of instances classified and don't care to find out whether they are equivalent to one another, it sometimes makes sense to process one instance at a time rather than simply dumping them all into the same model at once. So, for each instance: a) create a new reasoning model; b) load whatever ontology you want to use; c) load the instance; d) record the computed types; e) remove all statements from the model and go back to b).
Notes from the Happy Cat fiasco..
In current versions of Pellet and Jena, Jena's very nice listRDFTypes feature has a bit of a querk in it. If one of the types for your instance has an equivalent class, this logically should be added as another type for your instance, but this won't happen automatically. I guess its redundant - sort of.. But, in case you want to ~really list all of the inferred types, the following will do the trick:
package org.icapture.ontology.query;
import java.util.Iterator;
import org.mindswap.pellet.jena.PelletReasonerFactory;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
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.rdf.model.Resource;
public class DLplay {
/**
*@param args
*/
public static void main(String[] args) {
OntModelSpec init = new OntModelSpec(PelletReasonerFactory.THE_SPEC);
OntModel p = ModelFactory.createOntologyModel(init);
p.read("file:///Users/benjamgo/ontologies/HappyCat.owl");
for(Iterator i = p.listIndividuals();i.hasNext();){
Individual ind = (Individual)i.next();
System.out.println("Individual: "+ind.getLocalName()+" has the following types:");
for(Iterator t = ind.listRDFTypes(false); t.hasNext();){
Resource r = (Resource)t.next();
if(r.canAs(OntClass.class)){
OntClass c = (OntClass)r.as(OntClass.class);
System.out.println(prettyPrintClass(c));
for(Iterator eqs = c.listEquivalentClasses(); eqs.hasNext();){
OntClass eq_c = (OntClass)eqs.next();
if(!c.equals(eq_c)){
System.out.println(prettyPrintClass(eq_c));
}
}
}
}
}
}
public static String prettyPrintClass(OntClass c){
String pretty = c.getLocalName();
if(c.isRestriction()){
pretty = "restriction on property: "+c.asRestriction().getOnProperty();
} return pretty;
}
}
- Login to post comments
