[TRIG biplane]

For this month’s blog entry I originally planned to create a reference for RDF serialization formats. My idea was to create a table listing all the known formats, with links to their specs (when they have one), their age, origin, a sample, and some opinionated comments–for example, why creating new documents in RDF/XML made sense in 1999 but no longer does.

I found a few nice existing surveys, so instead of creating a new one I will list those:

  • The GraphDB RDF formats documentation page was created before I joined Ontotext (which, by the way, is merging with Semantic Web Company of PoolParty fame to become Graphwise) and has good information about the important formats.
  • The Neptune list has some nice descriptions of each.
  • The Medium article Understanding Linked Data Formats doesn’t cover many formats but it does include examples of the ones that are listed.
  • The W3C RDFSyntax page is a bit out of date (saying, for example, that Turtle is still in the process of becoming a W3C Recommendation) but it’s interesting for the historical perspective it provides on attempts to create serialization formats.

I tend to just use Turtle for everything. I have found N-Triples useful for certain experiments because you can split a file up at any line breaks (for example, with some shell text processing utilities) and be confident that the pieces will all be syntactically correct.

When I stored data with named graphs I used N-Quads, which are N-Triples with a graph name URI added to any line that represents a triple in a named graph. (N-Triples and especially N-Quads are difficult to write about because, if a single one of either can’t have a carriage return in it and you must use full URIs instead of prefixed names, it’s difficult to come up with realistic examples that properly fit on a line of a book page or browser paragraph.)

I never paid attention to TriG, which is now a bit embarrassing because TriG 1.1 has been a Recommendation for over ten years and it looks like a great way to represent data in named graphs. It’s basically Turtle with the SPARQL syntax for specifying triples in named graphs.

As an example, let’s first look at the update request example 338 from my book Learning SPARQL. I used it as part of an example in my previous blog entry to create two graphs and then put two triples in each of them as well as adding two triples to the default graph:

# filename: ex338.ru

PREFIX d:  <http://learningsparql.com/ns/data#>
PREFIX dm: <http://learningsparql.com/ns/demo#>

INSERT DATA
{
  d:x dm:tag "one" . 
  d:x dm:tag "two" . 

  GRAPH d:g1
  { 
    d:x dm:tag "three" . 
    d:x dm:tag "four" . 
  }

  GRAPH d:g2
  { 
    d:x dm:tag "five" . 
    d:x dm:tag "six" . 
  }
}

Instead of running that update query to load those six triples, I could have just loaded the following TriG file and gotten the same result:

@prefix d:  <http://learningsparql.com/ns/data#> .
@prefix dm: <http://learningsparql.com/ns/demo#> .

{
  d:x dm:tag "one" . 
  d:x dm:tag "two" .
}

GRAPH d:g1 {
    d:x dm:tag "three" . 
    d:x dm:tag "four" .
}

GRAPH d:g2 {
    d:x dm:tag "five" . 
    d:x dm:tag "six" .
}

The GRAPH keywords in this TriG sample, which you’ll recognize as the SPARQL way to say “here comes a named graph”, are actually optional. The curly braces around the “one” and “two” triples to delimit the default graph’s triples are also optional. With or without these optional bits, anyone who has been using Turtle and SPARQL for a few years will find TriG’s syntax to be intuitive.

The Wikipedia page about TriG has another nice example.

As I mentioned above, the current TriG Recommendation is release 1.1. Release 1.2 is underway, and it looks like its main goal is to incorporate reification, or the ability to make RDF statements about RDF statements, like the current ongoing work with Turtle. As part of the work on the next version of SPARQL, it would be nice to see CONSTRUCT queries use this kind syntax to support the creation of quads in queries. An extension in Apache Jena has supported this since 2015.

Either way, I’m sure I’ll be using TriG a lot more in the future. I just used it at work on Friday!


Comments? Reply to my tweet (or even better, my Mastodon message) announcing this blog entry.

CC BY-SA 2.0 biplane photo by Jun (and cropped)