RDF Cartridges (RDF Mappers or RDFizers) tutorial
RDF Cartridges This article explains how to implement a Python based RDFa Cartridge based on pyRDFa library.

## Setting up server

This tutorial explain steps to use the Python language to extend the Virtuoso Sponger.

The server must have installed latest Python hosting plugin. When it is installed and registered in the configuration file a new function python_exec will be available for developers.

Setup the Virtuoso server INI to include python module

    	[Plugins]
    	LoadPath = ../lib
    	Load1    = Hosting, hosting_python.so
    	...

The python_exec takes following arguments:

- code - a string containing the Python code
- function_name - a string containing the name of Python function to be executed
- param1 : a string containing first parameter
- param2 : a string containing second parameter
- as many parameters as Python function has

The Python based function must return a single string value.

## Setting up environment

Before to write cartridge you need to setup Python environment. To do this you need to download and install rdflib, pyRDFa. Read the pyRDFa paper for what additional libraries are needed.

If your Python installation have Zope support, you should disable the zope interfaces in rdflib. This is needed because Python C-API expirence problems when using sub modules within C code. In order to disable you can comment out following lines in [rdflib_home]/rdflib/__init__.py :

        36 #from rdflib.interfaces import IIdentifier, classImplements
        37 #classImplements(URIRef, IIdentifier)
        38 #classImplements(BNode, IIdentifier)
        39 #classImplements(Literal, IIdentifier)

then do:

        perl setup.py build
        perl setup.py --user install

## RDF Cartridge implementation notes

The implementation consist of two steps:

- Make a copy of the localRDFa.py and use as template to run pyRDFa extractor over single string stream. The details can be seen in source of pyRDFa.py code attached to this tutorial.
- Make a Virtuoso/PL based stored procedure to call the Pyhton based extractor. The source of this function can be seen in rdf_cartridge.sql script.

The stored procedure is used to do two main operations: to call 'processString' Python function inside pyRDFa.py script and to load the result in the Virtuoso RDF store.

Another important item is to register the cartridge with Sponger. this is done by insert statement into DB.DBA.SYS_RDF_MAPPERS table. Note that in this example the new cartridge will be disabled if you run that code, if you want to enable and test the cartridge the flag in RM_ENABLED column must be 1. or enable the crtridge via conductor.