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

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.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 Method Summary collapse

Constructor Details

#initializeBlendingEngine

Returns a new instance of BlendingEngine.



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

def initialize
  @spaces  = {}
  @blends  = {}
end

Instance Method Details

#add_element_to_space(space_id:, name:, properties: {}) ⇒ Object



25
26
27
28
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 25

def add_element_to_space(space_id:, name:, properties: {})
  space = @spaces.fetch(space_id) { raise ArgumentError, "Space #{space_id} not found" }
  space.add_element(name: name, properties: properties)
end

#add_relation_to_space(space_id:, from:, to:, type:) ⇒ Object



30
31
32
33
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 30

def add_relation_to_space(space_id:, from:, to:, type:)
  space = @spaces.fetch(space_id) { raise ArgumentError, "Space #{space_id} not found" }
  space.add_relation(from: from, to: to, type: type)
end

#best_blends(limit: 5) ⇒ Object



80
81
82
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 80

def best_blends(limit: 5)
  @blends.values.sort_by { |b| -b.quality_score }.first(limit)
end

#blend(space_a_id:, space_b_id:, blend_type: :double_scope) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 35

def blend(space_a_id:, space_b_id:, blend_type: :double_scope)
  return nil unless BLEND_TYPES.include?(blend_type.to_sym)

  raise ArgumentError, "Max blends (#{MAX_BLENDS}) reached" if @blends.size >= MAX_BLENDS

  space_a = @spaces.fetch(space_a_id) { raise ArgumentError, "Space #{space_a_id} not found" }
  space_b = @spaces.fetch(space_b_id) { raise ArgumentError, "Space #{space_b_id} not found" }

  generic_space = extract_generic_space(space_a, space_b)
  merged        = merge_elements(space_a, space_b)
  emergent      = generate_emergent(space_a, space_b)

  blended_elements = {
    merged_elements:     merged,
    emergent_properties: emergent
  }

  b = Blend.new(
    input_space_ids:  [space_a_id, space_b_id],
    generic_space:    generic_space,
    blended_elements: blended_elements,
    blend_type:       blend_type
  )
  @blends[b.id] = b
  b
end

#blend_quality(blend_id:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 84

def blend_quality(blend_id:)
  blend = @blends.fetch(blend_id) { raise ArgumentError, "Blend #{blend_id} not found" }
  {
    blend_id:      blend_id,
    quality_score: blend.quality_score,
    quality_label: blend.quality_label,
    strength:      blend.strength,
    use_count:     blend.use_count,
    stale:         blend.stale?
  }
end

#compress_blend(blend_id:, removed_element:) ⇒ Object



67
68
69
70
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 67

def compress_blend(blend_id:, removed_element:)
  blend = @blends.fetch(blend_id) { raise ArgumentError, "Blend #{blend_id} not found" }
  blend.compress(removed_element: removed_element)
end

#create_space(name:, domain:) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 17

def create_space(name:, domain:)
  raise ArgumentError, "Max spaces (#{MAX_SPACES}) reached" if @spaces.size >= MAX_SPACES

  space = MentalSpace.new(name: name, domain: domain)
  @spaces[space.id] = space
  space
end

#decay_staleObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 96

def decay_stale
  count = 0
  @blends.each_value do |b|
    next unless b.stale?

    new_strength = (b.strength - DECAY_RATE).clamp(STRENGTH_FLOOR, STRENGTH_CEILING)
    b.instance_variable_set(:@strength, new_strength)
    count += 1
  end
  count
end

#elaborate_blend(blend_id:, emergent_property:) ⇒ Object



62
63
64
65
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 62

def elaborate_blend(blend_id:, emergent_property:)
  blend = @blends.fetch(blend_id) { raise ArgumentError, "Blend #{blend_id} not found" }
  blend.elaborate(emergent_property: emergent_property)
end

#find_blends(domain:) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 72

def find_blends(domain:)
  @blends.values.select do |b|
    b.input_space_ids.any? do |sid|
      @spaces[sid]&.domain == domain
    end
  end
end

#prune_weakObject



108
109
110
111
112
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 108

def prune_weak
  before = @blends.size
  @blends.delete_if { |_id, b| b.strength < 0.1 }
  before - @blends.size
end

#to_hObject



114
115
116
117
118
119
120
# File 'lib/legion/extensions/agentic/language/conceptual_blending/helpers/blending_engine.rb', line 114

def to_h
  {
    spaces_count: @spaces.size,
    blends_count: @blends.size,
    best_quality: @blends.values.map(&:quality_score).max || 0.0
  }
end