Nokia Sensor – First Thoughts

Nokia have released a new product called Sensor.

It’s a social networking tool that scans the local area via Bluetooth and reports back on other users in the area with Sensor enabled on their phones.

It’s very similar to Mobiluck in the way it works, but seems to have taken the concept to the next stage.

I had a similar idea a while back, and even got as far as coding the Bluetooth detection in Java. My idea was to tie into a FOAF file so you could look for friends or see who knew who in the area. The stumbling point was the difficulty in parsing RDF in J2ME. Ideally the parsing would have to be done on a seperate server somewhere, but like I said, it was just an idea that never got taken further.

When you load up Sensor you have a basic menu of options.

Nokia Sensor Menu

I’ve added a simple profile that can be exchanged with other Sensor users. Here I’ve taken a photo of Larry the Perl camel to use as the photo other Sensor users see when they check my profile out.

Nokia Sensor profile

When you have a profile, you can look for other Sensor users.

Nokia Sensor scanning

Of course, there aren’t any other Sensor users here at present so I can’t see what happens when there is someone else in the area.

It’ll be interesting to see the Bluetooth messages passed and if some bright spark will code up a desktop version. It defeats the object of course, but it’s still interesting to know how it works exactly.

It’s great to see Nokia supporting their handsets with great pieces of software like this and of course Lifeblog. Shame it’s just for Series 60 smart phones at present as this is the sort of thing that I can see the kids loving, and unfortunately not all of them can afford the best handsets.

I wonder when the first Sensor wedding will be… 🙂

Parsing RDF In Perl With RDF::Simple

In this article I’ll describe how to parse and extract data from an RDF file using Jo Walsh‘s RDF::Simple::Parser module in Perl.

RDF::Simple::Parser does what it says on the tin, it provides a simple way to parse RDF. Unfortunately, that can make it hard to extract data. All it returns from a successful parse of the RDF file, is what Jo calls a “bucket-o-triples”. This is just an array of arrays. The first array contains an list of all the triples. The second array contains the actual triples broken down so Subject is in position 0, Predicate is in position 1 and Object in position 2.

Let’s define these as constants in Perl as they’re not going to be changing.

use constant SUBJECT => 0;
use constant PREDICATE => 1;
use constant OBJECT => 2;

I’m going to use my usual example of my parsing my FOAF file, and I’ll be extracting the addresses of my friend’s FOAF files from it. See the example in What Is An RDF Triple, for a full breakdown of this.

We’ll define the two predicates we need to look for as constants.

use constant KNOWS_PREDICATE => 'http://xmlns.com/foaf/0.1/knows';
use constant SEEALSO_PREDICATE => 'http://www.w3.org/2000/01/rdf-schema#seeAlso';

We need to load in the FOAF file, so we’ll take advantage of File::Slurp’s read_file method to do this and put it in a variable called $file.

my $file = read_file('./foaf.rdf');

Before we can use RDF::Simple::Parser, we need to create an instance of it. I’ll set the base address to www.robertprice.co.uk in this case.

my $parser = RDF::Simple::Parser->new(base => 'http://www.robertprice.co.uk/');

Now we have the instance, we can pass in our FOAF file for parsing and get back our triples.

my @triples = $parser->parse_rdf($file);

Let’s take a quick look at my FOAF file to get an example triple.

I know Cal Henderson, and this is represented in my FOAF file as…

<foaf:knows>
<foaf:Person>
<foaf:nick>Cal</foaf:nick>
<foaf:name>Cal Henderson</foaf:name>
<foaf:mbox_sha1sum>2971b1c2fd1d4f0e8f99c167cd85d522a614b07b</foaf:mbox_sha1sum>
<rdfs:seeAlso rdf:resource="http://www.iamcal.com/foaf.xml"/>
</foaf:Person>
</foaf:knows>

Using the RDF validator we can get a the list of triples represented in this piece of RDF.


