Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions lib/rgl/dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,29 @@ def to_dot_graph(params = {})
fontsize = params['fontsize'] ? params['fontsize'] : '8'
graph = (directed? ? DOT::Digraph : DOT::Graph).new(params)
edge_class = directed? ? DOT::DirectedEdge : DOT::Edge
vertex_options = params['vertex'] || {}
edge_options = params['edge'] || {}

each_vertex do |v|
graph << DOT::Node.new(
'name' => vertex_id(v),
'fontsize' => fontsize,
'label' => vertex_label(v)
)
default_vertex_options = {
'name' => vertex_id(v),
'fontsize' => fontsize,
'label' => vertex_label(v)
}
each_vertex_options = default_vertex_options.merge(vertex_options)
vertex_options.each{|option, val| each_vertex_options[option] = val.call(v) if val.is_a?(Proc)}
graph << DOT::Node.new(each_vertex_options)
end

each_edge do |u, v|
graph << edge_class.new(
'from' => vertex_id(u),
'to' => vertex_id(v),
'fontsize' => fontsize
)
default_edge_options = {
'from' => vertex_id(u),
'to' => vertex_id(v),
'fontsize' => fontsize
}
each_edge_options = default_edge_options.merge(edge_options)
edge_options.each{|option, val| each_edge_options[option] = val.call(u, v) if val.is_a?(Proc)}
graph << edge_class.new(each_edge_options)
end

graph
Expand Down Expand Up @@ -75,12 +83,12 @@ def dotty(params = {})
# Use dot[http://www.graphviz.org] to create a graphical representation of
# the graph. Returns the filename of the graphics file.
#
def write_to_graphic_file(fmt='png', dotfile="graph")
def write_to_graphic_file(fmt='png', dotfile="graph", options={})
src = dotfile + ".dot"
dot = dotfile + "." + fmt

File.open(src, 'w') do |f|
f << self.to_dot_graph.to_s << "\n"
f << self.to_dot_graph(options).to_s << "\n"
end

unless system("dot -T#{fmt} #{src} -o #{dot}")
Expand Down
27 changes: 27 additions & 0 deletions test/dot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ def test_to_dot_digraph
end
end

def test_to_dot_digraph_with_options
graph = RGL::DirectedAdjacencyGraph["a", "b"]

begin
edge_labels = {}
graph.each_edge do |b, e|
key = "#{b}-#{e}"
edge_labels[key] = "#{b} to #{e}"
end

vertex_fontcolors = {'a' => 'green', 'b' => 'blue'}
vertex_fontcolor_setting = Proc.new{|v| vertex_fontcolors[v]}
vertex_settings = {'fontcolor' => vertex_fontcolor_setting, 'fontsize' => 12}

edge_label_setting = Proc.new{|b, e| edge_labels["#{b}-#{e}"]}
edge_settings = {'color' => 'red', 'label' => edge_label_setting}
dot_options = {'edge' => edge_settings,'vertex' => vertex_settings}
dot = graph.to_dot_graph(dot_options).to_s

assert_match(dot, /a \[\n\s*fontcolor = green,\n\s*fontsize = 12,\n\s*label = a\n\s*/)
assert_match(dot, /b \[\n\s*fontcolor = blue,\n\s*fontsize = 12,\n\s*label = b\n\s*/)
assert_match(dot, /a -> b \[\n\s*color = red,\n\s*fontsize = 8,\n\s*label = \"a to b\"\n/)
rescue
puts "Graphviz not installed?"
end
end

def test_to_dot_graph
graph = RGL::AdjacencyGraph["a", "b"]
def graph.vertex_label(v)
Expand Down