Get OWL restrictions for classes using Jena

Using the ontology for pizza , I want you to find all the toppings for American pizza. If I open the ontology at ProtΓ©gΓ©, I see that American pizza has the following limitations:

 hasTopping some MozerellaTopping hasTopping some TomatoTopping 

How can I get the same information through Jena?

+6
source share
2 answers

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 { /***********************************/ /* Constants */ /***********************************/ public static String BASE = "http://www.co-ode.org/ontologies/pizza/pizza.owl"; public static String NS = BASE + "#"; /***********************************/ /* External signature methods */ /***********************************/ 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() ); } } /***********************************/ /* Internal implementation methods */ /***********************************/ 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 ); // this would be so much easier in ruby ... String r = "union{ "; for (Iterator<? extends OntClass> i = uc.listOperands(); i.hasNext(); ) { r = r + " " + renderURI( i.next() ); } return r + "}"; } else { return renderURI( constraint ); } } protected Object renderURI( Resource onP ) { String qName = onP.getModel().qnameFor( onP.getURI() ); return qName == null ? onP.getLocalName() : qName; } } 

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} 
+9
source

Now (with Apache-jena 3.xx) there is one possible problem with org.apache.jena.ontology.OntModel and pizza.owl is that the Jena-Ont-API only supports OWL-1, and pizza - OWL-2 ontologies . Although this does not matter in the above example (Existential Quantification restrictions look the same for OWL1 and OWL2), in general, you cannot use OntModel to process an ontology as easily. As an option, there is an alternative to OntModel called ru.avicomp.ontapi.jena.model.OntGraphModel from ont-api . It is based on the same principles as jena OntModel. Perhaps this would be useful for someone.

Usage example (getting only objects-some-values-from constraints):

  String uri = "http://www.co-ode.org/ontologies/pizza/pizza.owl"; String ns = uri + "#"; OntGraphModel m = ru.avicomp.ontapi.jena.OntModelFactory.createModel(); try (InputStream in = ExamplePizza.class.getResourceAsStream("/pizza.ttl")) { m.read(in, uri, "ttl"); } ru.avicomp.ontapi.jena.model.OntClass clazz = m.getOntEntity(OntClass.class, ns + "American"); OntNOP prop = m.getOntEntity(OntNOP.class, ns + "hasTopping"); clazz.subClassOf() .filter(c -> c.canAs(OntCE.ObjectSomeValuesFrom.class)) .map(c -> c.as(OntCE.ObjectSomeValuesFrom.class)) .filter(c -> prop.equals(c.getOnProperty())) .map(OntCE.Value::getValue) .forEach(System.out::println); 

Of:

 http://www.co-ode.org/ontologies/pizza/pizza.owl#PeperoniSausageTopping(OntClass) http://www.co-ode.org/ontologies/pizza/pizza.owl#TomatoTopping(OntClass) http://www.co-ode.org/ontologies/pizza/pizza.owl#MozzarellaTopping(OntClass) 
0
source

Source: https://habr.com/ru/post/899377/


All Articles