ruby-graphviz 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -42,13 +42,12 @@ class GraphViz
42
42
  raise ColorException, "Bad alpha value"
43
43
  end
44
44
 
45
-
46
45
  @r = r
47
46
  @g = g
48
47
  @b = b
49
48
  @a = a
50
49
 
51
- @color = COLORS.index(rgba_string.downcase)
50
+ @color = COLORS.key(rgba_string.downcase)
52
51
 
53
52
  @h, @s, @v = rgb_to_hsv(@r, @g, @b)
54
53
  end
@@ -64,7 +63,7 @@ class GraphViz
64
63
 
65
64
  @r, @g, @b = hsv_to_rgb(@h, @s, @v)
66
65
 
67
- @color = COLORS.index(rgba_string.downcase);
66
+ @color = COLORS.key(rgba_string.downcase);
68
67
  end
69
68
 
70
69
  def name(c = nil)
@@ -175,23 +174,23 @@ class GraphViz
175
174
  q = _v * ( 1.0 - _s * f )
176
175
  t = _v * ( 1.0 - _s * ( 1 - f ) )
177
176
  case i
178
- when 0:
177
+ when 0
179
178
  r = _v
180
179
  g = t
181
180
  b = p
182
- when 1:
181
+ when 1
183
182
  r = q
184
183
  g = _v
185
184
  b = p
186
- when 2:
185
+ when 2
187
186
  r = p
188
187
  g = _v
189
188
  b = t
190
- when 3:
189
+ when 3
191
190
  r = p
192
191
  g = q
193
192
  b = _v
194
- when 4:
193
+ when 4
195
194
  r = t
196
195
  g = p
197
196
  b = _v
@@ -28,9 +28,9 @@ class GraphViz
28
28
  #
29
29
  # THIS METHOD IS DEPRECATED, PLEASE USE GraphViz::XML.graph.output
30
30
  #
31
- def output( *hOpt )
31
+ def output( *options )
32
32
  warn "GraphViz::XML.output is deprecated, use GraphViz::XML.graph.output"
33
- @graph.output( *hOpt )
33
+ @graph.output( *options )
34
34
  end
35
35
 
36
36
  private
@@ -39,58 +39,58 @@ class GraphViz
39
39
  # Create a graph from a XML file
40
40
  #
41
41
  # In:
42
- # * xFile : XML File
43
- # * *hOpt : Graph options:
42
+ # * xml_file : XML File
43
+ # * *options : Graph options:
44
44
  # * :text : show text nodes (default true)
45
45
  # * :attrs : show XML attributs (default true)
46
46
  #
47
- def initialize( xFile, *hOpt )
48
- @xNodeName = "00000"
49
- @bShowText = true
50
- @bShowAttrs = true
47
+ def initialize( xml_file, *options )
48
+ @node_name = "00000"
49
+ @show_text = true
50
+ @show_attributs = true
51
51
 
52
- if hOpt.nil? == false and hOpt[0].nil? == false
53
- hOpt[0].each do |xKey, xValue|
52
+ if options.nil? == false and options[0].nil? == false
53
+ options[0].each do |xKey, xValue|
54
54
  case xKey.to_s
55
55
  when "text"
56
- @bShowText = xValue
57
- hOpt[0].delete( xKey )
56
+ @show_text = xValue
57
+ options[0].delete( xKey )
58
58
  when "attrs"
59
- @bShowAttrs = xValue
60
- hOpt[0].delete( xKey )
59
+ @show_attributs = xValue
60
+ options[0].delete( xKey )
61
61
  end
62
62
  end
63
63
  end
64
64
 
65
- @oReXML = REXML::Document::new( File::new( xFile ) )
66
- @graph = GraphViz::new( "XML", *hOpt )
67
- _init( @oReXML.root() )
65
+ @rexml_document = REXML::Document::new( File::new( xml_file ) )
66
+ @graph = GraphViz::new( "XML", *options )
67
+ parse_xml_node( @rexml_document.root() )
68
68
  end
