Class: CPEE::Transformation::Traces

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

Overview

}}}

Instance Method Summary collapse

Instance Method Details

#add_breaks(context, front = true) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
# File 'lib/cpee/transformation/structures.rb', line 410

def add_breaks(context,front=true)
  trueloops = self.find_all{ |t| t.last == t.first }.length
  tb = Break.new(context)
  if trueloops == self.length
    self << (front ? [nil,tb] : [tb,nil])
  else
    self.each do |t|
      t << tb unless t.last == t.first ### an explicit break
    end
  end
end

#all_front?Boolean

find all loops. Loops need to have the same second to last

Returns:

  • (Boolean)


392
393
394
395
396
397
398
399
# File 'lib/cpee/transformation/structures.rb', line 392

def all_front?
  lo = Traces.new self.find_all{ |t| t.first == t.last }
  lo.uniq!
  num = 0
  search = lo.first[-2]
  self.each{|n| num += 1 if n.include?(search) }
  num == self.length
end

#all_loops?Boolean

first and last the same in all traces

Returns:

  • (Boolean)


386
387
388
389
390
# File 'lib/cpee/transformation/structures.rb', line 386

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

#all_tail?Boolean

second to last the same as in all traces second to last because last must be the same as current

Returns:

  • (Boolean)


403
404
405
406
407
408
# File 'lib/cpee/transformation/structures.rb', line 403

def all_tail?
  num = 0
  search = self.first[-2]
  self.each{|n| num += 1 if n[-2] == search }
  num == self.length
end

#eliminate(loops) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/cpee/transformation/structures.rb', line 469

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
    cutted = self[i].shift(maxcut)
    loops << cutted if cutted.length > 1 ### if only the loop node is left, no need to attach
  end
end

#eliminate_front(loops) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/cpee/transformation/structures.rb', line 436

def eliminate_front(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
    self[i].shift(maxcut)
  end
end

#empty!Object



321
322
323
# File 'lib/cpee/transformation/structures.rb', line 321

def empty!
  self.delete_if{true}
end

#extendObject



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/cpee/transformation/structures.rb', line 504

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

  # 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
      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
end

#find_endnodeObject



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/cpee/transformation/structures.rb', line 530

def find_endnode
  # supress loops
  trcs = self.dup
  # dangerous TODO
  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)


362
363
364
# File 'lib/cpee/transformation/structures.rb', line 362

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

#first_nodeObject



329
330
331
# File 'lib/cpee/transformation/structures.rb', line 329

def first_node
  self.first.first
end

#include_in_all?(e) ⇒ Boolean

Returns:

  • (Boolean)


379
380
381
382
383
# File 'lib/cpee/transformation/structures.rb', line 379

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

#incoming(node) ⇒ Object



370
371
372
373
374
375
376
377
# File 'lib/cpee/transformation/structures.rb', line 370

def incoming(node)
  tcount = 1
  self.each do |t|
    break if t.length == 1
    tcount += 1 if t.last == node
  end
  tcount
end

#initialize_copy(other) ⇒ Object

{{{



302
303
304
305
# File 'lib/cpee/transformation/structures.rb', line 302

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

#legend(nid = true) ⇒ Object



340
341
342
343
344
345
# File 'lib/cpee/transformation/structures.rb', line 340

def legend(nid=true)
  ret = "Legend:\n"
  a = self.flatten.uniq
  a.each {|n| ret << "  " + (nid ? n.niceid.to_s : n.id.to_s) + ' ' + n.type.to_s + ' ' + n.label.to_s + "\n" }
  ret
end

#loops_and_partial_loopsObject



427
428
429
430
431
432
433
434
# File 'lib/cpee/transformation/structures.rb', line 427

def loops_and_partial_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!
  lo
end

#loops_onlyObject



422
423
424
425
426
# File 'lib/cpee/transformation/structures.rb', line 422

def loops_only
  lo = Traces.new self.find_all{ |t| t.first == t.last }
  lo.uniq!
  lo
end

#pop_all(what = nil) ⇒ Object



354
355
356
357
358
359
360
# File 'lib/cpee/transformation/structures.rb', line 354

def pop_all(what=nil)
  if what.nil?
    self.each{ |tr| tr.pop }
  else
    self.each{ |tr| tr.pop if tr.last == what }
  end
end

#remove(trcs) ⇒ Object



307
308
309
310
311
# File 'lib/cpee/transformation/structures.rb', line 307

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

#remove_by_endnode(enode) ⇒ Object



312
313
314
315
316
317
318
319
# File 'lib/cpee/transformation/structures.rb', line 312

def remove_by_endnode(enode)
  self.delete_if do |t|
    t[0] != enode
  end
  # self.delete_if do |t|
  #   t[0] == enode && t.length <= 2
  # end
end

#remove_emptyObject



325
326
327
# File 'lib/cpee/transformation/structures.rb', line 325

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

#same_firstObject



366
367
368
# File 'lib/cpee/transformation/structures.rb', line 366

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

#second_nodesObject



332
333
334
# File 'lib/cpee/transformation/structures.rb', line 332

def second_nodes
  self.map { |t| t.length > 1 ? t[1] : t[0] }
end

#segment_by(endnode) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/cpee/transformation/structures.rb', line 549

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

#shift_allObject



351
352
353
# File 'lib/cpee/transformation/structures.rb', line 351

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

#shortestObject



336
337
338
# File 'lib/cpee/transformation/structures.rb', line 336

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

#to_s(nid = true) ⇒ Object



347
348
349
# File 'lib/cpee/transformation/structures.rb', line 347

def to_s(nid = true)
  "TRACES: " + self.collect { |t| t.empty? ? '' : t.collect{|n| ("%2d" % (nid ? n.niceid : n.id.to_i)) + "(i#{n.incoming},o#{n.outgoing})" }.join('') }.join("\n        ")
end