[英]how can i add some triple to my Ontology by Jena?
I have instance1
of class1
and instance2
of class2
. 我有
instance1
的class1
和instance2
的class2
。 Also I have defined HasName(object property)
in my ontology. 我也在我的本体中定义了
HasName(object property)
。 Now, how can I add the triple ( instance1 HasName instance2
) to my ontology by jena? 现在,如何通过jena将triple(
instance1 HasName instance2
)添加到我的本体?
Here's a way without dealing with intermediate Statements
. 这是一种不处理中间
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
You could also chain some of these calls in a builder-ish pattern. 您还可以使用builder-ish模式链接其中一些调用。
Resource instance2 = model.createResource(instance2Uri).addProperty(RDF.type, class2);
model.createResource(instance1Uri).addProperty(RDF.type, class1).addProperty(hasName, instance2);
In Jena, this can be done by creating an instance of a Statement (a triple, or quad), then committing the statement to an instance of a Model . 在Jena中,可以通过创建Statement的实例(三元组或四元组),然后将语句提交给Model的实例来完成。
For example, consider the following: 例如,请考虑以下事项:
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
Where subject
, predicate
and object
are instance elements of your triple with types conforming to the interface for ResourceFactory.createStatement() . 其中
subject
, predicate
和object
是三元组的实例元素,其类型符合ResourceFactory.createStatement()的接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.