Class: Graph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, underlying = RGL::DirectedAdjacencyGraph.new) ⇒ Graph

Returns a new instance of Graph.



6
7
8
9
10
11
# File 'lib/graph.rb', line 6

def initialize(config, underlying=RGL::DirectedAdjacencyGraph.new)
  @config = config
  @underlying = underlying
  @edge_properties = {}
  @node_properties = {}
end

Instance Attribute Details

#underlyingObject (readonly)

Returns the value of attribute underlying.



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

def underlying
  @underlying
end

Instance Method Details

#add_edge(from, to, opts) ⇒ Object



19
20
21
22
23
# File 'lib/graph.rb', line 19

def add_edge(from, to, opts)
  log("edge: #{from} -> #{to}")
  @underlying.add_edge(from, to)
  @edge_properties[[from, to]] = opts
end

#add_node(name, opts) ⇒ Object



13
14
15
16
17
# File 'lib/graph.rb', line 13

def add_node(name, opts)
  log("node: #{name}, opts: #{opts}")
  @underlying.add_vertex(name)
  @node_properties[name] = opts
end

#filter(source, destination) ⇒ Object



25
26
27
# File 'lib/graph.rb', line 25

def filter(source, destination)
  @underlying = GraphFilter.new(underlying).filter(source, destination)
end

#log(msg) ⇒ Object



37
38
39
# File 'lib/graph.rb', line 37

def log(msg)
  puts msg if @config.debug?
end

#output(renderer) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/graph.rb', line 29

def output(renderer)
  @underlying.each_vertex { |v| renderer.add_node(v, @node_properties[v] || {}) }
  @underlying.each_edge { |u, v|
    renderer.add_edge(u, v, opts(u, v))
  }
  renderer.output
end