Class: CPEE::ProcessTransformation::Traces

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

Overview

}}}

Instance Method Summary collapse

Instance Method Details

#add_breaksObject



323
324
325
326
327
328
329
330
331
332
# File 'lib/cpee/processtransformation/structures.rb', line 323

def add_breaks
  trueloops = self.find_all{ |t| t.last == t.first }.length
  if trueloops == self.length
    self << [self.first_node] ### the blank conditional so that we get a break
  else
    self.each do |t|
      t << Break.new(1) unless t.last == t.first ### an explicit break
    end
  end  
end

#all_loops?Boolean

Returns:

  • (Boolean)


316
317
318
319
320
# File 'lib/cpee/processtransformation/structures.rb', line 316

def all_loops?
  num = 0
  self.each{|n| num += 1 if n.first == n.last }
  num == self.length
end

#eliminate(loops) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/cpee/processtransformation/structures.rb', line 342

def eliminate(loops)
  ### find nested loops
  self.each_with_index do |t,i|
    maxcut = 0
    ### find out which common parts the traces share with theloops
    loops.each do |l|
      maxcut.upto(l.length) do |i|
        maxcut = i if t[0...i] == l[0...i]
      end
    end
    ### in case of nested loop (common part occurs at end of loop), include the whole
    0.upto (maxcut-1) do |j|
      if self[i][j] == self[i].last
        loops << self[i].shift(self[i].length)
      end
    end  
  end
  loops.uniq!
  loops.remove_empty
  self.remove_empty

  ### cut from non-nested loops
  self.each_with_index do |t,i|
    maxcut = 0
    ### find out which common parts the traces share with theloops
    loops.each do |l|
      maxcut.upto(l.length) do |i|
        maxcut = i if t[0...i] == l[0...i]
      end
    end
    loops << self[i].shift(maxcut)
  end
end

#empty!Object



267
268
269
# File 'lib/cpee/processtransformation/structures.rb', line 267

def empty!
  self.delete_if{true}
end

#extendObject



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/cpee/processtransformation/structures.rb', line 376

def extend
  # find largest common
  max = nil
  sh = self.shortest
  sh = sh[0..-2] if sh.first == sh.last
  sh.each_with_index do |e,i|
    if self.same_position_in_all?(e,i)
      max = e
    else  
      break
    end
  end

  # if last is the largest common do nothing
  # else append from last to largest common
  self.each do |t|
    unless t.last == max
      last = t.last
      t.last.incoming = 1
      if t.index(last) && t.index(max)
        (t.index(last) + 1).upto(t.index(max)) do |i|
          t << t[i]
        end 
      end
    end  
  end

  max.incoming = self.length
  max
end

#find_endnodeObject



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/cpee/processtransformation/structures.rb', line 414

def find_endnode
  # supress loops
  trcs = self.dup
  trcs.delete_if { |t| t.uniq.length < t.length }

  # find common node (except loops)
  enode = nil
  unless trcs.empty?
    trcs.first.each do |n|
      if trcs.include_in_all?(n)
        enode = n
        break
      end  
    end
  end  
  enode
end

#finished?Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/cpee/processtransformation/structures.rb', line 297

def finished?
  self.reduce(0){|sum,t| sum += t.length} == 0
end

#first_nodeObject



275
276
277
# File 'lib/cpee/processtransformation/structures.rb', line 275

def first_node
  self.first.first
end

#include_in_all?(e) ⇒ Boolean

Returns:

  • (Boolean)


305
306
307
308
309
# File 'lib/cpee/processtransformation/structures.rb', line 305

def include_in_all?(e)
  num = 0
  self.each{|n| num += 1 if n.include?(e)} 
  num == self.length
end

#initialize_copy(other) ⇒ Object

{{{



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

def initialize_copy(other)
 super
 self.map!{ |t| t.dup }
end

#loopsObject



334
335
336
337
338
339
340
# File 'lib/cpee/processtransformation/structures.rb', line 334

def loops
  lo = Traces.new self.find_all{ |t| t.first == t.last }
  self.each do |t|
    lo << t if lo.second_nodes.include?(t[1])
  end
  lo.uniq
end

#pop_allObject



293
294
295
# File 'lib/cpee/processtransformation/structures.rb', line 293

def pop_all
  self.each{ |tr| tr.pop }
end

#remove(trcs) ⇒ Object



261
262
263
264
265
# File 'lib/cpee/processtransformation/structures.rb', line 261

def remove(trcs)
  trcs.each do |t|
    self.delete(t)
  end  
end

#remove_emptyObject



271
272
273
# File 'lib/cpee/processtransformation/structures.rb', line 271

def remove_empty
  self.delete_if{|t| t.empty? }
end

#same_firstObject



301
302
303
# File 'lib/cpee/processtransformation/structures.rb', line 301

def same_first
  (n = self.map{|t| t.first }.uniq).length == 1 ? n.first : nil
end

#same_position_in_all?(e, i) ⇒ Boolean

Returns:

  • (Boolean)


310
311
312
313
314
# File 'lib/cpee/processtransformation/structures.rb', line 310

def same_position_in_all?(e,i)
  num = 0
  self.each{|n| num += 1 if n[i] == e}
  num == self.length
end

#second_nodesObject



278
279
280
# File 'lib/cpee/processtransformation/structures.rb', line 278

def second_nodes
  self.map { |t| t[1] }
end

#segment_by(endnode) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/cpee/processtransformation/structures.rb', line 432

def segment_by(endnode)
  # cut shit until common node, return the shit you cut away
  tracesgroup = self.group_by{|t| t.first}.map do |k,trace|
    coltrace = trace.map do |t|
      # slice upto common node, collect the sliced away part
      len = t.index(endnode)
      if len
        cut = t.slice!(0...len)
        cut << t.first
      else # if endnode is nil, then return the whole
        t
      end
    end.uniq
    Traces.new(coltrace)
  end
  [tracesgroup,endnode]
end

#segment_by_loops(loops) ⇒ Object



407
408
409
410
411
412
# File 'lib/cpee/processtransformation/structures.rb', line 407

def segment_by_loops(loops)
  # supress loops
  self.delete_if { |t| loops.include?(t) }
  self.eliminate(loops)
  loops.extend
end

#shift_allObject



290
291
292
# File 'lib/cpee/processtransformation/structures.rb', line 290

def shift_all
  self.each{ |tr| tr.shift }
end

#shortestObject



282
283
284
# File 'lib/cpee/processtransformation/structures.rb', line 282

def shortest
  self.min_by{|e|e.length}
end

#to_sObject



286
287
288
# File 'lib/cpee/processtransformation/structures.rb', line 286

def to_s
  "TRACES: " + self.collect { |t| t.empty? ? '' : t.collect{|n| "%2d" % n.niceid }.join('') }.join("\n        ")
end