2013-10-23 11 views
1

我有一個RDF本體大約是20MB。我試圖添加個人,如下面的代碼。添加更多的個人現有的RDF本體

FileManager.get().addLocatorClassLoader(RDFWriter.class.getClassLoader()); 
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF); 
model.read("Ontology/LocationOntology_New2.owl"); 
String preFix = "LocationOntology_New.owl#"; 
OntClass Region = model.getOntClass(preFix+"Region"); 
Individual sabara = model.createIndividual(preFix+"Sabaragamuwa",Region); 
try { 
    PrintStream p = new PrintStream("Ontology/LocationOntology_New2.owl"); 
    model.write(p,null); 
    p.close(); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} 

但是,這段代碼花費了很多時間將模型寫回加載的文件。它似乎從頭開始寫入所有內容(不更新現有文件)。有沒有人知道如何解決這個問題?

+0

這將是任何類型的系統,你需要閱讀整個文件,以建立一個內存資源的情況。但是,如果您將數據存儲在N三元組中,但每行只有一個三元組,則可以簡單地使用新信息創建一個_new_模型,然後_append_將其添加到現有文件中。它仍然會很好地形成,而且會很快。這也可以正常工作,因爲在你的例子中,你不會使用現有模型中的任何數據。你只是創建新的三元組。 –

回答

1

我不認爲這是可以解決的。那意味着耶拿必須決定你做了什麼樣的改變。事實上,如果您只添加了新的實例,那麼將它們附加到文件就足夠了。但是,您也可能會進行更改,例如爲某個類添加超類,然後必須更新此類定義。

+0

什麼ü想想SPARQL更新,我發現這個文件好像我們可以用SPARQL更新現有的RDF文檔http://www.w3.org/Submission/SPARQL-Update/ – Proceso

+0

隨着SPARQL更新,你可以更新RDF圖,這與文檔不一樣。我不太瞭解Jena,他們是否已經實施了一種方法來決定如何更新文檔以反映對其做出的更改。 – Rhand

+2

不,我們還沒有和你建議它的文本文檔格式基本上是不可能的,因爲有針對任何給定的模型 – RobV

1

雖然我RobV's point同意,在一般情況下,這是很難做到的,如果你在OWL工作(而不是普通的RDF),你可以這一點,如果您的OWL本體序列化爲RDF是然後在N-Triples中序列化。下面的代碼(帶註釋)顯示了你如何做到這一點。

的這裏的想法是,如果你只需要添加新的內容,如果你使用的是把每行一個RDF三元,格式,那麼你可以在新的三元組簡單地附加在沒有任何麻煩的內容。我展示的第一個模型就像磁盤上的本體模型。在這裏,我只創建它來表明本體中的類聲明使用一個三元組,Region a owl:Class。儘管區域由IRI識別,並且只要您知道它的IRI,就不需要整個本體論來指代資源。在模型,您可以創建類型區的個體,你可以簡單地認爲模型的三元組附加到磁盤上的文件。

import com.hp.hpl.jena.ontology.Individual; 
import com.hp.hpl.jena.ontology.OntClass; 
import com.hp.hpl.jena.ontology.OntModel; 
import com.hp.hpl.jena.ontology.OntModelSpec; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 

public class IncrementalOWLUpdates { 
    public static void main(String[] args) { 
     final String NS = "http://example.org/"; 

     // This is like the model on disk, and contains the class declaration 
     // that you wouldn't want to write out each time. 
     System.out.println("=== content of ontology on disk ==="); 
     final OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); 
     final OntClass Region = model.createClass(NS+"Region"); 
     model.write(System.out, "N-Triples"); 

     // This is the new model that you would build to contain the new triples 
     // that you want to add to the original model. Note that it _doesn't_ 
     // contain the class declaration, but only the new triples about the 
     // new individual. If you open the original ontology file and append this 
     // output, you've updated the ontology without reading it all into memory. 
     System.out.println("=== new content to append ==="); 
     final OntModel update = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); 
     final Individual newHampshire = update.createIndividual(NS+"NewHampshire", Region); 
     newHampshire.addLabel("New Hampshire", "en"); 
     update.write(System.out, "N-Triples"); 
    } 
} 
=== content of ontology on disk === 
<http://example.org/Region> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> . 
=== new content to append === 
<http://example.org/NewHampshire> <http://www.w3.org/2000/01/rdf-schema#label> "New Hampshire"@en . 
<http://example.org/NewHampshire> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Region> . 
+0

對於這個特殊問題非常好的解決方案。如果需要,您甚至可以通過引用模型來使用原始文件的一部分。 – Rhand

相關問題