Readable Turtle
We continue our exploration of Turtle, which is a common RDF syntax, using an example that we started working with in the previous video.
Hi, my name is Yulia.
In this video we'll keep talking about Turtle, which is a common RDF syntax. We'll use an example that we started working with in the previous video, so if you're looking for additional context, check it out by following the link in the description.
So far in our RDF exploration we've used Turtle and got this far in representing that I'm a person, and my name is Yulia. Before we add more data to this RDF resource, let's make this information more human-friendly by learning Turtle syntax shortcuts and abbreviations.
Let's start with a shortcut. The first Triple identifies the type of thing that this data is about. We're saying that the resource at hand is about "yulia" who is of "type" "person". Or using a shortcut:
<https://id.inrupt.com/yulia> a <http://schema.org/Person> .
"a" in Turtle stands for the URL for type that we just replaced.
Now let's look at how abbreviations work in Turtle. Notice how there are repetitions in this set of Triples.
<https://id.inrupt.com/yulia> and <https://schema.org/> are repeated twice each. To make it easier to read, let's create abbreviations for them.
For that we can use the literal @prefix ahead of the set of Triples and assign the abbreviation that we will use for each of them in this document.
@prefix yulia: <https://id.inrupt.com/yulia/>
@prefix schema: <https://schema.org/>
Now we can replace all instances of this Digital Me URL with yulia, and all instances of https://schema.org/ with schema. We are able to do this in place of either part of each Triple, whether it is the subject, predicate or object. The prefix is separated from the unique value at that URL using a colon.
@prefix yulia: <https://id.inrupt.com/yulia/>
@prefix schema: <https://schema.org/>
yulia: a schema:Person .
yulia: schema:givenName "Yulia" .
Great! It's much easier to read now. Yulia is a person, yulia has a given name "Yulia". However, there is still redundancy, so let's use Turtle syntax to abbreviate further.
Consecutive predicate-object pairs that pertain to the same subject can be separated by a semi-colon without having to repeat the subject that they have in common. Let's implement that.
@prefix yulia: <https://id.inrupt.com/yulia/>
@prefix schema: <https://schema.org/>
yulia: a schema:Person ;
schema:givenName "Yulia" ;
Amazing! Now we can read this like one sentence.
Yulia is a person whose given name is Yulia. Notice that we still need a period to complete this statement according to both English and Turtle syntax rules.
Now that we know more Turtle syntax rules, we can add another node to our little web of Linked Data. Here it says that Yulia owns myCar and its name is Otto. We could add this information to this same RDF file, or we can create a separate document for myCar. This car might have its own place on the web, and aside from the name, it might have other properties that are important to store online, like make, model, or mileage. For now we'll go with the former and add myCar to the Digital Me document that we've been working with so far.
Adding this node means adding three Triples to the Digital Me RDF document. One, about the relationship of ownership, then the fact that myCar is a car, and last, but not least, that it has a name.
Let's start by using Turtle to describe Otto.
<https://id.inrupt.com/yulia#myCar> a schema:Car ;
schema:name "Otto" .
The subject, in this case, is stating that this document also has information about the myCar. The hash sign denotes where the data about myCar is located within this resource and we refer to it as a fragment. So when we need information about this whole RDF file, we can use the URL that ends with yulia, but when we're looking for a fragment of that data that pertains to Otto, this URL with fragment myCar, will take us directly to that bit of data.
Here we are using Schema.org's definition of a Car, making the statement that myCar is of type Car. And the same vocabulary for the definition of name.
We're still missing a crucial bit of information, and that is yulia owning this car. Now that myCar has its own set of Triples pertaining to it, it can be referred to by its unique URL.
@prefix yulia: <https://id.inrupt.com/yulia/> .
@prefix schema: <https://schema.org/> .
yulia: a schema:Person ;
schema:givenName "Yulia" ;
schema:owns <https://id.inrupt.com/yulia#myCar> .
We can also use the prefix defined earlier in the doc to shorten this even more.
@prefix yulia: <https://id.inrupt.com/yulia/> .
@prefix schema: <https://schema.org/> .
yulia: a schema:Person ;
schema:givenName "Yulia" ;
schema:owns yulia:myCar .
yulia:myCar a schema:Car ;
schema:name "Otto" .
Now we have a couple of nodes from our original example described using Turtle syntax.
In the next video we're going to continue to build out this set of self-describing connected data nodes using yet another RDF syntax, called JSON-LD.
Let's recap what we learned about Turtle in this video.
Turtle stands for Terse RDF Triple Language, and is a syntax for RDF. It allows for shortcuts and abbreviations. Specifically:
- "a" stands for <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> stating that a thing or subject is of a particular type.
- @prefix allows to define shortcuts for commonly used URLs within the set of Triples in an RDF file.
- Semicolons allow to list properties that pertain to the same subject.
- A period denotes the end of a Triple.
- A fragment(#) in a URL allows us to create direct references to subjects that live in the same document.
There are other helpful shortcuts and tools in Turtle, but we won't dive into them now. If you're interested in learning more about Turtle, check out the links in the description.
- RDF by W3C
- Semantic Web by Ruben Verborgh
- Turtle by W3C