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.
163 164 165 166 167 168 169 170 171 172 |
# 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 |
Instance Attribute Details
#edges ⇒ Array<Accessibility::Graph::Edge> (readonly)
List of edges in the graph.
160 161 162 |
# File 'lib/accessibility/graph.rb', line 160 def edges @edges end |
#nodes ⇒ Array<Accessibility::Graph::Node> (readonly)
List of nodes in the UI hierarchy.
154 155 156 |
# File 'lib/accessibility/graph.rb', line 154 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.
180 181 182 183 184 185 186 187 188 |
# 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! ⇒ String
Generate the PNG file for the graph and save it to a temporary file. The path to the temporary file will be returned.
208 209 210 211 212 213 214 |
# 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 ⇒ String
Generate the dot
graph code. You should take this string and
feed it to the dot
program to have it generate the graph.
195 196 197 198 199 200 201 |
# 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 |