Thanks Lorenz I figured out the union thing too so I ended up using the below SPARQL instead and it seems to work fine but would love to know what is the equivalent using Jena API
"SELECT DISTINCT ?s WHERE { " + "?s a owl:Class . " + "FILTER (!isBlank(?s)) " + "FILTER (?s != owl:Thing && ?s != owl:Nothing) . " + "OPTIONAL { " + " ?s rdfs:subClassOf ?super . " + " FILTER (?super != rdfs:Resource && ?super != owl:Thing && ?s != ?super) " + " } . " + "FILTER (!bound(?super))" + "}"); Regards On Thu, Sep 5, 2019 at 8:00 PM Lorenz Buehmann < buehm...@informatik.uni-leipzig.de> wrote: > schema.org contains a bunch of anonymous classes like the union of other > classes which are used as domain or range of a property, that's why you > get null values because they do not have a URI. If you'd just call > > |System.out.println(clazz);| > > > you'd see the blank node Ids. > > > Among all those blank nodes, there is indeed the one top level class > http://schema.org/Thing . > > > I thought just adding a filter on the iterator is enough, i.e. > > |topClazzez = topClazzez.filterDrop(OntResource::isAnon);| > > > but while it doesn't return the blank nodes anymore, it leads to an > exception: > > > |Exception in thread "main" > org.apache.jena.ontology.ConversionException: Cannot convert node > http://www.w3.org/2000/01/rdf-schema#Class to OntClass: it does not have > rdf:type owl:Class or equivalent|| > || at > org.apache.jena.ontology.impl.OntClassImpl$1.wrap(OntClassImpl.java:82)|| > || at org.apache.jena.enhanced.EnhNode.convertTo(EnhNode.java:152)|| > || at org.apache.jena.enhanced.EnhNode.convertTo(EnhNode.java:31)|| > || at > org.apache.jena.enhanced.Polymorphic.asInternal(Polymorphic.java:62)|| > || at org.apache.jena.enhanced.EnhNode.as(EnhNode.java:107)|| > || at > > org.apache.jena.ontology.impl.OntResourceImpl.lambda$listDirectPropertyValues$8(OntResourceImpl.java:1536)|| > || at > org.apache.jena.util.iterator.Map1Iterator.next(Map1Iterator.java:46)|| > || at > > org.apache.jena.ontology.impl.OntResourceImpl.computeDirectValues(OntResourceImpl.java:1580)|| > || at > > org.apache.jena.ontology.impl.OntResourceImpl.listDirectPropertyValues(OntResourceImpl.java:1553)|| > || at > > org.apache.jena.ontology.impl.OntClassImpl.listSuperClasses(OntClassImpl.java:180)|| > || at > > org.apache.jena.ontology.impl.OntClassImpl.isHierarchyRoot(OntClassImpl.java:739)|| > || at > > org.apache.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:56)|| > || at > > org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)|| > || at > > org.apache.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:55)|| > || at com.company.Main.main(Main.java:25)| > > > > On 05.09.19 03:11, Alex To wrote: > > Hi I am trying to load schema.org ontology and get all top classes > > using ontModel.listHierarchyRootClasses() but can't get the expected > > results with different OntModelSpec. > > > > If I use OWL_MEM, it lists 2500+ records with all the records have "null" > > URI. > > If I use OWL_DL_MEM, it lists 0 records > > > > The code is very simple as follows > > > > Model model = ModelFactory.createDefaultModel(); > > model.read("https://schema.org/docs/schemaorg.owl"); > > OntModel ontModel = > > ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, model); > > ExtendedIterator<OntClass> topClazzez = > ontModel.listHierarchyRootClasses(); > > while (topClazzez.hasNext()) { > > OntClass clazz = topClazzez.next(); > > System.out.println(clazz.getURI()); > > } > > > > A minimal Maven project ready to run to demonstrate my problem is here > > https://github.com/AlexTo/jena-lab (have a look at the Main.java) > > > > Thanks a lot > --