1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# Created by Luke A. Kanies on 2007-11-07.
# Copyright (c) 2007. All rights reserved.
require 'puppet/external/gratr/dot'
require 'puppet/relationship'
require 'puppet/external/gratr/search'
# A hopefully-faster graph class to replace the use of GRATR.
class Puppet::SimpleGraph
include GRATR::Graph::Search
# An internal class for handling a vertex's edges.
class VertexWrapper
attr_accessor :in, :out, :vertex
# Remove all references to everything.
def clear
@adjacencies[:in].clear
@adjacencies[:out].clear
@vertex = nil
end
def initialize(vertex)
@vertex = vertex
@adjacencies = {:in => Hash.new { |h,k| h[k] = [] }, :out => Hash.new { |h,k| h[k] = [] }}
#@adjacencies = {:in => [], :out => []}
end
# Find adjacent vertices or edges.
def adjacent(options)
direction = options[:direction] || :out
options[:type] ||= :vertices
return @adjacencies[direction].values.flatten if options[:type] == :edges
return @adjacencies[direction].keys
end
# Add an edge to our list.
def add_edge(direction, edge)
@adjacencies[direction][other_vertex(direction, edge)] << edge
end
# Return all known edges.
def edges
[:in, :out].collect { |dir| @adjacencies[dir].values }.flatten
end
# Test whether we share an edge with a given vertex.
def has_edge?(direction, vertex)
return true if @adjacencies[direction][vertex].length > 0
return false
end
# The other vertex in the edge.
def other_vertex(direction, edge)
case direction
when :in: edge.source
else
edge.target
end
end
# Remove an edge from our list. Assumes that we've already checked
# that the edge is valid.
def remove_edge(direction, edge)
@adjacencies[direction][other_vertex(direction, edge)].delete(edge)
end
end
def initialize
@vertices = {}
@edges = []
end
# Clear our graph.
def clear
@vertices.each { |vertex, wrapper| wrapper.clear }
@vertices.clear
@edges.clear
end
# Whether our graph is directed. Always true. (Used by the GRATR search lib.)
def directed?
true
end
# Return a reversed version of this graph.
def reversal
result = self.class.new
vertices.each { |vertex| result.add_vertex!(vertex) }
edges.each do |edge|
newedge = edge.class.new(edge.target, edge.source, edge.label)
result.add_edge!(newedge)
end
result
end
# Return the size of the graph. Used by GRATR.
def size
@vertices.length
end
# Return the graph as an array. Again, used by GRATR.
def to_a
@vertices.keys
end
# Add a new vertex to the graph.
def add_vertex!(vertex)
return false if vertex?(vertex)
setup_vertex(vertex)
true # don't return the VertexWrapper instance.
end
# Remove a vertex from the graph.
def remove_vertex!(vertex)
return nil unless vertex?(vertex)
@vertices[vertex].edges.each { |edge| remove_edge!(edge) }
@vertices[vertex].clear
@vertices.delete(vertex)
end
# Test whether a given vertex is in the graph.
def vertex?(vertex)
@vertices.include?(vertex)
end
# Return a list of all vertices.
def vertices
@vertices.keys
end
# Add a new edge. The graph user has to create the edge instance,
# since they have to specify what kind of edge it is.
def add_edge!(source, target = nil, label = nil)
if target
edge = Puppet::Relationship.new(source, target, label)
else
edge = source
end
[edge.source, edge.target].each { |vertex| setup_vertex(vertex) unless vertex?(vertex) }
@vertices[edge.source].add_edge :out, edge
@vertices[edge.target].add_edge :in, edge
@edges << edge
true
end
# Find a matching edge. Note that this only finds the first edge,
# not all of them or whatever.
def edge(source, target)
@edges.each_with_index { |test_edge, index| return test_edge if test_edge.source == source and test_edge.target == target }
end
def edge_label(source, target)
return nil unless edge = edge(source, target)
edge.label
end
# Is there an edge between the two vertices?
def edge?(source, target)
return false unless vertex?(source) and vertex?(target)
@vertices[source].has_edge?(:out, target)
end
def edges
@edges.dup
end
# Remove an edge from our graph.
def remove_edge!(edge)
@vertices[edge.source].remove_edge(:out, edge)
@vertices[edge.target].remove_edge(:in, edge)
# Here we are looking for an exact edge, so we don't want to use ==, because
# it's too darn expensive (in testing, deleting 3000 edges went from 6 seconds to
# 0.05 seconds with this change).
@edges.each_with_index { |test_edge, index| @edges.delete_at(index) and break if edge.equal?(test_edge) }
nil
end
# Find adjacent edges.
def adjacent(vertex, options = {})
return [] unless wrapper = @vertices[vertex]
return wrapper.adjacent(options)
end
private
# An internal method that skips the validation, so we don't have
# duplicate validation calls.
def setup_vertex(vertex)
@vertices[vertex] = VertexWrapper.new(vertex)
end
public
# LAK:FIXME This is just a paste of the GRATR code with slight modifications.
# Return a DOT::DOTDigraph for directed graphs or a DOT::DOTSubgraph for an
# undirected Graph. _params_ can contain any graph property specified in
# rdot.rb. If an edge or vertex label is a kind of Hash then the keys
# which match +dot+ properties will be used as well.
def to_dot_graph (params = {})
params['name'] ||= self.class.name.gsub(/:/,'_')
fontsize = params['fontsize'] ? params['fontsize'] : '8'
graph = (directed? ? DOT::DOTDigraph : DOT::DOTSubgraph).new(params)
edge_klass = directed? ? DOT::DOTDirectedEdge : DOT::DOTEdge
vertices.each do |v|
name = v.to_s
params = {'name' => '"'+name+'"',
'fontsize' => fontsize,
'label' => name}
v_label = vertex_label(v)
params.merge!(v_label) if v_label and v_label.kind_of? Hash
graph << DOT::DOTNode.new(params)
end
edges.each do |e|
params = {'from' => '"'+ e.source.to_s + '"',
'to' => '"'+ e.target.to_s + '"',
'fontsize' => fontsize }
e_label = edge_label(e)
params.merge!(e_label) if e_label and e_label.kind_of? Hash
graph << edge_klass.new(params)
end
graph
end
# Output the dot format as a string
def to_dot (params={}) to_dot_graph(params).to_s; end
# Call +dotty+ for the graph which is written to the file 'graph.dot'
# in the # current directory.
def dotty (params = {}, dotfile = 'graph.dot')
File.open(dotfile, 'w') {|f| f << to_dot(params) }
system('dotty', dotfile)
end
# Use +dot+ to create a graphical representation of the graph. Returns the
# filename of the graphics file.
def write_to_graphic_file (fmt='png', dotfile='graph')
src = dotfile + '.dot'
dot = dotfile + '.' + fmt
File.open(src, 'w') {|f| f << self.to_dot << "\n"}
system( "dot -T#{fmt} #{src} -o #{dot}" )
dot
end
end
|