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

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/inference/magnet/helpers/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ Field

Returns a new instance of Field.



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

def initialize(name:)
  @id          = SecureRandom.uuid
  @name        = name
  @pole_ids    = []
  @alignment   = 0.0
  @flux_density = 0.0
  @created_at  = Time.now.utc
end

Instance Attribute Details

#alignmentObject (readonly)

Returns the value of attribute alignment.



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

def alignment
  @alignment
end

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#flux_densityObject (readonly)

Returns the value of attribute flux_density.



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

def flux_density
  @flux_density
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pole_idsObject (readonly)

Returns the value of attribute pole_ids.



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

def pole_ids
  @pole_ids
end

Instance Method Details

#add_pole(pole_id) ⇒ Object



23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/field.rb', line 23

def add_pole(pole_id)
  return false if @pole_ids.include?(pole_id)

  @pole_ids << pole_id
  true
end

#alignment_labelObject



52
53
54
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/field.rb', line 52

def alignment_label
  Constants.label_for(Constants::ALIGNMENT_LABELS, @alignment)
end

#calculate_alignment!(poles) ⇒ Object



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

def calculate_alignment!(poles)
  field_poles = poles.values.select { |p| @pole_ids.include?(p.id) }
  return self if field_poles.empty?

  @flux_density = field_poles.sum(&:strength) / field_poles.size.to_f
  @alignment    = compute_alignment(field_poles).round(10)
  self
end

#chaotic?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/field.rb', line 48

def chaotic?
  @alignment < 0.2
end

#coherent?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/field.rb', line 44

def coherent?
  @alignment >= 0.6
end

#pole_countObject



56
57
58
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/field.rb', line 56

def pole_count
  @pole_ids.size
end

#remove_pole(pole_id) ⇒ Object



30
31
32
33
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/field.rb', line 30

def remove_pole(pole_id)
  removed = @pole_ids.delete(pole_id)
  !removed.nil?
end

#to_hObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/legion/extensions/agentic/inference/magnet/helpers/field.rb', line 60

def to_h
  {
    id:              @id,
    name:            @name,
    pole_ids:        @pole_ids.dup,
    alignment:       @alignment,
    flux_density:    @flux_density,
    pole_count:      pole_count,
    coherent:        coherent?,
    chaotic:         chaotic?,
    alignment_label: alignment_label,
    created_at:      @created_at
  }
end