Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

adding meta-data to references-as-graph-arcs

by AidanLee (Chaplain)
on Feb 14, 2004 at 23:13 UTC ( [id://329088]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

AidanLee has asked for the wisdom of the Perl Monks concerning the following question:

I've got a set of modules that use objects to represent nodes on a graph, and uses simple references to represent the arcs between them. What I'm trying to do now is introduce a concept whereby I can declare an arc as 'active' or 'inactive' without severing the arc. Similar to how you can leave a lamp plugged into the wall socket, and the electricity is either on or off.

What I'm wondering is if there might be a good metaphor using simple perl syntax to represent this phenomenon without a secondary data structure to store this active/inactive information. I tried adding a level of indirection, such as:
#active: push @{$object->{related}}, $object2; #inactive @{$object->{related}}, \$object2;
but that got to be a nightmare in remembering to dereference it everywhere. So I'm now wondering if there's a way to tag the reference in such a way as to be able to test for it's 'activeness'. Scalar::Util's weaken() function would be a great metaphor for this (testing with the isweak() function) but I can't afford to have weaken's intended effect as a side effect.

Note that I don't want to annotate the targeted object. The object isn't 'active' or 'inactive'. It's the arc drawn between the two.

I'm willing to entertain black magic at this point even if it doesn't prove feasible down the line. I'm a stickler for useable, straightforward syntax, even if it means some vodoo underneath.

update: After some discussion in the chatterbox with Mr. Muskrat and atcroft it became obvious that I hadn't provided enough information:
  • The arc can represent a one-to-one, one-to-many, many-to-one, or many-to-many relationship, and as such is implemented as an object that is itself a tied array (tied for some of the magic i already do underneath).
  • When acting as a to-one relationship it knows to only return the first element of it's array, and will only store one value.
  • It is an array rather than a hash because i need to preserve order in 'to-Many' relationships.
  • These object are being passed into Template Toolkit templates, and as such I am trying to keep their useage as straightforward as possible.
    [% FOREACH related_obj = object.related %] ... [% END %]
At the moment the best strategy seems to be to keep the active/inactive information in a parallel data structure inside the tied array. That way i can do something like
[% IF object.related.isActive( related_obj ) %]...[% END %]
but if anyone has any other interesting approaches to this I'd be glad to hear them. The simpler for the template authors, the better.

Replies are listed 'Best First'.
Re: adding meta-data to references-as-graph-arcs
by kvale (Monsignor) on Feb 15, 2004 at 01:24 UTC
    It seems to me that dealing with references and and parallel decorations of references seems pretty complex for folk not expert in these things. A simpler approach may be to abstract away the graph representation and algorithms from the objects themselves and create a graph object proper with objects associated with the nodes.

    This can probably be done most easily by using Graph::Directed to handle graph stuff and adding node number to the object's data to be set upon initialization. The module has all the (basic) graph methods you could want, with no worries about dereferencing, just simple method calls.

    -Mark

Re: adding meta-data to references-as-graph-arcs
by chromatic (Archbishop) on Feb 15, 2004 at 02:02 UTC

    I'm half tempted to suggest using dualvar() (from Scalar::Util, I believe), overriding the boolean comparator in your class to use that value.

    It's worth tons of cool points if it works, but I wouldn't really do it that way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://329088]
Approved by Mr. Muskrat
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.