Introduction
I plan to give an introduction to features like COUNT, FILTER REGEX and GROUP BY which are supported by Tracker‘s SPARQL engine. We support more such features but I have to start the introduction somewhere. And overloading people with introductions to all features wont help me much with explaining things.
Since my last introduction to RDF and SPARQL I have added a few relationships and actors to the game.
We have Morrel, Max and Sasha being dogs, Sheeba and Query are cats, Picca is still a parrot, Fred and John are contacts. Fred claims that John is his friend. I changed the ontology to allow friendships between the animals too: Sasha claims that Morrel and Max are her friends. Sheeba claims Query is her friend. John bought Query. Fred being inspired by John decided to also get some pets: Morrel, Sasha and Sheeba.
Ontology
Let’s put this story in Turtle:
<test:Picca> a test:Parrot, test:Pet ;
test:name "Picca" .
<test:Max> a test:Dog, test:Pet ;
test:name "Max" .
<test:Morrel> a test:Dog, test:Pet ;
test:name "Morrel" ;
test:hasFriend <test:Max> .
<test:Sasha> a test:Dog, test:Pet ;
test:name "Sasha" ;
test:hasFriend <test:Morrel> ;
test:hasFriend <test:Max> .
<test:Sheeba> a test:Cat, test:Pet ;
test:name "Sheeba" ;
test:hasFriend <test:Query> .
<test:Query> a test:Cat, test:Pet ;
test:name "Query" .
<test:John> a test:Contact ;
test:owns <test:Max> ;
test:owns <test:Picca> ;
test:owns <test:Query> ;
test:name "John" .
<test:Fred> a test:Contact ;
test:hasFriend <test:John> ;
test:name "Fred" ;
test:owns <test:Morrel> ;
test:owns <test:Sasha> ;
test:owns <test:Sheeba> .
Querytime!
Let’s first start with all friend relationships:
SELECT ?subject ?friend
WHERE { ?subject test:hasFriend ?friend }
test:Morrel, test:Max
test:Sasha, test:Morrel
test:Sasha, test:Max
test:Sheeba, test:Query
test:Fred, test:John
Just counting these is pretty simple. In SPARQL all selectable fields must have a variable name, so we add the “as c” here.
SELECT COUNT (?friend) AS c
WHERE { ?subject test:hasFriend ?friend }
5
We counted friend relationships, of course. Let’s say we want to count how many friends each subject has. This is a more interesting query than the previous one.
SELECT ?subject COUNT (?friend) AS c
WHERE { ?subject test:hasFriend ?friend }
GROUP BY ?subject
test:Fred, 1
test:Morrel, 1
test:Sasha, 2
test:Sheeba, 1
Actually, we’re only interested in the human friends:
SELECT ?subject COUNT (?friend) AS c
WHERE { ?subject test:hasFriend ?friend .
?friend a test:Contact
} GROUP BY ?subject
test:Fred, 1
No no, we are only interested in friends that are either cats or dogs:
SELECT ?subject COUNT (?friend) AS c
WHERE { ?subject test:hasFriend ?friend .
?friend a ?type .
FILTER ( ?type = test:Dog || ?type = test:Cat)
} GROUP BY ?subject"
test:Morrel, 1
test:Sasha, 2
test:Sheeba, 1
Now we are only interested in friends that are either a cat or a dog, but whose name starts with a ‘S’.
SELECT ?subject COUNT (?friend) as c
WHERE { ?subject test:hasFriend ?friend ;
test:name ?n .
?friend a ?type .
FILTER ( ?type = test:Dog || ?type = test:Cat) .
FILTER REGEX (?n, '^S', 'i')
} GROUP BY ?subject
test:Sasha, 2
test:Sheeba, 1
Conclusions
Should we stop talking about ontologies and start talking about searchboxes and user interfaces instead? Although I certainly agree more UI-stuff is needed, I’m not sure yet. RDF and SPARQL are also about relationships and roles. Not just about matching stuff. Whenever we explain the new Tracker to people, most are stuck with ‘matching’ in their mind. They don’t think about a lot of other use-cases.
Such a search is just one use-case starting point: user entered a random search string and gives zero other meaning about what he needs. Many more situations can be starting points: When I select a contact in a user interface designed to show an archive of messages that he once sent to me, the searchbox becomes much more narrow, much more helpful.
As soon as you have RDF and SPARQL, and with Tracker you do, an application developer can start taking into account relationships between resources: The relationship between a contact in Instant Messaging and the attachments in an E-mail that he as a person has sent to you. Why not combine it with friendship relationships synced from online services?
With a populated store you can make the relationship between a friend who joined you on a trip, and photos of a friend of your friend who suggested the holiday location.
With GeoClue integration we could link his photos up with actual location markers. You’d find these photos that came from the friend of your friend, and we could immediately feed the location markers to the GPS software on your phone.
I really hope application developers have more imagination than just global searchboxes.
And this is just a use-case that is technically already possible with today’s high-end phones.