Sunday, September 11, 2005

A Snake In Redland

Fucked off that I couldn't get the php bindings for reland working under windows, and impressed that I could... you know... use python with a bit of guessing, I'm starting something.

I like the ActiveState installers a lot. Go get Python 2.4 for your windows boxen.
Next, Redland win32 bindings.

Install everything.

Start the Interactive Shell.

Enter the following.

import RDF

model = RDF.Model()
print model

You should then have a nice bit of well formed, but empty RDF/XML. Let's add stuff.



o = RDF.Node(RDF.Uri("http://www.person.com/me/"))
p = RDF.Node(RDF.Uri("http://xmlns.com/foaf/0.1/"))
s = RDF.Node(RDF.Uri("http://www.person.com/you/"))

t = RDF.Statement(o,p,s)

model.append(t)

print model


Holy smokes, batman! We've just added a triple to our RDF. Not exactly what we wanted, though - we'd be better off describing a me knows you relationship in a well known vocabulary - FOAF for instance.

Sadly, crschmidt tells me, "the redland serialiser sucks! Pass it through cwm."

More tinkering, and we come up with the following:

import RDF

rdf = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
foaf = RDF.NS("http://xmlns.com/foaf/0.1/")
model = RDF.Model()
serial = RDF.RDFXMLSerializer()


o = RDF.Node(RDF.Uri("http://www.me.com/"))
p = RDF.Node(rdf['type'])
s = RDF.Node(foaf['Person'))

me = RDF.Statement(o,p,s)

o = RDF.Node(RDF.Uri("http://www.you.com/"))
you = RDF.Statement(o,p,s)

p = RDF.Node(foaf['knows'])

knows = RDF.Statement(o,p,RDF.Uri("http://www.me.com/"))

model.append(me)
model.append(you)
model.append(knows)
serial.set_namespace('foaf','http://xmlns.com/foaf/0.1/')

RDF.RDFXMLSerializer.serialize_model_to_file(serial,"d:/out.rdf", model)


Valid RDF is produced, though we can clean it up a bit with a run through CWM.

Redland:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://www.me.com/">
<rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.you.com/">
<rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.you.com/">
<foaf:knows rdf:resource="http://www.me.com/"/>
</rdf:Description>
</rdf:RDF>


Redland + CWM:

<!-- Processed by Id: cwm.py,v 1.164 2004/10/28 17:41:59 timbl Exp -->
<!-- using base file:///out.rdf-->


<rdf:RDF xmlns="http://xmlns.com/foaf/0.1/"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<Person rdf:about="http://www.me.com/">
</Person>

<Person rdf:about="http://www.you.com/">
<knows rdf:resource="http://www.me.com/"/>
</Person>
</rdf:RDF>


In total it took me about two hours - most of that time spent hearing about poor baby lambs from Chloe, my roommate, rather than learning.

Alright, we can create an serialise any kind of RDF we want now, where to next?

2 comments:

Anonymous said...

Shame php with windows isn't working for you.

Anyway, you might try the RDF/XML-abbrev serializer inside redland directly. It has a few bugs but mostly works and produces prettier output.

Something like:
serial=RDF.Serialializer(name="rdfxml-abbrev")
instead of the serial line you have.

Dan said...

Cheers Dave! I'll give it a crack