model for my problem (network simulator)

G

Gergely Kontra

Hello!

I'd like to ask for help for my problem. Maybe the Q isn't
ruby-specific, but I WILL implement it in ruby, and I want to make a
rubyish program. I hope I can learn from the problem.

So: I had to write a network simulator. It consists of nodes, link
between nodes. The delay of the link should be user-programmable (maybe
proc is the right way, but it must be evaluated in the object's scope,
because delay is not independent of the link's state...

And finally the simulator has agents. Agents are programs, which can
migrate. Migration is requested by agent. (Request goes to the node, and
the node does the actual migration...)
Agent must know about their own location, it can ask the node about
outgoing links (so nodes must know about links ending at the node, of
course).

Shall I submit my code? Or did anybody has a model for it already?

thx in advance
Gergo
 
R

Robert Klemme

Gergely Kontra said:
Hello!

I'd like to ask for help for my problem. Maybe the Q isn't
ruby-specific, but I WILL implement it in ruby, and I want to make a
rubyish program. I hope I can learn from the problem.

So: I had to write a network simulator. It consists of nodes, link
between nodes. The delay of the link should be user-programmable (maybe
proc is the right way, but it must be evaluated in the object's scope,
because delay is not independent of the link's state...

And finally the simulator has agents. Agents are programs, which can
migrate. Migration is requested by agent. (Request goes to the node, and
the node does the actual migration...)
Agent must know about their own location, it can ask the node about
outgoing links (so nodes must know about links ending at the node, of
course).

Shall I submit my code? Or did anybody has a model for it already?

What was your question exactly? You seem to have implemented the simulator
already...

As a general OO rule of thumb, take all the interesting nouns and make them
classes. This seems to be pretty much straightforward in your case.

The thing I find more interesting is the scheduling. This could be done
like this (attached).

Regards

robert
 
G

Gergely Kontra

The thing I find more interesting is the scheduling. This could be done
like this (attached).
Hey!
I have almost the same code for scheduling. I just used something
similar to this. The only difference: I used relative time, so my method
is called after...
Maybe it worth to make something generally usable?

BTW
Hash.new {|h,k| h[k]= []} can be written as
Hash.new {[]}
 
R

Robert Klemme

Gergely Kontra said:
Hey!
I have almost the same code for scheduling. I just used something
similar to this. The only difference: I used relative time, so my method
is called after...

Do you convert to abs time internally or how do you make sure the order is
correct?
Maybe it worth to make something generally usable?

=> RAA
BTW
Hash.new {|h,k| h[k]= []} can be written as
Hash.new {[]}

Nope, that's not the same:

irb(main):001:0> h=Hash.new {[]}
=> {}
irb(main):002:0> h[1]
=> []
irb(main):003:0> h
=> {}
irb(main):004:0> h=Hash.new {|h,k| h[k]=[]}
=> {}
irb(main):005:0> h[1]
=> []
irb(main):006:0> h
=> {1=>[]}
irb(main):007:0> RUBY_VERSION
=> "1.8.1"

Your solution makes the idiom "h[key] << something" unusable since all
those arrays never make it into the hash.

robert
 
G

Gergely Kontra

Nope, that's not the same:
irb(main):001:0> h=Hash.new {[]}
irb(main):002:0> h[1]
irb(main):003:0> h
=> {}
irb(main):004:0> h=Hash.new {|h,k| h[k]=[]}
irb(main):005:0> h[1]
irb(main):006:0> h
=> {1=>[]}
Your solution makes the idiom "h[key] << something" unusable since all
those arrays never make it into the hash.

Oh, yes, I run into the problem, and this is the weird case, where
h[key] << something and h[key] <<= something is different...

Gergo
 
R

Robert Klemme

Gergely Kontra said:
Nope, that's not the same:
irb(main):001:0> h=Hash.new {[]}
irb(main):002:0> h[1]
irb(main):003:0> h
=> {}
irb(main):004:0> h=Hash.new {|h,k| h[k]=[]}
irb(main):005:0> h[1]
irb(main):006:0> h
=> {1=>[]}
Your solution makes the idiom "h[key] << something" unusable since all
those arrays never make it into the hash.

Oh, yes, I run into the problem, and this is the weird case, where
h[key] << something and h[key] <<= something is different...

Note also that h[key] << something is more efficient since it does only one
hash lookup and no hash update.

Regards

robert
 
G

Gergely Kontra

What was your question exactly? You seem to have implemented the simulator
already...

Just one little thing (chicken-egg problem): the links should know about
the 2 ending nodes, and nodes must know about in/outgoing links. So how
to easily administrate it?

Gergo
 
Z

Zsban Ambrus

"rubyish program" -- does this mean a program in ruby? probably since
you post this here
Just one little thing (chicken-egg problem): the links should know about
the 2 ending nodes, and nodes must know about in/outgoing links. So how
to easily administrate it?

That's easy in ruby, as ruby is garbage-collected, so it can clean up
circular references by itself. Thus, you can add a list of both the in and
outgoing links to each nodes.
 
R

Robert Klemme

Zsban Ambrus said:
"rubyish program" -- does this mean a program in ruby? probably since
you post this here


That's easy in ruby, as ruby is garbage-collected, so it can clean up
circular references by itself. Thus, you can add a list of both the in and
outgoing links to each nodes.

IMHO the question was rather how to manage those references so that they
are always in sync. That would be the typical problem with bi directional
references in OO languages. Typically you manually have to write code for
that (unless you find a library that does it gracefully) but if done
properly you need to do it only once and can then reuse that code.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top