69
69
 
70
- def _init( oXMLNode ) #:nodoc:
71
- xLocalNodeName = @xNodeName.clone
72
- @xNodeName.succ!
70
+ def parse_xml_node( xml_node ) #:nodoc:
71
+ local_node_name = @node_name.clone
72
+ @node_name.succ!
73
73
 
74
- label = oXMLNode.name
75
- if oXMLNode.has_attributes? == true and @bShowAttrs == true
76
- label = "{ " + oXMLNode.name
74
+ label = xml_node.name
75
+ if xml_node.has_attributes? == true and @show_attributs == true
76
+ label = "{ " + xml_node.name
77
77
 
78
- oXMLNode.attributes.each do |xName, xValue|
78
+ xml_node.attributes.each do |xName, xValue|
79
79
  label << "| { #{xName} | #{xValue} } "
80
80
  end
81
81
 
82
82
  label << "}"
83
83
  end
84
- @graph.add_node( xLocalNodeName, "label" => label, "color" => "blue", "shape" => "record" )
84
+ @graph.add_node( local_node_name, "label" => label, "color" => "blue", "shape" => "record" )
85
85
 
86
86
  ## Act: Search and add Text nodes
87
- if oXMLNode.has_text? == true and @bShowText == true
88
- xTextNodeName = xLocalNodeName.clone
89
- xTextNodeName << "111"
87
+ if xml_node.has_text? == true and @show_text == true
88
+ text_node_name = local_node_name.clone
89
+ text_node_name << "111"
90
90
 
91
91
  xText = ""
92
92
  xSep = ""
93
- oXMLNode.texts().each do |l|
93
+ xml_node.texts().each do |l|
94
94
  x = l.value.chomp.strip
95
95
  if x.length > 0
96
96
  xText << xSep << x
@@ -99,20 +99,20 @@ class GraphViz
99
99
  end
100
100
 
101
101
  if xText.length > 0
102
- @graph.add_node( xTextNodeName, "label" => xText, "color" => "black", "shape" => "ellipse" )
103
- @graph.add_edge( xLocalNodeName, xTextNodeName )
102
+ @graph.add_node( text_node_name, "label" => xText, "color" => "black", "shape" => "ellipse" )
103
+ @graph.add_edge( local_node_name, text_node_name )
104
104
  end
105
105
  end
106
106
 
107
107
  ## Act: Search and add attributs
108
108
  ## TODO
109
109
 
110
- oXMLNode.each_element( ) do |oXMLChild|
111
- xChildNodeName = _init( oXMLChild )
112
- @graph.add_edge( xLocalNodeName, xChildNodeName )
110
+ xml_node.each_element( ) do |xml_child_node|
111
+ child_node_name = parse_xml_node( xml_child_node )
112
+ @graph.add_edge( local_node_name, child_node_name )
113
113
  end
114
114
 
115
- return( xLocalNodeName )
115
+ return( local_node_name )
116
116
  end
117
117
 
118
118
  end
@@ -7,9 +7,6 @@ class GraphVizTest < Test::Unit::TestCase
7
7
  @graph = GraphViz::new( :G )
8
8
  end
9
9
 
10
- # def teardown
11
- # end
12
-
13
10
  def test_graph
14
11
  assert(@graph, 'Create graph faild.')
15
12
 
@@ -43,4 +40,69 @@ class GraphVizTest < Test::Unit::TestCase
43
40
 
44
41
  assert_equal( "\"Hello\\n\\\"world\\\"\\l\"", n['label'].to_s, 'Get attribut for node faild.' )
45
42
  end
