Class: Accessibility::Graph
- Inherits:
-
Object
- Object
- Accessibility::Graph
- Defined in:
- lib/accessibility/graph.rb
Overview
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.
Defined Under Namespace
Instance Attribute Summary collapse
-
#edges ⇒ Array<Accessibility::Graph::Edge>
readonly
List of edges in the graph.
-
#nodes ⇒ Array<Accessibility::Graph::Node>
readonly
List of nodes in the UI hierarchy.
Instance Method Summary collapse
-
#build! ⇒ Object
Construct the list of nodes and edges for the graph.
-
#generate_png! ⇒ String
Generate the PNG file for the graph and save it to a temporary file.
-
#initialize(root) ⇒ Graph
constructor
A new instance of Graph.
-
#to_dot ⇒ String
Generate the
dot
graph code.
Constructor Details
#initialize(root) ⇒ Graph
Returns a new instance of Graph.
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/accessibility/graph.rb', line 159 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 |
Instance Attribute Details
#edges ⇒ Array<Accessibility::Graph::Edge> (readonly)
List of edges in the graph.
156 157 158 |
# File 'lib/accessibility/graph.rb', line 156 def edges @edges end |
#nodes ⇒ Array<Accessibility::Graph::Node> (readonly)
List of nodes in the UI hierarchy.
150 151 152 |
# File 'lib/accessibility/graph.rb', line 150 def nodes @nodes end |
Instance Method Details
#build! ⇒ Object
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.
176 177 178 179 180 181 182 183 184 |
# File 'lib/accessibility/graph.rb', line 176 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! ⇒ String
Generate the PNG file for the graph and save it to a temporary file. The path to the temporary file will be returned.
204 205 206 207 208 209 210 |
# File 'lib/accessibility/graph.rb', line 204 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 ⇒ String
Generate the dot
graph code. You should take this string and
feed it to the dot
program to have it generate the graph.
191 192 193 194 195 196 197 |
# File 'lib/accessibility/graph.rb', line 191 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 |