Class: Legion::Extensions::Agentic::Homeostasis::Rhythm::Helpers::Rhythm

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb

Constant Summary

Constants included from Constants

Constants::AMPLITUDE_LABELS, Constants::CIRCADIAN_PERIOD, Constants::COGNITIVE_DIMENSIONS, Constants::DEFAULT_PHASE_OFFSET, Constants::MAX_AMPLITUDE, Constants::MAX_HISTORY, Constants::MAX_RHYTHMS, Constants::MIN_AMPLITUDE, Constants::PHASE_LABELS, Constants::RHYTHM_TYPES, Constants::ULTRADIAN_PERIOD

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, rhythm_type:, dimension:, period:, amplitude: 0.5, phase_offset: 0.0) ⇒ Rhythm

Returns a new instance of Rhythm.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 15

def initialize(id:, name:, rhythm_type:, dimension:, period:,
               amplitude: 0.5, phase_offset: 0.0)
  @id           = id
  @name         = name
  @rhythm_type  = rhythm_type
  @dimension    = dimension
  @period       = period
  @amplitude    = amplitude.to_f.clamp(MIN_AMPLITUDE, MAX_AMPLITUDE)
  @phase_offset = phase_offset.to_f
  @created_at   = Time.now.utc
end

Instance Attribute Details

#amplitudeObject (readonly)

Returns the value of attribute amplitude.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 12

def amplitude
  @amplitude
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 12

def created_at
  @created_at
end

#dimensionObject (readonly)

Returns the value of attribute dimension.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 12

def dimension
  @dimension
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 12

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 12

def name
  @name
end

#periodObject (readonly)

Returns the value of attribute period.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 12

def period
  @period
end

#phase_offsetObject (readonly)

Returns the value of attribute phase_offset.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 12

def phase_offset
  @phase_offset
end

#rhythm_typeObject (readonly)

Returns the value of attribute rhythm_type.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 12

def rhythm_type
  @rhythm_type
end

Instance Method Details

#amplitude_labelObject



51
52
53
54
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 51

def amplitude_label
  AMPLITUDE_LABELS.each { |range, label| return label if range.cover?(@amplitude) }
  :low
end

#current_phaseObject



41
42
43
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 41

def current_phase
  phase_at(Time.now.utc)
end

#current_valueObject



32
33
34
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 32

def current_value
  value_at(Time.now.utc)
end

#falling?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 68

def falling?
  phase_label == :falling
end

#peak?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 56

def peak?
  phase_label == :peak
end

#phase_at(time) ⇒ Object



36
37
38
39
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 36

def phase_at(time)
  elapsed = time.to_f - @created_at.to_f + @phase_offset
  (elapsed % @period) / @period
end

#phase_labelObject



45
46
47
48
49
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 45

def phase_label
  phase = current_phase
  PHASE_LABELS.each { |range, label| return label if range.cover?(phase) }
  :trough
end

#rising?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 64

def rising?
  phase_label == :rising
end

#to_hObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 72

def to_h
  {
    id:              @id,
    name:            @name,
    rhythm_type:     @rhythm_type,
    dimension:       @dimension,
    period:          @period,
    amplitude:       @amplitude,
    phase_offset:    @phase_offset,
    current_value:   current_value.round(4),
    current_phase:   current_phase.round(4),
    phase_label:     phase_label,
    amplitude_label: amplitude_label
  }
end

#trough?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 60

def trough?
  phase_label == :trough
end

#value_at(time) ⇒ Object



27
28
29
30
# File 'lib/legion/extensions/agentic/homeostasis/rhythm/helpers/rhythm.rb', line 27

def value_at(time)
  elapsed = time.to_f - @created_at.to_f + @phase_offset
  @amplitude * (0.5 + (0.5 * Math.sin(2 * Math::PI * (elapsed / @period))))
end