Sha256: 4d438147b461e524d8b58c056fb9e7196f37235aca875522a8462d4a7ca5b52c

Contents?: true

Size: 1.54 KB

Versions: 16

Compression:

Stored size: 1.54 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!
        @next_time += sampling
      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

16 entries across 16 versions & 1 rubygems

Version Path
y_petri-2.2.4 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.2.3 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.2.2 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.2.1 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.2.0 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.51 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.50 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.49 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.48 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.47 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.46 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.45 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.44 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.42 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.40 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.1.39 lib/y_petri/simulation/timed/recorder.rb