class Accessibility::Graph

DOT graph generator for AXElements. It can generate the digraph code for a UI subtree. That code can then be given to GraphViz to generate an image for the graph.

You can learn more about generating graphs in the [Debugging tutorial](github.com/Marketcircle/AXElements/wiki/Debugging).

[Learn more about GraphViz](www.graphviz.org/).

Attributes

edges[R]

List of edges in the graph.

@return [Array<Accessibility::Graph::Edge>]

nodes[R]

List of nodes in the UI hierarchy.

@return [Array<Accessibility::Graph::Node>]

Public Class Methods

new(root) click to toggle source

@param root [AX::Element]

# File lib/accessibility/graph.rb, line 163
def initialize root
  root_node   = Node.new(root)
  @nodes      = [root_node]
  @edges      = []

  # exploit the ordering of a breadth-first enumeration to simplify
  # the creation of edges for the graph. This only works because
  # the UI hiearchy is a simple tree.
  @edge_queue = Array.new(root.children.size, root_node)
end

Public Instance Methods

build!() click to toggle source

Construct the list of nodes and edges for the graph.

The secret sauce is that we create an edge queue to exploit the breadth first ordering of the enumerator, which makes building the edges very easy.

# File lib/accessibility/graph.rb, line 180
def build!
  Accessibility::Enumerators::BreadthFirst.new(nodes.last.element).each do |element|
    nodes << node = Node.new(element)
    edges << Edge.new(node, @edge_queue.shift)
    # should use #size_of(:children), but that doesn't in all cases
    @edge_queue.concat Array.new(element.children.size, node)
  end
  @built = true
end
generate_png!() click to toggle source

Generate the PNG file for the graph and save it to a temporary file. The path to the temporary file will be returned.

@return [String] path to the saved PNG file

# File lib/accessibility/graph.rb, line 208
def generate_png!
  build! unless @built
  file = Tempfile.new 'ax_elements_graph'
  file.write self.to_dot
  `dot -Tpng #{file.path} > #{file.path}.png`
  "#{file.path}.png"
end
to_dot() click to toggle source

Generate the ‘dot` graph code. You should take this string and feed it to the `dot` program to have it generate the graph.

@return [String]

# File lib/accessibility/graph.rb, line 195
def to_dot
  graph  = "digraph {\n"
  graph << nodes.map(&:to_dot).join(";\n")
  graph << "\n\n"
  graph << edges.map(&:to_dot).join(";\n")
  graph << "\n}\n"
end