43
+
44
+ def test_search
45
+ g = GraphViz::new( "G" )
46
+ g.node["shape"] = "ellipse"
47
+ g.node["color"] = "black"
48
+
49
+ g["color"] = "black"
50
+
51
+ c0 = g.add_graph( "cluster0" )
52
+ c0["label"] = "process #1"
53
+ c0["style"] = "filled"
54
+ c0["color"] = "lightgrey"
55
+ a0 = c0.add_node( "a0", "style" => "filled", "color" => "white" )
56
+ a1 = c0.add_node( "a1", "style" => "filled", "color" => "white" )
57
+ a2 = c0.add_node( "a2", "style" => "filled", "color" => "white" )
58
+ a3 = c0.add_node( "a3", "style" => "filled", "color" => "white" )
59
+ c0.add_edge( a0, a1 )
60
+ c0.add_edge( a1, a2 )
61
+ c0.add_edge( a2, a3 )
62
+
63
+ c1 = g.add_graph( "cluster1", "label" => "process #2" )
64
+ b0 = c1.add_node( "b0", "style" => "filled", "color" => "blue" )
65
+ b1 = c1.add_node( "b1", "style" => "filled", "color" => "blue" )
66
+ b2 = c1.add_node( "b2", "style" => "filled", "color" => "blue" )
67
+ b3 = c1.add_node( "b3", "style" => "filled", "color" => "blue" )
68
+ c1.add_edge( b0, b1 )
69
+ c1.add_edge( b1, b2 )
70
+ c1.add_edge( b2, b3 )
71
+
72
+ start = g.add_node( "start", "shape" => "Mdiamond" )
73
+ endn = g.add_node( "end", "shape" => "Msquare" )
74
+
75
+ g.add_edge( start, a0 )
76
+ g.add_edge( start, b0 )
77
+ g.add_edge( a1, b3 )
78
+ g.add_edge( b2, a3 )
79
+ g.add_edge( a3, a0 )
80
+ g.add_edge( a3, endn )
81
+
82
+ assert g
83
+
84
+ assert_equal g.get_node("start"), start
85
+ assert_equal g.find_node("start"), start
86
+ assert_equal g.search_node("start"), start
87
+
88
+ assert_nil g.get_node("a0")
89
+ assert_equal g.find_node("a0"), a0
90
+ assert_equal g.search_node("a0"), a0
91
+
92
+ assert_nil c0.get_node("start")
93
+ assert_equal c0.find_node("start"), start
94
+ assert_nil c0.search_node("start")
95
+
96
+ assert_equal c0.get_node("a0"), a0
97
+ assert_equal c0.find_node("a0"), a0
98
+ assert_equal c0.search_node("a0"), a0
99
+
100
+ assert_nil c1.get_node("start")
101
+ assert_equal c1.find_node("start"), start
102
+ assert_nil c1.search_node("start")
103
+
104
+ assert_nil c1.get_node("a0")
105
+ assert_equal c1.find_node("a0"), a0
106
+ assert_nil c1.search_node("a0")
107
+ end
46
108
  end
