dynamic range_type?

2 views
Skip to first unread message

Bruce

unread,
Jun 11, 2008, 9:27:32 PM6/11/08
to rdfalchemy-dev
I'm running into a problem with the following:

class Bookmark(rdfSubject):
rdf_type = BM['Bookmark']
# a problem if the range is an Article
recalls = rdfSingle(BM.recalls, range_type=BIBO.Document)
modified = rdfSingle(DC.modified)
creator = rdfSingle(DC.creator)

So I've got two main subjects here: a Bookmark and a Document. But
that related Document may well be an Article, which in turn is related
to a Periodical.

I need to be able to grab those periodical titles where appropriate
from a template, so am imagining chaining the 'recalls' attribute so
that I can do:

print x.recalls.periodical.title

So is the right approach?

If yes, would there be some way to set the range_type dynamically? I'm
trying to create a method that does this, but am not sure how to get
this working:

class Bookmark(rdfSubject):
def recalls_range_type():
"""
Sets the range for the related Document.
"""
# need to test for dc:isPartOf; how?
if [DC['isPartOf']]:
return BIBO.Article
else:
return BIBO.Document

rdf_type = BM['Bookmark']
recalls = rdfSingle(BM.recalls, range_type=recalls_range_type())
date = rdfSingle(DC.date)
modified = rdfSingle(DC.modified)
creator = rdfSingle(DC.creator)

Bruce

Philip Cooper

unread,
Jun 12, 2008, 10:44:13 AM6/12/08
to rdfalch...@googlegroups.com
Bruce wrote:
> I'm running into a problem with the following:
>
> class Bookmark(rdfSubject):
> rdf_type = BM['Bookmark']
> # a problem if the range is an Article
> recalls = rdfSingle(BM.recalls, range_type=BIBO.Document)

> that related Document may well be an Article, which in turn is related


> to a Periodical.
>
> I need to be able to grab those periodical titles where appropriate
> from a template, so am imagining chaining the 'recalls' attribute so
> that I can do:
>
> print x.recalls.periodical.title
>
> So is the right approach?

I would think what you want is

print x.recalls.title

That seems natural to me... x.recalls returns a BIBO:Document and you
get the title of the document. Now if the issue is that for an
article, you want the periodical title and not the article title then
you do have to change your approach.

If that is the case, the "change" appears localized to the Bookmark
class. that means that a Bookmark title is a different animal.

how about an approach more like:

class Bookmark(rdfSubject):
rdf_type = BM['Bookmark']
# a problem if the range is an Article
recalls = rdfSingle(BM.recalls, range_type=BIBO.Document)

@property
def title(self):
doc = self.recalls
if BIBO.Article == doc[RDF.type]:
return doc.periodical.title
# or maybe return doc.isPartOf.title
# or whaever works
else:
return doc.title


The approach here is to define a custom "property" that does a small bit
of custom logic you want.


Then you just use:

x.title

--
Phil

Bruce

unread,
Jun 12, 2008, 3:01:53 PM6/12/08
to rdfalchemy-dev


On Jun 12, 10:44 am, Philip Cooper <philip.coo...@openvest.com> wrote:

> I would think what you want is
>
>    print x.recalls.title

Well, I want both. The RDF looks like:

ex:1 a bm:Bookmark ;
bm:recalls <http://nytimes.com/1> .

<http://nytimes.com/1> a bibo:Article ;
dc:title "Some Article Title" ;
dc:isPartOf <http://nytimes.com> .

<http://nytimes.com> a bibo:Periodical ;
dc:title "New York Times" .


The problem is, sometimes one bookmarks a Document that is not part of
some other thing. I have this problem because I define the Article
class as having a "periodical" attribute that maps from dc:isPartOf,
but that is not present in the Document class.

> That seems natural to me...  x.recalls returns a BIBO:Document and you
> get the title of the document.    Now if the issue is that for an
> article, you want the periodical title and not the article title then
> you do have to change your approach.

Per above, I always need the title, but in many cases I also need the
linked periodical title also. So I either need to move the isPartOf
thing to the Document class, or I need some other way to set the
range_type of "recalls" differently depending on the resource that
recalls refers to.

> If that is the case, the "change" appears localized to the Bookmark
> class.  that means that a Bookmark title is a different animal.
>
> how about an approach more like:
>
> class Bookmark(rdfSubject):
>      rdf_type = BM['Bookmark']
>      # a problem if the range is an Article
>      recalls = rdfSingle(BM.recalls, range_type=BIBO.Document)
>
>      @property
>      def title(self):
>          doc = self.recalls
>          if BIBO.Article == doc[RDF.type]:
>               return doc.periodical.title
>               # or maybe return doc.isPartOf.title
>               # or whaever works
>          else:
>               return doc.title
>
> The approach here is to define a custom "property" that does a small bit
> of custom logic you want.

OK, I'll see if I can work this out; thanks!

Bruce
Reply all
Reply to author
Forward
0 new messages