Class: Legion::Extensions::Agentic::Language::ConceptualBlending::Helpers::Blend

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb

Constant Summary

Constants included from Constants

Constants::BLEND_TYPES, Constants::COMPRESSION_PENALTY, Constants::DECAY_RATE, Constants::DEFAULT_STRENGTH, Constants::ELABORATION_BOOST, Constants::MAX_BLENDS, Constants::MAX_HISTORY, Constants::MAX_MAPPINGS, Constants::MAX_SPACES, Constants::QUALITY_LABELS, Constants::STALE_THRESHOLD, Constants::STRENGTH_CEILING, Constants::STRENGTH_FLOOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_space_ids:, generic_space:, blended_elements:, blend_type: :double_scope) ⇒ Blend

Returns a new instance of Blend.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 15

def initialize(input_space_ids:, generic_space:, blended_elements:, blend_type: :double_scope)
  @id               = ::SecureRandom.uuid
  @input_space_ids  = input_space_ids
  @generic_space    = generic_space
  @blended_elements = blended_elements
  @blend_type       = blend_type
  @strength         = DEFAULT_STRENGTH
  @use_count        = 0
  @created_at       = Time.now.utc
  @last_used_at     = Time.now.utc
end

Instance Attribute Details

#blend_typeObject (readonly)

Returns the value of attribute blend_type.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def blend_type
  @blend_type
end

#blended_elementsObject (readonly)

Returns the value of attribute blended_elements.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def blended_elements
  @blended_elements
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def created_at
  @created_at
end

#generic_spaceObject (readonly)

Returns the value of attribute generic_space.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def generic_space
  @generic_space
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def id
  @id
end

#input_space_idsObject (readonly)

Returns the value of attribute input_space_ids.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def input_space_ids
  @input_space_ids
end

#last_used_atObject (readonly)

Returns the value of attribute last_used_at.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def last_used_at
  @last_used_at
end

#strengthObject (readonly)

Returns the value of attribute strength.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def strength
  @strength
end

#use_countObject (readonly)

Returns the value of attribute use_count.



12
13
14
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 12

def use_count
  @use_count
end

Instance Method Details

#compress(removed_element:) ⇒ Object



40
41
42
43
44
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 40

def compress(removed_element:)
  @blended_elements[:merged_elements]&.delete(removed_element)
  @strength = (@strength - COMPRESSION_PENALTY).clamp(STRENGTH_FLOOR, STRENGTH_CEILING)
  self
end

#elaborate(emergent_property:) ⇒ Object



33
34
35
36
37
38
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 33

def elaborate(emergent_property:)
  @blended_elements[:emergent_properties] ||= []
  @blended_elements[:emergent_properties] << emergent_property
  @strength = (@strength + ELABORATION_BOOST).clamp(STRENGTH_FLOOR, STRENGTH_CEILING)
  self
end

#quality_labelObject



57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 57

def quality_label
  score = quality_score
  QUALITY_LABELS.each do |range, label|
    return label if range.cover?(score)
  end
  :trivial
end

#quality_scoreObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 46

def quality_score
  element_count   = Array(@blended_elements[:merged_elements]).size
  emergent_count  = Array(@blended_elements[:emergent_properties]).size
  use_factor      = [@use_count / 10.0, 1.0].min

  raw = ((element_count / 10.0) * 0.4) +
        ((emergent_count / 5.0) * 0.4) +
        (use_factor * 0.2)
  raw.clamp(STRENGTH_FLOOR, STRENGTH_CEILING)
end

#stale?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 65

def stale?
  (Time.now.utc - @last_used_at).to_i > STALE_THRESHOLD
end

#to_hObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 69

def to_h
  {
    id:               @id,
    input_space_ids:  @input_space_ids,
    generic_space:    @generic_space,
    blended_elements: @blended_elements,
    blend_type:       @blend_type,
    strength:         @strength,
    use_count:        @use_count,
    quality_score:    quality_score,
    quality_label:    quality_label,
    stale:            stale?,
    created_at:       @created_at,
    last_used_at:     @last_used_at
  }
end

#use!Object



27
28
29
30
31
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blend.rb', line 27

def use!
  @use_count    += 1
  @last_used_at  = Time.now.utc
  self
end