ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

RDFS(RDF Schema)介绍和使用举例(二)

RDFS(RDF Schema)介绍和使用举例(二) 目录1. RDFS 核心词汇2. RDFS 的主要推理规则3. Turtle 示例定义 RDFS Schema4. 实例数据5. SPARQL 查询示例6. Python 使用示例rdflib owlrl7. RDFS 的局限8. 总结RDFSRDF 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 ?D2子属性传递如果 ?x ?p ?y且 ?p rdfs:subPropertyOf ?q 则 ?x ?q ?y3定义域推理如果 ?x ?p ?y且 ?p rdfs:domain ?C 则 ?x rdf:type ?C4值域推理如果 ?x ?p ?y且 ?p rdfs:range ?C 则 ?y rdf:type ?C5子属性继承 domain/range如果 ?p rdfs:subPropertyOf ?q且 ?q rdfs:domain ?C 则 ?p rdfs:domain ?Crange 同理。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/Acme7. 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。
返回列表