Class: InventoryRefresh::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory_refresh/graph.rb,
lib/inventory_refresh/graph/topological_sort.rb

Direct Known Subclasses

InventoryCollection::Graph

Defined Under Namespace

Classes: TopologicalSort

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ Graph

Returns a new instance of Graph.

Parameters:



8
9
10
11
12
13
14
# File 'lib/inventory_refresh/graph.rb', line 8

def initialize(nodes)
  @nodes       = nodes
  @edges       = []
  @fixed_edges = []

  construct_graph!(@nodes)
end

Instance Attribute Details

#edgesObject

Returns the value of attribute edges.



5
6
7
# File 'lib/inventory_refresh/graph.rb', line 5

def edges
  @edges
end

#fixed_edgesObject

Returns the value of attribute fixed_edges.



5
6
7
# File 'lib/inventory_refresh/graph.rb', line 5

def fixed_edges
  @fixed_edges
end

#nodesObject

Returns the value of attribute nodes.



5
6
7
# File 'lib/inventory_refresh/graph.rb', line 5

def nodes
  @nodes
end

Instance Method Details

#to_graphviz(layers: nil) ⇒ String

Returns graph in GraphViz format, as a string. So it can be displayed.

Parameters:

  • layers (Array<Array>) (defaults to: nil)

    Array of arrays(layers) of InventoryCollection objects

Returns:

  • (String)

    Graph in GraphViz format



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/inventory_refresh/graph.rb', line 20

def to_graphviz(layers: nil)
  node_names = friendly_unique_node_names
  s = []

  s << "digraph {"
  (layers || [nodes]).each_with_index do |layer_nodes, i|
    s << "  subgraph cluster_#{i} {  label = \"Layer #{i}\";" unless layers.nil?

    layer_nodes.each do |n|
      s << "    #{node_names[n]}; \t// #{n.inspect}"
    end

    s << "  }" unless layers.nil?
  end

  s << "  // edges:"
  edges.each do |from, to|
    s << "  #{node_names[from]} -> #{node_names[to]};"
  end
  s << "}"
  "#{s.join("\n")}\n"
end