Triple Subject Predicate Object
1 genid:ARP40722 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://xmlns.com/foaf/0.1/Person
2 genid:ARP40722 http://xmlns.com/foaf/0.1/nick "Cal"
3 genid:ARP40722 http://xmlns.com/foaf/0.1/name "Cal Henderson"
4 genid:ARP40722 http://xmlns.com/foaf/0.1/mbox_sha1sum "2971b1c2fd1d4f0e8f99c167cd85d522a614b07b"
5 genid:ARP40722 http://www.w3.org/2000/01/rdf-schema#seeAlso http://www.iamcal.com/foaf.xml
6 genid:me http://xmlns.com/foaf/0.1/knows genid:ARP40722

The part we are interested are triples 5 and 6. We can see that triple 6 has Predicate value the same as our KNOWS_PREDICATE constant, and triple 5 has the Predicate value of our SEEALSO_PREDICATE constant. The part this links the two is that triple 6 has the Object value of triple 5’s Subject.

We know if we search for triples with the same predicate as our KNOWS_PREDICATE we’ll get triples that are to do with people I know. We can use Perl’s grep function to get these triples, then we can interate over them in a foreach loop.

foreach my $known (grep { $_->[PREDICATE] eq KNOWS_PREDICATE } @triples) {

We are only interest in the triples that have the same Subject as matching triple’s Object. Again, we can use grep to get these out so we can interate over them.

foreach my $triple (grep { $_->[SUBJECT] eq $known->[OBJECT] } @triples) {

Now we just need to make sure that the triple’s Predicate matches our SEEALSO_PREDICATE constant, and if it does, we can print out the value of it from it’s Object.

if ($triple->[PREDICATE] eq SEEALSO_PREDICATE) {
print $triple->[OBJECT], "n"
}

Let’s put this all together into a working example…

#!/usr/bin/perl -w
use strict;
use File::Slurp;
use RDF::Simple::Parser;
## constants defining position of triple components in
## RDF::Simple triple lists.
use constant SUBJECT => 0;
use constant PREDICATE => 1;
use constant OBJECT => 2;
## some known predicates.
use constant KNOWS_PREDICATE => 'http://xmlns.com/foaf/0.1/knows';
use constant SEEALSO_PREDICATE => 'http://www.w3.org/2000/01/rdf-schema#seeAlso';
## read in my foaf file and put it in $file.
my $file = read_file('./foaf.rdf');
## create a new parser, using my domain as a base.
my $parser = RDF::Simple::Parser->new(base => 'http://www.robertprice.co.uk/');
## parse my foaf file, and return a list of triples.
my @triples = $parser->parse_rdf($file);
## iterate over a list of triples matching the KNOWN_PREDICATE value.
foreach my $known (grep { $_->[PREDICATE] eq KNOWS_PREDICATE } @triples) {
## iteratve over a list of triples that have the same subject
## as one of our KNOWN_PREDICATE triples object.
foreach my $triple (grep { $_->[SUBJECT] eq $known->[OBJECT] } @triples) {
## find triples that match the SEEALSO_PREDICATE
if ($triple->[PREDICATE] eq SEEALSO_PREDICATE) {
## print out the object, should be the address
## of my friends foaf file.
print $triple->[OBJECT], "n"
}
}
}

The example will load in the FOAF file, parse it and print out any friends of mine that have FOAF files defined by the seeAlso predicate.

Querying RDF In Perl With RDFStore

Apart from RDF::Core and Redland, another option for parsing and querying RDF in Perl is RDFStore. This also provides the Perl RDQL::Parser module used by the very useful DBD::RDFStore driver.

Following on from the previous examples showing how to extract information from my FOAF file using RDF::Core (Query RDF In Perl With RDF::Core) and RDF::Redland (Querying RDF In Perl With RDF::Redland), here I’ll re-implement the query using RDFStore.

As a quick recap from the previous articles, here is the bit of RDF we want to extract information from.

<foaf:knows>
<foaf:Person>
<foaf:nick>Cal</foaf:nick>
<foaf:name>Cal Henderson</foaf:name>
<foaf:mbox_sha1sum>2971b1c2fd1d4f0e8f99c167cd85d522a614b07b</foaf:mbox_sha1sum>
<rdfs:seeAlso rdf:resource="http://www.iamcal.com/foaf.xml"/>
</foaf:Person>
</foaf:knows>

The solution used to extract the data from the RDF looks a lot more Perl-like than the previous examples we have seen.

If you have ever queried databases using SQL in Perl, then you have certainly come across the powerful DBI module. This abstracts the common database usage making it possible to very easily port your applications between various databases. One of the best things about using RDFStore is that it provides a DBD driver allowing you to use standard DBI methods when querying your RDF data. Unlike other modules that make you create triple stores and factory methods, RDFStore lets that be hidden from you.

To start with we’ll need to create a database handle using DBI and the DBD::RDFStore modules and store it in the variable $dbh.

my $dbh = DBI->connect("DBI:RDFStore:");

This creates a database on the fly, but we can connect to an existing database on a local or remote server if we so wished.

Now we need to create our RDQL query. It looks very similar to the query we used in the Redland example.

my $query = $dbh->prepare(<<QUERY);
SELECT ?name ?nick ?seeAlso ?mbox_sha1sum
FROM <file:foaf.rdf>
WHERE
(?x <rdf:type> <foaf:Person>),
(?x <foaf:name> ?name)
(?x <foaf:nick> ?nick)
(?x <rdfs:seeAlso> ?seeAlso)
(?x <foaf:mbox_sha1sum> ?mbox_sha1sum)
AND
(?nick eq 'Cal')
USING
foaf for <http://xmlns.com/foaf/0.1/>,
QUERY

Here we’re selecting the values the name, nick, seeAlso and mbox_sha1sum triples for a Person with the nick of Cal. We’ve explicitly set where our triples come from using the FROM clause. In this case, it’s the file foaf.rdf, which contains my FOAF information.

We have the query in the variable $query, so lets execute it.

$query->execute();

We can use standard DBI methods to fetch the data from our query. Here I’m going to create some bound variables to keep any matching data in.

my ($name, $seeAlso, $mbox_sha1sum, $nick);
$query->bind_columns($name, $nick, $seeAlso, $mbox_sha1sum);

Now we just have to fetch each row that matches our query and print them out.

while ($query->fetch()) {
print $name->toString, "n";
print $nick->toString, "n";
print $seeAlso->toString, "n";
print $mbox_sha1sum->toString, "n";
}

The values returned are either RDFStore::Literal or RDFStore::Resource objects, so we have to use their toString methods to print them.

To tidy up, we’ll finish our query and disconnect from our database.

$query->finish;
$dbh->disconnect;

That’s it! It really is as simple as that.

Let’s put this all together now to produce our final example code listing.

#!/usr/bin/perl -w
## An example showing how to use RDFStore and RDQL::Parser to
## extract information from a FOAF file.
## Copyright 2004 - Robert Price - http://www.robertprice.co.uk/
use strict;
use DBI;
## create a DBI connection to our NodeFactory.
my $dbh = DBI->connect("DBI:RDFStore:");
## prepare our query.
my $query = $dbh->prepare(<<QUERY);
SELECT ?name ?nick ?seeAlso ?mbox_sha1sum
FROM <file:foaf.rdf>
WHERE
(?x <rdf:type> <foaf:Person>),
(?x <foaf:name> ?name)
(?x <foaf:nick> ?nick)
(?x <rdfs:seeAlso> ?seeAlso)
(?x <foaf:mbox_sha1sum> ?mbox_sha1sum)
AND
(?nick eq 'Cal')
USING
foaf for <http://xmlns.com/foaf/0.1/>,
QUERY
## execute the query.
$query->execute();
## define some holding variables and bind them to our query results.
my ($name, $seeAlso, $mbox_sha1sum, $nick);
$query->bind_columns($name, $nick, $seeAlso, $mbox_sha1sum);
## while we have results being returned...
while ($query->fetch()) {
## print out the values.
## As these can be RDFStore::Literal or RDFStore::Resource's we
## need to use the toString method of these objects to print.
print $name->toString, "n";
print $nick->toString, "n";
print $seeAlso->toString, "n";
print $mbox_sha1sum->toString, "n";
}
## end the query and disconnect.
$query->finish;
$dbh->disconnect;

In conclusion, RDFStore provides a very clean and Perlish interface to querying RDF data. The code implements a DBD module allowing standard DBI methods to be used, making it quick and simple for Perl developers to learn and use effectively.

What Is An RDF Triple?

An RDF file should parse down to a list of triples.

A triple consists of a subject, a predicate, and an object. But what do these actually mean?

The subject is, well, the subject. It identifies what object the triple is describing.

The predicate defines the piece of data in the object we are giving a value to.

The object is the actual value.

Let’s take a quick look at my FOAF file to get an example triple.

I know Cal Henderson, and this is represented in my FOAF file as…

<foaf:knows>
<foaf:Person>
<foaf:nick>Cal</foaf:nick>
<foaf:name>Cal Henderson</foaf:name>
<foaf:mbox_sha1sum>2971b1c2fd1d4f0e8f99c167cd85d522a614b07b</foaf:mbox_sha1sum>
<rdfs:seeAlso rdf:resource="http://www.iamcal.com/foaf.xml"/>
</foaf:Person>
</foaf:knows>

Using the RDF validator we can get a the list of triples represented in this piece of RDF.


Triple Subject Predicate Object
1 genid:ARP40722 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://xmlns.com/foaf/0.1/Person
2 genid:ARP40722 http://xmlns.com/foaf/0.1/nick "Cal"
3 genid:ARP40722 http://xmlns.com/foaf/0.1/name "Cal Henderson"
4 genid:ARP40722 http://xmlns.com/foaf/0.1/mbox_sha1sum "2971b1c2fd1d4f0e8f99c167cd85d522a614b07b"
5 genid:ARP40722 http://www.w3.org/2000/01/rdf-schema#seeAlso http://www.iamcal.com/foaf.xml
6 genid:me http://xmlns.com/foaf/0.1/knows genid:ARP40722

But what does this actually mean? Lets go through it line by line.

Triple 1 – This has a subject of genid:ARP40722. You would have noticed that this didn’t appear in our original RDF, so where did it come from? It’s actually a bnode, and the value is generated by the RDF Validator. It’s purpose is to make sure we can identify the subject where it hasn’t been specifically named. In this case, we look to triple 6 and see that it has been generated there as the value of that object. The predicate of line 1 is the rdf:type, and the object is foaf:Person. This all corresponds to the code in <foaf:Person> in our FOAF extract.

Triple 2 – You’ll notice this has the same subject as triple 1. This isn’t a coincidence as the triple is part of the same foaf:Person. The predicate says we are defining the foaf:nick property, and the object is Cal. So we know that this foaf:Person has a nickname of Cal. This all corresponds to the line <foaf:nick>Cal</foaf:nick> in our FOAF extract.

Triple 3 – Yet again the subject is the same as triple 1. The predicate is foaf:name, and the object is Cal Henderson. So we know the name of the person in this foaf:Person is Cal Henderson. This represents the line <foaf:name>Cal Henderson</foaf:name>

Triple 4 – Yet again the subject is the same as triple 1. The predicate is foaf:mbox_sha1sum and the object is the SHA1 sum relating to Cal’s email address. This represents the line <foaf:mbox_sha1sum>2971b1c2fd1d4f0e8f99c167cd85d522a614b07b</foaf:mbox_sha1sum>

Triple 5 – Yet again the subject is the same. The predicate is rdfs:seeAlso and the object is http://www.iamcal.com/foaf.xml. We now know that if we want more information on Cal, we can see his FOAF file at that URL. This represents the line <rdfs:seeAlso rdf:resource="http://www.iamcal.com/foaf.xml"/>

Triple 6 – Here the subject is genid:me. This is because at the start of my FOAF file, in an area not show above I defined the id as me. The predicate is foaf:knows and the object is genid:ARP40722. This is saying that I know the person defined by the subject genid:ARP40722. In this case, it’s Cal, and his details have been shown in the previous 5 triples. This relates to the <foaf:knows> block.

Hopefully this has shown how the RDF has been parsed into triples, and how they relate to each other.

Shelley Powers, in her excellent book Practical RDF, helpfully describes triples as the following.

  • Each RDF triple is made up of subject, predicate and object.
  • Each RDF triple is a complete and unique fact.
  • An RDF triple is a 3-tuple, which is made up of a subject, predicate and object – which are respectively a uriref or bnode; a uriref; and a uriref, bnode or literal.
  • Each RDF triple can be joined with other RDF triples, but it still retains its own unique meaning, regardless of the complexity of the models in which it is included.