我有instance1
ofclass1
和instance2
of class2
。HasName(object property)
我也在我的本体中定义了。现在,如何instance1 HasName instance2
通过 jena 将三元组 ( ) 添加到我的本体中?
问问题
7697 次
2 回答
12
这是一种无需处理 middle 的方法Statements
。
// RDF Nodes -- you can make these immutable in your own vocabulary if you want -- see Jena's RDFS, RDF, OWL, etc vocabularies
Resource class1 = ResourceFactory.createResource(yourNamespace + "class1");
Resource class2 = ResourceFactory.createResource(yourNamespace + "class1");
Property hasName = ResourceFactory.createProperty(yourNamespace, "hasName"); // hasName property
// The RDF Model
Model model = ... // Use your preferred method to get an OntModel, InfModel, or just regular Model
Resource instance1 = model.createResource(instance1Uri);
Resource instance2 = model.createResource(instance2Uri);
// Create statements
instance1.addProperty(RDF.type, class1); // Classification of instance1
instance2.addProperty(RDF.type, class2); // Classification of instance2
instance1.addProperty(hasName, instance2); // Edge between instance1 and instance2
您还可以将其中一些调用链接到构建器模式中。
Resource instance2 = model.createResource(instance2Uri).addProperty(RDF.type, class2);
model.createResource(instance1Uri).addProperty(RDF.type, class1).addProperty(hasName, instance2);
于 2010-11-17T06:12:55.237 回答
2
在 Jena 中,这可以通过创建Statement的实例(三元组或四元组),然后将该语句提交给Model的实例来完成。
例如,考虑以下情况:
OntModel model = ModelFactory.createOntologyModel(); // an ont model instance
...
Statement s = ResourceFactory.createStatement(subject, predicate, object);
model.add(s); // add the statement (triple) to the model
其中subject
和是三元组的实例元素predicate
,object
其类型符合ResourceFactory.createStatement()的接口。
于 2010-11-17T05:54:00.377 回答