Method: RGL::Graph#to_dot_graph

Defined in:
lib/rgl/dot.rb

#to_dot_graph(params = {}) ⇒ Object

Return a DOT::Digraph for directed graphs or a DOT::Graph for an undirected RGL::Graph. params can contain any graph property specified in rdot.rb.



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
# File 'lib/rgl/dot.rb', line 48

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

  each_vertex do |v|
    default_vertex_options = {
      'name'     => vertex_id(v),
      'fontsize' => fontsize,
      'label'    => vertex_label(v)
    }
    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

  edges.each do |edge|
    default_edge_options = {
      'from'     => edge.source,
      'to'       => edge.target,
      'fontsize' => fontsize
    }

    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

  graph
end