☰
RDFS(RDF Schema)介绍和使用举例(二)
2026/10/7 14:46:07 网站建设 项目流程

目录

1. RDFS 核心词汇

2. RDFS 的主要推理规则

3. Turtle 示例:定义 RDFS Schema

4. 实例数据

5. SPARQL 查询示例

6. Python 使用示例:rdflib + owlrl

7. RDFS 的局限

8. 总结


RDFS(RDF Schema)是在 RDF 之上的一层“轻量本体语言”。RDF 只负责表达三元组事实,而 RDFS 用来定义这些事实中出现的**类、属性、层次关系、定义域和值域**,并支持简单的推理。

一句话:RDF 说“有什么事实”,RDFS 说“这些事实里的类/属性是什么,以及它们如何推导出更多事实”。

1. RDFS 核心词汇

RDFS 命名空间:http://www.w3.org/2000/01/rdf-schema#,通常写为 rdfs: 。

常用词汇:

注意:rdfs:domain和 rdfs:range 在 RDFS 中不是“校验约束”,而是推理规则。即使数据违反,也不会报错,反而会推出新的类型。

2. RDFS 的主要推理规则

RDFS 推理机通常支持以下规则:

(1)子类传递

如果 ?x rdf:type ?C,且 ?C rdfs:subClassOf ?D 则 ?x rdf:type ?D

(2)子属性传递

如果 ?x ?p ?y,且 ?p rdfs:subPropertyOf ?q 则 ?x ?q ?y

(3)定义域推理

如果 ?x ?p ?y,且 ?p rdfs:domain ?C 则 ?x rdf:type ?C

(4)值域推理

如果 ?x ?p ?y,且 ?p rdfs:range ?C 则 ?y rdf:type ?C

(5)子属性继承 domain/range

如果 ?p rdfs:subPropertyOf ?q,且 ?q rdfs:domain ?C 则 ?p rdfs:domain ?C

range 同理。

3. Turtle 示例:定义 RDFS Schema

下面定义“人”“组织”“员工”三个类,以及“工作于”“雇主”两个属性。

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix ex: <http://example.org/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . # 类定义 ex:Person a rdfs:Class ; rdfs:label "人"@zh ; rdfs:comment "人类"@zh . ex:Organization a rdfs:Class ; rdfs:label "组织"@zh . ex:Employee a rdfs:Class ; rdfs:subClassOf ex:Person ; rdfs:label "员工"@zh . # 属性定义 ex:worksFor a rdf:Property ; rdfs:label "工作于"@zh ; rdfs:domain ex:Person ; rdfs:range ex:Organization . ex:employer a rdf:Property ; rdfs:subPropertyOf ex:worksFor ; rdfs:label "雇主"@zh .

这里:

ex:Employee rdfs:subClassOf ex:Person:员工是人。 ex:worksFor rdfs:domain ex:Person:工作于某组织的主语是人。 ex:worksFor rdfs:range ex:Organization:工作于的宾语是组织。 ex:employer rdfs:subPropertyOf ex:worksFor:雇主关系是工作于关系的子属性

4. 实例数据

ex:Alice a ex:Employee ; foaf:name "Alice" ; ex:employer ex:Acme . ex:Acme a ex:Organization ; foaf:name "Acme Corp" .

显式写出的三元组只有:

ex:Alice rdf:type ex:Employee . ex:Alice ex:employer ex:Acme . ex:Acme rdf:type ex:Organization .

但经过 RDFS 推理后,会推出:

ex:Alice rdf:type ex:Person . # Employee 是 Person 的子类 ex:Alice ex:worksFor ex:Acme . # employer 是 worksFor 的子属性 ex:Acme rdf:type ex:Organization . # worksFor 的 range ex:Alice rdf:type ex:Person . # worksFor 的 domain(重复)

所以即使查询时只写 `ex:Person` 和 `ex:worksFor`,也能查到 Alice 和 Acme。

5. SPARQL 查询示例

查询所有“人”以及他们“工作于”的组织:

PREFIX ex: <http://example.org/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?person ?org WHERE { ?person a ex:Person . ?person ex:worksFor ?org . }

如果推理机已经运行,结果会包含:

person = ex:Alice org = ex:Acme

如果没有推理机,也可以手动沿着子类/子属性查询,但会复杂很多:

PREFIX ex: <http://example.org/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?person ?org WHERE { ?person a ?cls . ?cls rdfs:subClassOf* ex:Person . ?person ?p ?org . ?p rdfs:subPropertyOf* ex:worksFor . }

6. Python 使用示例:rdflib + owlrl

安装:

pip install rdflib owlrl

代码:

from rdflib import Graph, Namespace, RDF, RDFS, Literal from rdflib.namespace import FOAF from owlrl import DeductiveClosure, RDFS_Semantics g = Graph() EX = Namespace("http://example.org/") g.bind("ex", EX) g.bind("foaf", FOAF) g.bind("rdfs", RDFS) # 1. 定义 RDFS Schema g.add((EX.Person, RDF.type, RDFS.Class)) g.add((EX.Organization, RDF.type, RDFS.Class)) g.add((EX.Employee, RDF.type, RDFS.Class)) g.add((EX.Employee, RDFS.subClassOf, EX.Person)) g.add((EX.worksFor, RDF.type, RDF.Property)) g.add((EX.worksFor, RDFS.domain, EX.Person)) g.add((EX.worksFor, RDFS.range, EX.Organization)) g.add((EX.employer, RDF.type, RDF.Property)) g.add((EX.employer, RDFS.subPropertyOf, EX.worksFor)) # 2. 添加实例数据 g.add((EX.Alice, RDF.type, EX.Employee)) g.add((EX.Alice, FOAF.name, Literal("Alice"))) g.add((EX.Alice, EX.employer, EX.Acme)) g.add((EX.Acme, RDF.type, EX.Organization)) g.add((EX.Acme, FOAF.name, Literal("Acme Corp"))) # 3. 执行 RDFS 推理 DeductiveClosure(RDFS_Semantics).expand(g) # 4. 查询 q = """ PREFIX ex: <http://example.org/> SELECT ?person ?org WHERE { ?person a ex:Person . ?person ex:worksFor ?org . } """ for row in g.query(q): print(row.person, row.org)

输出类似:

http://example.org/Alice http://example.org/Acme

7. RDFS 的局限

RDFS 表达力有限,它不能表达:

- 基数约束:如“一个人只能有一个生日”
- 不相交类:如“人和组织不能是同一个东西”
- 等价类/属性:owl:equivalentClass
- 逆属性:owl:inverseOf
- 传递属性、对称属性、函数属性
- 复杂约束和一致性检查

这些需要 OWL 或 SHACL。RDFS 更适合做:

- 轻量级知识图谱模式
- 数据集成中的词汇表对齐
- 简单的类型和属性层次推理
- 为 OWL 本体打基础

8. 总结

-RDF:用三元组表达事实。
-RDFS:在 RDF 上定义类和属性,支持子类、子属性、domain、range 推理。
-典型用法:先定义 schema,再写实例数据,然后用 RDFS 推理机扩展图,最后用 SPARQL 查询。
-注意:rdfs:domain / rdfs:range 是推理规则,不是数据校验;复杂约束请用 OWL/SHACL。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询