Class: Legion::Extensions::Agentic::Inference::Analogical::Helpers::StructureMap

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.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

#initialize(source_domain:, target_domain:, mappings:, mapping_type:, strength: nil) ⇒ StructureMap

Returns a new instance of StructureMap.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 17

def initialize(source_domain:, target_domain:, mappings:, mapping_type:, strength: nil)
  @id            = SecureRandom.uuid
  @source_domain = source_domain
  @target_domain = target_domain
  @mappings      = mappings
  @mapping_type  = mapping_type
  @strength      = (strength || Constants::DEFAULT_STRENGTH).clamp(
    Constants::STRENGTH_FLOOR,
    Constants::STRENGTH_CEILING
  )
  @times_used    = 0
  @created_at    = Time.now.utc
  @last_used_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/inference/analogical/helpers/structure_map.rb', line 14

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 14

def id
  @id
end

#last_used_atObject (readonly)

Returns the value of attribute last_used_at.



14
15
16
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 14

def last_used_at
  @last_used_at
end

#mapping_typeObject (readonly)

Returns the value of attribute mapping_type.



14
15
16
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 14

def mapping_type
  @mapping_type
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



14
15
16
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 14

def mappings
  @mappings
end

#source_domainObject (readonly)

Returns the value of attribute source_domain.



14
15
16
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 14

def source_domain
  @source_domain
end

#strengthObject (readonly)

Returns the value of attribute strength.



14
15
16
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 14

def strength
  @strength
end

#target_domainObject (readonly)

Returns the value of attribute target_domain.



14
15
16
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 14

def target_domain
  @target_domain
end

#times_usedObject (readonly)

Returns the value of attribute times_used.



14
15
16
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 14

def times_used
  @times_used
end

Instance Method Details

#decayObject



68
69
70
71
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 68

def decay
  @strength = (@strength - Constants::DECAY_RATE).clamp(Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING)
  self
end

#productive?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 86

def productive?
  state == :productive
end

#reinforce(amount: Constants::REINFORCEMENT_RATE) ⇒ Object



58
59
60
61
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 58

def reinforce(amount: Constants::REINFORCEMENT_RATE)
  @strength = (@strength + amount).clamp(Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING)
  self
end

#similarity_scoreObject



46
47
48
49
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 46

def similarity_score
  (Constants::STRUCTURAL_WEIGHT * structural_score) +
    (Constants::SURFACE_WEIGHT * surface_score)
end

#stateObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 73

def state
  thresholds = Constants::STATE_THRESHOLDS
  if @strength >= thresholds[:productive]
    :productive
  elsif @strength >= thresholds[:validated]
    :validated
  elsif @strength >= thresholds[:candidate]
    :candidate
  else
    :stale
  end
end

#structural_scoreObject



32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 32

def structural_score
  return 0.0 if mappings.empty?

  relational_count = mappings.count { |_src, tgt| tgt.is_a?(Hash) && tgt[:type] == :relational }
  relational_count.to_f / mappings.size
end

#surface_scoreObject



39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 39

def surface_score
  return 0.0 if mappings.empty?

  attribute_count = mappings.count { |_src, tgt| !(tgt.is_a?(Hash) && tgt[:type] == :relational) }
  attribute_count.to_f / mappings.size
end

#to_hObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 90

def to_h
  {
    id:               @id,
    source_domain:    @source_domain,
    target_domain:    @target_domain,
    mappings:         @mappings,
    mapping_type:     @mapping_type,
    strength:         @strength,
    structural_score: structural_score,
    surface_score:    surface_score,
    similarity_score: similarity_score,
    state:            state,
    times_used:       @times_used,
    created_at:       @created_at,
    last_used_at:     @last_used_at
  }
end

#use!Object



51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 51

def use!
  @times_used   += 1
  @last_used_at  = Time.now.utc
  reinforce(amount: Constants::REINFORCEMENT_RATE * 0.5)
  self
end

#weaken(amount: Constants::REINFORCEMENT_RATE) ⇒ Object



63
64
65
66
# File 'lib/legion/extensions/agentic/inference/analogical/helpers/structure_map.rb', line 63

def weaken(amount: Constants::REINFORCEMENT_RATE)
  @strength = (@strength - amount).clamp(Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING)
  self
end