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