虽然我同意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> .