write to multiple contexts in a single session

7 views
Skip to first unread message

zuotian

unread,
Feb 15, 2012, 3:26:59 PM2/15/12
to surfrdf
Hi,

I have been using rdflib for a while, and just recently switched to
surfrdf. I really like the project.

My current script needs to write triples to a sesame store in multiple
contexts (named graphs). For example

store = Store(reader='sesame2', ..., default_context='http://
context1')
session.Session(store)
Person = session.get_class(ns.FOAF.Person)

person1 = Person()
person.save()

# now I need to write another person to a different context.
person2 = Person()
person2.foaf_knows = person1
person2.save()

How can I save person2 to context2? Do I need to create a different
store and recreate Person class? The script will generate 1 million
triples in 20k named graphs.

Thanks,
Zuotian

Cosmin Basca

unread,
Feb 15, 2012, 3:44:18 PM2/15/12
to surfrdf
Hi Zuotian,

I haven't tried this solution, but it should work. You need to change
the value of the *context* attribute before saving. For example in you
code:

# this is save din the default context you declared for the store
person1 = Person()
person1.save()

# to save the second person in a diff context do this:
person2 = Person(context='http://mysecondcontext.com')
person2.foaf_knows = person1
person2.save()

# another way to do it is like this:
person2 = Person()
person2.context = 'http://mysecondcontext.com'
person2.save()

Hope this helps,
Cheers,
Cosmin

zuotian

unread,
Feb 16, 2012, 5:21:03 AM2/16/12
to surfrdf
Great! That's exactly what I am looking for.

Thanks,
Zuotian

zuotian

unread,
Feb 24, 2012, 8:37:41 AM2/24/12
to surfrdf
Hi Cosmin,

One more scenario.

# Suppose I have person1 in default context
person1 = Person()
person1.context = context1
person1.foaf_name = "me"
person1.save()

# now I want to add more triples about person1 but save them in a
different context.
person1.foaf_knows = "you"
person1.context = context2
person1.save()

When the second save is called, not surprisingly, person1's foaf_name
is also saved to context2. So I reinitialized person1 for context2, so
that foaf_name is no longer in context2. However, the triple "person1
rdf:type Person" still exists in both contexts.

My question is, is there a way to save an object and only write out
the properties that has not been save to the store?

Thanks,
Zuotian

Cosmin Basca

unread,
Feb 24, 2012, 5:09:35 PM2/24/12
to surfrdf
Hi Zuotian,

Surf is not intended to be a generic RDF management framework, it's
main purpose is to bridge the gap between the object-oriented paradigm
and that of RDF. The short answer to your scenario is no. Let me
detail: when an instance of a class is created it will always have a
triple of type <instance_subject, rdf:type, MyClassType> associated,
that triple will go into the current context when saved. As with most
things in python, you can "hack" a solution for your problem but it
won't be pretty. You can either remove the rdf:type triple from the
store after you save your instances or re-implement the graph() method
of your particular resource either for all instances (via subclassing)
or a specific instance by replacing the method dynamically
(person1.graph = my_graph_method). Inside this method you should
remove that triple before you return the graph. This is however a hack
and I would advise you not to do it as it can introduce hard to spot
bugs, potentially breaking down Surf. Hope this clarifies your
question,

Cheers,
Cosmin
Reply all
Reply to author
Forward
0 new messages