RDFAlchemy with Pylons

28 views
Skip to first unread message

Kless

unread,
May 25, 2008, 7:10:11 AM5/25/08
to rdfalchemy-dev
Does anybody has integrated RDFAlchemy into a Pylons/SQLAlchemy
project? Could give any example

gjhi...@googlemail.com

unread,
May 25, 2008, 1:19:14 PM5/25/08
to rdfalchemy-dev


On May 25, 12:10 pm, Kless <jonas....@googlemail.com> wrote:
> Does anybody has integrated RDFAlchemy into a Pylons/SQLAlchemy
> project? Could give any example

It seems fairly straightforward. The company_test.py test file in
rdfalchemy/tests has most of what's required. I used that code as the
basis for a model/triples.py file and a controllers/triples.py file
(appended). It seemed to work okay in the latest Pylons-tip.

model/triples.py
============

from rdfalchemy import rdfSubject, rdfSingle, rdfMultiple
from rdflib import ConjunctiveGraph, Namespace, Literal, BNode, URIRef

from StringIO import StringIO

n3data = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix ov: <http://owl.openvest.org/2005/10/Portfolio#>.
@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
@prefix iso3166: <http://www.daml.org/2001/09/countries/iso-3166-
ont#>.

ov:C_US_SUNW a ov:Company;
ov:GICS_2 ov:GICS_2_45;
ov:companyName "Sun Microsystems";
ov:country iso3166:US;
ov:nasdaqSymbol "SUNW";
ov:secCik "0000709519";
ov:symbol "JAVA".

