Sha256: c4576e1d50fe1d75dfa0296a59c5a5c79ca6003a44b0b52becb83feb0939ef7d

Contents?: true

Size: 1.73 KB

Versions: 22

Compression:

Stored size: 1.73 KB

Contents

module YPetri::Simulation::Timed
  # Timed aspect of the recorder.
  #
  class Recorder < YPetri::Simulation::Recorder
    TIME_DECIMAL_PLACES = 5

    attr_reader :next_time
    attr_accessor :sampling
    delegate :time,
             :default_sampling,
             to: :simulation

    # Apart from the vanilla version arguments, timed recorder takes +:sampling+
    # argument.
    # 
    def initialize sampling: default_sampling, next_time: time, **nn
      super
      @sampling = sampling
      @next_time = next_time
    end

    # Construct a new recording based on +features+.
    # 
    def new_recording
      features.new_dataset type: :timed
    end

    # Like +YPetri::Simulation::Recorder#reset+, but allowing for an additional
    # named argument +:next_time+ that sets the next sampling time, and
    # +:sampling:, resetting the sampling period.
    # 
    def reset! sampling: default_sampling, next_time: time, **nn
      super
      @sampling = sampling
      @next_time = next_time
    end

    # Hook to be called by simulators whenever the state changes (every time
    # that simulation +time+ is incremented).
    # 
    def alert
      t = time.round( 9 )
      t2 = next_time.round( 9 )
      if t >= t2 then # it's time to sample
        sample!
        begin
          @next_time += sampling
        rescue NoMethodError => err
          ( puts "Here go error #{err}"; Kernel::p @next_time; Kernel::p sampling )
        end
                                      
      end
    end

    private

    # Records the current state as a pair { sampling_time => system_state }.
    # 
    def sample!
      sampling_time = time.round( TIME_DECIMAL_PLACES )
      super sampling_time
    end
  end # class Recorder
end # YPetri::Simulation

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
y_petri-2.1.7 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.6 lib/y_petri/simulation/timed/recorder.rb