Class: Legion::Extensions::Agentic::Affect::Appraisal::Helpers::AppraisalEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb

Constant Summary

Constants included from Constants

Constants::APPRAISAL_EMOTIONS, Constants::COPING_TYPES, Constants::DECAY_RATE, Constants::DEFAULT_INTENSITY, Constants::INTENSITY_CEILING, Constants::INTENSITY_FLOOR, Constants::MAX_APPRAISALS, Constants::MAX_COPING_STRATEGIES, Constants::MAX_HISTORY, Constants::PRIMARY_DIMENSIONS, Constants::REAPPRAISAL_DISCOUNT, Constants::SECONDARY_DIMENSIONS

Instance Method Summary collapse

Constructor Details

#initializeAppraisalEngine

Returns a new instance of AppraisalEngine.



12
13
14
15
16
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 12

def initialize
  @appraisals        = {}
  @coping_strategies = {}
  @history           = []
end

Instance Method Details

#add_coping_strategy(name:, coping_type:, effectiveness:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 45

def add_coping_strategy(name:, coping_type:, effectiveness:)
  return false unless COPING_TYPES.include?(coping_type)
  return false if @coping_strategies.size >= MAX_COPING_STRATEGIES

  @coping_strategies[name] = {
    name:          name,
    coping_type:   coping_type,
    effectiveness: effectiveness.to_f.clamp(INTENSITY_FLOOR, INTENSITY_CEILING)
  }
  true
end

#appraise(event:, primary:, secondary:, domain: nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 18

def appraise(event:, primary:, secondary:, domain: nil)
  record = Appraisal.new(event: event, primary: primary, secondary: secondary, domain: domain)
  prune_appraisals if @appraisals.size >= MAX_APPRAISALS
  @appraisals[record.id] = record
  archive(record)
  record
end

#by_domain(domain:) ⇒ Object



78
79
80
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 78

def by_domain(domain:)
  @appraisals.values.select { |rec| rec.domain == domain }
end

#by_emotion(emotion:) ⇒ Object



74
75
76
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 74

def by_emotion(emotion:)
  @appraisals.values.select { |rec| rec.emotional_outcome == emotion }
end

#decay_allObject



92
93
94
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 92

def decay_all
  @appraisals.each_value(&:decay!)
end

#emotional_patternObject



86
87
88
89
90
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 86

def emotional_pattern
  counts = Hash.new(0)
  recent_appraisals.each { |rec| counts[rec.emotional_outcome] += 1 }
  counts.sort_by { |_, cnt| -cnt }.to_h
end

#evaluate_coping(appraisal_id:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 57

def evaluate_coping(appraisal_id:)
  record = @appraisals[appraisal_id]
  return { effectiveness: 0.0, resolved: false } unless record
  return { effectiveness: 0.0, resolved: false } unless record.coping_strategy

  strategy    = @coping_strategies[record.coping_strategy]
  base        = strategy ? strategy[:effectiveness] : DEFAULT_INTENSITY
  resolved    = record.intensity < 0.3
  {
    appraisal_id:  appraisal_id,
    coping:        record.coping_strategy,
    effectiveness: base,
    intensity:     record.intensity,
    resolved:      resolved
  }
end

#reappraise(appraisal_id:, new_primary:, new_secondary:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 26

def reappraise(appraisal_id:, new_primary:, new_secondary:)
  record = @appraisals[appraisal_id]
  return nil unless record

  record.reappraise(new_primary: new_primary, new_secondary: new_secondary)
  archive(record)
  record
end

#select_coping(appraisal_id:, coping_type:) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 35

def select_coping(appraisal_id:, coping_type:)
  record = @appraisals[appraisal_id]
  return nil unless record

  strategy = find_best_strategy(coping_type)
  name = strategy ? strategy[:name] : coping_type.to_s
  record.assign_coping(name)
  record
end

#to_hObject



96
97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 96

def to_h
  {
    appraisals:        @appraisals.transform_values(&:to_h),
    coping_strategies: @coping_strategies,
    history_size:      @history.size
  }
end

#unresolvedObject



82
83
84
# File 'lib/legion/extensions/agentic/affect/appraisal/helpers/appraisal_engine.rb', line 82

def unresolved
  @appraisals.values.select { |rec| rec.coping_strategy.nil? }
end