Prefix SPARQL queries

17 views
Skip to first unread message

Chris Wj

unread,
Jul 3, 2010, 3:59:30 PM7/3/10
to sur...@googlegroups.com
I am connecting to a Virtuoso SPARQL endpoint, and I need a way to prepend queries with a special syntax to enable inference support. The text that needs to go before the SELECT keyword is "define input:inference 'myontologyrules'".
So the full query is
define input:inference 'myontologyrules'
SELECT * FROM <mygraphuri> where {?s ?p ?o}

What do you think the best way to accomplish this and still use surf classes? I still want to be able to say "session.get_class(surf.ns.MYNS['myclassname'])).all()" and get the objects back, but with inference support on the back end.

Pēteris Caune

unread,
Jul 15, 2010, 10:05:36 AM7/15/10
to surfrdf
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

Chris Wj

unread,
Jul 15, 2010, 4:58:21 PM7/15/10
to sur...@googlegroups.com
Peteris, this is great. I will be trying it out asap. Thank you for starting this and showing how to create such a plugin.

2010/7/15 Pēteris Caune <cuu...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "surfrdf" group.
To post to this group, send email to sur...@googlegroups.com.
To unsubscribe from this group, send email to surfrdf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/surfrdf?hl=en.


Reply all
Reply to author
Forward
0 new messages