我如何在Jena的Ontology中添加一些三元组? [英] how can i add some triple to my Ontology by Jena?

查看:267
本文介绍了我如何在Jena的Ontology中添加一些三元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有class1instance1class2instance2.另外,我在本体中定义了HasName(object property).现在,如何通过耶拿将三元组(instance1 HasName instance2)添加到我的本体中?

I have instance1 of class1 and instance2 of class2. Also I have defined HasName(object property) in my ontology. Now, how can I add the triple (instance1 HasName instance2) to my ontology by jena?

推荐答案

这是一种无需处理中间Statements的方法.

Here's a way without dealing with intermediate 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.

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

这篇关于我如何在Jena的Ontology中添加一些三元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