我有一个大约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();
}
但是这段代码花了很多时间将模型写回加载的文件。它似乎从头开始编写所有内容(不更新现有文件)。有没有人知道如何解决这个问题?
答案 0 :(得分:1)
我不认为这可以解决。这意味着耶拿必须决定你做出了哪些改变。实际上,如果您只添加了新实例,那么将它们附加到文件就足够了。但是,您可能还需要进行更改,例如向某个类添加超类,然后必须更新此类定义。
答案 1 :(得分:1)
虽然我同意RobV's point,但一般情况下,如果你在OWL(而不是普通的RDF)工作,这很难做到,你可以做到这一点,如果你的OWL本体被序列化为RDF,然后在N-Triples中序列化。以下代码(带注释)显示了如何执行此操作。
这里的想法是,如果您只是添加新内容,并且如果您使用的格式每行放置一个RDF三元组,那么您可以简单地将新三元组添加到内容中而不会有任何问题。我展示的第一个模型就像你在磁盘上的本体模型。在这里,我只创建它以显示本体中的类声明使用一个三元组Region a owl:Class
。但是,区域由IRI标识,只要您知道其IRI,您就不需要整个本体来引用资源。在 new 模型中,您可以创建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> .