-
Notifications
You must be signed in to change notification settings - Fork 12
/
Primer.html
333 lines (278 loc) · 14.5 KB
/
Primer.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Primer - Getting into the semantic web and RDF using N3</title>
<link href="doc/style.css" rel="stylesheet" type="text/css" />
</head>
<body xml:lang="en" lang="en">
<div class="noprint">
<a href="/">W3C</a> | <a href="/2001/sw/Overview.html">Semantic Web</a> | <a
href="/2000/01/sw/Overview.html">Advanced Development</a> | <a
href="/2000/10/swap/">SWAP</a> | <a href="/2000/10/swap/doc/">Tutorial</a> |
Primer</div>
<h1>Primer: Getting into RDF & Semantic Web using N3</h1>
<p>[<a href="http://www.w3.org/2003/03/Translations/byTechnology?technology=Primer-html">translations into other languages
</a>]</p>
<p>The world of the semantic web, as based on RDF, is really simple at the
base. This article shows you how to get started. It uses a simplified
teaching language -- Notation 3 or N3 -- which is basically equivalent to RDF
in its XML syntax, but easier to scribble when getting started.</p>
<h2 id="L553">Subject, verb and object</h2>
<p>In RDF, information is simply a collection of statements, each with a
subject, verb and object - and nothing else. In N3, you can write an RDF
triple just like that, with a period:</p>
<pre><#pat> <#knows> <#jo> .</pre>
<p>Everything, be it subject, verb, or object, is identified with a Uniform
Resource Identifier. This is something like <http://www.w3.org/> or
<http://www.w3.org/2000/10/swap/test/s1.n3#includes>, but when
everything is missed out before the "#" it identifies <#pat> in the
current document whatever it is.</p>
<p>There is one exception: the object (only) can be a literal, such as a
string or integer:</p>
<pre><#pat> <#knows> <#jo> .
<#pat> <#age> 24 .</pre>
<p>The verb "knows" is in RDF called a "property" and thought of as a noun
expressing a relation between the two. In fact you can write</p>
<pre><#pat> <#child> <#al> .</pre>
<p>alternatively, to make it more readable, as either</p>
<pre><#pat> has <#child> <#al> .</pre>
<p>or</p>
<pre><#al> is <#child> of <#pat> .</pre>
<p>There are two shortcuts for when you have several statements about the
same subject: a semicolon ";" introduces another property of the same
subject, and a comma introduces another object with the same predicate and
subject.</p>
<pre><#pat> <#child> <#al>, <#chaz>, <#mo> ;
<#age> 24 ;
<#eyecolor> "blue" .</pre>
<p>So, for example, the data in the table</p>
<table border="1" align="center">
<tbody>
<tr>
<td></td>
<td>age</td>
<td>eyecolor</td>
</tr>
<tr>
<td>pat</td>
<td>24</td>
<td>blue</td>
</tr>
<tr>
<td>al</td>
<td>3</td>
<td>green</td>
</tr>
<tr>
<td>jo</td>
<td>5</td>
<td>green</td>
</tr>
</tbody>
</table>
<p>could be written</p>
<pre> <#pat> <#age> 24; <#eyecolor> "blue" .
<#al> <#age> 3; <#eyecolor> "green" .
<#jo> <#age> 5; <#eyecolor> "green" .</pre>
<p>Sometimes there are things involved in a statement don't actually have any
identifier you want to give them - you know one exists but you only want to
give the properties . You represent this by square brackets with the
properties inside.</p>
<pre><#pat> <#child> [ <#age> 4 ] , [ <#age> 3 ].</pre>
<p>You could read this as #pat has a #child which has #age of "4" and a
#child which has an #age of "3". There are two important things to
remember</p>
<ul>
<li>The identifiers are just identifiers - the fact that the letters p a t
are used doesn't tell anyone or any machine that we are talking about
anyone whose name is "Pat" -- unless we say <#pat> <#name>
"Pat". The same applies to the verbs - never take the actual letters c h
i l d as telling you what it means - we will find out how to do that
later.</li>
<li>The square brackets declare that something exists with the given
properties, but don't give you a way to refer to it elsewhere in this or
another document.</li>
</ul>
<p>If we actually want to use a name, we could have written the table above
as</p>
<pre> [ <#name> "Pat"; <#age> 24; <#eyecolor> "blue" ].
[ <#name> "Al" ; <#age> 3; <#eyecolor> "green" ].
[ <#name> "Jo" ; <#age> 5; <#eyecolor> "green" ].</pre>
<p>There are many ways of combining square brackets - but you can figure that
out from the examples later on. There is not much left learn about using N3
to express data, so let us move on.</p>
<h2 id="Sharing">Sharing concepts</h2>
<p>The semantic web can't define in one document what something means. That's
something you can do in english (or occasionally in math) but when we really
communicate using the concept "title", (such in a library of congress catalog
card or a web page), we rely on a shared concept of "title". On the semantic
web, we share quite precisely by using exactly the same URI for the concept
of title.</p>
<p>I could try to give the title of an N3 document by</p>
<pre><> <#title> "A simple example of N3".</pre>
<p>(The <> being an empty URI reference always refers to the document
it is written in.) The <#title> refers to the concept of #title as
defined by the document itself. This won't mean much to the reader. However,
a group of people created a list of properties called the <a
href="http://purl.oclc.org/dc/">Dublin Core</a>, among which is their idea of
title, which they gave the identifier</p>
<p><http://purl.org/dc/elements/1.1/title>. So we can make a much
better defined statement if we say</p>
<pre><> <http://purl.org/dc/elements/1.1/title>
"Primer - Getting into the Semantic Web and RDF using N3".</pre>
<p>That of course would be a bit verbose - imagine using such long
identifiers for everything like #age and #eyecolor above. So N3 allows you to
set up a shorthand prefix for the long part - the part we call the
<dfn>namespace</dfn>. You set it up using "@prefix" like this:</p>
<pre>@prefix dc: <http://purl.org/dc/elements/1.1/> .
<> dc:title
"Primer - Getting into the Semantic Web and RDF using N3".</pre>
<p>Note that when you use a prefix, you use a colon instead of a hash between
dc and title, and you don't use the <angle brackets> around the whole
thing. This is much quicker. This is how you will see and write almost all
your predicates in N3. Once set up, a prefix can be used for the rest of the
file.</p>
<p>There are an increasingly large number of RDF vocabularies for you to
refer to - check the <a href="../../../../RDF/">RDF home page</a> and things
linked from it - and you can build your own for your own applications very
simply.</p>
<p>From now, on we are going to use some well known namespaces, and so to
save space, I will just assume the prefixes</p>
<pre>@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .</pre>
<p>These are the RDF, RDF schema, and OWL namespaces, respectively. They give
us the core terms which we can bootstrap ourselves into the semantic web. I
am also going to assume that the empty prefix stands for the document we are
writing, which we can say in N3 as</p>
<pre>@prefix : <#> .</pre>
<p>This means we could have the example above as</p>
<pre>:pat :child [ :age 4 ] , [ :age 3 ].</pre>
<p>which is slightly fewer characters to type. Now you understand how to write
data in N3, you can start making up your own vocabularies, because they are
just data themselves.</p>
<h2>Making vocabularies</h2>
<p>Things like dc:title above are RDF <dfn><a id="Properties1"
name="Properties1">Properties</a></dfn>. When you want to define a new
vocabulary you define new classes of things and new properties. When you say
what type of thing something is, you say a <dfn><a id="Class2"
name="Class2">Class</a></dfn> it belongs to.</p>
<p>The property which tells you what type something is is
<code>rdf:type</code> which can be abbreviated to N3 to just <code>a</code>.
So we can define a class of person</p>
<pre>:Person a rdfs:Class.</pre>
<p>In the same document, we could introduce an actual person</p>
<pre>:Pat a :Person.</pre>
<p>Classes just tell you about the thing which is in them. An object can be
in many classes. There doesn't have to be any hierarchical relationship --
think of Person, AnimateObject, Animal, TallPerson, Friend, and so on. If
there is a relationship between two classes you can state it - check out the
properties (of classes) in the <a href="http://www.w3.org/TR/rdf-schema/">RDF
Schema</a> and <a href="http://www.w3.org/TR/owl-guide/">OWL</a>
vocabularies.</p>
<pre>:Woman a rdfs:Class; rdfs:subClassOf :Person .</pre>
<p>A property is something which is used to declare a relationship between
two things.</p>
<pre>:sister a rdf:Property.</pre>
<p>Sometimes when a relationship exists between two things, you immediately
know something about them, which you can express as a class. When the subject
of any property must be in a class, that class is a <dfn><a name="domain"
id="domain">domain</a></dfn> of the property. When the object must be in a
class, that class is called the <dfn><a name="range"
id="range">range</a></dfn> of a property. A property can have many domains
and ranges, but typically one specifies one.</p>
<pre>:sister rdfs:domain :Person;
rdfs:range :Woman.</pre>
<p>Note the class identifiers start with capitals and properties with lower
case letters. This is not a rule, but it is a good convention to stick to.
Note also that because the domain of rdfs:range and rdfs:domain themselves is
rdf:Property, it follows that :sister is a rdf:Property without it being
stated explicitly.</p>
<h4>Equivalence</h4>
<p>Often, you define a vocabulary where one or more of the terms, whether or
not you realized it when you started, is in fact exactly the same as one in
another vocabulary. This is a really useful titbit of information for any
machine or person dealing with the information! The property of equivalence
between two terms is so useful and fundamental that N3 has a special
shorthand for it, "=".</p>
<pre>:Woman = foo:FemaleAdult .
:Title a rdf:Property; = dc:title .</pre>
<p>Tip: Use other people's vocabularies when you can - it helps interchange
of data. When you define your own vocabulary which includes synonyms, do
record the equivalence because this, likewise, will help present and future
processors process your and others' data in meaningful ways.</p>
<h4>Choosing a namespace and publishing your vocabulary</h4>
<p>Good on-line documentation for vocabulary terms helps people read and
write RDF data. Writers need to see how a term is supposed to be used;
readers need to see what it is supposed to mean. People developing software
which uses the terms need to know in particular detail exactly what each URI
means.</p>
<p>If you document your vocabulary using the <a
href="http://www.w3.org/TR/rdf-schema/">RDF Schema</a> and <a
href="http://www.w3.org/TR/owl-guide/">OWL</a> vocabularies, then your
documentation will be machine-readable in a variety of interesting and useful
ways, as mentioned above and covered in more detail in <a
href="doc/ontologies">Vocabulary Documentation</a>. This kind of
RDF-documentation-in-RDF is sometimes called a "schema" or an "ontology."</p>
<div id="publish">
<p>The easiest way to help people find your documentation is to make the URIs
you create as vocabulary terms also work in a web browser. This happens
automatically if you follow the naming convention we use here, where the
vocabulary definition document has a URI like
<code>http://example.com/terms</code> and it refers to its terms like
<code><#Woman></code>. With the @prefix declaration above, this gives
the URI <code>http://example.com/terms#Woman</code> which should work in any
browser to display the definition document.</p>
<p>Ideally, you should publish your documentation on the web using a server
and portion of URI-space which are owned by an organization which can commit
to maintaining them well into the future. That way, many years down the road,
RDF data using your terms will still be documented and potentially
understandable. The convention of putting the current year into the URI can
help with stability; some day people may be tempted to re-use
<code>http://example.com/food-vocabulary</code>, but they will probably only
touch <code>http://example.com/2003/food-vocabulary</code>, when they really
mean to upgrade the documentation there. In some circumstances you can also
achieve increased stability by using a specialized domain name which may be
insulated from possible organizational renaming and trademark issues.</p>
</div>
<p>Of course if you are just playing around, you can use a file (say
<code>mydb.n3</code>) in the same directory as the rest of your work. When
you do that, your can simply use <<code>mydb.n3#></code> as your
namespace identifier, because in N3 (as in HTML), the URIs can be specified
relative to the current location.</p>
<div class="noprint">
<h3>More</h3>
<p>Now you know all you need to start creating your own vocabularies, or
ontologies, and you have pointers to where to look for the richer ways of
defining them.You don't have to go any further, as what you have now will
allow you to create new applications, and create schemas, data files, and
programs which interchange and manipulate data for the semantic web.</p>
<p>At this point, you should be getting the hang of it and be writing stuff.
To give you some more ideas, though, there is a <a href="Examples">longer
list of more complex and varied examples</a>. These come with less tutorial
explanation.</p>
<p>Or, you can continue with a tutorial which goes into mroe features of the
language, explaining how to process you data and involve other data on the
Web. In that case, next bit is about: <a href="doc/Shortcuts.html">Shortcuts
and long cuts</a></p>
</div>
<div class="noprint">
<hr />
<h2 id="References">References</h2>
<ul>
<li><a href="Examples.html">Many More Examples</a></li>
<li><a href="/DesignIssues/Notation3.html">Notation3 - Design Issues
article</a></li>
</ul>
</div>
<div class="noprint">
<address>
<a href="/People/Berners-Lee/">Tim BL</a>, with his director hat off<br />
<small>$Id$</small>
</address>
</div>
</body>
</html>