Chapter 9. Introspecting Objects from Data
A common problem you face when working with graph databases is figuring out how to access the data easily and efficiently in your code. By using an OWL ontology to describe the common shape of our data, we can map OWL classes in our graph database to Python objects directly and easily, making it easy to read and write data from the database. This approach to database programming is commonly called object-relational mapping and has been popularized in libraries like Python’s SQLObject, Java’s Hibernate, and Ruby’s ActiveRecord. Performing this kind of mapping is very easy with a graph database and an OWL ontology, as we’ll demonstrate in this chapter. The RDFObject framework described here demonstrates how easy it is to tightly integrate code with graph data.
RDFObject Examples
Let’s get a quick overview of how the RDFObject framework works by
looking at some examples using our film data. We’ve started by loading the OWL ontology into a Sesame repository called
semprog
running on localhost. First we’ll initialize
the connection to the Sesame repository and create an
RDFObjectFactory
, the base object for creating
RDFObjectGraphs
, which we will use to
access our graph data. The RDFObjectFactory
queries the Sesame graph
for ontology information on initialization and then pre-computes and caches mappings from Python
attributes to RDF URIs:
>>> from rdfobject import * >>> sc = SesameConnection("localhost:8080", "semprog") >>> factory = RDFObjectGraphFactory(sc) ...
Get Programming the Semantic Web now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.