概述
rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="Edit-Time-Data" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_editdata.mso"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">
地址:http://www.w3.org/2000/10/swap/doc/formats
下面通过不同的语言和格式描述同一件事情:一个叫Pat的人,也被称为Pat Smith或Patrick Smith,他有一个叫Rover的宠物狗。
英语(非常非正式)
There is person, Pat, known as "Pat Smith" and "Patrick Smith". Pat has a pet dog named "Rover".
英语超文本(非正式)
这里术语的歧义通过为词加上超链接来区分,这些链接是否有效,依赖于涉及到的服务器。
Pat is a human with the names "Pat Smith" and "Patrick Smith". Pat has a pet, a dog, with the name "Rover".
N3
@prefix : <http://www.w3.org/2000/10/swap/test/demo1/about-pat#> .
@prefix bio: <http://www.w3.org/2000/10/swap/test/demo1/biology#> .
@prefix per: <http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#> .
:pat a bio:Human;
per:name "Pat Smith",
"Patrick Smith";
per:pet [
a bio:Dog;
per:name "Rover" ] .
Directed Labeled Graph有向标记图
图见原文……
N-Triples
使用@prefix:
@prefix : <http://www.w3.org/2000/10/swap/test/demo1/about-pat#> .
@prefix bio: <http://www.w3.org/2000/10/swap/test/demo1/biology#> .
@prefix per: <http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
:pat rdf:type bio:Human.
:pat per:name "Pat Smith".
:pat per:name "Patrick Smith".
:pat per:pat _:genid1.
_:genid1 rdf:type bio:Dog.
_:genid1 per:name "Rover".
标准形式:
<http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/swap/test/demo1/biology#Human> .
<http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat> <http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name> "Pat Smith" .
<http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat> <http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name> "Patrick Smith" .
<http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat> <http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#pet> _:genid1 .
_:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/swap/test/demo1/biology#Dog> .
_:genid1 <http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name> "Rover" .
RDF/XML
<rdf:RDF xmlns="http://www.w3.org/2000/10/swap/test/demo1/about-pat#"
xmlns:bio="http://www.w3.org/2000/10/swap/test/demo1/biology#"
xmlns:per="http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<bio:Human rdf:about="#pat">
<per:name>Pat Smith</per:name>
<per:name>Patrick Smith</per:name>
<per:pet>
<bio:Dog>
<per:name>Rover</per:name>
</bio:Dog>
</per:pet>
</bio:Human>
</rdf:RDF>
XML(但不是RDF/XML)
Striped或alternating-normal form方案使用为应用设计的标记语言,转换至三元形式或者从三元形式转换过来需要使用特定的软件。
<Human>
<uri>http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat</uri>
<name>Pat Smith</name>
<pet>
<Dog>
<name>Rover</name>
</Dog>
</pet>
</Human>
另一选择是使用XML Triples。这不需要特定领域的标记。有些候选DTDs/Schemes已经被提议作参考,不过这只是一个草案(strawman)。对于一些应用,这种语法可能比RDF/XML或striped syntax更容易。
<!DOCTYPE Graph [
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<!ENTITY bio "http://www.w3.org/2000/10/swap/test/demo1/biology#">
<!ENTITY ns1 "http://www.w3.org/2000/10/swap/test/demo1/about-pat#">
<!ENTITY per "http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#">
]>
<Graph>
<Triple>
<subject><uri>&ns1;pat</uri></subject>
<predicate><uri>&rdf;type</uri></predicate>
<object><uri>&bio;Human</uri></object>
</Triple>
<Triple>
<subject><uri>&ns1;pat</uri></subject>
<predicate><uri>&per;name</uri></predicate>
<object><literal>Pat Smith</literal></object>
</Triple>
<Triple>
<subject><uri>&ns1;pat</uri></subject>
<predicate><uri>&per;pet</uri></predicate>
<object><nodeID>genid1</nodeID></object>
</Triple>
<Triple>
<subject><nodeID>genid1</nodeID></subject>
<predicate><uri>&rdf;type</uri></predicate>
<object><uri>&bio;Dog</uri></object>
</Triple>
<Triple>
<subject><nodeID>genid1</nodeID></subject>
<predicate><uri>&per;name</uri></predicate>
<object><literal>Rover</literal></object>
</Triple>
</Graph>
Prolog
不使用命名空间,很简洁:
human(pat).
dog(rover). % we have to assign a name
name(pat, "Pat Smith").
name(pat, "Patrick Smith").
name(rover, "Rover").
pet(pat, rover).
使用命名空间的方案之一:
ns(ns1_, "http://www.w3.org/2000/10/swap/test/demo1/about-pat").
ns(bio_, "http://www.w3.org/2000/10/swap/test/demo1/biology#").
ns(per_, "http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#").
bio_Human(ns1_pat).
bio_Dog(rover). # unprefix could be be NodeIDs...
per_name(ns1_pat, "Pat Smith").
per_name(ns1_pat, "Patrick Smith").
per_name(rover, "Rover").
per_pet(ns1_pat, rover).
Javascript与Python
RDF模型可以相当好的映射到一些通用编程结构,尤其是那些解释型的面向对象语言,比如JavaScript和Python。当然,这种映射并不完美。
忽略URI和基数问题的简单方案—只允许Pat有一个名字和一个宠物:
pat = Human()
rover = Dog()
pat.name = "Pat Smith"
rover.name = "Rover"
pat.pet = rover
考虑基数的方案:
pat = Human()
rover = Dog()
pat.name.append("Pat Smith")
rover.name.append("Rover")
pat.pet.append(rover
再完善一点,需要这样—这会丢掉内置模型的简单性:
pat = Resource()
rover = Resource()
pat.addProperty( ns.rdf.type, ns.bio.Human)
rover.addProperty(ns.rdf.type, ns.bio.Dog)
pat.addProperty( ns.per.name, "Pat Smith")
rover.addProperty(ns.per.name, "Rover")
pat.addProperty( ns.perpet, rover)
SQL
Table Relatiing URIs to Internal IDs
如果uri是NULL,那么这个资源就是匿名的,如aNode, RDF/XML中的NodeID。
CREATE TABLE uri (
id INT AUTO_INCREMENT PRIMARY KEY, # PRIMARY = UNIQUE and NOT NULL
uri BLOB, # BLOB is also called LONGVARBINARY
UNIQUE KEY uri (uri(64)) # length is just a tuning knob
);
INSERT INTO uri (uri) VALUES ('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
INSERT INTO uri (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/biology#Human');
INSERT INTO uri (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/biology#Dog');
INSERT INTO uri (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name');
INSERT INTO uri (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#pet');
INSERT INTO uri (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat');
INSERT INTO uri (uri) VALUES (NULL); # this is rover, who has no URI
Id | Uri |
1 | http://www.w3.org/1999/02/22-rdf-syntax-ns#type |
2 | http://www.w3.org/2000/10/swap/test/demo1/biology#Human |
3 | http://www.w3.org/2000/10/swap/test/demo1/biology#Dog |
4 | http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name |
5 | http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#pet |
6 | http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat |
7 | NULL |
选择1:一张表,每个断言一列
这是一个简单、凭直觉的方案,但是:
² 每次使用一个新断言,就要添加一个新列
² Predicates must be known to be individual-valued or data-valued; and if data-valued, then what type?
² 无法拥有多值属性
CREATE TABLE resource (
id INT PRIMARY KEY,
type INT, # http://www.w3.org/1999/02/22-rdf-syntax-ns#type
name varchar(255), # http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name
pet INT # http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#pet
);
INSERT INTO resource (id, type, name, pet) VALUES (6, 2, 'Pat Smith', 7);
INSERT INTO resource (id, type, name) VALUES (7, 3, 'Rover');
Id
| type
| name
| Pet
|
6
| 2
| Pat Smith
| 7
|
7
| 3
| Rover
| NULL
|
选择2:每个类一张表,每个断言一列
这里,由于不支持继承,所以human.name和dog.name之间有概念冗余。
CREATE TABLE human ( # http://www.w3.org/2000/10/swap/test/demo1/biology#Human
id INT PRIMARY KEY,
name varchar(255), # http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name
pet INT # http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#pet
);
CREATE TABLE dog ( # http://www.w3.org/2000/10/swap/test/demo1/biology#Dog
id INT PRIMARY KEY,
name varchar(255) # http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name
);
INSERT INTO human VALUES (6, 'Pat Smith', 7);
INSERT INTO dog VALUES (7, 'Rover');
mysql> select * from human, dog where human.pet=dog.id;
Id | name | Pet | id | Name |
6 | Pat Smith | 7 | 7 | Rover |
选择3:每个断言一张表
这里指出完全一样的值。
CREATE TABLE name ( # http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name
subject INT NOT NULL,
object varchar(255),
INDEX(subject),
UNIQUE INDEX(subject, object)
);
CREATE TABLE pet ( # http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#pet
subject INT NOT NULL,
object INT,
INDEX(subject),
UNIQUE INDEX(subject, object)
);
CREATE TABLE type ( # http://www.w3.org/1999/02/22-rdf-syntax-ns#type
subject INT NOT NULL,
object INT,
INDEX(subject),
UNIQUE INDEX(subject, object)
);
INSERT INTO name VALUES (6, 'Pat Smith');
INSERT INTO name VALUES (6, 'Patrick Smith');
INSERT INTO name VALUES (7, 'Rover');
INSERT INTO pet VALUES (6, 7);
INSERT INTO type VALUES (6, 2);
INSERT INTO type VALUES (7, 3);
mysql> select * from name;
subject
| object
|
6
| Pat Smith
|
6
| Patrick Smith
|
7
| Rover
|
mysql> select * from pet;
Subject
| Object
|
6
| 7
|
mysql> select * from type;
subject | Object |
6 | 2 |
7 | 3 |
选择4:一个三元式的表,一个资源的表
如果用文字和URIs重新将URIs表做成一个资源表resources,会有更多选择。
CREATE TABLE resources (
id INT AUTO_INCREMENT PRIMARY KEY, # PRIMARY = UNIQUE and NOT NULL
# either provide a uri
uri BLOB,
# or a literal_value, which might have a datatype and language
literal_value BLOB,
datatype INT,
language VARCHAR(5),
UNIQUE KEY (uri(64)) # length is just a tuning knob
);
INSERT INTO resources (uri) VALUES ('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
INSERT INTO resources (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/biology#Human');
INSERT INTO resources (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/biology#Dog');
INSERT INTO resources (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name');
INSERT INTO resources (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#pet');
INSERT INTO resources (uri) VALUES ('http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat');
INSERT INTO resources (uri) VALUES (NULL); # this is rover, who has no URI
INSERT INTO resources (literal_value) VALUES ('Pat Smith');
INSERT INTO resources (literal_value) VALUES ('Patrick Smith');
INSERT INTO resources (literal_value) VALUES ('Rover');
mysql> select * from resources;
id | uri | Literal_value | datatype | language |
1 | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | NULL | NULL | NULL |
2 | http://www.w3.org/2000/10/swap/test/demo1/biology#Human | NULL | NULL | NULL |
3 | http://www.w3.org/2000/10/swap/test/demo1/biology#Dog | NULL | NULL | NULL |
4 | http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#name | NULL | NULL | NULL |
5 | http://www.w3.org/2000/10/swap/test/demo1/friends-vocab#pet | NULL | NULL | NULL |
6 | http://www.w3.org/2000/10/swap/test/demo1/about-pat#pat | NULL | NULL | NULL |
7 | NULL | NULL | NULL | NULL |
8 | NULL | Pat Smith | NULL | NULL |
9 | NULL | Patrick Smith | NULL | NULL |
10 | NULL | Rover | NULL | NULL |
三元式:
CREATE TABLE triples (
subject INT NOT NULL,
predicate INT NOT NULL,
object INT NOT NULL,
UNIQUE INDEX(subject, predicate, object),
INDEX(predicate, object),
INDEX(object, predicate)
);
INSERT INTO triples VALUES (6, 4, 8);
INSERT INTO triples VALUES (6, 4, 9);
INSERT INTO triples VALUES (7, 4, 10);
INSERT INTO triples VALUES (6, 1, 2);
INSERT INTO triples VALUES (7, 1, 3);
INSERT INTO triples VALUES (6, 5, 7);
mysql> select * from triples;
subject | predicate | object |
6 | 1 | 2 |
6 | 4 | 8 |
6 | 4 | 9 |
6 | 5 | 7 |
7 | 1 | 3 |
7 | 4 | 10 |
mysql> select s.id, s.uri, p.uri as "predicate",
o.id, o.uri, o.literal_value as "lit"
from triples, resources as s, resources as p, resources as o
where s.id=triples.subject AND
p.id=triples.predicate AND
o.id=triples.object;
subject predicate object ObjectValue
id | uri | predicate | id | uri | lit |
6 | ……#pat | ……#type | 2 | ……#Human | NULL |
6 | ……#pat | ……#name | 8 | NULL | Pat Smith |
6 | ……#pat | ……#name | 9 | NULL | Patrick Smith |
6 | ……#pat | ……#pet | 7 | NULL | NULL |
7 | NULL | ……#type | 3 | ……#Dog | NULL |
7 | NULL | ……#name | 10 | NULL | Rover |
But Remember
当心将RDF看做序列化对象的一种格式(Beware of thinking of RDF as a format for serializing object),语义网是不同的。
² Any document can (potentially) say anything about anything. There is no set of "slots" or "attributes" for a class. The properties defined in a schema are not the only properties which one can use to describe something which is in that class.
² An object can be in many classes. When you create a semantic web document about something, others can deduce more things about it, in vocabularies you have never heard of.
² Entity-Relationship and UML diagrams are useful for describing RDF -- so long as you remember the above.
一个有趣的挑战是,怎样使类似 N3 的语言和面向对象的程序语言融为一体。最后
以上就是刻苦大象为你收集整理的Comparing Formats(Comparing with other data formats)学习笔记的全部内容,希望文章能够帮你解决Comparing Formats(Comparing with other data formats)学习笔记所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复