class Newral::Graphs::Path

Attributes

edges[R]

Public Class Methods

new( edges:[], allow_circular_paths: true ) click to toggle source
# File lib/newral/graphs/path.rb, line 10
def initialize( edges:[], allow_circular_paths: true )
  @edges = edges.dup
  @allow_circular_paths = allow_circular_paths
end

Public Instance Methods

add_edge( edge ) click to toggle source
# File lib/newral/graphs/path.rb, line 15
def add_edge( edge )
  last_edge = @edges.last 
  raise Errors::CanOnlyConnectToLastEdge,[last_edge,edge] unless  @edges.empty? || last_edge.end_node == edge.start_node 
  raise Errors::CircularPath unless @allow_circular_paths && !@edges.index{|edge1| edge1.start_node == edge.end_node || edge1.end_node == edge.end_node }
  @edges << edge   
  self  
end
cost() click to toggle source
# File lib/newral/graphs/path.rb, line 27
def cost 
  @edges.inject(0){ |value,edge| value+edge.cost }
end
end_node() click to toggle source
# File lib/newral/graphs/path.rb, line 37
def end_node
  @edges.last.end_node
end
length() click to toggle source
# File lib/newral/graphs/path.rb, line 23
def length 
  @edges.length 
end
start_node() click to toggle source
# File lib/newral/graphs/path.rb, line 32
def start_node
  @edges.first.start_node
end
to_s() click to toggle source
# File lib/newral/graphs/path.rb, line 41
def to_s
  @edges.join(', ')
end