为现有的RDF本体添加更多个体 [英] Adding more individuals to existing RDF ontology

查看:200
本文介绍了为现有的RDF本体添加更多个体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约20MB的RDF本体。我尝试添加个人,如下面的代码。

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 ?

推荐答案

虽然我同意 RobV的观点,一般来说,如果你在OWL工作(而不是普通的RDF),这很难做到,如果您的OWL本体被序列化为RDF然后在N-Triples中序列化,那么可以执行此操作。以下代码(带注释)显示了如何执行此操作。

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.

这里的想法是,如果您只是添加新内容并且使用的是放置格式每行一个RDF三元组,然后您可以毫无困难地将新三元组添加到内容中。我展示的第一个模型就像你在磁盘上的本体模型。这里我只创建了它来显示本体中的类声明使用一个三元组, Region a owl:Class 。但是,区域由IRI标识,只要您知道其IRI,您就不需要整个本体来引用资源。在 new 模型中,您可以创建Region类型的个体,您只需将该模型的三元组附加到磁盘上的文件即可。

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

这篇关于为现有的RDF本体添加更多个体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