【问题标题】:Converting list of names into RDF Schema (RDFS)将名称列表转换为 RDF Schema (RDFS)
【发布时间】:2025-12-03 11:05:01
【问题描述】:

我是语义网的新手。我的问题是:如何在 Java 程序中使用 RDF Schema?我不应该使用 Java Jena API 进行转换。有没有办法这样做?我有一个 Java 程序中的名称列表,并希望将它们转换为 RDF Schema。您的帮助将不胜感激。

【问题讨论】:

  • 您是否尝试在 Java 程序中使用 RDFS 属性和类型?你能发布一个代码 sn-p 来更好地解释你的问题吗?
  • 是的。这正是我一直在尝试做的。我的程序不会真正帮助你。我使用 restFB 从 Facebook 提取了我所有的朋友列表,并且应该在这个列表中使用 RDFS。此时如何使用 RDFS?我的意思是我应该从哪里开始?
  • 你想做什么? RDFS 只是一个 RDF 词汇表。按照惯例,它是一组具有某种含义的 URI。当描述一些资源(RDF 是资源描述框架)时,您会使用这些 URI。说您想将名称列表转换为 RDFS 是没有意义的,但是说您想使用 RDFS 来描述它可能是有意义的。你想描述什么,你想说什么?
  • 好的。让我们这样说吧。忘记我刚才提到的清单。您说 RDFS 用于描述资源是正确的。我想做的是如何在Java程序中技术上应用RDFS? Jena 可能非常有用,但在这种情况下我不应该使用它。有什么简单的例子可以帮我看看吗?!
  • 不清楚你想如何使用它。 RDF 是一种基于语句的数据表示格式,也就是三元组,每个语句的形式为 [主语、谓词(或属性)、宾语]。例如,您可能有 [user:1281433 hasUsername "Joshua Taylor"]。 RDFS 只是一组 IRI,其中一些在用作属性和对象时具有常规含义。你想对他们做什么?输出 RDF?某些陈述的原因?完全不同的东西?就目前而言,这有点像在问,“我只是有一本包含一些单词及其定义的字典,我如何在 Java 中使用它们?”

标签: java rdf semantic-web owl rdfs


【解决方案1】:

根据 cmets 中的一些说明,看来这里需要的只是表示一些基本 RDF 的能力。如果您要在不使用某些支持库的情况下手动编写 RDF,最容易使用的格式是N-Triples。您需要按属性识别所有资源,并学习如何使用文字(可以是纯字符串、带有语言标签的字符串或字符串和数据类型)。这是一个简单的 RDF 文档,它声明了两个类 Person 和 Animal、一个属性 hasPet,并包含 FDR 拥有(拥有?)一个名为 Fala 的宠物的声明。

<http://example.org/Fala> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Animal> .
<http://example.org/Animal> <http://www.w3.org/2000/01/rdf-schema#label> "Animal"@en .
<http://example.org/Animal> <http://www.w3.org/2000/01/rdf-schema#comment> "The class of (non-human) animals"@en .
<http://example.org/Animal> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://example.org/Person> <http://www.w3.org/2000/01/rdf-schema#label> "Person"@en .
<http://example.org/Person> <http://www.w3.org/2000/01/rdf-schema#comment> "The class of people." .
<http://example.org/Person> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://dbpedia.org/resource/Franklin_D._Roosevelt> <http://example.org/hasPet> <http://example.org/Fala> .
<http://dbpedia.org/resource/Franklin_D._Roosevelt> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Person> .
<http://example.org/hasPet> <http://www.w3.org/2000/01/rdf-schema#comment> "used to indicate that the object is a pet of the subject"@en .
<http://example.org/hasPet> <http://www.w3.org/2000/01/rdf-schema#label> "has pet"@en .
<http://example.org/hasPet> <http://www.w3.org/2000/01/rdf-schema#range> <http://example.org/Animal> .
<http://example.org/hasPet> <http://www.w3.org/2000/01/rdf-schema#domain> <http://example.org/Person> .
<http://example.org/hasPet> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .

