11.  N-Triples

Note also that N-Triples module is separated from RDFS system. You must load it to use it.

11.1  N-Triple Parser

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"))

read-NTriple-file accepter-fun [ file [ code ]]
[Function]
This function reads and parses a file that contains N-Triple format data, then passes three strings of subject/predicate/object to accepter-fun. The code is a character code of file. The default value of code is ':utf-8'.

addTriple-from-file subject predicate object
[Function]
subject, predicate, and object are strings. This function interprets three parameters, and adds the triple data into memory. Actually, this function interns three symbols or interns three uris of parameters, and invokes addTriple.

11.2  Adding N-Triples

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.

addTriple subject (predicate (eql rdf:type)) object
[Method]
If subject and/or object is a URI, it is changed to an iri. If object is a symbol or iri, it is changed to an instance of rdfs:Class. Finally, subject is defined as an instance of object using addClass if object is a metaclass, or addInstance if object is a class. When subject is already an instance of the object, nothing happens. This method involves the range entailment on rdf:type for an undefined object.

addTriple subject (predicate (eql rdfs:subClassOf)) object
[Method]
If subject and/or object is a URI, it is changed to an iri. If subject and/or object is an iri, it is changed to an instance of rdfs:Class. If subject and/or object is a resource but not a class, it is changed to an instance of rdfs:Class. In the case that subject and object are classes, if subject is already a subclass of object, then nothing is done, else this method redefines superclasses of subject with addClass with the most specific concepts of previous superclasses and object.

addTriple subject (predicate (eql rdfs:subPropertyOf)) object
[Method]
If subject and/or object is a URI, it is changed to an iri. If subject and/or object is an iri, it is changed to an instance of rdf:Property. If subject and/or object is a resource but not a property, it is changed to an instance of rdf:Property. In the case that subject and object are properties, if subject is already a subproperty of object, then nothing is done, else this method redefines subject as an subproperty of object using addInstance with the most specific properties of previous superproperties and object.

addTriple subject (predicate rdf:Property) object
[Method]
If subject is a URI, it is changed to an iri. If subject is a symbol or an iri, and object is a symbol or an iri, then object is defined with the range constraint from predicate, if it is not defined, otherwise it is redefined with the range constraint. If subject is a symbol or an iri, and object is a number or string or a resource, then subject is defined using the domain restriction from predicate. If subject is a resource, it is checked using the domain restriction from predicate and this triple is installed using addInstance or addClass with the most specific domain class.

addTriple subject (predicate symbol) object
[Method]
When predicate is undefined, then defines it as instance of rdf:Property. This function recursive-calls with getting the value of predicate.

addTriple subject (predicate net.uri:uri) object
[Method]
This function translates predicate URI to a QName, then recursively calls with the predicate QName. This function also adds predicate URI to rdf:about slot of predicate object.

addTriple (subject net.uri:uri) predicate object
[Method]
This method changes subject to iri, then recursively calls with the subject of iri.

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"))))

defTriple subject predicate object
[Macro]
/. subject predicate object
[Macro]
./ subject predicate object
[Macro]
This macro makes the form '(addTriple subject predicate object)'.

11.3  N-Triple Writer

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.

get-triple resource
[Function]
returns the definition of resource in S-expression of triples. Some nodeIDs are named as 'gxnnn' for anonymous nodes in triple.

write-triple triple [ stream ]
[Function]
This function prints out triple, which is a list of three element subject/predicate/object.

write-nt resource [ stream ]
[Function]
This function gets triples of resource with get-triple and prints out them using write-triple. For anonymous resource, a nodeID to which a resource object is bound must be given. This functions returns no value.


Author:
Seiji Koide. Copyright (c) 2006 GALAXY EXPRESS CORPORATION. Mar. 2006
Copyright (c) 2007-2010 Seiji Koide. Oct.2010