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.37 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.36 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.35 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.34 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.33 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.31 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.30 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.26 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.25 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.24 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.22 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.21 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.20 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.18 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.17 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.16 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.12 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.11 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.10 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.9 lib/y_petri/simulation/timed/recorder.rb