Class: LemonGraph::Graphviz
- Inherits:
-
Object
- Object
- LemonGraph::Graphviz
- Defined in:
- lib/lemongraph/graphviz.rb
Instance Attribute Summary collapse
-
#str ⇒ String
readonly
A graphviz representation of the graph this instance was build with.
Instance Method Summary collapse
-
#initialize(graph, **maps) ⇒ Graphviz
constructor
Returns a new Graphviz object build from graph graph.
-
#to_image(type) ⇒ String
The binary content of the image.
-
#to_svg ⇒ String
An image as a SVG string.
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.
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 = {} = {} 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 [map_name] = map end end if graph.is_a?(Digraph) then @str << "digraph {\n" else @str << "graph {\n" end .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
#str ⇒ String (readonly)
Returns 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.
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_svg ⇒ String
Returns an image as a SVG string.
172 173 174 |
# File 'lib/lemongraph/graphviz.rb', line 172 def to_svg to_image(:svg) end |