Here is my solution. I just printed the lines you are asking for, but I hope you can see how to use Jena OntAPI to go through the ontology graph and select the things you are interested in.
package examples; import java.util.Iterator; import com.hp.hpl.jena.ontology.*; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Resource; public class PizzaExample { public static String BASE = "http://www.co-ode.org/ontologies/pizza/pizza.owl"; public static String NS = BASE + "#"; public static void main( String[] args ) { new PizzaExample().run(); } public void run() { OntModel m = getPizzaOntology(); OntClass american = m.getOntClass( NS + "American" ); for (Iterator<OntClass> supers = american.listSuperClasses(); supers.hasNext(); ) { displayType( supers.next() ); } } protected OntModel getPizzaOntology() { OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); m.read( BASE ); return m; } protected void displayType( OntClass sup ) { if (sup.isRestriction()) { displayRestriction( sup.asRestriction() ); } } protected void displayRestriction( Restriction sup ) { if (sup.isAllValuesFromRestriction()) { displayRestriction( "all", sup.getOnProperty(), sup.asAllValuesFromRestriction().getAllValuesFrom() ); } else if (sup.isSomeValuesFromRestriction()) { displayRestriction( "some", sup.getOnProperty(), sup.asSomeValuesFromRestriction().getSomeValuesFrom() ); } } protected void displayRestriction( String qualifier, OntProperty onP, Resource constraint ) { String out = String.format( "%s %s %s", qualifier, renderURI( onP ), renderConstraint( constraint ) ); System.out.println( "american pizza: " + out ); } protected Object renderConstraint( Resource constraint ) { if (constraint.canAs( UnionClass.class )) { UnionClass uc = constraint.as( UnionClass.class );
Which produces the following output:
american pizza: some pizza:hasTopping pizza:MozzarellaTopping american pizza: some pizza:hasTopping pizza:PeperoniSausageTopping american pizza: some pizza:hasTopping pizza:TomatoTopping american pizza: all pizza:hasTopping union{ pizza:MozzarellaTopping pizza:TomatoTopping pizza:PeperoniSausageTopping}
source share