Reputation: 2060
public static void main(String[] args) {
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
String fileName = "C:/Users/Felipe/Desktop/workspace/JenaTutorial/ontrdf.rdf";
try {
InputStream inputStream = new FileInputStream(fileName);
model.read(inputStream, "RDF/XML");
inputStream.close();
} catch (Exception e) {
System.out.println(e.getClass());
System.out.println(e.getMessage());
}
ExtendedIterator<OntClass> it = model.listClasses();
while (itI.hasNext()) {
OntClass ontclass = it.next();
System.out.println(ontclass.getLocalName());
}
}
I want to list only the real six classes that I inserted on my ontology (Year, Publisher, Language, Country, Book, and Author). I don't know what all those lines after the first six means. Does someone know how I can filter the output?
The console shows:
Year
Publisher
Language
Country
Book
Author
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
Exception in thread "main" java.util.NoSuchElementException
at com.hp.hpl.jena.util.iterator.FilterIterator.next(FilterIterator.java:92)
at tutorial.HelloRDFWorld.main(HelloRDFWorld.java:35)
Upvotes: 2
Views: 637
Reputation: 85923
If you update your code to print the class, rather than its local name, you'll get slightly more useful results:
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Year
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Publisher
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Language
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Country
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Book
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Author
560855f:146ffa04e87:-7fe2
560855f:146ffa04e87:-7ff9
560855f:146ffa04e87:-7ff7
…
Those values at the end (truncated) are blank node identifiers. The ontology has lots of anonymous restriction classes in it. E.g.,
<Class rdf:about="http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Author">
<rdfs:subClassOf>
<Restriction>
<onProperty rdf:resource="http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#authorHasAuthorshipOfBook"/>
<someValuesFrom rdf:resource="http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Book"/>
</Restriction>
</rdfs:subClassOf>
…
This is the axiom
Author subClassOf (authorHasAuthorshipOfBook some Book)
The important thing to recognize is that (authorHasAuthorshipOfBook some Book)
is a class. It's the class of things that have authorship of a Book. It can have instances, superclasses, subclasses, etc., just like any other class. As such, it appears in listClasses. You can get the results that you want if you only print those that are URI resources:
while ( it.hasNext() ) {
OntClass klass = it.next();
if ( klass.isURIResource() ) {
System.out.println( klass );
}
}
Of course, Jena's extended iterators let you filter, so you can actually keep the same iteration code, but use a slightly different iterator:
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.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.Filter;
public class ListOntClassesExample {
public static void main(String[] args) {
OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
model.read( "http://pastebin.com/raw.php?i=MaeEbaux" );
ExtendedIterator<OntClass> it = model.listClasses().filterKeep( new Filter<OntClass>() {
@Override
public boolean accept(OntClass o) {
return o.isURIResource();
}
});
while ( it.hasNext() ) {
OntClass klass = it.next();
System.out.println( klass );
}
}
}
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Year
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Publisher
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Language
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Country
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Book
http://www.semanticweb.org/ikop/ontologies/untitled-ontology-46#Author
Upvotes: 5