前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【RDF】:RDF、Turtle、rdflib

【RDF】:RDF、Turtle、rdflib

作者头像
WEBJ2EE
发布2021-09-02 15:41:38
1.5K0
发布2021-09-02 15:41:38
举报
文章被收录于专栏:WebJ2EE
代码语言:javascript
复制
目录
1. 快速回顾 
2. RDF 1.1 语法
  2.1. Graph-based Data Model
  2.2. Resources and Statements
  2.3. RDF Vocabularies and Namespace IRIs
  2.4. RDF Documents and Syntaxes
  2.5. RDF Graphs
  2.6. Triples
  2.7. IRIs
  2.8. Blank Nodes
  2.9. Fragment Identifiers
3. RDF 1.1 Turtle 语法
  3.1. 是什么
  3.2. Simple Triples
  3.3. Predicate Lists
  3.4. Object Lists
  3.5. IRIs
  3.6. RDF Literals
  3.7. RDF Blank Nodes
  3.8. Nesting Unlabeled Blank Nodes in Turtle
4. 使用 rdflib 操作 RDF 
  4.1. 是什么
  4.2. Protege 如何使用 RDF 表达实体
  4.3. 使用 rdflib 操作 RDF

1. 快速回顾

  • RDF —— Resource Description Framework
  • The RDF is a framework for representing information in the Web.
  • RDF 的核心是三元组(Triples),由主语(Subject)、谓语(Predict)和宾语(Object)构成
  • RDFS(RDF Schema)是RDF,是对RDF的扩展,扩展了描述应用程序专用的类和属性的框架。
  • OWL(Web Ontology Language) 是对 RDFS 的扩展,增加了推理规则。
  • Protege 是开源的本体开发工具。
  • Turtle 是一种 RDF 图的序列化方式,它比 RDF/XML 序列化方式紧凑,比 N-Triples 可读性好。

2. RDF 1.1 语法

2.1. Graph-based Data Model

The core structure of the abstract syntax is a set of triples, each consisting of a subject, a predicate and an object. A set of such triples is called an RDF graph. An RDF graph can be visualized as a node and directed-arc diagram, in which each triple is represented as a node-arc-node link.

There can be three kinds of nodes in an RDF graph: IRIs, literals, and blank nodes.

2.2. Resources and Statements

Any IRI or literal(这里注意RDF图中的结点一共有3类:IRI、literal、blank node) denotes something in the world (the "universe of discourse"). These things are called resources. Anything can be a resource, including physical things, documents, abstract concepts, numbers and strings; the term is synonymous with "entity" as it is used in the RDF Semantics specification.

The resource denoted by an IRI is called its referent, and the resource denoted by a literal is called its literal value.

Literals have datatypes that define the range of possible values, such as strings, numbers, and dates.

Asserting an RDF triple says that some relationship, indicated by the predicate, holds between the resources denoted by the subject and object. This statement corresponding to an RDF triple is known as an RDF statement. The predicate itself is an IRI and denotes a property, that is, a resource that can be thought of as a binary relation. (Relations that involve more than two entities can only be indirectly expressed in RDF)

Unlike IRIs and literals, blank nodes do not identify specific resources. Statements involving blank nodes say that something with the given relationships exists, without explicitly naming it.

2.3. RDF Vocabularies and Namespace IRIs

An RDF vocabulary is a collection of IRIs intended for use in RDF graphs. For example, the IRIs documented in [RDF11-SCHEMA] are the RDF Schema vocabulary. RDF Schema can itself be used to define and document additional RDF vocabularies.

The IRIs in an RDF vocabulary often begin with a common substring known as a namespace IRI. Some namespace IRIs are associated by convention with a short name known as a namespace prefix. Some examples:

In some serialization formats it is common to abbreviate IRIs that start with namespace IRIs by using a namespace prefix in order to assist readability. For example, the IRI http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral would be abbreviated as rdf:XMLLiteral.

2.4. RDF Documents and Syntaxes

An RDF document is a document that encodes an RDF graph or RDF dataset in a concrete RDF syntax, such as Turtle [TURTLE], RDFa [RDFA-PRIMER], JSON-LD [JSON-LD], or TriG [TRIG]. RDF documents enable the exchange of RDF graphs and RDF datasets between systems.

