On 5/31/06, Miles Waller <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm hoping someone can help with me with this question about rdflib.
> Apologies in advance as i'm new to rdflib
> (and to rdf a bit too) and this might be a newbie question.
>
> I have some code which I am using to construct an rdf sequence.  I call
> 'addItemToSequence' several times
> to build up the sequence, having set up the graph elsewhere.
>
> ns_client_site1_url = "http://example.com/ns/client-site1#";
> ns_client_site1 = Namespace(ns_client_site1_url)
> site = ns_client_site1
>
> def addItemToSequence(source_id, target_id)
>     g.add( (site[source_uid], RDF.Seq, URIRef('children_of_%s' %
> source_uid) ) )
>     s = Seq( g, URIRef('children_of_%s' % source_uid) )
>     g.add( (URIRef('children_of_%s' % source_uid), RDF.li,
> site[target_uid]) )
>
> I get the following rdf out of it:
>
> <rdf:RDF
>   xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
>   <rdf:Description rdf:about="http://example.com/ns/client-site1#124";>
>     <rdf:Seq>
>       <rdf:Description rdf:about="children_of_124">
>         <rdf:li rdf:resource="http://example.com/ns/client-site1#157"/>
>         <rdf:li rdf:resource="http://example.com/ns/client-site1#151"/>
>
>       </rdf:Description>
>     </rdf:Seq>
>   </rdf:Description>
> </rdf:RDF>
>
> The intent is to produce the rdf required to describe a simple,
> hierarchical site map as used by a website,
> which i can then query.  In the example, the resources and
> target_uid/source_uid represent the unique ids
> of the pages that are related.
>
> Now, my questions are:
>
> - have i created a sequence correctly here?

No, the sequence wasn't properly created.  The rdf:li is RDF/XML
synactic short cut to using rdf:_1,rdf:_2,etc.. explicitely.  See:
http://www.w3.org/TR/rdf-syntax-grammar/#section-Syntax-list-elements

Also the rdf:Seq is mean't to be the object of an rdf:type statement
(identifiying the resource as a sequence) - you are using it
incorrectly.

Finally, rdflib.Graph.Seq is a convenience class for iterating over an
rdf:Seq, not creating one (thought it would probably be useful to
support creating a rdf:Seq as well).  What (I think) you want is:

ns_client_site1_url = "http://example.com/ns/client-site1#";
ns_client_site1 = Namespace(ns_client_site1_url)
site = ns_client_site1

def addItemToSequenceAtOnce(source_id, target_ids)
    for target_id in target_ids:
        g.add( (URIRef('children_of_%s' % source_id),
RDFNS['_'+target_ids.index(target_id)],
site[target_id]) )

def addItemToSequence(source_id, target_id)
    s = Seq( g, URIRef('children_of_%s' % source_id) )
    nextIndex = len(s) + 1
    g.add( (URIRef('children_of_%s' % source_id), RDFNS['_'+nextIndex],
site[target_id]) )

g.add((URIRef('children_of_%s' % source_uid),RDF.type,RDF.Seq)
addItemToSequence(URIRef('children_of_%s' % source_uid),target_id_1)
addItemToSequence(URIRef('children_of_%s' % source_uid),target_id_2)
... or ...
addItemToSequenceAtOnce(URIRef('children_of_%s' %
source_uid),[target_id_1,target_id_2])

> i.e. am i _always_ going to
> get the items back in the same order, or
> do i just get them back in this order because of a quirk of
> python/rdflib.

You should always get them back in the same order due to the
rdf:_1,rdf:_2, etc..member properties.  Order is inherent with those
properties.

>
> - finally, if i want to change the order of items in the sequence, how
> should this be done?  delete all the items
> and add them again in the right order?

I think that would be your only option

> do sequences provide any help to
> 'promote' items at all?  should they?

I'm not sure what you mean by promote.
_______________________________________________
Dev mailing list
[email protected]
http://rdflib.net/mailman/listinfo/dev

Reply via email to