这就是它的全部内容。用 URI 表示事物,并写一些句子。使用库来生成 RDF 更为常见,因为否则您将不得不担心 URI 中允许使用哪些字符以及类似的东西,而适当的库会处理这些问题的检查。无论如何,您都可以生成它,但是您通常会在 Java 中生成文本输出。

作为参考,Turtle 和 RDF/XML 中的相同 RDF 图如下。 Turtle 更易于人类阅读,手写也不太难,但用程序编写不像 N-Triples 那样容易。 RDF/XML 根本不应该手写。

@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex:      <http://example.org/> .
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix dbpedia:  <http://dbpedia.org/resource/> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

ex:Animal
      a       rdfs:Class ;
      rdfs:comment "The class of (non-human) animals"@en ;
      rdfs:label "Animal"@en .

ex:Fala
      a       ex:Animal .

ex:hasPet
      a       rdf:Property ;
      rdfs:comment "used to indicate that the object is a pet of the subject"@en ;
      rdfs:domain ex:Person ;
      rdfs:label "has pet"@en ;
      rdfs:range ex:Animal .

<http://dbpedia.org/resource/Franklin_D._Roosevelt>
      a       ex:Person ;
      ex:hasPet ex:Fala .

ex:Person
      a       rdfs:Class ;
      rdfs:comment "The class of people." ;
      rdfs:label "Person"@en .
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:ex="http://example.org/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:dbpedia="http://dbpedia.org/resource/"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <rdfs:Class rdf:about="http://example.org/Animal">
    <rdfs:label xml:lang="en">Animal</rdfs:label>
    <rdfs:comment xml:lang="en">The class of (non-human) animals</rdfs:comment>
  </rdfs:Class>
  <rdfs:Class rdf:about="http://example.org/Person">
    <rdfs:label xml:lang="en">Person</rdfs:label>
    <rdfs:comment>The class of people.</rdfs:comment>
  </rdfs:Class>
  <rdf:Property rdf:about="http://example.org/hasPet">
    <rdfs:comment xml:lang="en">used to indicate that the object is a pet of the subject</rdfs:comment>
    <rdfs:label xml:lang="en">has pet</rdfs:label>
    <rdfs:range rdf:resource="http://example.org/Animal"/>
    <rdfs:domain rdf:resource="http://example.org/Person"/>
  </rdf:Property>
  <ex:Person rdf:about="http://dbpedia.org/resource/Franklin_D._Roosevelt">
    <ex:hasPet>
      <ex:Animal rdf:about="http://example.org/Fala"/>
    </ex:hasPet>
  </ex:Person>
</rdf:RDF>

【讨论】:

  • 非常感谢您。请问还有一个问题吗?我可以使用什么库或编辑器来应用此示例?我想试试看结果。再次感谢您的帮助和耐心。
  • @user2864315 好吧,说实话,我写了一些 Jena 代码来生成本体,然后用几种不同的格式写出来。您说您不想使用 Jena,所以恐怕我没有任何特别有用的建议。当我手写 Turtle 时,我通常使用emacs
  • @user2864315 另外,如果这回答了您的问题,您可以accept an answer 让其他用户知道它对您有用,并减少没有接受答案的问题数量。它还为您和回答者提供了一些声誉积分。
  • 我正在尝试使用 Java RIO(使用 RIO 编写 RDF)。你有没有尝试过?问题是我不应该使用 Jena。任何想法?非常感谢您的所有回答。
  • @user2864315 我没用过。请注意,“要求我们推荐或查找工具、库或最喜欢的非现场资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及到目前为止所做的工作解决这个问题。”虽然关于查找库的问题是重要的问题,但它们恰好不是一个非常适合 Stack Overflow 的问题。那里有很多 RDF 库。在大多数情况下,您将对它们执行相同的操作:创建模型、添加语句并序列化模型。
最近更新 更多