1

我有一个大约 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();  
}

但是这段代码需要很长时间才能将模型写回加载的文件。似乎它从头开始编写所有内容(不更新现有文件)。有谁知道如何解决这个问题?

4

2 回答 2

1

我不认为这可以解决。这意味着 Jena 将不得不决定你做了什么样的改变。事实上,如果您只添加了新实例,那么将它们附加到文件中就足够了。但是,您也可以进行更改,例如将超类添加到某个类,然后必须更新该类定义。

于 2013-10-23T09:52:57.460 回答
1

虽然我同意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> .
于 2013-10-23T15:24:44.690 回答