2.5. RDF Graphs

An RDF graph is a set of RDF triples.

2.6. Triples

An RDF triple consists of three components:

  • the subject, which is an IRI or a blank node
  • the predicate, which is an IRI
  • the object, which is an IRI, a literal or a blank node

An RDF triple is conventionally written in the order subject, predicate, object.

IRIs, literals and blank nodes are collectively known as RDF terms.

2.7. IRIs

An IRI (Internationalized Resource Identifier) within an RDF graph is a Unicode string [UNICODE] that conforms to the syntax defined in RFC 3987 [RFC3987].

IRIs in the RDF abstract syntax must be absolute, and may contain a fragment identifier.

2.8. Blank Nodes

Blank node identifiers are local identifiers that are used in some concrete RDF syntaxes or RDF store implementations. They are always locally scoped to the file or RDF store, and are not persistent or portable identifiers for blank nodes. Blank node identifiers are not part of the RDF abstract syntax, but are entirely dependent on the concrete syntax or implementation. The syntactic restrictions on blank node identifiers, if any, therefore also depend on the concrete RDF syntax or implementation. Implementations that handle blank node identifiers in concrete syntaxes need to be careful not to create the same blank node from multiple occurrences of the same blank node identifier except in situations where this is supported by the syntax.

2.9. Fragment Identifiers

RDF uses IRIs, which may include fragment identifiers, as resource identifiers. The semantics of fragment identifiers is defined in RFC 3986 [RFC3986]: They identify a secondary resource that is usually a part of, view of, defined in, or described in the primary resource, and the precise semantics depend on the set of representations that might result from a retrieval action on the primary resource.

3. RDF 1.1 Turtle 语法

3.1. 是什么

A Turtle document is a textual representations of an RDF graph, a concrete syntax for RDF.

A Turtle document allows writing down an RDF graph in a compact textual form. An RDF graph is made up of triples consisting of a subject, predicate and object.

代码语言:javascript
复制
@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .

<#green-goblin>
    rel:enemyOf <#spiderman> ;
    a foaf:Person ;    # in the context of the Marvel universe
    foaf:name "Green Goblin" .

<#spiderman>
    rel:enemyOf <#green-goblin> ;
    a foaf:Person ;
    foaf:name "Spiderman", "Человек-паук"@ru .

3.2. Simple Triples

The simplest triple statement is a sequence of (subject, predicate, object) terms, separated by whitespace and terminated by '.' after each triple.

代码语言:javascript
复制
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .

3.3. Predicate Lists

Often the same subject will be referenced by a number of predicates. The predicateObjectList production matches a series of predicates and objects, separated by ';', following a subject. This expresses a series of RDF Triples with that subject and each predicate and object allocated to one triple. Thus, the ';' symbol is used to repeat the subject of triples that vary only in predicate and object RDF terms.

代码语言:javascript
复制
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> ;
        <http://xmlns.com/foaf/0.1/name> "Spiderman" .
代码语言:javascript
复制
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .
<http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Spiderman" .

3.4. Object Lists

As with predicates often objects are repeated with the same subject and predicate. The objectList production matches a series of objects separated by ',' following a predicate. This expresses a series of RDF Triples with the corresponding subject and predicate and each object allocated to one triple. Thus, the ',' symbol is used to repeat the subject and predicate of triples that only differ in the object RDF term.

代码语言:javascript
复制
<http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Spiderman", "Человек-паук"@ru .
代码语言:javascript
复制
<http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Spiderman" .
<http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Человек-паук"@ru .

3.5. IRIs

IRIs may be written as relative or absolute IRIs or prefixed names. Relative and absolute IRIs are enclosed in '<' and '>' and may contain numeric escape sequences (described below). For example <http://example.org/#green-goblin>.

Relative IRIs like <#green-goblin> are resolved relative to the current base IRI. A new base IRI can be defined using the '@base' or 'BASE' directive.

The token 'a' in the predicate position of a Turtle triple represents the IRI http://www.w3.org/1999/02/22-rdf-syntax-ns#type .

