Class: CPEE::Transformation::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/cpee/transformation/structures.rb

Overview

}}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



188
189
190
191
# File 'lib/cpee/transformation/structures.rb', line 188

def initialize
  @nodes = {}
  @links = []
end

Instance Attribute Details

#flowObject (readonly)

{{{



182
183
184
# File 'lib/cpee/transformation/structures.rb', line 182

def flow
  @flow
end

#nodesObject (readonly)

{{{



182
183
184
# File 'lib/cpee/transformation/structures.rb', line 182

def nodes
  @nodes
end

Instance Method Details



256
257
258
# File 'lib/cpee/transformation/structures.rb', line 256

def add_link(l)
  @links << l
end

#add_node(n, ntype = nil) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/cpee/transformation/structures.rb', line 235

def add_node(n,ntype=nil)
  if @nodes.include?(n.id)
    case ntype
      when :outgoing
        @nodes[n.id].inc_outgoing!
      when :incoming
        @nodes[n.id].inc_incoming!
    end
    if @nodes[n.id].label.nil? ||  @nodes[n.id].label.strip == ''
      @nodes[n.id].label = n.label
    end
    @nodes[n.id]
  else
    @nodes[n.id] = n
  end
end

#clean_up(&bl) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/cpee/transformation/structures.rb', line 206

def clean_up(&bl)
  selnodes = []
  @nodes.each do |k,n|
    ret = bl.call(n)
    selnodes << n if ret
  end
  selnodes.each do |n|
    if n.incoming > 1 || n.outgoing > 1
      raise "#{n.inspect} - not a simple node to remove"
    end
    to,from = nil
    @links.each do |f|
      to = f if f.to == n.id
      from = f if f.from == n.id
    end
    if to && from
      to.to = from.to
      @links.delete(from)
      @nodes.delete(n.id)
    else
      raise "#{n.inspect} - could not remove flow"
    end
  end
end

#find_node(niceid) ⇒ Object



184
185
186
# File 'lib/cpee/transformation/structures.rb', line 184

def find_node(niceid)
  @nodes.find{|k,v| v.niceid == niceid }
end

#find_script_id(s) ⇒ Object



231
232
233
# File 'lib/cpee/transformation/structures.rb', line 231

def find_script_id(s)
  @nodes.find_all{|k,n| n.script_id == s}
end


252
253
254
# File 'lib/cpee/transformation/structures.rb', line 252

def link(f,t)
  @links.find{ |x| x.from == f && x.to == t }
end

#next_node(from) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/cpee/transformation/structures.rb', line 265

def next_node(from)
  if (nodes = next_nodes(from)).length == 1
    nodes.first
  else
    raise "#{from.inspect} - multiple outgoing connections"
  end
end

#next_nodes(from) ⇒ Object



260
261
262
263
# File 'lib/cpee/transformation/structures.rb', line 260

def next_nodes(from)
  links = @links.find_all { |x| x.from == from.id }
  links.map{|x| @nodes[x.to] }
end

#to_sObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cpee/transformation/structures.rb', line 193

def to_s
  s = ''
  s << "Nodes:\n"
  @nodes.each do |k,v|
    s << "  #{v.niceid} -> #{v.id}, #{v.label}\n"
  end
  s << "Links:\n"
  @links.each do |e|
    s << "  #{e.from} -> #{e.to} #{e.condition ? "(#{e.condition})" : ''}\n"
  end
  s
end