ruby-graphviz 0.9.11 → 0.9.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/AUTHORS +1 -0
  2. data/README.rdoc +49 -7
  3. data/bin/ruby2gv +2 -2
  4. data/examples/sample07.rb +2 -2
  5. data/examples/sample08.rb +7 -7
  6. data/examples/sample30.rb +1 -1
  7. data/examples/sample39.rb +11 -0
  8. data/examples/sample40.rb +13 -42
  9. data/examples/sample41.rb +8 -0
  10. data/examples/{p2p.rb → sample42.rb} +1 -1
  11. data/examples/{HTML-Labels.rb → sample43.rb} +2 -2
  12. data/examples/{arrowhead.rb → sample44.rb} +0 -0
  13. data/examples/{shapes.rb → sample45.rb} +1 -1
  14. data/examples/{testorder.rb → sample46.rb} +0 -0
  15. data/examples/{testxml.rb → sample47.rb} +1 -1
  16. data/examples/{graphviz.org/cluster.rb → sample48.rb} +1 -1
  17. data/examples/{graphviz.org/hello_world.rb → sample49.rb} +1 -1
  18. data/examples/{graphviz.org/lion_share.rb → sample50.rb} +1 -1
  19. data/examples/{graphviz.org/process.rb → sample51.rb} +1 -1
  20. data/examples/{graphviz.org/TrafficLights.rb → sample52.rb} +1 -1
  21. data/examples/sample53.rb +26 -0
  22. data/examples/sample54.rb +26 -0
  23. data/examples/sample99.rb +70 -0
  24. data/lib/graphviz.rb +31 -8
  25. data/lib/graphviz/attrs.rb +1 -1
  26. data/lib/graphviz/constants.rb +2 -2
  27. data/lib/graphviz/edge.rb +44 -20
  28. data/lib/graphviz/family_tree.rb +56 -5
  29. data/lib/graphviz/family_tree/couple.rb +16 -6
  30. data/lib/graphviz/family_tree/generation.rb +19 -5
  31. data/lib/graphviz/family_tree/person.rb +39 -13
  32. data/lib/graphviz/family_tree/sibling.rb +13 -0
  33. data/lib/graphviz/node.rb +11 -3
  34. data/lib/graphviz/nothugly.rb +42 -0
  35. data/lib/graphviz/nothugly/nothugly.xsl +321 -0
  36. data/lib/graphviz/parser.rb +1 -1
  37. data/lib/graphviz/xml.rb +1 -1
  38. data/test/test_examples.rb +4 -0
  39. metadata +44 -58
  40. data/examples/maketest.bat +0 -109
  41. data/examples/maketest.sh +0 -109
  42. data/examples/sample30.rb.ps +0 -923
  43. data/examples/sample37.rb.dot +0 -50
  44. data/examples/sample37.rb.png +0 -0
  45. data/test/output/sample01.rb.png +0 -0
  46. data/test/output/sample02.rb.png +0 -0
  47. data/test/output/sample03.rb.png +0 -0
  48. data/test/output/sample04.rb.png +0 -0
  49. data/test/output/sample05.rb.png +0 -0
  50. data/test/output/sample06.rb.png +0 -0
  51. data/test/output/sample07.rb.png +0 -0
  52. data/test/output/sample08.rb.png +0 -0
  53. data/test/output/sample09.rb.png +0 -0
  54. data/test/output/sample10.rb.png +0 -0
  55. data/test/output/sample11.rb.png +0 -0
  56. data/test/output/sample12.rb.png +0 -0
  57. data/test/output/sample13.rb.png +0 -0
  58. data/test/output/sample14.rb.png +0 -0
  59. data/test/output/sample15.rb.png +0 -0
  60. data/test/output/sample16.rb.png +0 -0
  61. data/test/output/sample17.rb.png +0 -0
  62. data/test/output/sample18.rb.png +0 -0
  63. data/test/output/sample19.rb.png +0 -0
  64. data/test/output/sample20.rb.png +0 -0
  65. data/test/output/sample21.rb.html +0 -3
  66. data/test/output/sample21.rb.png +0 -0
  67. data/test/output/sample22.rb.html +0 -5
  68. data/test/output/sample22.rb.png +0 -0
  69. data/test/output/sample23.rb.png +0 -0
  70. data/test/output/sample24.rb.png +0 -0
  71. data/test/output/sample25.rb.png +0 -0
  72. data/test/output/sample26.rb.png +0 -0
  73. data/test/output/sample28.rb.png +0 -0
  74. data/test/output/sample29.rb.svg +0 -21
  75. data/test/output/sample30.rb.ps +0 -268
  76. data/test/output/sample31.rb.png +0 -0
  77. data/test/output/sample32.rb.png +0 -0
  78. data/test/output/sample37.rb.dot +0 -50
  79. data/test/output/sample37.rb.png +0 -0
  80. data/test/output/sample38.rb.png +0 -0
  81. data/test/output/sample40.rb.png +0 -0
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Gregoire Lejeune <gregoire.lejeune@free.fr>
1
+ # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Gregoire Lejeune <gregoire.lejeune@free.fr>
2
2
  #
