mepython said:
This will do the trick. I had brain f*rt; I will do one small change: I
will create simulation attribute at class level (for Processes and
Resources), so that I have to assign it only once. Thanks.
In that case I'd make Simulation a singleton. Then you don't need any
assignments at all but can access the global Simulation instance from all
parts of the system and also it's much clearer from a documentation
perspective. I'd definitely not use a class variable in Process and
Resource for that.
However, both approaches will break if there are several simulations. Also
I have the feeling that rather lazyness dictates this one assignment only
policy than design. I'm curious to learn why it is a problem with having
simulation as a member in Process and Resource.
I might need
some more help when I need Processes to hold Resources,
interrupt/kill/restart other Processes and so on.
Maybe I missed something, but something like this?
class Simulation
attr_reader :time
attr_reader :events
def initialize
@time = 5.0
@events = ['hello', 'goodbye']
end
end
class Processes
attr_accessor :simulation
def do_sth
# operate on @simulation
puts @simulation.events
end
def do_sth2
# operate on @simulation
puts @simulation.time
end
end
class Resources
attr_accessor :simulation
def do_sth
end
end
Btw, with a little refactoring:
module SimulationClient
attr_accessor :simulation
end
class Simulation
def create(cl,*a,&b)
x = cl.new(*a,&b)
x.simulation = self if SimulationClient === x
x
end
end
class Process
include SimulationClient
...
end
class Resource
include SimulationClient
...
end
s = Simulation.new
p1 = s.create Process, "event processor"
....
If you use Simulation as factory for processes and resources then it's easy
to encapsulate the assignment in one place. Although I don't know the
details of your app it sounds a reasonable thing to do as the simulation is
the central piece and that uses / has processes and has resources.
Kind regards
robert