2016-05-06 34 views
0

我正在嘗試爲項目加載一些本體。現在我使用bioportal的本體,其中一些非常重,200 mb或400 mb,並且java需要花費大量的時間來加載它們。如何使用jena快速加載ontolgy?

我不知道我是否以正確的方式加載文件或有任何解決方法,使加載時間更小。

這是我的代碼,我在那裏裝載本體的一部分:

System.out.println("loading the model..."); 
    model = ModelFactory.createOntologyModel(OntModelSpec.OWL_LITE_MEM); 
    listPaths = new LinkedList(); 
    //sc = new Scanner(System.in); 

    try{ 

     if(args[0].equals("0")){ 

      InputStream in = new FileInputStream("ontologies/camera.owl"); 
      model.read(in, null); 
      NS = NS0; 
      in.close(); 

     }else if(args[0].equals("1")){ 

      InputStream in = new FileInputStream("ontologies/NCITNCBO.owl"); 
      model.read(in, null); 
      NS = NS1; 
      //in.close(); 

     }else if(args[0].equals("2")){ 

      InputStream in = new FileInputStream("ontologies/HL7.ttl"); 
      model.read(in,null, "TTL"); 
      NS = "http://purl.bioontology.org/ontology/HL7/"; 
      in.close(); 

     }else if(args[0].equals("3")){ 

      InputStream in = new FileInputStream("ontologies/LOINC.ttl"); 
      model.read(in,null, "TTL"); 
      NS = "http://purl.bioontology.org/ontology/LNC/"; 
      in.close(); 

     } 
    }catch(Exception e){ 
     System.out.println("erro: " + e.getMessage()); 
    } 
    System.out.println("Ontology loaded!"); 

    //some info about the ontology 
    int maxChildcount = 0; 
    int numClasses = 0; 
    int numChilds = 0; 
    int numChildsClass = 0; 
    Iterator i = model.listClasses(); 

    while(i.hasNext()){ 
     numClasses++; 
     OntClass c = (OntClass) i.next(); 

     //get num childs 
     if(c.hasSubClass()){ 
      Iterator i2 = c.listSubClasses(); 
        while(i2.hasNext()){ 
         i2.next(); 
         numChilds++; 
         numChildsClass++; 

        } 

      if(numChildsClass > maxChildcount) 
       maxChildcount = numChildsClass; 
     } 

     numChildsClass = 0; 
    } 

    System.out.println("NumChilds: " + numChilds + " MaxChildCLass: " + maxChildcount + " numClasses: " + numClasses); 
    System.out.println("Paths for class: " + NS + args[1]); 

回答

0

使用FileInputStream圍繞BufferedInputStream,以確保內容適當緩衝。另外,請不要在第二個電話中註釋掉in.close()