A Simple API for RDF by Mark Nottingham, mnot@pobox.com
Sparts is a simple, resource-centric API for RDF graphs, built on top of rdflib_.
Sparta requires rdflib_ 2.4+.
To install::
python setup.py install
For installation help::
python setup.py install --help
In examples/::
example.py: Example of use example-out.txt: ouput of example script rss.py: example RSS 1.0 feed parser rss_schema.xml: partial RDF Schema/OWL for RSS 1.0 example
For more information, see sparta.py and http://github.com/mnot/sparta/.
The easiest way to get started is to play with it; take a look at the example files above. You can also take a look through the preliminary documentation below.
Sparta is a wrapper around an rdflib_ Graph. To use it, you must first instantiate one or more Graphs, make any necessary prefix mappings, and then instantiate a ThingFactory.
Prefix bindings allow URIs to be referred to with a short name. For example, if "http://www.example.com/foo#" is mapped to the prefix "foo", then the URI "http://www.example.com/foo#bar" can be referred to in Sparta with the name "foo_bar".
Prefix bindings are made by calling the normal bind(prefix, URI) method on the rdflib graph. You can also bind a complete URI to a string with the addAlias(alias, URI) method on the ThingFactory (this accommodates URIs that are awkward or impossible to map using prefixes).
To be instantiated, a ThingFactory requires one argument; the Graph (or store) that is to be used. Optionally, you can also give a schema_store argument, which points to a separate store that contains the schema hints used to help Sparta map RDF into Python datatypes and objects. If this is not specified, the primary store will be used.
This is a common idiom for setting up Sparta::
from rdflib.Graph import Graph store = Graph() store.parse([URI]) store.bind([prefix], [URI]) Thing = ThingFactory(store)
Once you've bound any prefixes you need and set up the store, you're ready to work with RDF data.
An RDF node is represented as a Python object in Sparta, whose properties correspond to RDF arcs. To start working with a node, you must instantiate it with its identity; there are three ways to do this.
A node's properties can be accessed and changed by name, using the prefix mapping as explained above. For example::
print foo.rdf_type
will print the 'rdf_type' property of the 'foo' node.
There are two ways to access a property's values, depending on what Sparta knows about it through the schema store. If it is an owl:FunctionalProperty, or if the subject is subclassed to restrict that property with either a owl:maxCardinality or a owl:cardinality_ of "1", the property can be accessed as a normal, singular value; that is, it can be accessed as explained above, assigned with the '=' operator, deleted with 'del', and so forth.
Otherwise, the property's value is assumed to have a cardinality greater than one, and implements a subset of the Python set() interface. For example, you can add to the set with the add method, like this::
foo.children.add("bob")
test for membership with the in operator, and so forth. See the PropertySet class for the exact methods implemented.
An RDF predicate with one of the following as its rdfs:range_ (according to the schema store) will be mapped to these Python datatypes:
.. _rdflib: http://rdflib.net/ .. _bnode: http://www.w3.org/TR/rdf-primer/#structuredproperties .. _cardinality: http://www.w3.org/TR/owl-ref/#cardinality .. _maxCardinality: http://www.w3.org/TR/owl-ref/#maxCardinality-def .. _FunctionalProperty: http://www.w3.org/TR/owl-ref/#FunctionalProperty-def .. _range: http://www.w3.org/TR/rdf-schema/#ch_range