Module: Legion::Extensions::Agentic::Inference::Analogical::Runners::AnalogicalReasoning

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb

Instance Method Summary collapse

Instance Method Details

#analogical_reasoning_statsObject



92
93
94
95
96
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 92

def analogical_reasoning_stats(**)
  stats = engine.to_h
  log.debug "[analogical_reasoning] stats total=#{stats[:total_analogies]}"
  { success: true }.merge(stats)
end

#apply_analogy(analogy_id:, source_element:) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 41

def apply_analogy(analogy_id:, source_element:, **)
  result = engine.apply_analogy(analogy_id: analogy_id, source_element: source_element)

  log.debug "[analogical_reasoning] apply id=#{analogy_id[0..7]} " \
            "element=#{source_element} mapped=#{result[:mapped]}"

  { success: true }.merge(result)
end

#create_analogy(source_domain:, target_domain:, mappings:, mapping_type: :relational, strength: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 13

def create_analogy(source_domain:, target_domain:, mappings:, mapping_type: :relational, strength: nil, **)
  unless Helpers::Constants::MAPPING_TYPES.include?(mapping_type)
    return { success: false, error: :invalid_mapping_type,
             valid_types: Helpers::Constants::MAPPING_TYPES }
  end

  analogy = engine.create_analogy(
    source_domain: source_domain,
    target_domain: target_domain,
    mappings:      mappings,
    mapping_type:  mapping_type,
    strength:      strength
  )

  log.debug "[analogical_reasoning] created analogy id=#{analogy.id[0..7]} " \
            "#{source_domain}->#{target_domain} type=#{mapping_type}"

  { success: true, analogy_id: analogy.id, source_domain: source_domain,
    target_domain: target_domain, mapping_type: mapping_type,
    strength: analogy.strength, state: analogy.state }
end

#cross_domain_transfer(analogy_id:, source_knowledge:) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 61

def cross_domain_transfer(analogy_id:, source_knowledge:, **)
  result = engine.cross_domain_transfer(analogy_id: analogy_id, source_knowledge: source_knowledge)

  log.debug "[analogical_reasoning] transfer id=#{analogy_id[0..7]} " \
            "transferred=#{result[:transferred]} coverage=#{result[:coverage]&.round(2)}"

  { success: true }.merge(result)
end

#evaluate_similarity(source:, target:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 50

def evaluate_similarity(source:, target:, **)
  score = engine.evaluate_similarity(source: source, target: target)
  above_threshold = score >= Helpers::Constants::SIMILARITY_THRESHOLD

  log.debug "[analogical_reasoning] similarity=#{score.round(3)} " \
            "above_threshold=#{above_threshold}"

  { success: true, similarity_score: score, above_threshold: above_threshold,
    threshold: Helpers::Constants::SIMILARITY_THRESHOLD }
end

#find_analogies(domain:) ⇒ Object



35
36
37
38
39
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 35

def find_analogies(domain:, **)
  analogies = engine.find_analogies(domain: domain)
  log.debug "[analogical_reasoning] find domain=#{domain} count=#{analogies.size}"
  { success: true, domain: domain, analogies: analogies.map(&:to_h), count: analogies.size }
end

#productive_analogiesObject



79
80
81
82
83
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 79

def productive_analogies(**)
  analogies = engine.productive_analogies
  log.debug "[analogical_reasoning] productive count=#{analogies.size}"
  { success: true, analogies: analogies.map(&:to_h), count: analogies.size }
end

#reinforce_analogy(analogy_id:, success:) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 70

def reinforce_analogy(analogy_id:, success:, **)
  result = engine.reinforce_analogy(analogy_id: analogy_id, success: success)

  log.debug "[analogical_reasoning] reinforce id=#{analogy_id[0..7]} " \
            "success=#{success} new_state=#{result[:state]}"

  { success: true }.merge(result)
end

#update_analogical_reasoningObject



85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb', line 85

def update_analogical_reasoning(**)
  engine.decay_all
  pruned = engine.prune_stale
  log.debug "[analogical_reasoning] decay+prune pruned=#{pruned}"
  { success: true, pruned: pruned }
end