Hi Chris,
one way to accomplish this would be a new reader/writer plugin, based
on sparql_protocol. Here's a quick experimental plugin I put together:
http://dl.dropbox.com/u/244687/surf.sparql_protocol_virtuoso.zip
Unzip it, install it with "sudo python setup.py install"
Here's the gist of it, the ReaderPlugin class:
from sparql_protocol.reader import ReaderPlugin as
BaseReaderPlugin
class ReaderPlugin(BaseReaderPlugin):
def __init__(self, *args, **kwargs):
""" Initialize plugin. """
# By default, inferencing is disabled.
self.__query_prefix = ""
super(ReaderPlugin, self).__init__(*args, **kwargs)
def enable_inferencing(self, ruleset):
""" Enable inference rules identified by `ruleset`. """
self.__query_prefix = """ define input:inference "%s" """
% ruleset
def disable_inferencing(self):
""" Disable inferencing on future queries. """
self.__query_prefix = ""
def execute_sparql(self, q_string, format = 'JSON'):
""" Execute SPARQL query, optionally adding query prefix.
"""
q_string = self.__query_prefix + q_string
return BaseReaderPlugin.execute_sparql(self, q_string,
format)
As you can see, it subclasses ReaderPlugin of sparql_protocol and
overrides "execute_sparql" method to add the prefix to queries. And it
has two extra methods to turn inferencing on and off. Here's how one
would use it:
store_with_inferencing = surf.Store(reader =
"sparql_protocol_virtuoso", ... other parameters ... )
store_with_inferencing.reader.enable_inferencing("myontologyrules")
session_with_inferencing = surf.Session(store_with_inferencing)
While playing with this I discovered one potential problem you might
also run into once inferencing is enabled. With inferencing, if you
are loading an instance of class that has a superclass, the instance
will have *two* or more rdf:types defined--the real type and the
inferenced type(s). They are returned in no particular order, and SuRF
picks the first one. So the class of returned instance can be
different from what you expect, this can cause problems, especially,
if using class mapping.
Easy way to get around this: use inferencing only when you really need
it. Either:
* turn it on when needed and off afterwards. This is only safe in
single-thread situation, in multi-threaded situation multiple threads
turning inferencing on and off can step on each others toes
* in multi-threaded environment, define two stores, and two sessions:
one with inferencing and the other without. Use whichever is
appropriate at each time. Not elegant but works.
Peteris