lib/rgl/dot.rb in rgl-0.6.2 vs lib/rgl/dot.rb in rgl-0.6.3

- old
+ new

@@ -8,55 +8,87 @@ # functions in this modul execute the GraphViz executables _dot_ or _dotty_. require 'rgl/rdot' module RGL - module Graph - # Returns a label for vertex v. Default is v.to_s def vertex_label(v) v.to_s end def vertex_id(v) v end + # Set the configuration values for the given vertex + def set_vertex_options(vertex, **options) + @vertex_options ||= {} + @vertex_options[vertex] ||= {} + + RGL::DOT::NODE_OPTS.each do |opt| + @vertex_options[vertex][:"#{opt}"] = options[:"#{opt}"] if options.key?(:"#{opt}") + end + end + + # Set the configuration values for the given edge + def set_edge_options(u, v, **options) + edge = edge_class.new(u, v) + @edge_options ||= {} + @edge_options[edge] ||= {} + + RGL::DOT::EDGE_OPTS.each do |opt| + @edge_options[edge][:"#{opt}"] = options[:"#{opt}"] if options.key?(:"#{opt}") + end + end + # Return a {DOT::Digraph} for directed graphs or a {DOT::Graph} for an # undirected {Graph}. _params_ can contain any graph property specified in # rdot.rb. # def to_dot_graph(params = {}) params['name'] ||= self.class.name.gsub(/:/, '_') 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| - default_vertex_options = { + 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)} + each_vertex_options = default_vertex_options + + if @vertex_options && @vertex_options[v] + RGL::DOT::NODE_OPTS.each do |opt| + if @vertex_options[v].key?(:"#{opt}") + each_vertex_options["#{opt}"] = @vertex_options[v].fetch(:"#{opt}") + end + end + end graph << DOT::Node.new(each_vertex_options) end - each_edge do |u, v| + edges.each do |edge| default_edge_options = { - 'from' => vertex_id(u), - 'to' => vertex_id(v), + 'from' => edge.source, + 'to' => edge.target, '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)} + + each_edge_options = default_edge_options + + if @edge_options && @edge_options[edge] + RGL::DOT::EDGE_OPTS.each do |opt| + if @edge_options[edge].key?(:"#{opt}") + each_edge_options["#{opt}"] = @edge_options[edge].fetch(:"#{opt}") + end + end + end graph << edge_class.new(each_edge_options) - end + end graph end # Output the DOT-graph to stream _s_. @@ -79,10 +111,10 @@ end # Use dot[https://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", options={}) + 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(options).to_s << "\n"