Class: Legion::Extensions::Agentic::Inference::Analogical::Helpers::AnalogyEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Inference::Analogical::Helpers::AnalogyEngine
show all
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb
Constant Summary
Constants included
from Constants
Constants::ANALOGY_STATES, Constants::DECAY_RATE, Constants::DEFAULT_STRENGTH, Constants::MAPPING_TYPES, Constants::MAX_ANALOGIES, Constants::MAX_DOMAINS, Constants::MAX_HISTORY, Constants::REINFORCEMENT_RATE, Constants::SIMILARITY_THRESHOLD, Constants::STATE_THRESHOLDS, Constants::STRENGTH_CEILING, Constants::STRENGTH_FLOOR, Constants::STRUCTURAL_WEIGHT, Constants::SURFACE_WEIGHT
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of AnalogyEngine.
14
15
16
17
18
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 14
def initialize
@analogies = {}
@domains = Set.new
@history = []
end
|
Instance Attribute Details
#history ⇒ Object
Returns the value of attribute history.
12
13
14
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 12
def history
@history
end
|
Instance Method Details
#apply_analogy(analogy_id:, source_element:) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 45
def apply_analogy(analogy_id:, source_element:)
analogy = @analogies[analogy_id]
return { found: false } unless analogy
target_element = analogy.mappings[source_element]
return { found: true, mapped: false } unless target_element
analogy.use!
record_history(:applied, analogy_id)
{
found: true,
mapped: true,
source_element: source_element,
target_element: target_element,
confidence: analogy.strength,
analogy_id: analogy_id
}
end
|
#by_domain(domain:) ⇒ Object
115
116
117
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 115
def by_domain(domain:)
find_analogies(domain: domain)
end
|
#by_type(type:) ⇒ Object
119
120
121
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 119
def by_type(type:)
@analogies.values.select { |analogy| analogy.mapping_type == type }
end
|
#create_analogy(source_domain:, target_domain:, mappings:, mapping_type:, strength: nil) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 20
def create_analogy(source_domain:, target_domain:, mappings:, mapping_type:, strength: nil)
evict_oldest_analogy if @analogies.size >= Constants::MAX_ANALOGIES
analogy = StructureMap.new(
source_domain: source_domain,
target_domain: target_domain,
mappings: mappings,
mapping_type: mapping_type,
strength: strength
)
@analogies[analogy.id] = analogy
register_domain(source_domain)
register_domain(target_domain)
record_history(:created, analogy.id)
analogy
end
|
#cross_domain_transfer(analogy_id:, source_knowledge:) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 73
def cross_domain_transfer(analogy_id:, source_knowledge:)
analogy = @analogies[analogy_id]
return { transferred: false, reason: :not_found } unless analogy
transferred = {}
source_knowledge.each do |key, value|
mapped_key = analogy.mappings.fetch(key, nil)
transferred[mapped_key] = value if mapped_key
end
analogy.use!
record_history(:transferred, analogy_id)
{
transferred: true,
analogy_id: analogy_id,
source_domain: analogy.source_domain,
target_domain: analogy.target_domain,
result: transferred,
coverage: coverage_ratio(source_knowledge, transferred)
}
end
|
#decay_all ⇒ Object
123
124
125
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 123
def decay_all
@analogies.each_value(&:decay)
end
|
#evaluate_similarity(source:, target:) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 65
def evaluate_similarity(source:, target:)
return 0.0 if source.empty? || target.empty?
structural = structural_overlap(source, target)
surface = surface_overlap(source, target)
(Constants::STRUCTURAL_WEIGHT * structural) + (Constants::SURFACE_WEIGHT * surface)
end
|
#find_analogies(domain:) ⇒ Object
39
40
41
42
43
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 39
def find_analogies(domain:)
@analogies.values.select do |analogy|
analogy.source_domain == domain || analogy.target_domain == domain
end
end
|
#productive_analogies ⇒ Object
111
112
113
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 111
def productive_analogies
@analogies.values.select(&:productive?)
end
|
#prune_stale ⇒ Object
127
128
129
130
131
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 127
def prune_stale
stale_ids = @analogies.select { |_id, analogy| analogy.state == :stale }.keys
stale_ids.each { |id| @analogies.delete(id) }
stale_ids.size
end
|
#reinforce_analogy(analogy_id:, success:) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 96
def reinforce_analogy(analogy_id:, success:)
analogy = @analogies[analogy_id]
return { reinforced: false, reason: :not_found } unless analogy
if success
analogy.reinforce
record_history(:reinforced, analogy_id)
else
analogy.weaken
record_history(:weakened, analogy_id)
end
{ reinforced: true, analogy_id: analogy_id, strength: analogy.strength, state: analogy.state }
end
|
#to_h ⇒ Object
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/analogy_engine.rb', line 133
def to_h
{
total_analogies: @analogies.size,
total_domains: @domains.size,
productive_count: productive_analogies.size,
history_count: @history.size,
domains: @domains.to_a,
analogy_states: state_counts
}
end
|