monora / rgl

RGL is a framework for graph data structures and algorithms in Ruby.
https://monora.github.io/rgl/
Other
415 stars 59 forks source link

Refactor edge/vertex settings to no longer require individual options initialization #92

Closed r0ckarong closed 1 year ago

r0ckarong commented 1 year ago

In the current implementation the user will use the methods set_vertex_options and set_edge_options location in dot.rb to set the options for the respective element of the graph. For this to work properly the user needs to initialize two procs and two hashes with all the options they want to use.

require 'rgl/adjacency'
require 'rgl/dot'

graph = RGL::DirectedAdjacencyGraph['a', 'b', 'b', 'c']

graph.add_edge('a', 'b')
graph.add_edge('a', 'c')

# Vertex Settings
graph.set_vertex_options('a', label: 'This is A', shape: 'box3d', fontcolor: 'green', fontsize: 16)
graph.set_vertex_options('b', label: 'This is B', shape: 'tab', fontcolor: 'red', fontsize: 14)
graph.set_vertex_options('c', shape: 'tab', fontcolor: 'blue')

# Edge Settings
graph.set_edge_options('a', 'b', label: 'NotCapitalEdge', style: 'dotted', direction: 'back', color: 'yellow')
graph.set_edge_options('a', 'c', weight: 5, color: 'blue')

# puts @vertex_options
# puts @edge_options

get_vertex_setting = proc { |v| graph.vertex_options[v] }
get_edge_setting = proc { |b, e| graph.edge_options[graph.edge_class.new(b, e)] }

# To configure more options, add the respective keys and the proc call here
# Then provide the respective key:value to set_vertex_options
# Any hard coded values for a key will be applied to all nodes
vertex_options = {
  'label' => get_vertex_setting,
  'shape' => get_vertex_setting,
  'fontcolor' => get_vertex_setting,
  'fontsize' => get_vertex_setting
}

# To configure more options, add the respective keys and the proc call here
# Then provide the respective key:value to set_edge_options
# Any hard coded values for a key will be applied to all edges
edge_options = {
  'label' => get_edge_setting,
  'dir' => get_edge_setting,
  'color' => get_edge_setting,
  'style' => get_edge_setting,
  'weight' => get_edge_setting,
  'constraint' => get_edge_setting,
  'headlabel' => get_edge_setting,
  'taillabel' => get_edge_setting
}

# Options
  dot_options = { 'edge' => edge_options, 'vertex' => vertex_options }

graph.write_to_graphic_file('png', 'graph', dot_options)

We should move vertex_options and edge_options and the respective procs somewhere into the initialize phase for the graph so they get populated with all valid options automatically and the user does not have to concern themselves with it and just pass the respective option combination to the methods. I have not yet found the right place in the classes where this would apply to all graph types and can be used properly.

https://github.com/monora/rgl/pull/89#issuecomment-1483091675 https://github.com/monora/rgl/pull/89#issuecomment-1483295214

r0ckarong commented 1 year ago

This is fixed in #94