Note also that N-Triples module is separated from RDFS system. You must load it to use it.
The N-Triples is a textual expression of RDF graph. A line in N-Triples represents one triple of subject/predicate/object. The proper syntax of N-Triple requires URI references and does not allow QNames. However, we relaxed the syntax to allow non-ASCII character sets and QNames instead of URI references.
The graph at Figure4.1 is expressed in N-Triples as follows. To represent a blank node, you need to use a nodeID in N-Triples. Note that there is one period at the end of each line, where the tilde, locally in the followings, means the line continuation.
<http://www.w3.org/TR/rdf-syntax-grammar>
<http://www.example.org/terms/editor> _:a01 .
_:a01
<http://www.example.org/terms/homePage>
<http://purl.org/net/dajobe/> .
_:a01
<http://www.example.org/terms/fullName> "Dave Becket"
.
<http://www.w3.org/TR/rdf-syntax-grammar>
<http://purl.org/dc/elements/1.1./title> ~
"RDF/XML Syntax
Specification (Revised)" .
As you see, the strict syntax of N-Triple is tedious for using URIs. So, we have relaxed it so as to accept the corresponding QNames instead of the URIs.
<http://www.w3.org/TR/rdf-syntax-grammar> ex:editor _:a01
.
_:a01 ex:homePage <http://purl.org/net/dajobe/> .
_:a01
ex:fullName "Dave Becket" .
<http://www.w3.org/TR/rdf-syntax-grammar>
dc:title ~
"RDF/XML Syntax Specification (Revised)" .
The function read-NTriple-file reads the relaxed N-Triple format file, and parses each line to three strings of subject/predicate/object. This function is usually used with addTriple-from-file, then addTriple-from-file accepts and interprets subject/predicate/object strings, including strings that stands for QNames. URIs for resources do not cause to invoke uri2symbol in reading, so the generated CLOS objects are bound only to iri values. QNames for resources cause to invoke a query for users in case that the namespace is unknown. In the example below, this shows a right and bottom corner part of Figure7.1.
gx-user(2): (read-NTriple-file #'addTriple-from-file
"RDFS/JenaEx.nt")
Warning: Entail in _:a001 vCard:Family "Smith":
.....
vCard:Family rdf:type rdf:Property.
Warning: Entail in _:a001 vCard:Family
"Smith":
..... _:a001 rdf:type rdfs:Resource
Warning: Entail in _:a001
vCard:Given "John":
..... vCard:Given rdf:type rdf:Property.
Warning:
Entail in <http://somewhere/JohnSmith> vCard:N _:a001:
..... vCard:N
rdf:type rdf:Property.
Warning: Entail in <http://somewhere/JohnSmith>
vCard:N _:a001:
..... <http://somewhere/JohnSmith> rdf:type
rdfs:Resource.
Warning: Entail in <http://somewhere/JohnSmith> vCard:FN
"John Smith":
..... vCard:FN rdf:type rdf:Property.
:done
gx-user(3):
(get-form _:a001)
(|rdfs:Resource| (vCard:Family "Smith") (vCard:Given
"John"))
gx-user(4): (get-form
<<http://somewhere/JohnSmith>>)
(|rdfs:Resource| (rdf:about
<http://somewhere/JohnSmith>)
(vCard:N (|rdfs:Resource| (vCard:Family
"Smith") (vCard:Given "John")))
(vCard:FN "John Smith"))
The method addTriple adds an N-Triple, i.e., one subject/predicate/object, into memory. In fact, there are many methods of addTriple. One method is a piece of building blocks, and each method burdens with a part of a whole work parted into pieces. In the followings, addTriple methods are categorized by combination patterns of parameter, but note that one category is also a collection of several methods.
For convenience to programmers, three define macros are prepared in S-expression. defTriple and /. and ./ are completely the same for N-Triple definition. After the example above of read-NTriple-file, the followings demonstrate the example of defTriple.
gx-user(2): (defpackage
vCard)
#<The vCard package>
gx-user(3): (defTriple
<http://somewhere/JohnSmith> vCard::FN "John Smith")
Warning: Entail
in <http://somewhere/JohnSmith> vCard:FN "John Smith":
..... vCard:FN
rdf:type rdf:Property.
Warning: Entail in <http://somewhere/JohnSmith>
vCard:FN "John Smith":
..... <http://somewhere/JohnSmith> rdf:type
rdfs:Resource
#<|rdfs:Resource| :anonymous>
gx-user(4): (defTriple
<http://somewhere/JohnSmith> vCard::N _:a001)
Warning: Entail in
<http://somewhere/JohnSmith> vCard:N _:a001:
..... vCard:N rdf:type
rdf:Property.
#<|rdfs:Resource| :anonymous>
gx-user(5): (defTriple
_:a001 vCard::Family "Smith")
Warning: Entail in _:a001 vCard:Family
"Smith":
..... vCard:Family rdf:type rdf:Property.
#<|rdfs:Resource|
:anonymous>
gx-user(6): (defTriple _:a001 vCard::Given "John")
Warning:
Entail in _:a001 vCard:Given "John":
..... vCard:Given rdf:type
rdf:Property.
#<|rdfs:Resource| :anonymous>
gx-user(7): (get-form
<<http://somewhere/JohnSmith>>)
(|rdfs:Resource| (rdf:about
<http://somewhere/JohnSmith>) (vCard:FN "John Smith")
(vCard:N
(|rdfs:Resource| (vCard:Family "Smith") (vCard:Given
"John"))))
The resource forms are also obtained as list of triples and also printed out in N-Triple format as well as the object-centered form by get-form and write-xml. The function get-triple returns a list of relaxed N-Triples in S-expression that are composed QNames rather than URIs, and write-nt prints them in exact N-Triples format. See the followings.
gx-user(12): (get-triple
<<http://somewhere/JohnSmith>>)
((
rdf:type |rdfs:Resource|)
( vCard:FN "John
Smith")
( vCard:N _:gx5)
(_:gx2
rdf:type |rdfs:Resource|)
(_:gx2 vCard:Family "Smith")
(_:gx2
vCard:Given "John"))
gx-user(13): (setf (documentation (find-package :vCard)
t)
"http://www.w3.org/2001/vcard-rdf/3.0")
"http://www.w3.org/2001/vcard-rdf/3.0"
gx-user(14):
(set-uri-namedspace-from-pkg (find-package :vCard))
#<The vCard
package>
gx-user(15): (write-nt
<<http://somewhere/JohnSmith>>)
<http://somewhere/JohnSmith>
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
~
<http://www.w3.org/2000/01/rdf-schema#Resource>
.
<http://somewhere/JohnSmith>
<http://www.w3.org/2001/vcard-rdf/3.0#FN> ~
"John Smith"
.
<http://somewhere/JohnSmith>
<http://www.w3.org/2001/vcard-rdf/3.0#N> _:gx10 .
_:gx3
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
~
<http://www.w3.org/2000/01/rdf-schema#Resource> .
_:gx3
<http://www.w3.org/2001/vcard-rdf/3.0#Family> "Smith" .
_:gx3
<http://www.w3.org/2001/vcard-rdf/3.0#Given> "John" .
Note that at the line number 12 and 15 in the above example, a nodeID symbol is automatically newly generated by get-triple, and given to write-nt rather than nodeID given by users.