ShorthandRDF

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

History

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

e.g.

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" <http://www.w3.org/2002/12/cal/date#>.

<#thisEvent> cyc:startingDate 7Apr2003.


would work like


<#thisEvent> cyc:startingDate <http://www.w3.org/2002/12/cal/date#7Apr2003>.


or perhaps use datatypes...


@datatype "[+-]?\d+" <http://...#integer>.
@datatype "\d\d?[A-Z][a-z][a-z]\d\d\d\d" <http://www.w3.org/2002/12/cal/types#FriendlyDate>.

:lunch :price 20; :date 7Apr2003.


would work like


:lunch :price "20"^^<http://...#integer>;
  :date "7Apr2003"^^<http://www.w3.org/2002/12/cal/types#FriendlyDate>.

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> .

@term

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

 @prefix foaf: <http://xmlns.com/foaf/0.1/> .
 @term called <http://xmlns.com/foaf/0.1/name> .
 @term Dude foaf:Person .
 @prefix foaf: <http://example.com/not-foaf/> . # doesn't effect previous definition!
 
 [] a Dude ; called "Toby Inkster" .

is equivalent to:

 @prefix foaf: <http://xmlns.com/foaf/0.1/> .
 
 [] a foaf:Person ; foaf:name "Toby Inkster" .

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

 @term Zero "0"^^<http://example.com/unit/Kelvin> .
 
 :thing :temperature Zero .

@pattern

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})" <http://example.com/day/$3/$2/$1> .
 
 [] :born 01-06-1980 .

is equivalent to:

 [] :born <http://example.com/day/1980/06/01> .

As is:

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

Or:

 @pattern "(?<d>\d{2})-(?<m>\d{2})-(?<y>\d{4})" <http://example.com/day/${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" <http://www.w3.org/2002/12/cal/date#> .

is shorthand for:

 @pattern "\d\d?[A-Z][a-z][a-z]\d\d\d\d" <http://www.w3.org/2002/12/cal/date#$0> .

And:

 @dtpattern "\d\d?[A-Z][a-z][a-z]\d\d\d\d" <http://www.w3.org/2002/12/cal/types#FriendlyDate> .

is shorthand for:

 @pattern "\d\d?[A-Z][a-z][a-z]\d\d\d\d" "$0"^^<http://www.w3.org/2002/12/cal/types#FriendlyDate> .

@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 <http://example.com/people/> .
 @pattern "\~(\S+)" <$1> .
 @base <http://example.com/documents/> .
 @pattern "Doc(\d+)" <$1> .
 @base <http://example.com/terms/> .
 
 Doc1234 <creator> ~tobyink .

Is equivalent to:

 <http://example.com/documents/1234>
     <http://example.com/terms/creator>
         <http://example.com/people/tobyink> .