A prefixed name is a prefix label and a local part, separated by a colon ":". A prefixed name is turned into an IRI by concatenating the IRI associated with the prefix and the local part. The '@prefix' or 'PREFIX' directive associates a prefix label with an IRI. Subsequent '@prefix' or 'PREFIX' directives may re-map the same prefix label.

代码语言:javascript
复制
@prefix somePrefix: <http://www.perceive.net/schemas/relationship/> .

<http://example.org/#green-goblin> somePrefix:enemyOf <http://example.org/#spiderman> .

IRI 综合示例

代码语言:javascript
复制
# A triple with all absolute IRIs
<http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> .

@base <http://one.example/> .
<subject2> <predicate2> <object2> .     # relative IRIs, e.g. http://one.example/subject2

BASE <http://one.example/>
<subject2> <predicate2> <object2> .     # relative IRIs, e.g. http://one.example/subject2

@prefix p: <http://two.example/> .
p:subject3 p:predicate3 p:object3 .     # prefixed name, e.g. http://two.example/subject3

PREFIX p: <http://two.example/>
p:subject3 p:predicate3 p:object3 .     # prefixed name, e.g. http://two.example/subject3

@prefix p: <path/> .                    # prefix p: now stands for http://one.example/path/
p:subject4 p:predicate4 p:object4 .     # prefixed name, e.g. http://one.example/path/subject4

@prefix : <http://another.example/> .    # empty prefix
:subject5 :predicate5 :object5 .        # prefixed name, e.g. http://another.example/subject5

:subject6 a :subject7 .                 # same as :subject6 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> :subject7 .

<http://伝言.example/?user=أكرم&amp;channel=R%26D> a :subject8 . # a multi-script subject IRI .

3.6. RDF Literals

Literals are used to identify values such as strings, numbers, dates.

代码语言:javascript
复制
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://example.org/#green-goblin> foaf:name "Green Goblin" .

<http://example.org/#spiderman> foaf:name "Spiderman" .

3.7. RDF Blank Nodes

RDF blank nodes in Turtle are expressed as _: followed by a blank node label which is a series of name characters.

代码语言:javascript
复制
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:alice foaf:knows _:bob .
_:bob foaf:knows _:alice .

3.8. Nesting Unlabeled Blank Nodes in Turtle

代码语言:javascript
复制
blankNodePropertyList  ::=  '[' predicateObjectList ']'
predicateObjectList  ::=  verb objectList (';' (verb objectList)?)*

In Turtle, fresh RDF blank nodes are also allocated when matching the production blankNodePropertyList. Both of these may appear in the subject or object position of a triple. That subject or object is a fresh RDF blank node. This blank node also serves as the subject of the triples produced by matching the predicateObjectList production embedded in a blankNodePropertyList.

代码语言:javascript
复制
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice" ] foaf:knows [
    foaf:name "Bob" ;
    foaf:knows [
        foaf:name "Eve" ] ;
    foaf:mbox <bob@example.com> ] .
代码语言:javascript
复制
_:a <http://xmlns.com/foaf/0.1/name> "Alice" .
_:a <http://xmlns.com/foaf/0.1/knows> _:b .
_:b <http://xmlns.com/foaf/0.1/name> "Bob" .
_:b <http://xmlns.com/foaf/0.1/knows> _:c .
_:c <http://xmlns.com/foaf/0.1/name> "Eve" .
_:b <http://xmlns.com/foaf/0.1/mbox> <bob@example.com> .

4. 使用 rdflib 操作 RDF

4.1. 是什么

RDFLib is a pure Python package for working with RDF.

4.2. Protege 如何使用 RDF 表达实体

  • IRI
  • Classes
  • Individuals
  • Restriction

4.3. 使用 rdflib 操作 RDF

参考:

RDF 1.1 Concepts and Abstract Syntax: https://www.w3.org/TR/2014/REC-rdf11-concepts-20140225/ rfc3987: https://www.ietf.org/rfc/rfc3987.txt RDF 1.1 Turtle: https://www.w3.org/TR/turtle/ rdflib: https://rdflib.readthedocs.io/en/stable/index.html#


本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-08-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WebJ2EE 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档