簡體   English   中英

向現有RDF本體中添加更多個人

[英]Adding more individuals to existing RDF ontology

我有一個約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();  
}

但是此代碼需要花費大量時間將模型寫回到已加載的文件。 似乎它是從頭開始編寫所有內容的(不更新現有文件)。 有誰知道如何解決這個問題?

我認為這無法解決。 這意味着耶拿必須決定您進行了哪種更改。 確實,如果您僅添加了新實例,那么將它們附加到文件中就足夠了。 但是,您可能還需要進行更改,例如將超類添加到某個類中,然后必須更新該類定義。

我同意RobV的觀點 ,即通常來說,如果您使用的是OWL(而不是普通的RDF),則很難做到這一點,但如果將OWL本體序列化為RDF,然后再將其序列化為N-,則可以做到這一點。三同。 以下代碼(帶有注釋)顯示了如何執行此操作。

這里的想法是,如果僅添加新內容,並且使用的格式是每行放置一個RDF三元組,則只需將新的三元組附加到內容中就沒有任何麻煩。 我展示的第一個模型就像磁盤上的本體模型。 在這里,我僅創建它來表明本體中的類聲明使用一個三元組,即Region a owl:Class 不過,區域是由IRI標識的,只要您知道其IRI,就不需要整個本體來引用資源。 模型中,您可以創建一個類型為Region的個人,並且可以簡單地將該模型的三元組附加到磁盤上的文件中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM