From W3C Wiki
Revision as of 21:58, 21 October 2011 by Tinkster (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Shorthand RDF


The XML serialization of RDF is sufficiently verbose that shorthand syntaxes naturally spring up in email, IRC, and handheld devices.


While NotationThree, a text editor, and a tool to convert to RDF/XML work well for programmer types, other AuthoringToolsForRDF generally work with more direct-manipulation style of interaction.

Hmm... I'm not likely to use the colons when scribbling in my PDA. Neither was norm. I like to write dates ala 7Apr2003. Hmm...

@namepattern "\d\d?[A-Z][a-z][a-z]\d\d\d\d" <>.

<#thisEvent> cyc:startingDate 7Apr2003.

would work like

<#thisEvent> cyc:startingDate <>.

or perhaps use datatypes...

@datatype "[+-]?\d+" <http://...#integer>.
@datatype "\d\d?[A-Z][a-z][a-z]\d\d\d\d" <>.

:lunch :price 20; :date 7Apr2003.

would work like

:lunch :price "20"^^<http://...#integer>;
  :date "7Apr2003"^^<>.

RDF-TriN3 Implementation

I'm implementing this in RDF-TriN3 and hoping to converge on a stable syntax. RDF-TriN3 implements this on top of an N3 parser, though implementing an RDF-compatible subset on top of a Turtle parser would also be a useful goal.

RDF-TriN3 currently supports the following shorthand extensions to N3:

 @term token <uri> .
 @term token "literal" .
 @pattern "regexp" <uri_template> .
 @pattern "regexp" "literal template" .
 @dtpattern "regexp" <datatype_uri> .
 @namepattern "regexp" <uri_prefix> .
 @import <uri> .
 @profile <uri> .


The shorthand @term token <uri> defines the token to be shorthand for the full URI <uri>. For example:

 @prefix foaf: <> .
 @term called <> .
 @term Dude foaf:Person .
 @prefix foaf: <> . # doesn't effect previous definition!
 [] a Dude ; called "Toby Inkster" .

is equivalent to:

 @prefix foaf: <> .
 [] a foaf:Person ; foaf:name "Toby Inkster" .

Terms (which are case-sensitive, like prefixes) can also be defined as literals:

 @term Zero "0"^^<> .
 :thing :temperature Zero .


The shorthand @pattern "regexp" <uri_template> treats the provided plain literal as a pattern to match against tokens as a regular expression.

 @pattern "(\d{2})-(\d{2})-(\d{4})" <$3/$2/$1> .
 [] :born 01-06-1980 .

is equivalent to:

 [] :born <> .

As is:

 @pattern "(?<d>\d{2})-(?<m>\d{2})-(?<y>\d{4})" <$y/$m/$d> .
 [] :born 01-06-1980 .


 @pattern "(?<d>\d{2})-(?<m>\d{2})-(?<y>\d{4})" <${y}/${m}/${d}> .
 [] :born 01-06-1980 .

Patterns can also expand to literals.

@namepattern and @dtpattern

@namepattern and @dtpattern are shortcut for things that could have been written out as @pattern:

 @namepattern "\d\d?[A-Z][a-z][a-z]\d\d\d\d" <> .

is shorthand for:

 @pattern "\d\d?[A-Z][a-z][a-z]\d\d\d\d" <$0> .


 @dtpattern "\d\d?[A-Z][a-z][a-z]\d\d\d\d" <> .

is shorthand for:

 @pattern "\d\d?[A-Z][a-z][a-z]\d\d\d\d" "$0"^^<> .

@import and @profile

@import essentially inlines an external N3, Turtle or Shorthand RDF file into the current file. It parses the file linked to, copies its triples into the current graph, and copies its terms, patterns and prefixes into the current parsing context. It does not alter the current parsing context's base URI. It cannot overwrite the empty prefix. It also adds this triple to the current graph:

 <> owl:imports <imported_uri> .

@profile performs a subset of @import. It just copies the terms, patterns and prefixes into the current parsing context. It does not add any triples to the current graph.

Resolution Order

Like @prefix and @base, the last matching rule "wins".

As @base may occur several times in an N3 document, changing the base URI, it is important to note that any patterns and tokens which are defined in terms of a relative URI use the base URI at the time of their definition, not the time of their use. For example:

 @base <> .
 @pattern "\~(\S+)" <$1> .
 @base <> .
 @pattern "Doc(\d+)" <$1> .
 @base <> .
 Doc1234 <creator> ~tobyink .

Is equivalent to:

         <> .