Sha256: 9b8c0dbc6ca3deae72bb9cd1385dc20b654cf97e1655f6300419ba12abc135cb

Contents?: true

Size: 1.94 KB

Versions: 17

Compression:

Stored size: 1.94 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, **named_args )
      super
      @sampling, @next_time = sampling, next_time
    end

    # Construct a new recording based on +features+.
    # 
    def new_recording
      features.DataSet.new 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, **named_args
      super.tap{ @sampling, @next_time = sampling, next_time }
    end

    # 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

    # Steps the simulation back. This prototype version of the method simply
    # reconstructs a new simulation at a given time (1 simulation step by
    # default) before the current time.
    # 
    def back! by=simulation.step
      time = simulation.time - by
      simulation.recording.reconstruct( at: simulation.recording.floor( time ) )
        .tap { |sim| sim.run! upto: time }
    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

17 entries across 17 versions & 1 rubygems

Version Path
y_petri-2.4.9 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.4.8 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.4.6 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.4.4 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.4.3 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.4.2 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.4.0 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.12 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.11 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.10 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.9 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.8 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.6 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.5 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.4 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.3 lib/y_petri/simulation/timed/recorder.rb
y_petri-2.3.2 lib/y_petri/simulation/timed/recorder.rb