Adding more individuals to existing RDF ontology

风格不统一 提交于 2019-12-08 05:46:18

问题


I have a RDF ontology which is about 20MB. I tried to add individuals as in the below code.

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();  
}

But this code takes so much time to write the model back to the loaded file. It seems like it writes everything from scratch ( not updating the existing file). Does anybody has any idea how to solve this ?


回答1:


I don't think this can be solved. That would mean that Jena would have to decide what kind of changes you made. Indeed if you've only added new instances then appending them to the file would be sufficient. However, you might also make changes such as adding a super class to some class and then this class definition would have to be updated.




回答2:


While I agree with RobV's point that, in general, this is difficult to do if you're working in OWL (as opposed to plain RDF), you can do this if your OWL ontology is serialized as RDF which is then serialized in N-Triples. The following code (with comments) shows how you can do this.

The idea here is that if you're only adding new content and if you use a format that puts one RDF triple per line, then you can simply append the new triples to the content without any trouble. The first model that I've shown is like your ontology model on disk. Here I've only created it to show that the class declaration in the ontology uses one triple, Region a owl:Class. Region is identified by an IRI, though, and as long as you know its IRI, you don't need the whole ontology just to refer to the resource. In a new model, you can create an individual that of type Region, and you can simply append the triples of that model to the file on disk.

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


来源:https://stackoverflow.com/questions/19537789/adding-more-individuals-to-existing-rdf-ontology

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!