diff --git a/lib/simplekit.rb b/lib/simplekit.rb index e9b14834780c9b5224d3785a1091c92a74623794..d55d1c8cbb132e965ee2a1db6c0842d9d6d81da9 100644 --- a/lib/simplekit.rb +++ b/lib/simplekit.rb @@ -1,15 +1,16 @@ require 'rubygems' require 'skewheap' -# Including SimpleKit gives you methods :run, :model_time, :schedule, -# and :halt as mixins. You <b>MUST NOT</b> provide your own implementations -# of methods with these names in your model. All but :run are -# delegated to the EventScheduler class. +# Including SimpleKit in your simulation model gives you methods +:run+, +# +:model_time+, +:schedule+, and +:halt+ as mixins. You <b>MUST NOT</b> +# provide your own implementations of methods with these names in your model. +# All but +:run+ are delegated to the +EventScheduler+ class. module SimpleKit - # This is the set of module methods to be passed to the EventScheduler + # The set of module methods to be passed to the EventScheduler DELEGATED_METHODS = [:model_time, :schedule, :halt] - # Run the model by creating a new EventScheduler and invoking its run method. + # Run your model by creating a new +EventScheduler+ and invoking its + # +run+ method. def run @mySim = EventScheduler.new(self) @mySim.run @@ -24,38 +25,39 @@ module SimpleKit end end - # This class provides the computation engine for a discrete event - # simulation model. It uses RubyGem's SkewHeap class as the priority + # Class +EventScheduler+ provides the computation engine for a discrete + # event simulation model. It uses the +SkewHeap+ RubyGem as a priority # queue implementation for the pending events list. # - # Users must create a model class which - # implements an init() method; - # Instantiates a model and invokes the run() method to start execution. + # Users must create a model class which: + # * implements an +init+ method; + # * Instantiates a model and invokes the +run+ method to start execution. class EventScheduler attr_reader :model_time, :user_model + # Initialize the +EventScheduler+ by remembering the specified model + # and setting up an empty event list. def initialize(the_model) @user_model = the_model @event_list = SkewHeap.new end - public - # Add an event to the pending events list. # - # param: event - the name of the event to be scheduled. - # param: delay - the amount of time which should elapse before - # the event executes. - # param: args - the list of arguments to pass to the event at - # invocation time. + # *Arguments*:: + # - +event+ -> the event to be scheduled. + # - +delay+ -> the amount of time which should elapse before + # the event executes. + # - +args+ -> an optional list of arguments to pass to the event + # at invocation time. def schedule(event, delay, *args) raise "Model scheduled event with negative delay." if (delay < 0) @event_list.push EventNotice.new(event, @model_time + delay, *args) end - # Start execution of a model. The simulation model_time is + # Start execution of a model. The simulation +model_time+ is # initialized to zero and the model is initialized via the - # mandatory init() method. Run() then enters a loop in which + # mandatory +init+ method. Then enter a loop in which # the pending event with smallest time is extracted from the # event list, the model_time is updated to the event time, # and the event method is invoked. @@ -69,17 +71,18 @@ module SimpleKit end # Clears the event list, which causes termination of the simulation. + # Never schedule any new events after invoking +halt+. def halt @event_list.clear end end - private - # This is a private helper class for the EventScheduler class. # Users should never try to access anything in this class directly. + private class EventNotice include Comparable + attr_reader :event, :time, :args def initialize(event, time, *args) diff --git a/simplekit.gemspec b/simplekit.gemspec index e2c27c3fe1ee0cb16e6763d46f16a3f22e5d2548..4e713abea3da7e02e8457035a2a391243662d05f 100644 --- a/simplekit.gemspec +++ b/simplekit.gemspec @@ -1,10 +1,10 @@ # -*- ruby -*- -_VERSION = "0.4.0" +_VERSION = "0.4.2" Gem::Specification.new do |s| s.name = "simplekit" s.version = _VERSION - s.date = "2013-01-14" + s.date = "2013-04-11" s.summary = "Discrete event simulation engine." s.homepage = "git://or.nps.edu/simplekit-ruby.git" s.email = "pjs@alum.mit.edu" @@ -16,7 +16,7 @@ Gem::Specification.new do |s| lgpl.txt lib/simplekit.rb ] - s.add_runtime_dependency 'skewheap', '~> 0.1.0' + s.add_runtime_dependency 'skewheap', '~> 1.0.0' s.required_ruby_version = '>= 1.8.1' s.license = 'LGPL' end