Archive:Development/Tutorials/Metadata/Nepomuk/RDFIntroduction (zh CN): Difference between revisions

From KDE TechBase
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/Metadata/Nepomuk/RDFIntroduction}}
 


{{TutorialBrowser|
{{TutorialBrowser|
series=[[../|Nepomuk]]|
series=[[../|Nepomuk]]|
name=Nepomuk的RDF和本体|
name=Nepomuk的资源描述框架(RDF)和本体(Ontologies)|
pre=|
pre=|
next=[[../ResourceGenerator|Using the Nepomuk Resource Class Generator]],[[../DataLayout|Nepomuk的数据布局]]|
next=[[../ResourceGenerator|Using the Nepomuk Resource Class Generator]],[[../DataLayout|Nepomuk的数据布局]]|
Line 19: Line 19:
===[http://www.w3.org/TR/rdf-primer/ RDF - 资源描述框架]===
===[http://www.w3.org/TR/rdf-primer/ RDF - 资源描述框架]===


RDF describes a way of storing data. While "classical" databases are based on tables RDF data consists on triples and only triples. Each triple, called <i>statement</i> consists of
RDF是一种描述数据存储的方法,与基于表格的经典数据库不同,RDF数据由三元组组成,被称之为<i>语句</i>(<i>statement</i>)的三元组组成方式是:
<pre>subject - predicate - object</pre>
 
The <i>subject</i> is a resource, the <i>predicate</i> is a relation, and the <i>object</i> is either another resource or a literal value.
<pre>主语(subject) -谓语(predicate) - 对象(object)</pre>
A <i>literal</i> can be a string, integer, double, or any other type defined by [http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html XML Schema], and it is even possible to define custom literal types.
<i>主语(subject)</i>是一个资源,<i>谓语(predicate)</i> 是一个关系,<i>对象(object)</i>可以是另外的资源或者一个值。这个<i></i>的数据类型可以是字符串、整数、双精度浮点数或者是在 [http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html XML Schema]中定义的类型,甚至可以是自定义的数据类型。
Thus RDF can represent statements such as "Mary - is mother of - Carl", or "Mary - was born on - 1970-02-23". These are statements about things, hence RDF is a good technology for ''元数据''
 
那么关于事物(Thing)的RDF数据可以表现为如下语句:"Mary - is mother of - Carl" 或者"Mary - was born on - 1970-02-23"。


To reduce ambiguity, resources and relations need to be uniquely identified; for example, in statement above, to identify a particular "Mary", and also to distinguish the maternal relationship from "Baghdad - is mother of - all battles".
为了降低表达上的模糊,资源和关系必须是唯一标识。例如,在上面的语句中,确定了一个特定的“Mary”,用以区分和“Baghdad – is mother of all battles” 的不同。
Since RDF was born as a web technology all resources and relations are identified by a ''URI'', [http://en.wikipedia.org/wiki/Uniform_Resource_Identifier Uniform Resource Identifier]. (Hence they have a namespace often ending in a <i>#</i> and a name. Typically abbreviation such as <i>foo:bar</i> are used.) Thus, a dataset in RDF is basically a graph where resources are the nodes, predicates the links, and literals act as leaves.
自RDF做为一个Web技术诞生以来,所有的资源和关系都是用''URI''[http://zh.wikipedia.org/wiki/%E7%BB%9F%E4%B8%80%E8%B5%84%E6%BA%90%E6%A0%87%E5%BF%97%E7%AC%A6 统一资源标识符])进行标识。(他们的命名空间经常用 <i>#</i>和一个名字结束,典型的用法是缩写为<i>foo:bar</i> ),因此一个RDF数据集基本上是一个graph ,资源(主语)是节点,关系(谓语)是连接,值是叶子。


RDF defines one important default property: <i>rdf:type</i> which allows to assign a type to a resource.
RDF定义了一个重要的默认属性 <i>rdf:type</i> ,通过这个属性指定资源的类型。


===[http://www.w3.org/TR/rdf-schema/ RDFS - RDF Schema]===
===[http://www.w3.org/TR/rdf-schema/ RDFS - RDF Schema]===

Latest revision as of 13:35, 23 June 2013


Nepomuk的资源描述框架(RDF)和本体(Ontologies)
Tutorial Series   [[../|Nepomuk]]
Previous  
What's Next   [[../ResourceGenerator|Using the Nepomuk Resource Class Generator]],[[../DataLayout|Nepomuk的数据布局]]
Further Reading   [[../Resources|Resource Handling with Nepomuk]],

[[../AdvancedQueries|Advanced Queries with SPARQL]], Sebastian Trueg的Nepomuk博客

Nepomuk的资源描述框架(RDF)和本体(Ontologies)

这个指南基于 Sebastian Trueg 的博文 Nepomuk Appendix A - RDF for Dummies in a Nutshell

在这儿讨论的所有本体随同 kdebase-runtime 一起安装,因此这些本体总是存在于 Nepomuk 数据仓库中,另外,他们的资源 URI 可以通过 Soprano::Vocabulary namespace (NIE 除外,它可以简单的使用 Soprano 的 onto2vocabularyclass 建立。)

RDF - 资源描述框架

RDF是一种描述数据存储的方法,与基于表格的经典数据库不同,RDF数据由三元组组成,被称之为语句statement)的三元组组成方式是:

主语(subject) -谓语(predicate) - 对象(object)

主语(subject)是一个资源,谓语(predicate) 是一个关系,对象(object)可以是另外的资源或者一个值。这个的数据类型可以是字符串、整数、双精度浮点数或者是在 XML Schema中定义的类型,甚至可以是自定义的数据类型。

那么关于事物(Thing)的RDF数据可以表现为如下语句:"Mary - is mother of - Carl" 或者"Mary - was born on - 1970-02-23"。

为了降低表达上的模糊,资源和关系必须是唯一标识。例如,在上面的语句中,确定了一个特定的“Mary”,用以区分和“Baghdad – is mother of – all battles” 的不同。 自RDF做为一个Web技术诞生以来,所有的资源和关系都是用URI统一资源标识符)进行标识。(他们的命名空间经常用 #和一个名字结束,典型的用法是缩写为foo:bar ),因此一个RDF数据集基本上是一个graph ,资源(主语)是节点,关系(谓语)是连接,值是叶子。

RDF定义了一个重要的默认属性 rdf:type ,通过这个属性指定资源的类型。

RDFS - RDF Schema

RDFS扩展了RDF,定义了一个资源和属性的集合,这个扩展基本上允许定义Ontology(本体)。RDFS不但定义了两个重要的类rdfs:Resourcerdfs:Class 用来引入实例和类型的区别,而且定义了属性的层次结构:rdfs:subClassofrdfs:subPropertyofrdfs:domainrdfs:range 指定属性的细节。

这些扩展允许建立新类和属性,与面向对象编程非常类似,例如:

@PREFIX foo: <http://foo.bar/types#>

foo:Human rdf:type rdfs:Class . //Human的类型是类
foo:Woman rdf:type rdfs:Class .  //Woman 的类型是类
foo:Woman rdfs:subClassOf foo:Human .  //Woman类是Human类的子类

foo:isMotherOf rdf:type rdf:Property . // isMotherof 的类型是属性
foo:isMotherOf rdfs:domain foo:Woman . // isMotherof 隶属Woman域
foo:isMotherOf rdfs:range foo:Human . // isMotherof 用于Human范围

foo:Mary rdf:type foo:Woman .  // Mary的类型是 Woman
foo:Mary foo:isMotherOf foo:Carl .  //一个Thing的三元语句描述

这是一个如何使用RDFS定义一个本体的简单例子(使用Turtle 语言)。在RDFS中最后两个重要的谓语(关系)是 rdfs:label and rdfs:comment ,为任意资源定义可读性标签和注释。

NRL:Nepomuk 表示语言(Nepomuk Representation Language)

Nepomuk开发NRL是为了扩展RDFS。对于NRL,我不准备详细解释它的一切,但我会就目前KDE而言非常重要的信息进行解释。

NRL最为重要的改变是从三元组描述到四"元组"描述的改变,第四个参数是已定义(存储)的用语句描述的graph(可能为空),这个graph (在Soprano中称为 context) 是这样一种资源,它可以对描述语句进行分组,允许对描述语句组进行追加。NRL定义了一组 graph(context)类型,其中很重要的是:nrl:InstanceBasenrl:Ontology。第一个定义了graph 包含的实例,第二个,你猜对了,它定义了包含类型和谓语的graphs。

为了使之更加清楚,我们用NRL扩展上面的例子:

@PREFIX foo: <http://foo.bar/types#>

foo:graph1 rdf:type nrl:Ontology .  // 资源foo:graph1的类型是本体
foo:graph2 rdf:type nrl:InstanceBase . // 资源foo:graph2的类型是基础实例

foo:Human rdf:type rdfs:Class foo:graph1. // 资源foo:Human类型是类,是本体 
foo:Woman rdf:type rdfs:Class foo:graph1. // 资源foo:Woman类型是类,是本体
foo:Woman rdfs:subClassOf foo:Human foo:graph1 . //资源foot:Woman是foo:Human的子类,是个本体

foo:isMotherOf rdf:type rdf:Property foo:graph1 . // 资源foo:isMotherOf的类型是属性,是个本体
foo:isMotherOf rdfs:domain foo:Woman foo:graph1 . // 资源foo:isMotherOf的作用域为foo:Woman,是个本体
foo:isMotherOf rdfs:range foo:Human foo:graph1 . // 资源foo:isMotherOf的范围是foo:Human,是个本体

foo:Mary rdf:type foo:Woman foo:graph2 . //资源Mary的类型为foo:Woman,是一个基础实例
foo:Mary foo:isMotherOf foo:Carl foo:graph2 . //资源Mary和资源foo:Carl是foo:isMotherOf的关系,是一个基础实例

但是,上述例子不能展现实体和本体的所有区别

NAO:Nepomuk 标注本体(Nepomuk Annotation Ontology)

你在KDE已经遇到过的由NAO定义的资源类型和属性是:nao:Tagnao:rating 。其实NAO也定义了 nao:created 属性,它为资源 (在下面的例子里是一个graph)分配一个 xls:dateTime 类型数据。 当一些信息被加入到Nepomuk库时,我们用这个方法存储相关信息。

foo:graph1 nao:created "2008-02-12T14:43.022Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> .

NIE — Nepomuk 信息元素本体(Nepomuk Information Element)

NIE 本体描述桌面资源文件像文件,文件夹,电子邮件,联系人,即时消息等等,它在文件索引系统诸如 Strigi 或者 tracker中用于描述提取的元数据。

Xesam - 桌面文件的元数据本体 因采用NIE而过时

Xesam是一个应用于strigi 索引桌面文件而开发出来的本体,它试图对桌面应用的大多数文件类型元数据定义类/类型和属性。简单的例子如id3标签或图像大小,或如发信人和收信任等电子邮件数据,在KDE桌面系统上strigi对文件元数据的检索结果使用Xesam定义的类和属性等数据保存在Nepomuk数据仓库内。

SPARQL - RDF查询语言

我们用SPARQL 查询RDF 数据库,它的语句格式被设计成近似SQL,但因为它迄今为止十分年轻,所以还不是很强大。

尽管这样,搜索“the mother of carl”这样简单的查询大致如下:

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix foo: <http://foo.bar/types#>

select ?r where { ?r foo:isMotherOf foo:Carl . }

或者如果采用NRL :

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix foo: <http://foo.bar/types#>
prefix nrl: <http://semanticdesktop.org/ontologies/2007/08/15/nrl#>

select ?r where { graph ?g { ?r foo:isMotherOf foo:Carl . } . ?g rdf:type nrl:InstanceBase . }

SPARQL quick reference是一份非常有价值的文档。

其他本体/定制本体

这里所说的本体是Nepomuk数据的基本形式,但他们不能满足各方面的需求,如果你想在Nepomuk中存储你自己的数据并且和其他信息联系起来,建议按照以下过程来做:

  • http://www.semanticdesktop.org/ontologies/ 中检查现有的标准本体中是否提供你所需要(或部分需要)的类和属性,这个网址中有很多标准本体,包括 NRL 和 NAO
  • 如果没有,联系 the Oscaf project 中与你需要获得的帮助相关的讨论组和开发组
  • 如果你的需求还不能得到解决,那就开始开发你自己的本体,可能的话建议将其作为Oscaf标准本体。