Class: YaGraph::Graph

Inherits:
Element show all
Defined in:
lib/yagraphlib.rb

Direct Known Subclasses

SubGraph

Constant Summary collapse

RANKDIR_LR =
"LR"

Instance Method Summary collapse

Methods inherited from Element

#random_id, #uid

Constructor Details

#initializeGraph

Returns a new instance of Graph.



43
44
45
46
47
# File 'lib/yagraphlib.rb', line 43

def initialize()
  @nodes = {}
  @edges = {}
  @subgraphs = {}
end

Instance Method Details

#edge?(s, e) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/yagraphlib.rb', line 53

def edge?(s, e)
  @edges.any? do |k, v|
    v.start == s and v.finish == e
  end
end

#edges(edge) ⇒ Object



49
50
51
# File 'lib/yagraphlib.rb', line 49

def edges(edge)
  @edges[edge.uid] ||= edge
end

#initial_nodesObject



71
72
73
74
75
76
77
78
# File 'lib/yagraphlib.rb', line 71

def initial_nodes()
  initial = {}
  @nodes.each {|k, n| initial[k] = n}
  @edges.each do |k, e|
    initial.delete(e.finish.uid)
  end
  initial.values
end

#nodes(node) ⇒ Object



59
60
61
# File 'lib/yagraphlib.rb', line 59

def nodes(node)
  @nodes[node.uid] ||= node
end

#rankdir(mode) ⇒ Object



67
68
69
# File 'lib/yagraphlib.rb', line 67

def rankdir(mode)
  @rankdir = mode
end

#subgraph(subgraph) ⇒ Object



63
64
65
# File 'lib/yagraphlib.rb', line 63

def subgraph(subgraph)
  @subgraphs[subgraph.uid] ||= subgraph
end

#terminal_nodesObject



80
81
82
83
84
85
86
87
# File 'lib/yagraphlib.rb', line 80

def terminal_nodes()
  terminal = {}
  @nodes.each {|k, n| terminal[k] = n}
  @edges.each do |k, e|
    terminal.delete(e.start.uid)
  end
  terminal.values
end

#to_graphviz(out) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/yagraphlib.rb', line 89

def to_graphviz(out)
  out.puts("digraph main {\n")
  out.puts("  rankdir=#{@rankdir}") if @rankdir
  @subgraphs.each {|k, s| s.to_graphviz(out) }
  @nodes.each {|k, n| n.to_graphviz(out) }
  @edges.each {|k, e| e.to_graphviz(out) }
  out.puts("}")
end