正如 UninformedUser 的评论中所说,感谢他,您可以执行以下操作:
model.listImportedOntologyURIs()
model.getImportedModel(uri).listClasses()
model.listClasses().filterDrop(importedClasses::contains)
因此,要打印模型的所有类而无需导入类:
import java.util.HashSet;
import java.util.Set;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.iterator.ExtendedIterator;
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
model.read("file:///Users/von/tools/data.owl", "RDF/XML");
Set<OntClass> importedClasses = new HashSet<>();
for (String uri : model.listImportedOntologyURIs()) {
importedClasses.addAll(model.getImportedModel(uri).listClasses().toSet());
}
ExtendedIterator<OntClass> it = model.listClasses().filterDrop(importedClasses::contains);
while (it.hasNext()) {
OntClass cls = it.next();
System.out.println(cls);
}