From: Robert Klemme Date: 2004-05-04T01:59:00+09:00 Subject: Re: model for my problem (network simulator) ------=_NextPart_000_0007_01C43140.1FF73860 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit "Gergely Kontra" schrieb im Newsbeitrag news:20040503161710.GA16300@mlabdial.hit.bme.hu... > 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 ------=_NextPart_000_0007_01C43140.1FF73860 Content-Type: application/octet-stream; name="schedule.rb" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="schedule.rb" #!/usr/bin/ruby # not really needed, just for completeness class Event def execute(simulator) # whatever end end class Simulator attr :now def initialize # quick and dirty: hash; other data structure is more efficient @schedule = Hash.new {|h,k| h[k] = []} @now = nil end def go until @schedule.empty? @now = @schedule.keys.sort[0] puts "Time = #{@now}" @schedule[@now].each {|event| event.execute(self) } @schedule.delete @now end @now = nil end def schedule(time, event) raise "Can't travel back in time" if @now && @now >= time @schedule[time] << event end end class SampleEvent < Event def initialize(count, interval) @count, @interval = count, interval end def execute(s) puts " Executing #{inspect}" @count -= 1 s.schedule( s.now + @interval, self ) if @count > 0 end end e1 = SampleEvent.new 10, 2 e2 = SampleEvent.new 7, 3 sim = Simulator.new sim.schedule( 0, e1 ) sim.schedule( 0, e2 ) sim.go ------=_NextPart_000_0007_01C43140.1FF73860--