module PROIEL::Visualization::Graphviz

Constants

DEFAULT_GRAPHVIZ_BINARY
DEFAULT_TEMPLATES
SUPPORTED_OUTPUT_FORMATS

Public Class Methods

generate(template, graph, output_format, options = {}) click to toggle source
# File lib/proiel/visualization/graphviz.rb, line 21
def self.generate(template, graph, output_format, options = {})
  raise ArgumentError, 'string or symbol expected' unless template.is_a?(String) or template.is_a?(Symbol)

  dot_code = generate_dot(template, graph, options)

  if output_format.to_sym == :dot
    dot_code
  else
    generate_image(dot_code, output_format, options)
  end
end
generate_dot(template, graph, options) click to toggle source
# File lib/proiel/visualization/graphviz.rb, line 63
def self.generate_dot(template, graph, options)
  unless options[:direction].nil? or %(TD LR).include?(options[:direction])
    raise ArgumentError, 'invalid direction'
  end

  filename = template_filename(template)

  content = File.read(filename)

  template = ERB.new(content, nil, '-')
  template.filename = filename

  TemplateContext.new(graph, options[:direction] || 'TD').generate(template)
end
generate_image(dot_code, output_format, options = {}) click to toggle source
# File lib/proiel/visualization/graphviz.rb, line 40
def self.generate_image(dot_code, output_format, options = {})
  raise ArgumentError, 'string expected' unless dot_code.is_a?(String)
  unless output_format.is_a?(String) or output_format.is_a?(Symbol)
    raise ArgumentError, 'string or symbol expected'
  end
  raise ArgumentError, 'invalid output format' unless SUPPORTED_OUTPUT_FORMATS.include?(output_format.to_sym)

  graphviz_binary = options[:graphviz_binary] || DEFAULT_GRAPHVIZ_BINARY

  result, errors = nil, nil

  Open3.popen3("dot -T#{output_format}") do |dot, img, err|
    dot.write dot_code
    dot.close

    result, errors = img.read, err.read
  end

  raise GraphvizError, "graphviz exited with errors: #{errors}" unless errors.nil? or errors == ''

  result
end
generate_to_file(template, graph, output_format, output_filename, options = {}) click to toggle source
# File lib/proiel/visualization/graphviz.rb, line 11
def self.generate_to_file(template, graph, output_format, output_filename, options = {})
  raise ArgumentError, 'string expected' unless output_filename.is_a?(String)

  result = PROIEL::Visualization::Graphviz.generate(template, graph, output_format, options)

  File.open(output_filename, 'w') do |f|
    f.write(result)
  end
end
template_filename(template) click to toggle source
# File lib/proiel/visualization/graphviz.rb, line 33
def self.template_filename(template)
  raise ArgumentError, 'string or symbol expected' unless template.is_a?(String) or template.is_a?(Symbol)
  raise ArgumentError, 'invalid template' unless DEFAULT_TEMPLATES.include?(template.to_sym)

  File.join(File.dirname(__FILE__), 'graphviz', "#{template}.dot.erb")
end