@@ -0,0 +1,102 @@
1
+ require 'test/unit'
2
+ $:.unshift(File.expand_path('../../lib',__FILE__))
3
+ require 'graphviz'
4
+ require 'graphviz/theory'
5
+ require 'graphviz/math/matrix'
6
+
7
+ class GraphVizTheoryTest < Test::Unit::TestCase
8
+ def setup
9
+ @g = GraphViz.digraph( "G", :path => "/usr/local/bin" ) do |g|
10
+ g.a[:label => "1"]
11
+ g.b[:label => "2"]
12
+ g.c[:label => "3"]
13
+ g.d[:label => "4"]
14
+ g.e[:label => "5"]
15
+ g.f[:label => "6"]
16
+
17
+ g.a << g.b
18
+ g.a << g.d
19
+ (g.a << g.f)[:weight => 6, :label => "6"]
20
+ g.b << g.c
21
+ g.b << g.d
22
+ g.b << g.e
23
+ g.c << g.d
24
+ (g.c << g.f)[:weight => 2, :label => "2"]
25
+ g.d << g.e
26
+ end
27
+
28
+ @t = GraphViz::Theory.new( @g )
29
+ end
30
+
31
+ def test_theory
32
+ assert @g, "Create graph failed!"
33
+ assert @t, "Theory failed!"
34
+ end
35
+
36
+ def test_adgency_matrix
37
+ adgency = GraphViz::Math::Matrix.new([
38
+ [0,1,0,1,0,1],
39
+ [0,0,1,1,1,0],
40
+ [0,0,0,1,0,1],
41
+ [0,0,0,0,1,0],
42
+ [0,0,0,0,0,0],
43
+ [0,0,0,0,0,0]
44
+ ])
45
+ assert_equal @t.adjancy_matrix, adgency, "Wrong adgency matrix"
46
+ end
47
+
48
+ def test_symetric
49
+ assert_equal false, @t.symmetric?
50
+ end
51
+
52
+ def test_incidence_matrix
53
+ incidence = GraphViz::Math::Matrix.new([
54
+ [ 1, 1, 1, 0, 0, 0, 0, 0, 0],
55
+ [-1, 0, 0, 1, 1, 1, 0, 0, 0],
56
+ [ 0, 0, 0,-1, 0, 0, 1, 1, 0],
57
+ [ 0,-1, 0, 0,-1, 0,-1, 0, 1],
58
+ [ 0, 0, 0, 0, 0,-1, 0, 0,-1],
59
+ [ 0, 0,-1, 0, 0, 0, 0,-1, 0]
60
+ ])
61
+ assert_equal @t.incidence_matrix, incidence
62
+ end
63
+
64
+ def test_degree
65
+ assert_equal 3, @t.degree(@g.get_node("a"))
66
+ assert_equal 4, @t.degree(@g.get_node("b"))
67
+ assert_equal 3, @t.degree(@g.get_node("c"))
68
+ assert_equal 4, @t.degree(@g.get_node("d"))
69
+ assert_equal 2, @t.degree(@g.get_node("e"))
70
+ assert_equal 2, @t.degree(@g.get_node("f"))
71
+ end
72
+
73
+ def test_laplacian_matrix
74
+ laplacian = GraphViz::Math::Matrix.new([
75
+ [3,-1, 0,-1, 0,-1],
76
+ [0, 4,-1,-1,-1, 0],
77
+ [0, 0, 3,-1, 0,-1],
78
+ [0, 0, 0, 4,-1, 0],
79
+ [0, 0, 0, 0, 2, 0],
80
+ [0, 0, 0, 0, 0, 2]
81
+ ])
82
+ assert_equal @t.laplacian_matrix, laplacian
83
+ end
84
+
85
+ def test_dijkstra_a_f
86
+ r = @t.moore_dijkstra(@g.a, @g.f)
87
+ assert r
88
+ assert_equal ["a", "b", "c", "f"], r[:path]
89
+ assert_equal 4.0, r[:distance]
90
+ end
91
+
92
+ def test_range
93
+ assert_equal [0, 1, 2, 3, 4, 3], @t.range
94
+ end
95
+
96
+ def test_critical_path
97
+ r = @t.critical_path
98
+ assert r
99
+ assert_equal [1, 6], r[:path]
100
+ assert_equal 6.0, r[:distance]
101
+ end
102
+ end
@@ -3,55 +3,52 @@ $:.unshift(File.expand_path('../../lib',__FILE__))
3
3
  require 'graphviz/utils/colors'
4
4
 
5
5
  class TypesTest < Test::Unit::TestCase
6
- def test_color_by_name
7
- brown = GraphViz::Utils::Colors.name("brown")
8
- assert brown
9
-
10
- assert_equal "brown", brown.name
11
-
12
- assert_equal "a5", brown.r
13
- assert_equal "2a", brown.g
14
- assert_equal "2a", brown.b
15
- assert_equal "#a52a2a", brown.rgba_string("#")
16
-
17
- assert_equal 0.0.to_s, brown.h.to_s
18
- assert_equal 0.745454545454545.to_s, brown.s.to_s
19
- assert_equal 0.647058823529412.to_s, brown.v.to_s
20
-
21
- assert_equal "0.0, 0.745454545454545, 0.647058823529412", brown.hsv_string
6
+ def setup
7
+ @brown_txt = GraphViz::Utils::Colors.name("brown")
8
+ @brown_hsv = GraphViz::Utils::Colors.hsv(0.0, 0.745454545454545, 0.647058823529412)
9
+ @brown_rgb = GraphViz::Utils::Colors.rgb("a5", "2a", "2a")
22
10
  end
