Class: Legion::Extensions::Agentic::Integration::Map::Helpers::CognitiveMapStore

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCognitiveMapStore

Returns a new instance of CognitiveMapStore.



12
13
14
15
16
17
18
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 12

def initialize
  @contexts = {}
  @current_context = :default
  @path_cache = {}
  @visit_history = []
  ensure_context(@current_context)
end

Instance Attribute Details

#current_contextObject (readonly)

Returns the value of attribute current_context.



10
11
12
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 10

def current_context
  @current_context
end

Instance Method Details

#add_location(id:, domain: :general, properties: {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 20

def add_location(id:, domain: :general, properties: {})
  return false if locations.size >= Constants::MAX_LOCATIONS && !locations.key?(id)

  locations[id] ||= Location.new(id: id, domain: domain, properties: properties)
  true
end

#clustersObject



87
88
89
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 87

def clusters
  GraphTraversal.connected_components(locations)
end

#connect(from:, to:, distance: Constants::DEFAULT_DISTANCE, bidirectional: true) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 40

def connect(from:, to:, distance: Constants::DEFAULT_DISTANCE, bidirectional: true)
  return false unless locations.key?(from) && locations.key?(to)

  locations[from].add_neighbor(to, distance: distance)
  locations[to].add_neighbor(from, distance: distance) if bidirectional
  invalidate_path_cache
  true
end

#context_switch(context_id:) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 102

def context_switch(context_id:)
  return { switched: false, reason: :max_contexts } if over_context_limit?(context_id)

  ensure_context(context_id)
  @current_context = context_id
  invalidate_path_cache
  { switched: true, context: context_id, location_count: locations.size }
end

#decay_allObject



95
96
97
98
99
100
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 95

def decay_all
  faded_ids = collect_faded
  faded_ids.each { |id| remove_location(id) }
  invalidate_path_cache if faded_ids.any?
  { decayed: locations.size, pruned: faded_ids.size }
end

#disconnect(from:, to:) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 49

def disconnect(from:, to:)
  return false unless locations.key?(from)

  locations[from].remove_neighbor(to)
  invalidate_path_cache
  true
end

#location(id) ⇒ Object



27
28
29
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 27

def location(id)
  locations[id]
end

#location_countObject



122
123
124
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 122

def location_count
  locations.size
end

#most_familiar(limit: 10) ⇒ Object



91
92
93
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 91

def most_familiar(limit: 10)
  locations.values.sort_by { |l| -l.familiarity }.first(limit).map(&:to_h)
end

#neighbors_of(id:) ⇒ Object



74
75
76
77
78
79
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 74

def neighbors_of(id:)
  loc = locations[id]
  return [] unless loc

  loc.neighbors.map { |nid, dist| { id: nid, distance: dist, category: Constants.distance_category(dist) } }
end

#reachable_from(id:, max_distance: 3.0) ⇒ Object



81
82
83
84
85
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 81

def reachable_from(id:, max_distance: 3.0)
  return [] unless locations.key?(id)

  GraphTraversal.bfs_reachable(locations, id, max_distance)
end

#remove_location(id) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 31

def remove_location(id)
  loc = locations.delete(id)
  return false unless loc

  locations.each_value { |l| l.remove_neighbor(id) }
  invalidate_path_cache
  true
end

#shortest_path(from:, to:) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 66

def shortest_path(from:, to:)
  return { found: false, reason: :missing_start } unless locations.key?(from)
  return { found: false, reason: :missing_end } unless locations.key?(to)
  return { found: true, path: [from], distance: 0.0 } if from == to

  fetch_or_compute_path(from, to)
end

#to_hObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 111

def to_h
  {
    context:        @current_context,
    context_count:  @contexts.size,
    location_count: locations.size,
    edge_count:     total_edges,
    visit_history:  @visit_history.size,
    cached_paths:   @path_cache.size
  }
end

#visit(id:) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb', line 57

def visit(id:)
  loc = locations[id]
  return { found: false } unless loc

  loc.visit
  record_visit(id)
  { found: true, id: id, visit_count: loc.visit_count, familiarity: loc.familiarity.round(4) }
end