ov:C_US_IBM a ov:Company;
ov:GICS_2 ov:GICS_2_45;
ov:companyName "International Business Machines Corp.";
ov:country iso3166:US;
ov:nyseSymbol "IBM";
ov:secCik "0000051143";
ov:symbol "IBM";
ov:yindustry "Diversified Computer Systems";
ov:ysector "Technology";
vcard:tel "914-499-1900";
vcard:url "http://www.ibm.com"^^<http://www.w3.org/2001/
XMLSchema#anyURI>;
ov:stockDescription "International Business Machines Corporation
(IBM) operates as an information technology (IT) company worldwide. It
has .....".
"""

OV = Namespace('http://owl.openvest.org/2005/10/Portfolio#')
VCARD = Namespace("http://www.w3.org/2006/vcard/ns#")

rdfSubject.db = ConjunctiveGraph()
rdfSubject.db.parse(StringIO(n3data), format='n3')

class Company(rdfSubject):
rdf_type = OV.Company
symbol = rdfSingle(OV.symbol,'symbol')
cik = rdfSingle(OV.secCik,'cik')
companyName = rdfSingle(OV.companyName)
address = rdfSingle(VCARD.adr)

controllers/triples.py
===============

# -*- coding: utf-8 -*-

from myapp.lib.base import *
from myapp.model.triples import *
import logging
log=logging.getLogger('rdfAlchemy')
## comment out to quiet debug messages
log.setLevel(logging.DEBUG)

class TriplesController(BaseController):
"""REST Controller styled on the Atom Publishing Protocol"""

def __before__(self):
c.userid = c.userid if c.userid else 1
try:
flash = http_session.get('flash')
if flash:
c.flash = flash
del http_session['flash']
http_session.save()
except Exception:
pass

def index(self):
co = Company.get_by(symbol='IBM')
op = '%s' % str(co)
co = Company.get_by(cik="0000051143")
assert co.symbol == "IBM"
op += '%s' % str(co)
for co in Company.ClassInstances():
op += "%s has an SEC symbol of %s" % (co.companyName,
co.cik)
Company.stockDescription = \
rdfSingle(OV.stockDescription,'stockDescription')
assert co.companyName == co[OV.companyName]
Company.industry = rdfSingle(OV.yindustry,'industry')
co = Company.get_by(symbol = 'JAVA')
op += '%s' % str(co)
co.industry = 'Computer stuff'
op += '%s' % str(co)
op += co.db.serialize(format='n3')
return op.replace('<', '&lt;')

Philip Cooper

unread,
May 25, 2008, 2:19:45 PM5/25/08
to rdfalch...@googlegroups.com

On May 25, 2008, at 5:10 AM, Kless wrote:


Does anybody has integrated RDFAlchemy into a Pylons/SQLAlchemy
project? Could give any example

I actually use RDFAlchemy for a couple of pylons projects.  Graham has already hit on the simplicity.

I've posted the beginnings of a cookbook page for pylons [1] that shows it working with development.ini and environment.py files, more closely tracking the sqlalchemy configuration.

Watch out for the notes at the bottom of that wiki page [1].  Threading issues need more attention and I don't know if I want to attack it with sqlalchemy/pylons connection pooling or just create a server protocol (twisted) and run the triplestore in a seperate process.

Both projects deserve attention but this one man circus can't get to it all right now :-(

--
Phil





Bruce D'Arcus

unread,
May 25, 2008, 6:12:12 PM5/25/08
to rdfalchemy-dev
On May 25, 2:19 pm, Philip Cooper <philip.coo...@openvest.com> wrote:

...

> I've posted the beginnings of a cookbook page for pylons [1] that
> shows it working with development.ini and environment.py files, more
> closely tracking the sqlalchemy configuration.

These kinds of examples are really useful.

> Watch out for the notes at the bottom of that wiki page [1].

Some of the language here is confusing (at least to me); for example:

"Mysql should be modified to use the sqlalchemy connection pooling."

"Should be modified" by whom? If by the user/third-party dev, then
how?

Second:

"rdflib is out of the picture as a backend. http performance penalties
can be significant!"

Does this warning only apply to Pylons? What if I'm using web.py,
which I'll deploy with mod_wsgi?

Bruce

Philip Cooper

unread,
May 25, 2008, 6:59:26 PM5/25/08
to rdfalch...@googlegroups.com
Bruce D'Arcus at about 5/25/08 4:12 PM said:
> On May 25, 2:19 pm, Philip Cooper <philip.coo...@openvest.com> wrote:

>> Watch out for the notes at the bottom of that wiki page [1].
>
> Some of the language here is confusing (at least to me); for example:
>
> "Mysql should be modified to use the sqlalchemy connection pooling."
>
> "Should be modified" by whom? If by the user/third-party dev, then
> how?
>

Whomever has the time. I will not get to it until I have a project
that calls for an rdflib/mysql combo. rdflib and pylons are both
projects in motion so I don't want to produce code that no one uses
and is soon obsoleted.

As for how, I guess if I could answer in great detail I could just do
it. The basic drill is to figure out how to use a connection pool and
which one to use.
you can look at pylons [1] and search for "Multiple engines"
I know how to get the db connections my mysql but i'm not sure about
how to handle a "scoped session" and how happy rdflib would be about
poking a db connection into the guts of the graph.__store._db

Hell, even if I get that to work, it's just one framework and one
backend. There is hope, I see that rdflib 3.0a has pkg_resources type
plugins for the stores[2]. plugins are good.

now my brain hurts and I'm going for a bike ride :-).

> Second:
>
> "rdflib is out of the picture as a backend. http performance penalties
> can be significant!"
>
> Does this warning only apply to Pylons? What if I'm using web.py,
> which I'll deploy with mod_wsgi?

That warning applies the rdfalchemy SesameGraph calls not to pylons or
mod_wsgi. SesameGraph currently uses the http sesame protocol so that
a program that expects to update all prices for stocks in the S&P 500
will end up making 500 seperate http requests with all of the
connection overhead on each one. Much slower that a dbapi where you
connect once and then perform 500 seperate calls to the protocol.

--
Phil

[1]http://wiki.pylonshq.com/display/pylonsdocs/Using+SQLAlchemy+with+Pylons
[2]http://code.google.com/p/rdflib/source/browse/tags/3.0.0a/rdflib/setup.py?r=1438

Kless

unread,
May 26, 2008, 4:46:05 AM5/26/08
to rdfalchemy-dev
Thanks! This information is very useful.

I'd use together RDFAlchemy with SQLAlchemy. Using RDFA only for
profiles data, and SQLA for identity -users, groups, permissions- and
rest of data in the web service.

I'll have to relation RDFA profiles with SQLA users.
Reply all
Reply to author
Forward
0 new messages