For Apache Jena 3.x.x (and also for 2.x) there is one potential problem related to the org.apache.jena.ontology.OntModel
and pizza.owl
: Jena supports only OWL1, but pizza is OWL2 ontology.
Ans, although, for the example above it doesn't matter ('Existential Quantification' restrictions looks identically both for OWL1 and OWL2, and Jena API can process it),
in general case you can't use org.apache.jena.ontology.OntModel
for processing ontology just as easily.
As an option there is an alternative named com.github.owlcs.ontapi.jena.model.OntModel
from ONT-API.
This model is based on the same principles as a Jena OntModel
, but it is strongly for OWL2 (and, also, it is not InfModel
- at time of writing).
Consider an example of usage (object-some-values-from restrictions for a class):
String web = "https://raw.githubusercontent.com/owlcs/ont-api/master/src/test/resources/ontapi/pizza.ttl";
// create an OWL2 RDF-view (Jena Model):
OntModel m = com.github.owlcs.ontapi.jena.OntModelFactory.createModel();
// load pizza ontology from web
m.read(web, "ttl");
// find class and property
OntClass clazz = m.getOntClass(m.expandPrefix(":American"));
OntObjectProperty prop = m.getObjectProperty(m.expandPrefix(":hasTopping"));
// list and print all some values from restrictions with desired property
clazz.superClasses()
.filter(c -> c.canAs(OntClass.ObjectSomeValuesFrom.class))
.map(c -> c.as(OntClass.ObjectSomeValuesFrom.class))
.filter(c -> prop.equals(c.getProperty()))
.map(x -> x.getValue())
.forEach(System.out::println);
The output:
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)