Oh, what was that Christmas song I was trying to remember? It was a duet... I know that. Wikipedia's "Christmas songs" list is enormous; I don't want to click through each of those to find it.
Anyway, I'll forget about that for now; time for a Perl Advent...
The semantic web is built around the idea of the "triple". The idea of the triple is so simple that a three year old can grasp it. (Trust me.)
To meaningfully describe anything we need at least three pieces of information:
We need to know what thing is being described.
We need to specify an attribute of the thing.
We need to provide a value for that attribute.
For example, assuming we have two
Person objects, in Perl we might write something like:
If you've ever told anyone your name, then you've used a triple.
More complex descriptions might be temporally or geographically qualified (Alice's spouse hasn't been Bob since the dawn of time), but information theory tells us that complex facts can be broken down into multiple triples, generally by taking a relationship and turning it into a "thing" in its own right:
So that's the triple. Three components. Any fewer would be useless; any more are just sugar.
A little terminology: in semantic web circles these three components are generally referred to as the subject, predicate and object respectively.
The semantic web takes the concept of the triple and marries it with the concept of "resources".
What is a resource? A resource is anything that can be identified with a uniform resource identifier (URI). (If you're unfamiliar with URIs, think URL, or "web address". They're almost the same thing. Almost.) This is a somewhat circular definition.
https://metacpan.org/ is a URI. It is an identifier for a resource. That particular identifier happens to identify a resource which is a document. There are plenty of other URIs that identify documents, images, videos, mailboxes, FTP sites, and so forth on the Internet.
But nothing in the URI specifications places any limits on what kind of things may be identified by a URI. You could generate a URI to identify a car, or a planet, or a person.
While you can "dereference"
https://metacpan.org/ to get (a copy of) the actual document identified by that URI, you cannot expect to dereference a URI representing a car to retrieve an actual car. This is the difference between URLs and URIs. URLs are locators; URIs are mere identifiers. URLs are a subset of URIs.
URIs give us a global namespace for identifying things. So in the semantic web, we have triples where each component is a resource (i.e. something that can be identified by a URI).
It is fine to have multiple URIs identifying the same thing (think multiple references to the same object in Perl), but multiple things identified by the same URI leads to ambiguity.
This model of triples of resources is referred to as the Resource Description Framework (snappy name, right?) or RDF.
My First Triple
Let's assume that I've chosen the URI
http://tobyinkster.co.uk/#i to identify myself. The inclusion of
#i means that it's different from the URL that identifies my homepage. (Remember, multiple things identified by the same URI leads to ambiguity; so the URI I use to identify myself must be different to the URI for my homepage.) Now I can state my name in RDF like this:
The second component there is a URI defined by the FOAF specification to identify the concept of having a name,
Note that the last component there is not a URI. A resource is anything that can be identified by a URI, but that doesn't always mean that it must be identified by a URI. Sometimes it's useful for a string to just be a string.
Introducing RDF::Trine and RDF::Query
While there are several other semantic web tool-kits on CPAN, RDF::Trine has emerged as the defacto standard.
RDF::Trine provides an object-oriented interface for dealing with resources and triples; persistent stores for triples, along with some basic methods for querying them; parsers and serialisers for various triple-based file formats (RDF/XML, Turtle, etc).
RDF::Trine is often used in conjunction with RDF::Query which provides an implementation of SPARQL, an SQL-like query and update language for triple stores. The author of RDF::Trine and RDF::Query is involved in the W3C group standardising SPARQL 1.1, and as a result his Perl libraries are very high quality and standards-compliant implementations.
Here's an example of using the previous triple in RDF::Trine:
What is it Good For?
RDF is a good way of representing highly heterogeneous data.
Imagine that you're storing the following data in an SQL database:
Alice is called Alice
Bob is called Bob
Alice is married to Bob
Easy peasy, right? You can probably already envisage the tables you'd use. Now let's store some more data:
Alice takes a drug called Foo-lin.
Foo-lin is manufactured by ACME Corp.
Foo-lin is a variety of insulin.
Bob likes "Fight Club".
"Fight Club" is a film.
"Fight Club" stars Brad Pitt.
Brad Pitt is called Brad.
Ten facts, and already the tables are spiralling out of control. Using RDF all that is just ten triples.
RDF allows you to model messy data; allows you to merge data from different data sources which have different models of the world. As long as two RDF data sources agree on URIs to identify things with, merging two databases can be as easy as concatenating two lists of triples.
All You Can Eat Data Buffet
There are over 30 billion RDF triples out there on the Internet. Because RDF uses URIs to identify things, and different data sources can use each others' URIs, this forms a loosely interlinked "web of data". (Just as web pages link to each other to form a "web of documents".)
DBpedia, a project to lift Wikipedia's information into RDF, might indicate the location for a particular World War II battle using a URI from the GeoNames project. Status.net, an open source microblogging platform, uses the same predicate URIs to describe people as LiveJournal.
I won't say that it's easy to use the entire web of data as a massive distributed database, but it's beginning to look possible.
For our conclusion, let's focus on just one of those linked open data projects: DBpedia.
The SPARQL Protocol
(Yes, that sounds like a new Dan Brown book.)
As well as being an SQL-like query language, SPARQL also includes a RESTful HTTP protocol for querying remote RDF data stores.
This gives me a list of five songs; much easier to narrow down the song I was looking for. Oh yes, that was it...