TripleStore

A TripleStore is an RDF Database. RDF defines a language for representing information about resources. A resource can be anything: an abstract concept, a pysical object, anything. Any resource may be given a resource identifier by which to refer to it. These resource identifiers as well as literal values are used when making statements about a resource. The statements have the form: subject predicate object and are often called triples.

A TripleStore is a place to add, remove and query for triples. Each triple in the TripleStore has the form:

subject
any resource identifier.
predicate
a resource identifier of any property.
object
either a resource identifier or a literal value.

There are two types of resource identifiers, namely, URIRef and BNode. A property is a type of resource and are used when talking about the particular aspect of a resource that the property represents. Properties must be represented by URIRefs and may not be given BNode identifiers; This is a somewhat arbitrary constraint, but none the less is clearly imposed by the specifications. A Literal is used for the object of a statement when it itself represents the object, and in which case the object has no resource identifier.

TripleStore Interface

There are three core TripleStore methods:

add(triple)
Adds triple, a tuple of the form (subject, predicate, object), to the TripleStore.
remove(triple)
Removes triple, a tuple of the form (subject, predicate, object), to the TripleStore.
triples(pattern)
A generator over all the triples in the TripleStore matching pattern. Pattern is a tuple of the form (subject, predicate, object) where each of subject, predicate and object are either specified or None to indicate any value will match.

TripleStore also implements the following convience methods on top of the triples method:

subjects(predicate=None, object=None)
predicates(subject=None, object=None)
objects(subject=None, predicate=None)
subject_predicates(object=None)
subject_objects(predicate=None)
predicate_objects(subject=None)

load(location, format="xml")
@@
save(location, format="xml")
@@
parse(source, format="xml")
@@
serialize(format="xml", stream=None)
@@

TripleStore also implement the following methods:

__iter__()
Not (usually) called directly but exists to support iteration over triples in a store. For example,
for triple in store: print triple
__contains__((subject, predicate, object))
Not (usually) called directly but exists to support the following:
if (s, p, o) in store: print "found it"
__len__(self)
Exists to support:
len(store)
__eq__(other)
Supports:
store_a==store_b
__iadd__(other)
Supports:
store += another_store
__isub__(other)
Supports:
store -= another_store
items(list)
transitive_objects(subject, property)
transitive_subjects(predicate, object)

See also some examples of using the TripleStores.