Class: Rattler::Util::GraphViz::DigraphBuilder

Inherits:
Object
  • Object
show all
Includes:
NodeBuilder
Defined in:
lib/rattler/util/graphviz/digraph_builder.rb

Overview

DigraphBuilder is used to build GraphViz objects representing trees of nodes.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeBuilder

#array_like?, #each_child_node_of, #node_label, #node_options, #node_shape, #record_like?

Constructor Details

#initialize(root, name = 'G') ⇒ DigraphBuilder

Create a new digraph builder for root.

Parameters:

  • root

    the root node

  • name (String) (defaults to: 'G')

    the name of the graph



26
27
28
29
30
31
# File 'lib/rattler/util/graphviz/digraph_builder.rb', line 26

def initialize(root, name='G')
  @root = root
  @g = ::GraphViz.digraph(name)
  @nodes = {}
  @node_serial = 0
end

Class Method Details

.digraph(root, name = 'G') ⇒ Object

Returns a new GraphViz digraph object representing root.

Parameters:

  • root

    the root node

  • name (String) (defaults to: 'G')

    the name of the graph

Returns:

  • a new GraphViz digraph object representing root



18
19
20
# File 'lib/rattler/util/graphviz/digraph_builder.rb', line 18

def self.digraph(root, name='G')
  self.new(root, name).digraph
end

Instance Method Details

#digraphObject

Returns a new GraphViz digraph object representing the root object.

Returns:

  • a new GraphViz digraph object representing the root object



34
35
36
37
38
39
# File 'lib/rattler/util/graphviz/digraph_builder.rb', line 34

def digraph
  @digraph ||= begin
    node(@root)
    @g
  end
end

#node(o) ⇒ GraphViz::Node

Return a GraphViz::Node object for o. Multiple requests with the same object return the same node object.

Parameters:

  • o

    an object

Returns:

  • (GraphViz::Node)

    a node object for o



46
47
48
49
50
51
52
53
# File 'lib/rattler/util/graphviz/digraph_builder.rb', line 46

def node(o)
  @nodes.fetch(o.object_id) do
    new_node = @g.add_nodes new_node_name, node_options(o)
    @nodes[o.object_id] = new_node
    each_child_node_of(o) {|_| new_node << node(_) }
    new_node
  end
end