23
11
 
24
- def test_color_by_hsv
25
- brown = GraphViz::Utils::Colors.hsv(0.0, 0.745454545454545, 0.647058823529412)
26
- assert brown
27
-
28
- assert_equal "brown", brown.name
29
-
30
- assert_equal "a5", brown.r
31
- assert_equal "2a", brown.g
32
- assert_equal "2a", brown.b
33
- assert_equal "#a52a2a", brown.rgba_string("#")
34
-
35
- assert_equal 0.0.to_s, brown.h.to_s
36
- assert_equal 0.745454545454545.to_s, brown.s.to_s
37
- assert_equal 0.647058823529412.to_s, brown.v.to_s
38
- assert_equal "0.0, 0.745454545454545, 0.647058823529412", brown.hsv_string
12
+ def test_color
13
+ assert @brown_txt
14
+ assert @brown_hsv
15
+ assert @brown_rgb
16
+ end
17
+
18
+ def test_color_by_name
19
+ assert_equal "brown", @brown_txt.name
20
+ assert_equal "brown", @brown_hsv.name
21
+ assert_equal "brown", @brown_rgb.name
39
22
  end
40
23
 
41
24
  def test_color_by_rgb
42
- brown = GraphViz::Utils::Colors.rgb("a5", "2a", "2a")
43
- assert brown
44
-
45
- assert_equal "brown", brown.name
46
-
47
- assert_equal "a5", brown.r
48
- assert_equal "2a", brown.g
49
- assert_equal "2a", brown.b
50
- assert_equal "#a52a2a", brown.rgba_string("#")
51
-
52
- assert_equal 0.0.to_s, brown.h.to_s
53
- assert_equal 0.745454545454545.to_s, brown.s.to_s
54
- assert_equal 0.647058823529412.to_s, brown.v.to_s
55
- assert_equal "0.0, 0.745454545454545, 0.647058823529412", brown.hsv_string
25
+ assert_equal "a5", @brown_txt.r
26
+ assert_equal "2a", @brown_txt.g
27
+ assert_equal "2a", @brown_txt.b
28
+ assert_equal "#a52a2a", @brown_txt.rgba_string("#")
29
+
30
+ assert_equal "a5", @brown_hsv.r
31
+ assert_equal "2a", @brown_hsv.g
32
+ assert_equal "2a", @brown_hsv.b
33
+ assert_equal "#a52a2a", @brown_hsv.rgba_string("#")
34
+
35
+ assert_equal "a5", @brown_rgb.r
36
+ assert_equal "2a", @brown_rgb.g
37
+ assert_equal "2a", @brown_rgb.b
38
+ assert_equal "#a52a2a", @brown_rgb.rgba_string("#")
39
+ end
40
+
41
+ def test_color_by_hsv
42
+ assert_equal @brown_rgb.h, @brown_txt.h
43
+ assert_equal @brown_rgb.s, @brown_txt.s
44
+ assert_equal @brown_rgb.v, @brown_txt.v
45
+
46
+ assert_equal @brown_rgb.hsv_string, @brown_txt.hsv_string
47
+
48
+ assert_equal 0.0.to_s, @brown_hsv.h.to_s
49
+ assert_equal 0.745454545454545.to_s, @brown_hsv.s.to_s
50
+ assert_equal 0.647058823529412.to_s, @brown_hsv.v.to_s
51
+
52
+ assert_equal "0.0, 0.745454545454545, 0.647058823529412", @brown_hsv.hsv_string
56
53
  end
57
54
  end