Class: Dsl::Graph::LoadGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/graph/load.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ LoadGraph

Returns a new instance of LoadGraph.



8
9
10
# File 'lib/dsl/graph/load.rb', line 8

def initialize(filename)
  @filename = filename
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dsl/graph/load.rb', line 12

def call
  data = YAML.load_file(@filename)
  graph_data = data["graph"]

  graph = Graph.new(graph_data["label"])
  nodes_by_label = {}

  graph_data["nodes"].each do |node_data|
    label = node_data["label"]
    nodes_by_label[label] = graph.add_node(label)
  end

  graph_data["edges"].each do |edge_data|
    from_node = nodes_by_label[edge_data["from"]]
    to_node   = nodes_by_label[edge_data["to"]]
    label     = edge_data["label"]
    graph.add_edge(from_node, to_node, label)
  end

  graph
end