Graph utilities

Graphs as Iterators

RDFLib graphs also override __iter__() in order to support iteration over the contained triples:

for subject,predicate,obj_ in someGraph:
   assert (subject,predicate,obj_) in someGraph, "Iterator / Container Protocols are Broken!!"

Set Operations on RDFLib Graphs

__iadd__() and __isub__() are overridden to support adding and subtracting Graphs to/from each other (in place):

  • G1 += G1
  • G2 -= G2

Basic Triple Matching

RDFLib graphs support basic triple pattern matching with a triples() function.

Graph.triples((s, p, o))

Generator over the triple store

Returns triples that match the given triple pattern. If triple pattern does not provide a context, all contexts will be searched.

This function is a generator of triples that match the pattern given by the arguments. The arguments of these are RDF terms that restrict the triples that are returned. Terms that are None are treated as a wildcard.

Managing Triples

Adding Triples

Triples can be added in two ways:

  • They may be added with with the parse() function.

    Graph.parse(source=None, publicID=None, format=None, location=None, file=None, data=None, **args)

    Parse source adding the resulting triples to the Graph.

    The source is specified using one of source, location, file or data.

    Parameters:
    • source: An InputSource, file-like object, or string. In the case of a string the string is the location of the source.
    • location: A string indicating the relative or absolute URL of the source. Graph’s absolutize method is used if a relative location is specified.
    • file: A file-like object.
    • data: A string containing the data to be parsed.
    • format: Used if format can not be determined from source. Defaults to rdf/xml.
    • publicID: the logical URI to use as the document base. If None specified the document location is used (at least in the case where there is a document location).
    Returns:

    self, the graph instance.

    Examples:

    >>> my_data = '''
    ... <rdf:RDF
    ...   xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    ...   xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'
    ... >
    ...   <rdf:Description>
    ...     <rdfs:label>Example</rdfs:label>
    ...     <rdfs:comment>This is really just an example.</rdfs:comment>
    ...   </rdf:Description>
    ... </rdf:RDF>
    ... '''
    >>> import tempfile
    >>> file_name = tempfile.mktemp()
    >>> f = file(file_name, "w")
    >>> f.write(my_data)
    >>> f.close()
    
    >>> g = Graph()
    >>> result = g.parse(data=my_data, format="application/rdf+xml")
    >>> len(g)
    2
    
    >>> g = Graph()
    >>> result = g.parse(location=file_name, format="application/rdf+xml")
    >>> len(g)
    2
    
    >>> g = Graph()
    >>> result = g.parse(file=file(file_name, "r"), format="application/rdf+xml")
    >>> len(g)
    2
    

    The first argument can be a source of many kinds, but the most common is the serialization (in various formats: RDF/XML, Notation 3, NTriples of an RDF graph as a string. The format parameter is one of n3, xml, or ntriples. publicID is the name of the graph into which the RDF serialization will be parsed.

  • Triples can also be added with the add() function:

    Graph.add((s, p, o))

    Add a triple with self as context

Removing Triples

Similarly, triples can be removed by a call to remove():

Graph.remove((s, p, o))

Remove a triple from the graph

If the triple does not provide a context attribute, removes the triple from all contexts.

RDF Literal Support

RDFLib Literals essentially behave like unicode characters with an XML Schema datatype or language attribute. The class provides a mechanism to both convert Python literals (and their built-ins such as time/date/datetime) into equivalent RDF Literals and (conversely) convert Literals to their Python equivalent. There is some support of considering datatypes in comparing Literal instances, implemented as an override to __eq__(). This mapping to and from Python literals is achieved with the following dictionaries:

PythonToXSD = {
    basestring : (None,None),
    float      : (None,XSD_NS+u'float'),
    int        : (None,XSD_NS+u'int'),
    long       : (None,XSD_NS+u'long'),
    bool       : (None,XSD_NS+u'boolean'),
    date       : (lambda i:i.isoformat(),XSD_NS+u'date'),
    time       : (lambda i:i.isoformat(),XSD_NS+u'time'),
    datetime   : (lambda i:i.isoformat(),XSD_NS+u'dateTime'),
}

Maps Python instances to WXS datatyped Literals

XSDToPython = {
    XSD_NS+u'time'               : (None,_strToTime),
    XSD_NS+u'date'               : (None,_strToDate),
    XSD_NS+u'dateTime'           : (None,_strToDateTime),
    XSD_NS+u'string'             : (None,None),
    XSD_NS+u'normalizedString'   : (None,None),
    XSD_NS+u'token'              : (None,None),
    XSD_NS+u'language'           : (None,None),
    XSD_NS+u'boolean'            : (None, lambda i:i.lower() in ['1','true']),
    XSD_NS+u'decimal'            : (float,None),
    XSD_NS+u'integer'            : (long ,None),
    XSD_NS+u'nonPositiveInteger' : (int,None),
    XSD_NS+u'long'               : (long,None),
    XSD_NS+u'nonNegativeInteger' : (int, None),
    XSD_NS+u'negativeInteger'    : (int, None),
    XSD_NS+u'int'                : (int, None),
    XSD_NS+u'unsignedLong'       : (long, None),
    XSD_NS+u'positiveInteger'    : (int, None),
    XSD_NS+u'short'              : (int, None),
    XSD_NS+u'unsignedInt'        : (long, None),
    XSD_NS+u'byte'               : (int, None),
    XSD_NS+u'unsignedShort'      : (int, None),
    XSD_NS+u'unsignedByte'       : (int, None),
    XSD_NS+u'float'              : (float, None),
    XSD_NS+u'double'             : (float, None),
    XSD_NS+u'base64Binary'       : (base64.decodestring, None),
    XSD_NS+u'anyURI'             : (None,None),
}

Maps WXS datatyped Literals to Python. This mapping is used by the toPython() method defined on all Literal instances.

Table Of Contents

Previous topic

Namespace Utilities

Next topic

Persistence

This Page