Class: Legion::Extensions::Agentic::Inference::Magnet::Helpers::MagnetEngine

Inherits:
Object
  • Object
show all
Includes:
Logging::Helper
Defined in:
lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMagnetEngine

Returns a new instance of MagnetEngine.



14
15
16
17
18
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 14

def initialize
  @poles           = {}
  @fields          = {}
  @interaction_log = []
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



12
13
14
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 12

def fields
  @fields
end

#interaction_logObject (readonly)

Returns the value of attribute interaction_log.



12
13
14
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 12

def interaction_log
  @interaction_log
end

#polesObject (readonly)

Returns the value of attribute poles.



12
13
14
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 12

def poles
  @poles
end

Instance Method Details

#add_pole_to_field(field_id:, pole_id:) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 110

def add_pole_to_field(field_id:, pole_id:)
  field = @fields[field_id]
  return { error: :field_not_found } unless field
  return { error: :pole_not_found } unless @poles[pole_id]

  added = field.add_pole(pole_id)
  { added: added, field_id: field_id, pole_id: pole_id }
end

#create_field(name:) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 34

def create_field(name:)
  return { error: :capacity_exceeded, max: Constants::MAX_FIELDS } if at_field_capacity?

  field = Field.new(name: name)
  @fields[field.id] = field
  field
end

#create_pole(polarity:, content:, strength: 0.5, material_type: :iron, domain: :general) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 20

def create_pole(polarity:, content:, strength: 0.5, material_type: :iron, domain: :general)
  return { error: :capacity_exceeded, max: Constants::MAX_POLES } if at_pole_capacity?

  pole = Pole.new(
    polarity:      polarity,
    content:       content,
    strength:      strength,
    material_type: material_type,
    domain:        domain
  )
  @poles[pole.id] = pole
  pole
end

#demagnetize_all!(rate: Constants::DECAY_RATE) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 67

def demagnetize_all!(rate: Constants::DECAY_RATE)
  count = 0
  @poles.each_value do |pole|
    pole.demagnetize!(rate)
    count += 1
  end
  { demagnetized: count, rate: rate }
end

#field_reportObject



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

def field_report
  recalculate_all_fields
  coherent  = @fields.values.select(&:coherent?)
  chaotic   = @fields.values.select(&:chaotic?)
  saturated = @poles.values.select(&:saturated?)
  weak      = @poles.values.select(&:weak?)

  {
    total_poles:        @poles.size,
    total_fields:       @fields.size,
    coherent_fields:    coherent.size,
    chaotic_fields:     chaotic.size,
    saturated_poles:    saturated.size,
    weak_poles:         weak.size,
    total_interactions: @interaction_log.size,
    strongest:          strongest_poles(limit: 3).map(&:to_h),
    top_fields:         most_aligned_fields(limit: 3).map(&:to_h)
  }
end

#interact(pole_a_id, pole_b_id) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 50

def interact(pole_a_id, pole_b_id)
  pole_a = @poles[pole_a_id]
  pole_b = @poles[pole_b_id]
  return { error: :pole_a_not_found } unless pole_a
  return { error: :pole_b_not_found } unless pole_b
  return { error: :same_pole } if pole_a_id == pole_b_id

  force = compute_force(pole_a, pole_b)
  type  = determine_interaction_type(pole_a, pole_b)

  apply_interaction(pole_a, pole_b, type)

  event = build_interaction_event(pole_a, pole_b, type, force)
  @interaction_log << event
  event
end

#magnetize(pole_id, rate: Constants::ATTRACTION_RATE) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 42

def magnetize(pole_id, rate: Constants::ATTRACTION_RATE)
  pole = @poles[pole_id]
  return { error: :not_found } unless pole

  pole.magnetize!(rate)
  { magnetized: true, id: pole_id, strength: pole.strength }
end

#most_aligned_fields(limit: 5) ⇒ Object



76
77
78
79
80
81
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 76

def most_aligned_fields(limit: 5)
  recalculate_all_fields
  @fields.values
         .sort_by { |f| -f.alignment }
         .first(limit)
end

#remove_pole_from_field(field_id:, pole_id:) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 119

def remove_pole_from_field(field_id:, pole_id:)
  field = @fields[field_id]
  return { error: :field_not_found } unless field

  removed = field.remove_pole(pole_id)
  { removed: removed, field_id: field_id, pole_id: pole_id }
end

#strongest_poles(limit: 5) ⇒ Object



83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb', line 83

def strongest_poles(limit: 5)
  @poles.values
        .reject(&:weak?)
        .sort_by { |p| -p.strength }
        .first(limit)
end