Class: Legion::Extensions::Agentic::Integration::SituationModel::Helpers::SituationModel

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

Constant Summary

Constants included from Constants

Constants::COHERENCE_CEILING, Constants::COHERENCE_FLOOR, Constants::CONTINUITY_LABELS, Constants::DECAY_RATE, Constants::DEFAULT_DIMENSION_VALUE, Constants::DIMENSIONS, Constants::MAX_EVENTS_PER_MODEL, Constants::MAX_HISTORY, Constants::MAX_MODELS, Constants::MODEL_HEALTH_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:) ⇒ SituationModel

Returns a new instance of SituationModel.



16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 16

def initialize(label:)
  @id             = SecureRandom.uuid
  @label          = label
  @events         = []
  @current_state  = DIMENSIONS.to_h { |dim| [dim, DEFAULT_DIMENSION_VALUE] }
  @created_at     = Time.now.utc
  @last_updated_at = Time.now.utc
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 14

def created_at
  @created_at
end

#current_stateObject (readonly)

Returns the value of attribute current_state.



14
15
16
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 14

def current_state
  @current_state
end

#eventsObject (readonly)

Returns the value of attribute events.



14
15
16
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 14

def events
  @events
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 14

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



14
15
16
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 14

def label
  @label
end

#last_updated_atObject (readonly)

Returns the value of attribute last_updated_at.



14
15
16
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 14

def last_updated_at
  @last_updated_at
end

Instance Method Details

#add_event(event) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 25

def add_event(event)
  previous = events.last
  events << event
  @current_state   = event.dimension_values.dup
  @last_updated_at = Time.now.utc
  previous ? event.continuity_with(previous) : 1.0
end

#coherenceObject



33
34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 33

def coherence
  return 1.0 if events.size <= 1

  pairs = events.each_cons(2).to_a
  total = pairs.sum { |a, b| b.continuity_with(a) }
  (total / pairs.size.to_f).clamp(COHERENCE_FLOOR, COHERENCE_CEILING)
end

#decay!Object



66
67
68
69
70
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 66

def decay!
  DIMENSIONS.each do |dim|
    current_state[dim] = (current_state[dim] - DECAY_RATE).clamp(COHERENCE_FLOOR, COHERENCE_CEILING)
  end
end

#dimension_trajectory(dimension) ⇒ Object



62
63
64
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 62

def dimension_trajectory(dimension)
  events.map { |e| e.dimension_values[dimension] }
end

#dominant_dimensionObject



46
47
48
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 46

def dominant_dimension
  current_state.max_by { |_, v| v }&.first
end

#event_boundaries(threshold: 0.3) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 54

def event_boundaries(threshold: 0.3)
  indices = []
  events.each_cons(2).with_index do |(a, b), idx|
    indices << (idx + 1) unless b.discontinuous_dimensions(a, threshold: threshold).empty?
  end
  indices
end

#health_labelObject



41
42
43
44
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 41

def health_label
  c = coherence
  MODEL_HEALTH_LABELS.find { |range, _| range.cover?(c) }&.last || :collapsed
end

#to_hObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 72

def to_h
  {
    id:              id,
    label:           label,
    event_count:     events.size,
    current_state:   current_state,
    coherence:       coherence,
    health_label:    health_label,
    created_at:      created_at.iso8601,
    last_updated_at: last_updated_at.iso8601
  }
end

#weakest_dimensionObject



50
51
52
# File 'lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb', line 50

def weakest_dimension
  current_state.min_by { |_, v| v }&.first
end