examples Package

These examples all live in ./examples in the source-distribution of RDFLib.

conjunctive_graphs Module

An RDFLib ConjunctiveGraph is an (unnamed) aggregation of all the named graphs within a Store. The get_context() method can be used to get a particular named graph for use such as to add triples to, or the default graph can be used

This example shows how to create named graphs and work with the conjunction (union) of all the graphs.

custom_datatype Module

RDFLib can map between RDF data-typed literals and Python objects.

Mapping for integers, floats, dateTimes, etc. are already added, but you can also add your own.

This example shows how rdflib.term.bind() lets you register new mappings between literal datatypes and Python objects

custom_eval Module

This example shows how a custom evaluation function can be added to handle certain SPARQL Algebra elements.

A custom function is added that adds rdfs:subClassOf “inference” when asking for rdf:type triples.

Here the custom eval function is added manually, normally you would use setuptools and entry_points to do it: i.e. in your setup.py:

entry_points = {
    'rdf.plugins.sparqleval': [
        'myfunc =     mypackage:MyFunction',
        ],
}
examples.custom_eval.customEval(ctx, part)[source]

Rewrite triple patterns to get super-classes

film Module

film.py: a simple tool to manage your movies review Simon Rozet, http://atonie.org/

  • manage directors and writers

  • manage actors

  • handle non IMDB uri

  • markdown support in comment

Requires download and import of Python imdb library from https://imdbpy.github.io/ - (warning: installation will trigger automatic installation of several other packages)

– Usage:

film.py whoami “John Doe <john@doe.org>”

Initialize the store and set your name and email.

film.py whoami

Tell you who you are

film.py http://www.imdb.com/title/tt0105236/

Review the movie “Reservoir Dogs”

class examples.film.Store[source]

Bases: object

__dict__ = mappingproxy({'__module__': 'examples.film', '__init__': <function Store.__init__>, 'save': <function Store.save>, 'who': <function Store.who>, 'new_movie': <function Store.new_movie>, 'new_review': <function Store.new_review>, 'movie_is_in': <function Store.movie_is_in>, '__dict__': <attribute '__dict__' of 'Store' objects>, '__weakref__': <attribute '__weakref__' of 'Store' objects>, '__doc__': None})
__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'examples.film'
__weakref__

list of weak references to the object (if defined)

movie_is_in(uri)[source]
new_movie(movie)[source]
new_review(movie, date, rating, comment=None)[source]
save()[source]
who(who=None)[source]
examples.film.help()[source]
examples.film.main(argv=None)[source]

foafpaths Module

SPARQL 1.1 defines path operators for combining/repeating predicates in triple-patterns.

We overload some Python operators on URIRefs to allow creating path operators directly in Python.

Operator

Path

p1 / p2

Path sequence

p1 | p2

Path alternative

p1 * '*'

chain of 0 or more p’s

p1 * '+'

chain of 1 or more p’s

p1 * '?'

0 or 1 p

~p1

p1 inverted, i.e. (s p1 o) <=> (o ~p1 s)

-p1

NOT p1, i.e. any property but p1

These can then be used in property position for s,p,o triple queries for any graph method.

See the docs for rdflib.paths for the details.

This example shows how to get the name of friends with a single query.

prepared_query Module

SPARQL Queries be prepared (i.e parsed and translated to SPARQL algebra) by the rdflib.plugins.sparql.prepareQuery() method.

When executing, variables can be bound with the initBindings keyword parameter

resource Module

RDFLib has a Resource class, for a resource-centric API.

A resource acts like a URIRef with an associated graph, and allows quickly adding or querying for triples where this resource is the subject.

rdfa_example Module

A simple example showing how to process RDFa from the web

simple_example Module

sleepycat_example Module

A simple example showing how to use a Sleepycat store to do on-disk persistence.

slice Module

RDFLib Graphs (and Resources) can be “sliced” with [] syntax

This is a short-hand for iterating over triples

Combined with SPARQL paths (see foafpaths.py) - quite complex queries can be realised.

See rdflib.graph.Graph.__getitem__() for details

smushing Module

A FOAF smushing example.

Filter a graph by normalizing all foaf:Persons into URIs based on their mbox_sha1sum.

Suppose I get two FOAF documents each talking about the same person (according to mbox_sha1sum) but they each used a rdflib.term.BNode for the subject. For this demo I’ve combined those two documents into one file:

This filters a graph by changing every subject with a foaf:mbox_sha1sum into a new subject whose URI is based on the sha1sum. This new graph might be easier to do some operations on.

An advantage of this approach over other methods for collapsing BNodes is that I can incrementally process new FOAF documents as they come in without having to access my ever-growing archive. Even if another 65b983bb397fb71849da910996741752ace8369b document comes in next year, I would still give it the same stable subject URI that merges with my existing data.

sparql_query_example Module

SPARQL Query using rdflib.graph.Graph.query()

The method returns a Result, iterating over this yields ResultRow objects

The variable bindings can be access as attributes of the row objects For variable names that are not valid python identifiers, dict access (i.e. with row[var] / __getitem__) is also possible.

vars contains the variables

sparql_update_example Module

SPARQL Update statements can be applied with rdflib.graph.Graph.update()

sparqlstore_example Module

A simple example showing how to use the SPARQLStore

swap_primer Module

This is a simple primer using some of the example stuff in the Primer on N3:

http://www.w3.org/2000/10/swap/Primer

transitive Module

An example illustrating how to use the transitive_subjects() and transitive_objects() graph methods

Formal definition

The transitive_objects() method finds all nodes such that there is a path from subject to one of those nodes using only the predicate property in the triples. The transitive_subjects() method is similar; it finds all nodes such that there is a path from the node to the object using only the predicate property.

Informal description, with an example

In brief, transitive_objects() walks forward in a graph using a particular property, and transitive_subjects() walks backward. A good example uses a property ex:parent, the semantics of which are biological parentage. The transitive_objects() method would get all the ancestors of a particular person (all nodes such that there is a parent path between the person and the object). The transitive_subjects() method would get all the descendants of a particular person (all nodes such that there is a parent path between the node and the person). So, say that your URI is ex:person.

This example would get all of your (known) ancestors, and then get all the (known) descendants of your maternal grandmother.

Warning

The transitive_objects() method has the start node as the first argument, but the transitive_subjects() method has the start node as the second argument.

User-defined transitive closures

The method transitiveClosure() returns transtive closures of user-defined functions.