Module: Plexus::Dot

Included in:
GraphBuilder
Defined in:
lib/plexus/dot.rb

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.



71
72
73
74
# File 'lib/plexus/dot.rb', line 71

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



65
66
67
# File 'lib/plexus/dot.rb', line 65

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

#to_dot_graph(params = {}) ⇒ DOT::DOTDigraph, DOT::DOTSubgraph

Creates a DOT::DOTDigraph for directed graphs or a DOT::DOTSubgraph for undirected graphs.

Parameters:

  • params (Hash) (defaults to: {})

    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.

Returns:

  • (DOT::DOTDigraph, DOT::DOTSubgraph)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/plexus/dot.rb', line 15

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 || v.__id__.to_s
    name = name.dup.gsub(/"/, "'")

    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|
    if e.source.to_s.nil?
      source_label = e.source.__id__.to_s
    else
      source_label = e.source.to_s.dup
    end

    if e.target.to_s.nil?
      target_label = e.target.__id__.to_s
    else
      target_label = e.target.to_s.dup
    end

    source_label.gsub!(/"/, "'")
    target_label.gsub!(/"/, "'")

    params = { 'from'     => '"'+ source_label + '"',
               'to'       => '"'+ target_label + '"',
               '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 Also known as: as_dot_graphic

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



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/plexus/dot.rb', line 78

def write_to_graphic_file(fmt = 'png', dotfile = 'graph')
  src = dotfile + '.dot'
  dot = dotfile + '.' + fmt

  # DOT::DOTSubgraph creates subgraphs, but that's broken.
  buffer = self.to_dot
  buffer.gsub!(/^subgraph/, "graph")

  File.open(src, 'w') {|f| f << buffer << "\n"}
  system( "dot -T#{fmt} #{src} -o #{dot}" )

  dot
end