【问题标题】:how can i add some triple to my Ontology by Jena?Jena 如何在我的本体中添加一些三元组?
【发布时间】:2010-10-20 07:01:31
【问题描述】:

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

【问题讨论】:

标签: java semantic-web jena ontology owl


【解决方案1】:

这是一种无需处理中间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);

【讨论】:

  • +1:我喜欢builder-pattern风格的结构,我以前没见过! :)
  • 嗨。答案是惊人的,但我确实有疑问。使用语句创建三元组和使用资源作为上述解决方案有什么区别?请务必为我澄清这一点。谢谢
  • 你能帮我解决这个问题吗stackoverflow.com/questions/57545298/…
【解决方案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() 的接口。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多