Class: RuTu::Graph

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

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



130
131
132
133
# File 'lib/RuTu.rb', line 130

def initialize
  @nodes = [] #Set.new
  @edges = [] #Set.new
end

Instance Method Details

#add(value, node_class) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/RuTu.rb', line 135

def add(value, node_class)
  rtn = @nodes.find { |n| n.same_value?(value) }
  if not rtn
    node_class = NullNode if false == value
    rtn = node_class.new(self, value)
    @nodes << rtn
  end
  rtn
end

#add_edge(from, to, relation) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/RuTu.rb', line 144

def add_edge(from, to, relation)
  rtn = @edges.find { |e| e.same?(from, to, relation) }
  if not rtn
    rtn = Edge.new(from, to, relation)
    @edges << rtn
  end
  rtn
end

#closure(init_nodes, relations, depth = -1)) ⇒ Object

depth = 0 => no-op depth = -1 => no limit



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/RuTu.rb', line 155

def closure(init_nodes, relations, depth = -1)
  nodes = Array.new init_nodes
  s = 0
  e = nodes.size
  while s < e && depth != 0
    for i in (s ... e) do
      relations.each do |r|
        nodes |= [ nodes[i].send(r) ] if nodes[i].respond_to?(r)
      end
    end
    depth = depth - 1
    s = e
    e = nodes.size 
  end
  nodes
end

#to_dotObject



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/RuTu.rb', line 172

def to_dot
  to_set = Set.new
  return <<-"EOG"
digraph G {
#    rankdir=LR
  node [fontname=Luxi]
  edge [fontname=Luxi]
    #{ @nodes.map { |n| n.to_dot + "\n" } }
    #{ @edges.map { |e| e.to_dot(to_set) + "\n" } }
}
EOG
end