我如何为Jena的Ontology添加一些三倍?

时间:2010-10-20 07:01:32

标签: java semantic-web jena ontology owl

我有instance1 class1instance2 class2。我也在我的本体中定义了HasName(object property)。现在,如何通过jena将三元组(instance1 HasName instance2)添加到我的本体中?

2 个答案:

答案 0 :(得分:9)

这是一种不处理中间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

您还可以使用builder-ish模式链接其中一些调用。

Resource instance2 = model.createResource(instance2Uri).addProperty(RDF.type, class2);
model.createResource(instance1Uri).addProperty(RDF.type, class1).addProperty(hasName, instance2);

答案 1 :(得分: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

其中subjectpredicateobject是三元组的实例元素,类型符合ResourceFactory.createStatement()的界面。