Class: GRAPH::Graph
- Inherits:
-
Object
- Object
- GRAPH::Graph
- Defined in:
- lib/sexpir/graph.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
Instance Method Summary collapse
- #<<(node) ⇒ Object
- #connect(source, sink, infos = {}) ⇒ Object
- #connected?(source, sink) ⇒ Boolean
- #delete(node) ⇒ Object
- #each_edge(&block) ⇒ Object
- #each_node(&block) ⇒ Object
- #edges ⇒ Object
- #include?(node) ⇒ Boolean
-
#initialize(name) ⇒ Graph
constructor
A new instance of Graph.
- #save_as(dotfilename) ⇒ Object
- #size ⇒ Object
- #to_dot ⇒ Object
- #unconnect(source, sink) ⇒ Object
Constructor Details
#initialize(name) ⇒ Graph
Returns a new instance of Graph.
8 9 10 11 |
# File 'lib/sexpir/graph.rb', line 8 def initialize name @name=name @nodes=[] end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/sexpir/graph.rb', line 7 def name @name end |
#nodes ⇒ Object
Returns the value of attribute nodes.
7 8 9 |
# File 'lib/sexpir/graph.rb', line 7 def nodes @nodes end |
Instance Method Details
#<<(node) ⇒ Object
13 14 15 |
# File 'lib/sexpir/graph.rb', line 13 def << node @nodes << node end |
#connect(source, sink, infos = {}) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/sexpir/graph.rb', line 25 def connect source,sink,infos={} raise "source nil" unless source raise "sink nil" unless sink @nodes << source unless @nodes.include? source @nodes << sink unless @nodes.include? sink source.to(sink,infos) end |
#connected?(source, sink) ⇒ Boolean
33 34 35 |
# File 'lib/sexpir/graph.rb', line 33 def connected? source,sink source.succ_edges.select{|edge| edge.sink==sink}.any? end |
#delete(node) ⇒ Object
42 43 44 45 46 |
# File 'lib/sexpir/graph.rb', line 42 def delete node node.preds.each{|pred| unconnect(pred,node)} node.succs.each{|succ| unconnect(node,succ)} @nodes.delete(node) end |
#each_edge(&block) ⇒ Object
52 53 54 |
# File 'lib/sexpir/graph.rb', line 52 def each_edge &block edges.each(&block) end |
#each_node(&block) ⇒ Object
48 49 50 |
# File 'lib/sexpir/graph.rb', line 48 def each_node &block @nodes.each(&block) end |
#edges ⇒ Object
56 57 58 |
# File 'lib/sexpir/graph.rb', line 56 def edges @nodes.collect{|n| n.succ_edges}.flatten end |
#include?(node) ⇒ Boolean
21 22 23 |
# File 'lib/sexpir/graph.rb', line 21 def include? node @nodes.include? node end |
#save_as(dotfilename) ⇒ Object
77 78 79 80 |
# File 'lib/sexpir/graph.rb', line 77 def save_as dotfilename dot_code||=to_dot() dot_code.save_as dotfilename end |
#size ⇒ Object
17 18 19 |
# File 'lib/sexpir/graph.rb', line 17 def size @nodes.size end |
#to_dot ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sexpir/graph.rb', line 60 def to_dot dot_code=Code.new dot_code << "digraph #{name} {" dot_code.indent=2 each_node do |node| dot_code << "#{node.object_id};" end each_edge do |edge| label=edge.infos.to_s dot_code << "#{edge.object_id} -> #{edge.object_id} [label=\"#{label}\"];" end dot_code.indent=0 dot_code << "}" return dot_code end |
#unconnect(source, sink) ⇒ Object
37 38 39 40 |
# File 'lib/sexpir/graph.rb', line 37 def unconnect source,sink source.succ_edges.delete_if{|edge| edge.sink==sink} sink.pred_edges.delete_if{|edge| edge.source==source} end |