Class: Legion::Extensions::Agentic::Language::ConceptualMetaphor::Helpers::MetaphorEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb

Constant Summary

Constants included from Constants

Constants::CONVENTIONALITY_LABELS, Constants::CONVENTIONALITY_THRESHOLD, Constants::DECAY_RATE, Constants::DEFAULT_STRENGTH, Constants::MAX_DOMAINS, Constants::MAX_HISTORY, Constants::MAX_MAPPINGS, Constants::MAX_METAPHORS, Constants::METAPHOR_TYPES, Constants::NOVELTY_THRESHOLD, Constants::REINFORCEMENT_BOOST, Constants::STALE_THRESHOLD, Constants::STRENGTH_CEILING, Constants::STRENGTH_FLOOR, Constants::STRENGTH_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetaphorEngine

Returns a new instance of MetaphorEngine.



14
15
16
17
18
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 14

def initialize
  @metaphors = {}
  @domains   = Set.new
  @history   = []
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 12

def history
  @history
end

Instance Method Details

#add_entailment(metaphor_id:, entailment:) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 54

def add_entailment(metaphor_id:, entailment:)
  metaphor = @metaphors[metaphor_id]
  return { success: false, reason: :not_found } unless metaphor

  metaphor.add_entailment(entailment)
  record_history(:entailment_added, metaphor_id)
  { success: true, metaphor_id: metaphor_id, entailment_count: metaphor.entailments.size }
end

#apply_metaphor(metaphor_id:, source_concept:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 42

def apply_metaphor(metaphor_id:, source_concept:)
  metaphor = @metaphors[metaphor_id]
  return { found: false } unless metaphor

  target = metaphor.map_concept(source_concept)
  return { found: true, mapped: false } unless target

  metaphor.use!
  record_history(:applied, metaphor_id)
  build_apply_result(metaphor, source_concept, target)
end

#by_type(metaphor_type:) ⇒ Object



89
90
91
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 89

def by_type(metaphor_type:)
  @metaphors.values.select { |m| m.metaphor_type == metaphor_type }
end

#conventional_metaphorsObject



77
78
79
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 77

def conventional_metaphors
  @metaphors.values.select(&:conventional?)
end

#create_metaphor(source_domain:, target_domain:, metaphor_type:, mappings:, strength: nil, conventionality: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 20

def create_metaphor(source_domain:, target_domain:, metaphor_type:,
                    mappings:, strength: nil, conventionality: nil)
  evict_oldest if @metaphors.size >= MAX_METAPHORS

  return { success: false, reason: :invalid_metaphor_type } unless METAPHOR_TYPES.include?(metaphor_type)

  metaphor = Metaphor.new(
    source_domain:   source_domain,
    target_domain:   target_domain,
    metaphor_type:   metaphor_type,
    mappings:        mappings,
    strength:        strength,
    conventionality: conventionality
  )

  @metaphors[metaphor.id] = metaphor
  register_domain(source_domain)
  register_domain(target_domain)
  record_history(:created, metaphor.id)
  metaphor
end

#decay_allObject



93
94
95
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 93

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

#find_by_domain(domain:) ⇒ Object



71
72
73
74
75
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 71

def find_by_domain(domain:)
  @metaphors.values.select do |m|
    m.source_domain == domain || m.target_domain == domain
  end
end

#find_by_source(source_domain:) ⇒ Object



67
68
69
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 67

def find_by_source(source_domain:)
  @metaphors.values.select { |m| m.source_domain == source_domain }
end

#find_by_target(target_domain:) ⇒ Object



63
64
65
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 63

def find_by_target(target_domain:)
  @metaphors.values.select { |m| m.target_domain == target_domain }
end

#novel_metaphorsObject



81
82
83
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 81

def novel_metaphors
  @metaphors.values.select(&:novel?)
end

#prune_weakObject



97
98
99
100
101
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 97

def prune_weak
  weak_ids = @metaphors.select { |_id, m| m.strength <= 0.05 }.keys
  weak_ids.each { |id| @metaphors.delete(id) }
  weak_ids.size
end

#strongest(limit: 5) ⇒ Object



85
86
87
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 85

def strongest(limit: 5)
  @metaphors.values.sort_by { |m| -m.strength }.first(limit)
end

#to_hObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor_engine.rb', line 103

def to_h
  {
    total_metaphors:    @metaphors.size,
    total_domains:      @domains.size,
    conventional_count: conventional_metaphors.size,
    novel_count:        novel_metaphors.size,
    history_count:      @history.size,
    domains:            @domains.to_a,
    type_counts:        type_counts
  }
end