Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by AidanLee (Chaplain)
on Feb 15, 2004 at 04:13 UTC ( #329088=perlquestion: print w/replies, xml ) Need Help??

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 chromatic (Archbishop) on Feb 15, 2004 at 07: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.

Re: adding meta-data to references-as-graph-arcs
by kvale (Monsignor) on Feb 15, 2004 at 06: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

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
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2022-05-22 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (80 votes). Check out past polls.

    Notices?