Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm working on some OO perl code. I wrote and have been using a module to help me do some neural network simulations for the psycholinguistics lab I work in. The smallest object is a node, and each node can know its name, level/layer in the network, current activation level, previous activation level, and neighbors. Once you've got some nodes, you throw them together into a model/network object, and use methods there to define the connections between nodes and the spread of activation.

Now other people in my lab want to be able to design and run simulations, and so I'm trying to provide a way for them to specify the design of the network without ever seeing the code. I'd like to be able to read in a plain text template, and use that to generate the nodes and to define the connections between them.

A sample config file might look like:

nodes: input, n1, n2, output input => n1 input => n2 n1 => output n2 => output
There will probably be some other parameters in there, but these are the only ones that have to do with the node and network objects.

I want to be able to generate a new node object for each name specified in the first line of the file, and then to put all of those objects into a network, and use the remaining lines to define the connections between the nodes. This means being able to refer to a node object by the name specified in the file.

I'm pretty sure my question is how to generate an anonymous object, set its name attribute, and then use its name attribute to call methods on it.

I've got a connect() method of the Model class that takes two objects from the Node class as its parameters. Once it's got the two node objects, it calls connects_to() on one and input_from() on the other (both from the Node class. So ideally,after reading in lines 2-5 of the file, I would be able to do something like:

$model->connect('input','n1'); $model->connect('input','n2'); $model->connect('n1','output'); $model->connect('n2','output');
and have it call the correct methods from the Node class without actually having given it Node objects as parameters (but having given it attributes corresponding to Node objects).

I'm pretty sure that either my constructor or my name() method (or both) needs to get cleverer, but I don't know what to do with them. A pared-down version of my constructor from the Node class looks like this (all I've done is taken out a bunch of the attributes that I define for the node, like LAYER, RESTING_LEVEL, and so on):

sub new { my $class = shift; my $self = {}; $self -> {NAME} = undef; bless $self, $class; return $self; }
and here's how I set the node's name:
sub name { my $self = shift; $self -> {NAME} = shift if (@_); return $self -> {NAME}; }

Like I said above, I think what I'm trying to do is build an anonymous object, set its name attribute, and then use the name attribute to call methods on it. I'm worried that I may not have been clear enough about what I need and what I'm trying to do, so if you have any questions or would like to see any of the code I referred to but didn't post, let me know and I'll include it in the comments.

/au


In reply to calling anonymous objects by their names by aufrank

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2023-02-07 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (40 votes). Check out past polls.

    Notices?