Class: Graph
- Inherits:
-
Object
- Object
- Graph
- Defined in:
- lib/rtl/graph.rb
Instance Attribute Summary collapse
-
#edges ⇒ Object
Returns the value of attribute edges.
-
#id ⇒ Object
Returns the value of attribute id.
-
#map ⇒ Object
Returns the value of attribute map.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
Class Method Summary collapse
- .rand_between(min, max) ⇒ Object
- .random(nbVertex, maxNbEdgesPerVertex = 2) ⇒ Object
- .random_pos(maxx = 800, maxy = 600) ⇒ Object
- .read_file(filename) ⇒ Object
Instance Method Summary collapse
- #each_edge(&block) ⇒ Object
- #each_vertex(&block) ⇒ Object
-
#initialize(id = nil, nodes = [], edges = []) ⇒ Graph
constructor
A new instance of Graph.
- #json_edge(edge) ⇒ Object
- #json_node(node) ⇒ Object
- #print_info ⇒ Object
- #shuffle(range = 0..800) ⇒ Object
- #write_file(filename) ⇒ Object
Constructor Details
#initialize(id = nil, nodes = [], edges = []) ⇒ Graph
Returns a new instance of Graph.
52 53 54 55 |
# File 'lib/rtl/graph.rb', line 52 def initialize id=nil,nodes=[],edges=[] @id=id @nodes,@edges=nodes,edges end |
Instance Attribute Details
#edges ⇒ Object
Returns the value of attribute edges.
50 51 52 |
# File 'lib/rtl/graph.rb', line 50 def edges @edges end |
#id ⇒ Object
Returns the value of attribute id.
50 51 52 |
# File 'lib/rtl/graph.rb', line 50 def id @id end |
#map ⇒ Object
Returns the value of attribute map.
50 51 52 |
# File 'lib/rtl/graph.rb', line 50 def map @map end |
#nodes ⇒ Object
Returns the value of attribute nodes.
50 51 52 |
# File 'lib/rtl/graph.rb', line 50 def nodes @nodes end |
Class Method Details
.rand_between(min, max) ⇒ Object
136 137 138 |
# File 'lib/rtl/graph.rb', line 136 def self.rand_between min,max (min..max).to_a.sample end |
.random(nbVertex, maxNbEdgesPerVertex = 2) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# 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) ⇒ Object
140 141 142 143 |
# 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 |
Instance Method Details
#each_edge(&block) ⇒ Object
151 152 153 154 155 |
# File 'lib/rtl/graph.rb', line 151 def each_edge &block @edges.each do |edge| yield edge end end |
#each_vertex(&block) ⇒ Object
145 146 147 148 149 |
# File 'lib/rtl/graph.rb', line 145 def each_vertex &block @nodes.each do |node| yield node end end |
#json_edge(edge) ⇒ Object
89 90 91 |
# File 'lib/rtl/graph.rb', line 89 def json_edge edge "{\"source\":\"#{edge.source.id}\",\"sink\":\"#{edge.sink.id}\"}," end |
#json_node(node) ⇒ Object
84 85 86 87 |
# 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 ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# 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) ⇒ Object
114 115 116 117 118 119 |
# 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) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# 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 |