# File lib/state_machine/machine.rb, line 1255
    def draw(options = {})
      options = {
        :name => "#{owner_class.name}_#{name}",
        :path => '.',
        :format => 'png',
        :font => 'Arial',
        :orientation => 'portrait',
        :output => true
      }.merge(options)
      assert_valid_keys(options, :name, :path, :format, :font, :orientation, :output)
      
      begin
        # Load the graphviz library
        require 'rubygems'
        require 'graphviz'
        
        graph = GraphViz.new('G',
          :output => options[:format],
          :file => File.join(options[:path], "#{options[:name]}.#{options[:format]}"),
          :rankdir => options[:orientation] == 'landscape' ? 'LR' : 'TB'
        )
        
        # Add nodes
        states.by_priority.each do |state|
          node = state.draw(graph)
          node.fontname = options[:font]
        end
        
        # Add edges
        events.each do |event|
          edges = event.draw(graph)
          edges.each {|edge| edge.fontname = options[:font]}
        end
        
        # Generate the graph
        graph.output if options[:output]
        graph
      rescue LoadError
        $stderr.puts 'Cannot draw the machine. `gem install ruby-graphviz` and try again.'
        false
      end
    end