class Graph
Attributes
edges[RW]
id[RW]
map[RW]
nodes[RW]
Public Class Methods
new(id=nil,nodes=[],edges=[])
click to toggle source
# File lib/rtl/graph.rb, line 52 def initialize id=nil,nodes=[],edges=[] @id=id @nodes,@edges=nodes,edges end
rand_between(min,max)
click to toggle source
# File lib/rtl/graph.rb, line 136 def self.rand_between min,max (min..max).to_a.sample end
random(nbVertex,maxNbEdgesPerVertex=2)
click to toggle source
# File lib/rtl/graph.rb, line 93 def self.random(nbVertex,maxNbEdgesPerVertex=2) nodes=(1..nbVertex).map{|i| h={ "id" => "#{i}", "pos"=> [rand(1000),rand(1000)] } Node.new(h) } edges=[] nodes.each_with_index do |node,idx| nb_edges=rand(0..maxNbEdgesPerVertex) nb_edges.times do edge=Edge.new edge.source=node edge.sink=sink=nodes.sample (edges << edge) unless nodes.index(sink)==idx end end Graph.new("g",nodes,edges) end
random_pos(maxx=800,maxy=600)
click to toggle source
# File lib/rtl/graph.rb, line 140 def self.random_pos(maxx=800,maxy=600) x,y=maxx/2,maxy/2 [self.rand_between(-x,x),self.rand_between(-y,y)] end
read_file(filename)
click to toggle source
# File lib/rtl/graph.rb, line 57 def self.read_file filename Parser.new.parse filename end
Public Instance Methods
each_edge() { |edge| ... }
click to toggle source
# File lib/rtl/graph.rb, line 151 def each_edge &block @edges.each do |edge| yield edge end end
each_vertex() { |node| ... }
click to toggle source
# File lib/rtl/graph.rb, line 145 def each_vertex &block @nodes.each do |node| yield node end end
json_edge(edge)
click to toggle source
# File lib/rtl/graph.rb, line 89 def json_edge edge "{\"source\":\"#{edge.source.id}\",\"sink\":\"#{edge.sink.id}\"}," end
json_node(node)
click to toggle source
# File lib/rtl/graph.rb, line 84 def json_node node fixed=node.fixed.to_s "{\"id\":\"#{node.id}\",\"pos\":#{node.pos.to_s},\"speed\":#{node.speed.to_s},\"fixed\":#{fixed}}," end
print_info()
click to toggle source
# File lib/rtl/graph.rb, line 121 def print_info puts "graph info".center(40,"=") puts "#vertices".ljust(30,'.')+nodes.size.to_s puts "#edges".ljust(30,'.')+edges.size.to_s puts "nodes".center(40,'-') nodes.each do |node| puts "#{node.id.ljust(10)} #{node.pos.x} #{node.pos.y}" end puts "edges".center(40,'-') edges.each do |edge| puts "#{edge.source.id} --> #{edge.sink.id}" end end
shuffle(range=0..800)
click to toggle source
# File lib/rtl/graph.rb, line 114 def shuffle range=0..800 @nodes.each do |node| nx,ny=rand(range),rand(range) node.pos=Vector.new(nx,ny) end end
write_file(filename)
click to toggle source
# File lib/rtl/graph.rb, line 61 def write_file filename json=Code.new json << "{" json.indent=2 json << "\"id\" : \"#{id}\"," json << "\"nodes\" : [" json.indent=4 nodes.each{|node| json << json_node(node)} json.indent=2 json << "]," json << "\"edges\" : [" json.indent=4 edges.each{|edge| json << json_edge(edge)} json.indent=2 json << "]" json.indent=0 json << "}" code=json.finalize code.gsub!(/\,\s*\]/,']') json=Code.new(code) json.save_as filename end