I just wrapped up the second of two rdflib-related libraries I wrote with the aim of bridging the gap between rdflib and 4Suite RDF. The latter (BoundVersaResult.py) is a little more interesting than the former in that it uses Sparta to allow the distinct components of a Versa query result to each be bound to appropriate python objects. 4Suite RDF's Versa implementation already provides such a binding:
- String -> Python unicode
- Number -> Python float
- Boolean -> Python boolean
- List -> Python list
- Set -> Python Sets
- Resource/BlankNodes -> Python unicode
The bindings for all the datatypes except Resource/BlankNodes are straight forward. This library extends the datatype binding to include the ability to bind Sparta Things to Versa Resources and BlankNodes. Since Sparta only works with rdflib Graphs, the FtRdfBackend.py module is used to wrap an rdflib.Graph around a 4Suite Model.
Sparta takes an RDF Graph and a defining Ontology which dictates the cardinality of properties bound to resource objects (Things). It allows an RDF Graph to be traversed (and extended) via pythonic idiom. The combination of being able to isolate resources by Versa query (or SPARQL queries eventually - as soon as the ongoing rdflib effort in that regard is completed) and bind them to python objects whose properties reflect the properties on the underlying RDF resources they are bound to is very cool, IMHO. The ability to provide an implementation agnostic way to modify an RDF graph, using a host language as expressive as Python is the icing on the cake. For example, check out the following code snippet demonstrating the use of this library:
#Setup FtRDF Model Memory.InitializeModule() db = Memory.GetDb('', '') db.begin() model = Model.Model(db) #Parse my del.icio.us rss feed szr = Dom.Serializer() delUri="http://del.icio.us/rss/chimezie/academic+rdf" domStr=urllib2.urlopen(delUri).read() dom = Domlette.NonvalidatingReader.parseString(domStr,'http://del.icio.us/rss/chimezie') szr.deserialize(model,dom,scope=delUri) #Setup rdflib.Graph with FtRDF Model as Backend, using FtRdf driver generator=VersaThingGenerator(model) #generator.query("type(rss:item)") for item in generator.query("type(rss:item)"): [pprint(link) for link in item.rss_link] print generator.query("distribute(@'%s','.-rss:title->*','.-dc:subject->*')"%item._id)
Note that (within the loop over the rss:items in the graph), the rss:link property returns an iterator over the possible values (since there is no defining ontology that could have specified that the rss:link property has a cardinality of 1, or is an inverse functional property - which would have caused Sparta to bind the rss_link property to a single object instead of an iterator).
The result of running this code:
u'http://lists.w3.org/Archives/Public/public-rdf-dawg/2004JulSep/0069' [[u'More on additional semantic information from Enrico Franconi on 2004-07-12 (public-rdf- firstname.lastname@example.org from July to September 2004)'], [u'academic architecture archive community dawg email logic query rdf reference semantic']] u'http://www.w3.org/TR/swbp-specified-values/' [[u'Representing Specified Values in OWL: "value partitions" and "value sets"'], [u'academic datatypes ontology owl rdf semantic standard w3c']] u'http://lists.w3.org/Archives/Public/public-rdf-dawg/2005JulSep/0386.html' [[u'boolean operators and type errors from Jeen Broekstra on 2005-09-07 (email@example.com from July to September 2005)'], [u'academic architecture archive community dawg email logic rdf reference semantic w3c']] u'http://www.w3.org/DesignIssues/Diff' [[u'RDF Diff, Patch, Update, and Sync -- Design Issues'], [u'academic paper rdf semantic standards tbl w3c']] u'http://www.w3.org/TR/rdf-dawg-uc/' [[u'RDF Data Access Use Cases and Requirements'], [u'academic architecture framework query rdf reference semantic specification standard w3c']] u'http://www.w3.org/DesignIssues/RDB-RDF' [[u'Relational Databases and the Semantic Web (in Design Issues)'], [u'academic architecture framework rdb rdf reference semantic tbl w3c']] u'http://www.w3.org/TR/swbp-n-aryRelations/' [[u'Defining N-ary Relations on the Semantic Web: Use With Individuals'], [u'academic logic ontology owl predicate rdf reference relationships semantic standard w3c']]