2017-05-30 52 views
0

我想查詢MESH本體,它包含281776多個類。 我想舉例說明與「牙科」相關的所有類 如何使用java Jena編寫查詢? 這是本體數據的形式查詢不知道類的ontologie

<!-- 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> 

回答

1
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");    

       } 
     } 

    } 
+0

謝謝你非常有用 –