annotate scripts/topology/tree.rb @ 250:38154e695025

Refactor
author sugi
date Tue, 11 Jun 2013 16:58:35 +0900
parents 5c704b9a9a87
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 def create_nodes(node_num)
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 (0..node_num - 1).map { |i|
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 i = "node" + i.to_s
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 }
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 end
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 def print_dot(connections)
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 puts "digraph test {"
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 connections.each { |connection|
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 print "\t"
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 print connection[0]
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 print " -> "
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 print connection[1]
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 print ' [label="' + connection[2] + '"]'
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 puts
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 }
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 puts "}"
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 end
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 node_num = ARGV[0].to_i
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 nodes = create_nodes(node_num)
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 connections = Array.new
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 nodes.each_with_index { |node, i|
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 parent = (i - 1) / 2;
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 child1 = 2 * i + 1;
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 child2 = 2 * i + 2;
31
5c704b9a9a87 add ring script
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
27 if parent >= 0 then
29
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 connections << [nodes[i], nodes[parent], "parent"]
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 end
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 if child1 < node_num then
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 connections << [nodes[i], nodes[child1], "child1"]
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 end
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 if child2 < node_num then
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 connections << [nodes[i], nodes[child2], "child2"]
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 end
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 }
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 print_dot(connections)
414fcce36e90 add output dot file scripts
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38