Skip to content
Snippets Groups Projects
Commit 502d7e09 authored by pjs's avatar pjs
Browse files

revised docs, updated gemspec to use latest skewheap


Signed-off-by: default avatarpjs <pjs@alum.mit.edu>
parent 47ab171f
No related branches found
No related tags found
No related merge requests found
require 'rubygems' require 'rubygems'
require 'skewheap' require 'skewheap'
# Including SimpleKit gives you methods :run, :model_time, :schedule, # Including SimpleKit in your simulation model gives you methods +:run+,
# and :halt as mixins. You <b>MUST NOT</b> provide your own implementations # +:model_time+, +:schedule+, and +:halt+ as mixins. You <b>MUST NOT</b>
# of methods with these names in your model. All but :run are # provide your own implementations of methods with these names in your model.
# delegated to the EventScheduler class. # All but +:run+ are delegated to the +EventScheduler+ class.
module SimpleKit 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] 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 def run
@mySim = EventScheduler.new(self) @mySim = EventScheduler.new(self)
@mySim.run @mySim.run
...@@ -24,38 +25,39 @@ module SimpleKit ...@@ -24,38 +25,39 @@ module SimpleKit
end end
end end
# This class provides the computation engine for a discrete event # Class +EventScheduler+ provides the computation engine for a discrete
# simulation model. It uses RubyGem's SkewHeap class as the priority # event simulation model. It uses the +SkewHeap+ RubyGem as a priority
# queue implementation for the pending events list. # queue implementation for the pending events list.
# #
# Users must create a model class which # Users must create a model class which:
# implements an init() method; # * implements an +init+ method;
# Instantiates a model and invokes the run() method to start execution. # * Instantiates a model and invokes the +run+ method to start execution.
class EventScheduler class EventScheduler
attr_reader :model_time, :user_model 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) def initialize(the_model)
@user_model = the_model @user_model = the_model
@event_list = SkewHeap.new @event_list = SkewHeap.new
end end
public
# Add an event to the pending events list. # Add an event to the pending events list.
# #
# param: event - the name of the event to be scheduled. # *Arguments*::
# param: delay - the amount of time which should elapse before # - +event+ -> the event to be scheduled.
# the event executes. # - +delay+ -> the amount of time which should elapse before
# param: args - the list of arguments to pass to the event at # the event executes.
# invocation time. # - +args+ -> an optional list of arguments to pass to the event
# at invocation time.
def schedule(event, delay, *args) def schedule(event, delay, *args)
raise "Model scheduled event with negative delay." if (delay < 0) raise "Model scheduled event with negative delay." if (delay < 0)
@event_list.push EventNotice.new(event, @model_time + delay, *args) @event_list.push EventNotice.new(event, @model_time + delay, *args)
end 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 # 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 # the pending event with smallest time is extracted from the
# event list, the model_time is updated to the event time, # event list, the model_time is updated to the event time,
# and the event method is invoked. # and the event method is invoked.
...@@ -69,17 +71,18 @@ module SimpleKit ...@@ -69,17 +71,18 @@ module SimpleKit
end end
# Clears the event list, which causes termination of the simulation. # Clears the event list, which causes termination of the simulation.
# Never schedule any new events after invoking +halt+.
def halt def halt
@event_list.clear @event_list.clear
end end
end end
private
# This is a private helper class for the EventScheduler class. # This is a private helper class for the EventScheduler class.
# Users should never try to access anything in this class directly. # Users should never try to access anything in this class directly.
private
class EventNotice class EventNotice
include Comparable include Comparable
attr_reader :event, :time, :args attr_reader :event, :time, :args
def initialize(event, time, *args) def initialize(event, time, *args)
......
# -*- ruby -*- # -*- ruby -*-
_VERSION = "0.4.0" _VERSION = "0.4.2"
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "simplekit" s.name = "simplekit"
s.version = _VERSION s.version = _VERSION
s.date = "2013-01-14" s.date = "2013-04-11"
s.summary = "Discrete event simulation engine." s.summary = "Discrete event simulation engine."
s.homepage = "git://or.nps.edu/simplekit-ruby.git" s.homepage = "git://or.nps.edu/simplekit-ruby.git"
s.email = "pjs@alum.mit.edu" s.email = "pjs@alum.mit.edu"
...@@ -16,7 +16,7 @@ Gem::Specification.new do |s| ...@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
lgpl.txt lgpl.txt
lib/simplekit.rb 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.required_ruby_version = '>= 1.8.1'
s.license = 'LGPL' s.license = 'LGPL'
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment