在不知道的情况下查询本体 类
Querying an ontologie without knowing classes
我想查询MESHOntology,它包含超过281776个class。
例如,我想要所有与单词 "dentistry" 相关的 classes
我应该如何使用 java Jena 编写查询?
这是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>
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");
}
}
}
我想查询MESHOntology,它包含超过281776个class。 例如,我想要所有与单词 "dentistry" 相关的 classes 我应该如何使用 java Jena 编写查询? 这是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>
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");
}
}
}