RDFLib
RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information. Through this library, Python is one of the main RDF manipulation languages, the other being Java. This library contains parsers/serializers for almost all of the known RDF serializations, such as RDF/XML, Turtle, N-Triples, & JSON-LD, many of which are now supported in their updated form. The library also contains both in-memory and persistent Graph back-ends for storing RDF information and numerous convenience functions for declaring graph namespaces, lodging SPARQL queries and so on. It is in continuous development with the most recent stable release, having been released on 18 April, 2020. It was originally created by Daniel Krech with the first release in November, 2002.
A number of other Python projects use rdflib for RDF manipulation, including:
- - A simple implementation of the OWL2 RL Profile
- - a Python SHACL validator
- - an add-on module for the Python Flask web framework used to deliver Linked Data
- - a Web Ontology Language documentation tool
History and status
Overview
RDFLib and Python idioms
RDFLib's use of various Python idioms mean it is fairly simple for programmers with only junior Python skills to manipulate RDF. On the other hand, the Python idioms are simple enough that someone familiar with RDF, but not Python, can probably work out how to use rdflib quite easily.The core class in RDFLib is which is a Python dictionary used to store collections of RDF triples in memory. It redefine certain built-in Python object methods in order to exhibit simple graph behaviour, such as simple graph merging via addition.
RDFLib graphs emulate container types and are best thought of as a set of 3-item triples:
set,
,
...
])
RDFLib graphs are not sorted containers; they have ordinary Python set operations, e.g.
add
methods that search triples and return them in arbitrary order.RDF graph terms
The following RDFLib classes model in a graph and inherit from a common class, which extends Python unicode. Instances of these are nodes in an RDF graph.-
Namespace utilities
>>> from rdflib import Namespace
>>> SDO = Namespace
>>> SDO.Person
https://schema.org/Person
>>> SDO
https://schema.org/url
Graphs as iterators
RDFLib graphs also override __iter__ in order to support iteration over the contained triples:for subject, predicate, object_ in someGraph:
assert 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 :- G1 += G1
- G2 -= G2
Basic triple matching
for subject, predicate, object_ in someGraph.triples:
print # prints all the triples with the predicate being https://schema.org/name
RDF convenience APIs (RDF collections / containers)
Managing triples
Adding triples
Triples can be added in two ways:- They may be added with the parse function. The first argument can be a source of many kinds, but the most common is the serialization. The format parameter is one of turtle, n3, xml, n-triples or JSON-LD. publicID is the name of the graph into which the RDF serialization will be parsed.
- Triples can also be added with the add function: add).
Removing triples
RDF Literal support
RDFLib 'Literal's essentially behave like Unicode characters with an XML Schema datatype or language attribute. The class provides a mechanism to both convert Python literals into equivalent RDF Literals and 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 =
Maps Python instances to WXS datatyped Literals
XSDToPython =
Maps WXS datatyped Literals to Python. This mapping is used by the toPython method defined on all Literal instances.
SPARQL querying
RDFLIb supports a majority of the current SPARQL 1.1 specification and includes a harness for the publicly available RDF DAWG test suite. Support for SPARQL is provided by two methods:-
rdflib.graph.query
- used to pose SPARQL SELECT or ASK queries to a graph -
rdflib.graph.update
- used to change graph content or return RDF using INSERT, DELETE and CONSTRUCT SPARQL statementsSerialization (NTriples, N3, and RDF/XML)
The RDF store API
A Universal RDF Store InterfaceThis document attempts to summarize some fundamental components of an RDF store. The motivation is to outline a standard set of interfaces for providing the necessary support needed in order to persist an RDF Graph in a way that is universal and not tied to any specific implementation. For the most part, the core RDF model is adhered to as well as terminology that is consistent with the RDF Model specifications. However, this suggested interface also extends an RDF store with additional requirements necessary to facilitate the aspects of Notation 3 that go beyond the RDF model to provide a framework for First Order Predicate Logic processing and persistence.
Terminology
;Context: A named, unordered set of statements. Also could be called a sub-graph. The named graphs literature and ontology are relevant to this concept. A context could be thought of as only the relationship between an RDF triple and a sub-graph in which it is found or the sub-graph itself.;Conjunctive Graph: This refers to the 'top-level' Graph. It is the aggregation of all the contexts within it and is also the appropriate, absolute boundary for closed world assumptions / models. This distinction is the low-hanging fruit of RDF along the path to the semantic web and most of its value is in real-world problems:
;Quoted Statement: A statement that isn't asserted but is referred to in some manner. Most often, this happens when we want to make a statement about another statement without necessarily saying these quoted statements. For example:
;Formula: A context whose statements are quoted or hypothetical.
;Terms: Terms are the kinds of objects that can appear in a quoted/asserted triple. This includes those that are core to RDF:
;Nodes: Nodes are a subset of the Terms that the underlying store actually persists. The set of such Terms depends on whether or not the store is formula-aware. Stores that aren't formula-aware would only persist those terms core to the RDF Model, and those that are formula-aware would be able to persist the N3 extensions as well. However, utility terms that only serve the purpose for matching nodes by term-patterns probably will only be terms and not nodes.
;Context-aware: An RDF store capable of storing statements within contexts is considered context-aware. Essentially, such a store is able to partition the RDF model it represents into individual, named, and addressable sub-graphs.
;Formula-aware: An RDF store capable of distinguishing between statements that are asserted and statements that are quoted is considered formula-aware.
;Conjunctive Query: Any query that doesn't limit the store to search within a named context only. Such a query expects a context-aware store to search the entire asserted universe. A formula-aware store is expected not to include quoted statements when matching such a query.
;N3 Round Trip: This refers to the requirements on a formula-aware RDF store's persistence mechanism necessary for it to be properly populated by a N3 parser and rendered as syntax by a N3 serializer.
;Transactional Store: An RDF store capable of providing transactional integrity to the RDF operations performed on it.
Interpreting syntax
The following Notation 3 document:=>
Could cause the following statements to be asserted in the store:
_:a log:implies _:b
This statement would be asserted in the partition associated with quoted statements
?x rdf:type :N3Programmer
Finally, these statements would be asserted in the same partition
?x :has _:c
_:c rdf:type :Migraine
Formulae and Variables as Terms
Formulae and variables are distinguishable from URI references, Literals, and BNodes by the following syntax:
- Formula
?x - Variable
They must also be distinguishable in persistence to ensure they can be round tripped. Other issues regarding the persistence of N3 terms.
Database management
An RDF store should provide standard interfaces for the management of database connections. Such interfaces are standard to most database management systemsThe following methods are defined to provide this capability:
- - Opens the store specified by the configuration string. If create is True a store will be created if it does not already exist. If create is False and a store does not already exist an exception is raised. An exception is also raised if a store exists, but there is insufficient permissions to open the store.
- - This closes the database connection. The commit_pending_transaction parameter specifies whether to commit all pending transactions before closing.
- - This destroys the instance of the store identified by the configuration string.
Triple interfaces
An RDF store could provide a standard set of interfaces for the manipulation, management, and/or retrieval of its contained triples :- - Adds the given statement to a specific context or to the model. The quoted argument is interpreted by formula-aware stores to indicate this statement is quoted/hypothetical. It should be an error to not specify a context and have the quoted argument be True. It should also be an error for the quoted argument to be True when the store is not formula-aware.
- - Returns an iterator over all the triples matching the given pattern. The pattern is specified by providing explicit statement terms, or None - which indicates a wildcard. NOTE: This interface is expected to return an iterator of tuples of length 3, corresponding to the 3 terms of matching statements, which can be either of : URIRef, Blank Node, Literal, Formula, Variable, or a Context.
A conjunctive query can be indicated by either providing a value of NULL/None/Empty string value for context or the identifier associated with the Conjunctive Graph.
- - Number of statements in the store. This should only account for non-quoted statements if the context is not specified, otherwise it should return the number of statements in the formula or context given.
Formula / context interfaces
- - Generator over all contexts in the graph. If triple is specified, a generator over all contexts the triple is in.
- -
Named graphs / conjunctive graphs
Formulae
RDFLib graphs support an additional extension of RDF semantics for formulae. For the academically inclined, Graham Klyne's 'formal' extension is probably a good read.Formulae are represented formally by the 'QuotedGraph' class and disjoint from regular RDF graphs in that their statements are quoted.
Persistence
RDFLib provides an abstracted Store API for persistence of RDF and Notation 3. The Graph class works with instances of this API for triple-based management of an RDF store including: garbage collection, transaction management, update, pattern matching, removal, length, and database management . Additional persistence mechanisms can be supported by implementing this API for a different store. Currently supported databases:- MySQL
- SQLite
- Berkeley DB
- Zope Object Database
- Random-access memory
- Redland RDF Application Framework
from rdflib import plugin
from rdflib.store import Store
plugin.get
'Higher-order' idioms
There are a few high-level APIs that extend RDFLib graphs into other Pythonic idioms. For more a more explicit Python binding, there are , & .Support
Documentation for RDFlib is online at and is both handwritten by contributors and auto-generated from code.For general “how do I…” queries, users are encouraged to https://stackoverflow.com and tag question with
. Developers wanting to discuss RDFlib mechanics can use the and anyone can raise Issues or submit code improvements via Pull Requests against the .