3
3
  # This program is free software; you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Gregoire Lejeune <gregoire.lejeune@free.fr>
1
+ # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Gregoire Lejeune <gregoire.lejeune@free.fr>
2
2
  #
3
3
  # This program is free software; you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -40,7 +40,7 @@
40
40
  # C => cluster
41
41
  #
42
42
  module Constants
43
- RGV_VERSION = "0.9.11"
43
+ RGV_VERSION = "0.9.12"
44
44
 
45
45
  ## Const: Output formats
46
46
  FORMATS = [
data/lib/graphviz/edge.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Gregoire Lejeune <gregoire.lejeune@free.fr>
1
+ # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Gregoire Lejeune <gregoire.lejeune@free.fr>
2
2
  #
3
3
  # This program is free software; you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -21,7 +21,9 @@ class GraphViz
21
21
  class Edge
22
22
  include Constants
23
23
  @xNodeOne
24
+ @xNodeOnePort
24
25
  @xNodeTwo
26
+ @xNodeTwoPort
25
27
  @oAttrEdge
26
28
  @oGParrent
27
29
 
@@ -29,22 +31,23 @@ class GraphViz
29
31
  # Create a new edge
30
32
  #
31
33
  # In:
32
- # * vNodeOne : First node
33
- # * vNodeTwo : Second node
34
+ # * vNodeOne : First node (can be a GraphViz::Node or a node ID)
35
+ # * vNodeTwo : Second node (can be a GraphViz::Node or a node ID)
34
36
  # * oGParrent : Graph
35
37
  #
36
38
  def initialize( vNodeOne, vNodeTwo, oGParrent = nil )
37
- if vNodeOne.class == String
38
- @xNodeOne = vNodeOne
39
- else
40
- @xNodeOne = vNodeOne.name
41
- end
42
-
43
- if vNodeTwo.class == String
44
- @xNodeTwo = vNodeTwo
45
- else
46
- @xNodeTwo = vNodeTwo.name
47
- end
39
+ @xNodeOne, @xNodeOnePort = getNodeNameAndPort( vNodeOne )
40
+ # if vNodeOne.class == String
41
+ # @xNodeOne = vNodeOne
42
+ # else
43
+ # @xNodeOne = vNodeOne.id
44
+ # end
45
+ @xNodeTwo, @xNodeTwoPort = getNodeNameAndPort( vNodeTwo )
46
+ # if vNodeTwo.class == String
47
+ # @xNodeTwo = vNodeTwo
48
+ # else
49
+ # @xNodeTwo = vNodeTwo.id
50
+ # end
48
51
 
49
52
  @oGParrent = oGParrent
50
53
 
@@ -112,13 +115,17 @@ class GraphViz
112
115
  xLink = " -- "
113
116
  end
114
117
 
115
- #xNodeNameOne = @xNodeOne.clone
116
- #xNodeNameOne = '"' << xNodeNameOne << '"' if xNodeNameOne.match( /^[a-zA-Z_]+[a-zA-Z0-9_\.]*$/ ).nil?
117
- xNodeNameOne = GraphViz.escape(@xNodeOne)
118
+ xNodeNameOne = if @xNodeOnePort.nil?
119
+ GraphViz.escape(@xNodeOne)
120
+ else
121
+ GraphViz.escape(@xNodeOne, true) + ":#{@xNodeOnePort}"
122
+ end
118
123
 
119
- #xNodeNameTwo = @xNodeTwo.clone
120
- #xNodeNameTwo = '"' << xNodeNameTwo << '"' if xNodeNameTwo.match( /^[a-zA-Z_]+[a-zA-Z0-9_\.]*$/ ).nil?
121
- xNodeNameTwo = GraphViz.escape(@xNodeTwo)
124
+ xNodeNameTwo = if @xNodeTwoPort.nil?
125
+ GraphViz.escape(@xNodeTwo)
126
+ else
127
+ GraphViz.escape(@xNodeTwo, true) + ":#{@xNodeTwoPort}"
128
+ end
122
129
 
123
130
  xOut = xNodeNameOne + xLink + xNodeNameTwo
124
131
  xAttr = ""
@@ -134,5 +141,22 @@ class GraphViz
134
141
 
135
142
  return( xOut )
136
143
  end
144
+
145
+ private
146
+ def getNodeNameAndPort( node )
147
+ name, port = nil, nil
148
+ if node.class == Hash
149
+ node.each do |k, v|
150
+ name, port = getNodeNameAndPort(k)
151
+ port = v
152
+ end
153
+ elsif node.class == String
154
+ name = node
155
+ else
156
+ name = node.id
157
+ end
158
+
159
+ return name, port
160
+ end
137
161
  end
138
162
  end
@@ -3,6 +3,7 @@ require 'graphviz'
3
3
  require 'graphviz/family_tree/generation'
4
4
  require 'graphviz/family_tree/person'
5
5
  require 'graphviz/family_tree/couple'
6
+ require 'graphviz/family_tree/sibling'
6
7
 
7
8
  class GraphViz
8
9
  class FamilyTree
@@ -14,8 +15,9 @@ class GraphViz
14
15
  # end
15
16
  def initialize( &block )
16
17
  @persons = {}
17
- @graph = GraphViz.new( "FamilyTree" )
18
- @generation = 0
18
+ @graph = GraphViz.new( "FamilyTree", :use => :neato )
19
+ @generation_number = 0
20
+ @generations = []
19
21
  @couples = {}
20
22
 
21
23
  instance_eval(&block) if block
@@ -33,8 +35,10 @@ class GraphViz
33
35
  # end
34
36
  # end
35
37
  def generation( &b )
36
- GraphViz::FamilyTree::Generation.new( @graph, @persons, self, @generation ).make( &b )
37
- @generation += 1
38
+ gen = GraphViz::FamilyTree::Generation.new( @graph, @persons, self, @generation_number )
39
+ gen.make( &b )
40
+ @generations << gen
41
+ @generation_number += 1
38
42
  end
39
43
 
40
44
  def persons #:nodoc:
@@ -43,7 +47,7 @@ class GraphViz
43
47
 
44
48
  def add_couple( x, y, node ) #:nodoc:
45
49
  @couples[x] = {} if @couples[x].nil?
46
- @couples[x][y] = GraphViz::FamilyTree::Couple.new( @graph, node )
50
+ @couples[x][y] = GraphViz::FamilyTree::Couple.new( @graph, node, [x, y] )
47
51
  @couples[y] = {} if @couples[y].nil?
48
52
  @couples[y][x] = @couples[x][y]
49
53
  end
@@ -57,9 +61,56 @@ class GraphViz
57
61
  persons[sym.to_s]
58
62
  end
59
63
 
64
+ # Family size
65
+ def size
66
+ @persons.size
67
+ end
68
+
60
69
  # Get the graph
61
70
  def graph
71
+ maxY = @generations.size
72
+ biggestGen, maxX = biggestGenerationNumberAndSize
73
+
74
+ puts "#{maxY} generations"
75
+ puts "Plus grosse generation : ##{biggestGen} avec #{maxX} personnes"
76
+
77
+ puts "traitement des générations..."
78
+
79
+ puts " #{biggestGen}:"
80
+ @generations[biggestGen].persons.each do |id, person|
81
+ puts " - #{id} : #{person.class}"
82
+ end
83
+
84
+ puts " Up..."
85
+ (0...biggestGen).reverse_each do |genNumber|
86
+ puts " #{genNumber}:"
87
+ @generations[genNumber].persons.each do |id, person|
88
+ puts " - #{id} : #{person.class}"
89
+ end
90
+ end
91
+
92
+ puts " Down..."
93
+ ((biggestGen+1)...maxY).each do |genNumber|
94
+ puts " #{genNumber}:"
95
+ @generations[genNumber].persons.each do |id, person|
96
+ puts " - #{id} : #{person.class}"
97
+ end
98
+ end
99
+
62
100
  @graph
63
101
  end
102
+
103
+ private
104
+ def biggestGenerationNumberAndSize
105
+ size = 0
106
+ number = 0
107
+ @generations.each do |gen|
108
+ if gen.size > size
109
+ size = gen.size
110
+ number = gen.number
111
+ end
112
+ end
113
+ return number, size
114
+ end
64
115
  end
65
116
  end
@@ -1,9 +1,11 @@
1
1
  class GraphViz
2
2
  class FamilyTree
3
3
  class Couple
4
- def initialize( g, n ) #:nodoc:
5
- @graph = g
6
- @node = n
4
+ def initialize( graph, node, persons ) #:nodoc:
5
+ @graph = graph
6
+ @node = node
7
+ @kids = []
8
+ @persons = persons
7
9
  end
8
10
 
9
11
  def node #:nodoc:
@@ -12,10 +14,14 @@ class GraphViz
12
14
 
13
15
  # Add kids to a couple
14
16
  def kids( *z )
17
+ @kids = GraphViz::FamilyTree::Sibling.new( z, @persons )
18
+
19
+ return
20
+
15
21
  if z.size == 1
16
22
  @graph.add_edge( @node, z[0].node, "dir" => "none" )
17
23
  else
18
- cluster = @graph.add_graph( "#{@node.name}Kids" )
24
+ cluster = @graph.add_graph( "#{@node.id}Kids" )
19
25
  cluster["rank"] = "same"
20
26
 
21
27
  last = nil
@@ -26,7 +32,7 @@ class GraphViz
26
32
  z.each do |person|
27
33
  count = count + 1
28
34
  if count == add
29
- middle = cluster.add_node( "#{@node.name}Kids", "shape" => "point" )
35
+ middle = cluster.add_node( "#{@node.id}Kids", "shape" => "point" )
30
36
  @graph.add_edge( @node, middle, "dir" => "none" )
31
37
  unless last.nil?
32
38
  cluster.add_edge( last, middle, "dir" => "none" )
@@ -34,7 +40,7 @@ class GraphViz
34
40
  last = middle
35
41
  end
36
42
 
37
- kid = cluster.add_node( "#{person.node.name}Kid", "shape" => "point" )
43
+ kid = cluster.add_node( "#{person.node.id}Kid", "shape" => "point" )
38
44
  @graph.add_edge( kid, person.node, "dir" => "none" )
39
45
 
40
46
  if add == 0 and count == link
@@ -48,6 +54,10 @@ class GraphViz
48
54
  end
49
55
  end
50
56
  end
57
+
58
+ def getKids
59
+ @kids
60
+ end
51
61
  end
52
62
  end
53
63
  end
@@ -3,13 +3,17 @@ class GraphViz
3
3
  class Generation
4
4
  def initialize( graph, persons, tree, gen_number ) #:nodoc:
5
5
  @graph = graph
6
- @persons = persons
7
- @cluster = @graph.add_graph( "Generation#{gen_number}" )
8
- @cluster["rank"] = "same"
6
+ @all_persons = persons
7
+ @persons = {}
9
8
  @tree = tree
9
+ @gen_number = gen_number
10
10
  end
11
11
 
12
- def persons #:nodoc:
12
+ def all_persons #:nodoc:
13
+ @all_persons
14
+ end
15
+
16
+ def persons
13
17
  @persons
14
18
  end
15
19
 
@@ -18,7 +22,17 @@ class GraphViz
18
22
  end
19
23
 
20
24
  def method_missing(sym, *args, &block) #:nodoc:
21
- persons[sym.to_s] ||= GraphViz::FamilyTree::Person.new( @graph, @cluster, @tree, sym.to_s )
25
+ all_persons[sym.to_s] ||= (persons[sym.to_s] ||= GraphViz::FamilyTree::Person.new( @graph, @tree, self, sym.to_s ))
26
+ end
27
+
28
+ # Generation number
29
+ def number
30
+ @gen_number
31
+ end
32
+
33
+ # Generation size
34
+ def size
35
+ @persons.size
22
36
  end
23
37
  end
24
38
  end
@@ -4,12 +4,30 @@ require 'graphviz'
4
4
  class GraphViz
5
5
  class FamilyTree
6
6
  class Person
7
- def initialize( graph, cluster, tree, name ) #:nodoc:
7
+ def initialize( graph, tree, generation, id ) #:nodoc:
8
8
  @graph = graph
9
- @cluster = cluster
10
- @node = @cluster.add_node( name )
9
+ @node = @graph.add_node( id )
11
10
  @node["shape"] = "box"
12
11
  @tree = tree
12
+ @generation = generation
13
+ @x, @y = 0, 0
14
+ @sibling = nil
15
+ end
16
+
17
+ def id
18
+ @node.id
19
+ end
20
+
21
+ def name
22
+ @node.label || @node.id
23
+ end
24
+
25
+ def sibling
26
+ @sibling
27
+ end
28
+
29
+ def sibling=(x)
30
+ @sibling=x
13
31
  end
14
32
 
15
33
  def couples #:nodoc:
@@ -27,6 +45,7 @@ class GraphViz
27
45
  @node["label"] = name
28
46
  @node["color"] = "blue"
29
47
  end
48
+
30
49
  # Define the current person as a boy
31
50
  #
32
51
  # greg.is_a_boy( "Greg" )
@@ -52,10 +71,10 @@ class GraphViz
52
71
  #
53
72
  # mu.is_maried_with greg
54
73
  def is_maried_with( x )
55
- node = @cluster.add_node( "#{@node.name}And#{x.node.name}" )
74
+ node = @graph.add_node( "#{@node.id}And#{x.node.id}" )
56
75
  node["shape"] = "point"
57
- @cluster.add_edge( @node, node, "dir" => "none" )
58
- @cluster.add_edge( node, x.node, "dir" => "none" )
76
+ @graph.add_edge( @node, node, "dir" => "none" )
77
+ @graph.add_edge( node, x.node, "dir" => "none" )
59
78
  @tree.add_couple( self, x, node )
60
79
  end
61
80
 
@@ -63,11 +82,11 @@ class GraphViz
63
82
  #
64
83
  # sophie.is_divorced_with john
65
84
  def is_divorced_with( x )
66
- node = @cluster.add_node( "#{@node.name}And#{x.node.name}" )
85
+ node = @graph.add_node( "#{@node.id}And#{x.node.id}" )
67
86
  node["shape"] = "point"
68
87
  node["color"] = "red"
69
- @cluster.add_edge( @node, node, "dir" => "none", "color" => "red" )
70
- @cluster.add_edge( node, x.node, "dir" => "none", "color" => "red" )
88
+ @graph.add_edge( @node, node, "dir" => "none", "color" => "red" )
89
+ @graph.add_edge( node, x.node, "dir" => "none", "color" => "red" )
71
90
  @tree.add_couple( self, x, node )
72
91
  end
73
92
 
@@ -75,19 +94,26 @@ class GraphViz
75
94
  #
76
95
  # simon.is_widower_of elisa
77
96
  def is_widower_of( x ) #veuf
78
- node = @cluster.add_node( "#{@node.name}And#{x.node.name}" )
97
+ node = @graph.add_node( "#{@node.id}And#{x.node.id}" )
79
98
  node["shape"] = "point"
80
99
  node["color"] = "green"
81
- @cluster.add_edge( @node, node, "dir" => "none", "color" => "green" )
82
- @cluster.add_edge( node, x.node, "dir" => "none", "color" => "green" )
100
+ @graph.add_edge( @node, node, "dir" => "none", "color" => "green" )
101
+ @graph.add_edge( node, x.node, "dir" => "none", "color" => "green" )
83
102
  @tree.add_couple( self, x, node )
84
103
  end
85
104
 
105
+ # Define the current person as dead
106
+ #
107
+ # jack.is_dead
108
+ def is_dead
109
+ @node["style"] = "filled"
110
+ end
111
+
86
112
  # Define the kids of a single person
87
113
  #
88
114
  # alice.kids( john, jack, julie )
89
115
  def kids( *z )
90
- GraphViz::FamilyTree::Couple.new( @graph, @node ).kids( *z )
116
+ GraphViz::FamilyTree::Couple.new( @graph, @node, [self] ).kids( *z )
91
117
  end
92
118
  end
93
119
  end
@@ -0,0 +1,13 @@
1
+ class GraphViz
2
+ class FamilyTree
3
+ class Sibling
4
+ def initialize( bns, parents )
5
+ @brothers_and_sisters = bns
6
+ @brothers_and_sisters.each do |person|
7
+ person.sibling = self
8
+ end
9
+ @parents = parents
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/graphviz/node.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Gregoire Lejeune <gregoire.lejeune@free.fr>
1
+ # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Gregoire Lejeune <gregoire.lejeune@free.fr>
2
2
  #
3
3
  # This program is free software; you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
@@ -28,7 +28,7 @@ class GraphViz
28
28
  # Create a new node
29
29
  #
30
30
  # In:
31
- # * xNodeName : Name of the node
31
+ # * xNodeName : ID of the node
32
32
  # * oGParrent : Graph
33
33
  #
34
34
  def initialize( xNodeName, oGParrent = nil )
@@ -38,9 +38,17 @@ class GraphViz
38
38
  end
39
39
 
40
40
  #
41
- # Get the node name
41
+ # Get the node ID
42
42
  #
43
43
  def name
44
+ warn "GraphViz::Node#name is deprecated, please use GraphViz::Node#id!"
45
+ return self.id
46
+ end
47
+
48
+ #
49
+ # Get the node ID
50
+ #
51
+ def id
44
52
  @xNodeName.clone
45
53
  end
46
54