Reputation: 15
I want to query MESH Ontology, it contains more than 281776 class. I want to have for example all the classes related to the word "dentistry" How should I write the query with java Jena? This is the form of the data in the ontology
<!-- http://bioonto.de/mesh.owl#D003813 -->
<owl:Class rdf:about="http://bioonto.de/mesh.owl#D003813">
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Dentistry</rdfs:label>
<rdfs:subClassOf rdf:resource="http://bioonto.de/mesh.owl#E06"/>
<rdfs:subClassOf rdf:resource="http://bioonto.de/mesh.owl#H02.163"/>
</owl:Class>
<!-- http://bioonto.de/mesh.owl#D003814 -->
<owl:Class rdf:about="http://bioonto.de/mesh.owl#D003814">
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Dentistry, Operative</rdfs:label>
<rdfs:subClassOf rdf:resource="http://bioonto.de/mesh.owl#E06.323"/>
<rdfs:subClassOf rdf:resource="http://bioonto.de/mesh.owl#H02.163.180"/>
</owl:Class>
<!-- http://bioonto.de/mesh.owl#D003815 -->
<owl:Class rdf:about="http://bioonto.de/mesh.owl#D003815">
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Dentists</rdfs:label>
<rdfs:subClassOf rdf:resource="http://bioonto.de/mesh.owl#M01.526.485.330"/>
<rdfs:subClassOf rdf:resource="http://bioonto.de/mesh.owl#N02.360.330"/>
</owl:Class>
Upvotes: 0
Views: 52
Reputation: 1531
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import java.io.InputStream;
public class Test {
// absolute path to your owl file
static final String inputFileName = "path and file name";
public static void main (String [] args) {
// creating ontology model without reasoner specification
OntModel model = ModelFactory.createOntologyModel();
// opening input owl file
InputStream in = FileManager.get().open(inputFileName);
// reading input owl file
model.read(in, "");
// getting all classes
ExtendedIterator classes = model.listClasses();
//iterating classes
while (classes.hasNext()) {
OntClass cls = (OntClass) classes.next();
// getting local class name - without prefix
String className = cls.getLocalName();
// case sensitive string containment check
if (className.contains("dentistry"));
System.out.print(className + "\n");
}
}
}
Upvotes: 1