Module: GRATR::Graph

Included in:
AdjacencyGraph
Defined in:
lib/gratr/dot.rb,
lib/gratr/search.rb,
lib/gratr/biconnected.rb,
lib/gratr/comparability.rb,
lib/gratr/chinese_postman.rb,
lib/gratr/digraph_distance.rb,
lib/gratr/strong_components.rb

Defined Under Namespace

Modules: Biconnected, ChinesePostman, Comparability, Distance, Search, StrongComponents

Instance Method Summary collapse

Instance Method Details

#dotty(params = {}, dotfile = 'graph.dot') ⇒ Object

Call dotty for the graph which is written to the file ‘graph.dot’ in the # current directory.



72
73
74
75
# File 'lib/gratr/dot.rb', line 72

def dotty (params = {}, dotfile = 'graph.dot')
  File.open(dotfile, 'w') {|f| f << to_dot(params) }
  system('dotty', dotfile)
end

#to_dot(params = {}) ⇒ Object

Output the dot format as a string



68
# File 'lib/gratr/dot.rb', line 68

def to_dot (params={}) to_dot_graph(params).to_s; end

#to_dot_graph(params = {}) ⇒ Object

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.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gratr/dot.rb', line 42

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::DOTDirectedArc : DOT::DOTArc
  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

#write_to_graphic_file(fmt = 'png', dotfile = 'graph') ⇒ Object

Use dot to create a graphical representation of the graph. Returns the filename of the graphics file.



79
80
81
82
83
84
85
86
87
# File 'lib/gratr/dot.rb', line 79

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