Class: LemonGraph::Graphviz

Inherits:
Object
  • Object
show all
Defined in:
lib/lemongraph/graphviz.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, **maps) ⇒ Graphviz

Returns a new LemonGraph::Graphviz object build from graph graph.

Keyword arguments can be provided.

  • keyword arguments starting with node_ are expected to be NodeMaps and will be applied to nodes
  • keyword arguments starting with edge_ are expected to be EdgeMaps and will be applied to edges
  • keyword arguments starting with arc_ are expected to be ArcMaps and will be applied to arcs

Other options can be provided as hashes or other objects.

Examples:

d = LemonGraph::Digraph.new
n = 7.times.collect{d.add_node}
n[0].arc_to n[1]
n[1].arc_to n[2]
n[1].arc_to n[3]
n[4].arc_from n[0]
n[5].arc_from n[4]
n[4].arc_to n[6]

nl = LemonGraph::NodeMap.new(d){|n| n.nb_outarcs > 0 ? n.id.to_s : "Leaf #{n.id}" }
nl[n[0]] = "Root 0"

nc = LemonGraph::NodeMap.new(d){|n| n.nb_outarcs > 0 ? :blue : :red }

al = LemonGraph::ArcMap.new(d){|a| "#{a.source.id} - #{a.target.id}"}

gv = LemonGraph::Graphviz.new(d
  node_label: nl,
  node_color: nc,
  arc_label: al,
  rankdir: 'LR',
  graph: {bgcolor: '#00FF00FF'},
  node: {fontname: 'DejaVu Sans', style: :filled, width: 0.4},
  edge: {penwidth: 0.75, arrowhead: :open}
)

puts gv.str
#=>
# digraph {
#   rankdir="LR";
#   graph [bgcolor="#00FF00FF"];
#   node [fontname="DejaVu Sans",style=filled,width=0.4];
#   edge [penwidth=0.75,arrowhead=open];
#   6 [label="Leaf 6",color=red];
#   5 [label="Leaf 5",color=red];
#   4 [label="4",color=blue];
#   3 [label="Leaf 3",color=red];
#   2 [label="Leaf 2",color=red];
#   1 [label="1",color=blue];
#   0 [label="Root 0",color=blue];
#   4 -> 6 [label="4 - 6"];
#   4 -> 5 [label="4 - 5"];
#   1 -> 3 [label="1 - 3"];
#   1 -> 2 [label="1 - 2"];
#   0 -> 4 [label="0 - 4"];
#   0 -> 1 [label="0 - 1"];
# }

Parameters:

Raises:

  • (TypeError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/lemongraph/graphviz.rb', line 86

def initialize(graph, **maps)
  raise TypeError.new("Expecting a #{Graph} or a #{Digraph}, not a #{graph.class}") unless graph.is_a?(Graph) or graph.is_a?(Digraph)
  @str = ''
  node_maps = {}
  arc_maps = {}
  edge_maps = {}
  options = {}
  maps.each do |map_name, map|
    m = map_name.to_s.match(/^(node|arc|edge)_(.+?)s?$/)
    if m then
      case m[1]
      when 'node'
        raise "expecting a #{NodeMap} for :node_*, not a #{map.class}" unless NodeMap === map
        node_maps[m[2].to_sym] = map
      when 'edge'
        raise "expecting a #{EdgeMap} for :edge_*, not a #{map.class}" unless EdgeMap === map
        edge_maps[m[2].to_sym] = map
      when 'arc'
        raise "expecting a #{ArcMap} for :arc_*, not a #{map.class}" unless ArcMap === map
        arc_maps[m[2].to_sym] = map
      end
    else
      #raise 'expecting a Hash for options' unless Hash === map
      options[map_name] = map
    end
  end
  if graph.is_a?(Digraph) then
    @str << "digraph {\n"
  else
    @str << "graph {\n"
  end
  options.each do |k, h|
    if Hash === h then
      @str << "  #{k} [#{h.collect{|k, v| "#{k}=#{String === v ? "\"#{v}\"" : v}"}.join(',')}];\n"
    else
      @str << "  #{k}=#{String === h ? "\"#{h}\"" : h};\n"
    end
  end
  unless node_maps.empty? then
    graph.each_node do |n|
      params = node_maps.collect { |map_name, map|
        v = map[n]
        next nil if v.nil?
        if v.is_a?(String) then
          "#{map_name}=\"#{v}\""
        else
          "#{map_name}=#{v}"
        end
      }.compact.join(',')
      @str << "  #{n.id} [#{params}];\n" unless params.empty?
    end
  end
  if graph.is_a?(Digraph) then
    graph.each_arc do |a|
      @str << "  #{a.source.id} -> #{a.target.id}"
      params = arc_maps.collect { |map_name, map|
        v = map[a]
        next nil if v.nil?
        if v.is_a?(String) then
          "#{map_name}=\"#{v}\""
        else
          "#{map_name}=#{v}"
        end
      }.compact.join(',')
      @str << "#{params.empty? ? '' : " [#{params}]"};\n"
    end
  else
    graph.each_edge do |e|
      @str << "  #{e.u.id} -- #{e.v.id}"
      params = edge_maps.collect { |map_name, map|
        v = map[e]
        next nil if v.nil?
        if v.is_a?(String) then
          "#{map_name}=\"#{v}\""
        else
          "#{map_name}=#{v}"
        end
      }.compact.join(',')
      @str << "#{params.empty? ? '' : " [#{params}]"};\n"
    end
  end
  @str << "}\n"
end

Instance Attribute Details

#strString (readonly)

Returns A graphviz representation of the graph this instance was build with.

Returns:

  • (String)

    A graphviz representation of the graph this instance was build with



24
25
26
# File 'lib/lemongraph/graphviz.rb', line 24

def str
  @str
end

Instance Method Details

#to_image(type) ⇒ String

Returns the binary content of the image.

Parameters:

  • type (Symbol)

    :svg, :png or pdf

Returns:

  • (String)

    the binary content of the image



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/lemongraph/graphviz.rb', line 191

def to_image(type)
  input = @str #.force_encoding('ASCII-8BIT')
  output = ''
#      output.force_encoding('ASCII-8BIT') if type == :png
  exit_status = nil
  Open3.popen3("dot #{type == :png ? '-Gdpi=90' : ''} -T#{type}") do |stdin, stdout, stderr, wait_thr|
    loop do
      unless input.empty? then
        n = stdin.write(input)
        input = input[n..-1]
        stdin.close if input.empty?
      end
      output += stdout.read
      begin
        while not(stderr.closed?) and (r = stderr.read_nonblock(8192)).length > 0 do
          $stderr.write r
        end
      rescue EOFError; end
      break if input.empty?
    end
    exit_status = wait_thr.value
  end
  raise "dot program exited with value #{exit_status.exitstatus}" unless exit_status.success?
  output.force_encoding('ASCII-8BIT')
  output
end

#to_svgString

Returns an image as a SVG string.

Returns:

  • (String)

    an image as a SVG string



172
173
174
# File 'lib/lemongraph/graphviz.rb', line 172

def to_svg
  to_image(:svg)
end