Class: Legion::Extensions::Agentic::Integration::Labyrinth::Helpers::Labyrinth
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Integration::Labyrinth::Helpers::Labyrinth
- Defined in:
- lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb
Instance Attribute Summary collapse
-
#breadcrumbs ⇒ Object
readonly
Returns the value of attribute breadcrumbs.
-
#current_node_id ⇒ Object
readonly
Returns the value of attribute current_node_id.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#labyrinth_id ⇒ Object
readonly
Returns the value of attribute labyrinth_id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#solved ⇒ Object
Returns the value of attribute solved.
Instance Method Summary collapse
- #add_node(node) ⇒ Object
- #backtrack! ⇒ Object
- #complexity ⇒ Object
- #complexity_label ⇒ Object
- #current_node ⇒ Object
- #drop_breadcrumb ⇒ Object
- #follow_thread ⇒ Object
-
#initialize(labyrinth_id:, name:, domain: nil) ⇒ Labyrinth
constructor
A new instance of Labyrinth.
- #lost? ⇒ Boolean
- #move_to!(node_id) ⇒ Object
- #path_length ⇒ Object
- #solved? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(labyrinth_id:, name:, domain: nil) ⇒ Labyrinth
Returns a new instance of Labyrinth.
13 14 15 16 17 18 19 20 21 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 13 def initialize(labyrinth_id:, name:, domain: nil) @labyrinth_id = labyrinth_id @name = name @domain = domain @nodes = {} @breadcrumbs = [] @current_node_id = nil @solved = false end |
Instance Attribute Details
#breadcrumbs ⇒ Object (readonly)
Returns the value of attribute breadcrumbs.
10 11 12 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10 def @breadcrumbs end |
#current_node_id ⇒ Object (readonly)
Returns the value of attribute current_node_id.
10 11 12 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10 def current_node_id @current_node_id end |
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
10 11 12 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10 def domain @domain end |
#labyrinth_id ⇒ Object (readonly)
Returns the value of attribute labyrinth_id.
10 11 12 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10 def labyrinth_id @labyrinth_id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10 def name @name end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
10 11 12 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10 def nodes @nodes end |
#solved ⇒ Object
Returns the value of attribute solved.
11 12 13 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 11 def solved @solved end |
Instance Method Details
#add_node(node) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 23 def add_node(node) raise ArgumentError, "nodes must be a #{Node}" unless node.is_a?(Node) raise ArgumentError, "max nodes (#{Constants::MAX_NODES}) reached" if @nodes.size >= Constants::MAX_NODES @nodes[node.node_id] = node @current_node_id ||= node.node_id if node.node_type == :entrance node end |
#backtrack! ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 47 def backtrack! return nil if @breadcrumbs.empty? target_id = @breadcrumbs.pop @current_node_id = target_id current_node end |
#complexity ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 103 def complexity return 0.0 if @nodes.empty? dead_end_count = @nodes.values.count(&:dead_end?) junction_count = @nodes.values.count(&:junction?) minotaur_count = @nodes.values.count { |n| n.node_type == :minotaur_lair } raw = ((dead_end_count * 0.3) + (junction_count * 0.2) + (minotaur_count * 0.5)) / @nodes.size.to_f raw.clamp(0.0, 1.0).round(10) end |
#complexity_label ⇒ Object
114 115 116 117 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 114 def complexity_label score = complexity Constants::COMPLEXITY_LABELS.find { |range, _| range.cover?(score) }&.last || :labyrinthine end |
#current_node ⇒ Object
99 100 101 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 99 def current_node @nodes[@current_node_id] end |
#drop_breadcrumb ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 55 def return unless @current_node_id current = @nodes[@current_node_id] current.visited = true if current @breadcrumbs << @current_node_id if @breadcrumbs.last != @current_node_id end |
#follow_thread ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 63 def follow_thread return nil if current_node.nil? unvisited = current_node.connections.find do |conn_id| node = @nodes[conn_id] node && !node.visited end return nil unless unvisited move_to!(unvisited) end |
#lost? ⇒ Boolean
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 80 def lost? return false if @current_node_id.nil? return false if solved? current = current_node return true if current.nil? unvisited_exits = current.connections.any? do |conn_id| node = @nodes[conn_id] node && !node.visited end !unvisited_exits && !current.dead_end? && @breadcrumbs.empty? end |
#move_to!(node_id) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 32 def move_to!(node_id) node = @nodes.fetch(node_id) { raise ArgumentError, "node #{node_id.inspect} not found in labyrinth" } current = current_node unless current.nil? || current.connections.include?(node_id) raise ArgumentError, "cannot move to #{node_id.inspect}: not connected from current node #{@current_node_id.inspect}" end node.visited = true @current_node_id = node_id @solved = true if node.node_type == :exit node end |
#path_length ⇒ Object
95 96 97 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 95 def path_length @breadcrumbs.size end |
#solved? ⇒ Boolean
76 77 78 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 76 def solved? @solved end |
#to_h ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 119 def to_h { labyrinth_id: @labyrinth_id, name: @name, domain: @domain, node_count: @nodes.size, current_node_id: @current_node_id, path_length: path_length, solved: @solved, complexity: complexity, complexity_label: complexity_label } end |