Module: TraceVisualization::Repetitions::Concatenation

Defined in:
lib/trace_visualization/repetitions/concatenation.rb

Class Method Summary collapse

Class Method Details

.concat_condition(left, right, delta, positions_min_size) ⇒ Object

Condition for potential concatenation



83
84
85
86
87
# File 'lib/trace_visualization/repetitions/concatenation.rb', line 83

def self.concat_condition(left, right, delta, positions_min_size)
  delta >= 0 && left.id != right.id && 
    left.positions_size  >= positions_min_size && 
    right.positions_size >= positions_min_size
end

.create_repetition(left, right, delta, lps, rps) ⇒ Object

Create repetition

Parameters:

  • left (TraceVisualization::Repetition)

    left parent

  • right (TraceVisualization::Repetition)

    right parent

  • delta
  • lps
  • rps


123
124
125
126
127
128
129
130
131
132
133
# File 'lib/trace_visualization/repetitions/concatenation.rb', line 123

def self.create_repetition(left, right, delta, lps, rps)
  r = left.class.new(left.length + right.length + delta, lps, rps)

  r.k          = left.k + right.k + delta
  r.pcount     = left.pcount + right.pcount
  r.left       = left
  r.right      = right
  r.strict_ids = left.strict_ids + right.strict_ids

  r
end

.process(context, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/trace_visualization/repetitions/concatenation.rb', line 7

def self.process(context, options = {})
  Utils.set_default_options(options, { :positions_min_size => 3, :k => 3 })
  k = options[:k]
  
  result = []
  
  useful_cnt = 0

  pairs_cnt = {}
  context..each do |item|
    for i in 0 ... item.size
      for j in i + 1 ... item.size
        left, right = item[i][0], item[j][0]
        delta = k - left.k - right.k

        next if not concat_condition(left, right, delta, options[:positions_min_size])

        key = (left.id << 32) + right.id
        val = (pairs_cnt[key] || 0) + 1
        pairs_cnt[key] = val
        next if val != options[:positions_min_size]

        lps, rps = process_common_positions(left, right, delta, context)

        if lps.size >= options[:positions_min_size]
          result << create_repetition(left, right, delta, lps, rps)
        end

        useful_cnt += 1        
      end
    end
  end
  
  options[:counter] << [k, useful_cnt] if options[:counter]
  
  process_new_repetitions(result, context)
  
  context.repetitions.concat(result)
end

.process_common_positions(left, right, delta, context) ⇒ Object

Attention Position arrays are modified in place which can lead to side effects. Don’t send left == right!



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/trace_visualization/repetitions/concatenation.rb', line 91

def self.process_common_positions(left, right, delta, context)
  lr_pos = left.left_positions
  lr_pos.collect! { |pos| pos + left.length + delta }

  rr_pos = right.left_positions
  
  cpr = lr_pos & rr_pos
  cpl = cpr.collect { |pos| pos - left.length - delta }
  
  idx = 0
  while idx < cpr.size
    xxx = context.mapping[cpl[idx] + left.length ... cpr[idx]]
    xxx = xxx.join if xxx.instance_of? Array
    if xxx.scan(TraceVisualization::FORBIDDEN_CHARS).size != 0
      cpr.delete_at(idx)
      cpl.delete_at(idx)
    else
      idx += 1
    end
  end

  lr_pos.collect! { |lpos| lpos - left.length - delta }

  [cpl, cpr]
end

.process_full_search(rs, k, context, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/trace_visualization/repetitions/concatenation.rb', line 51

def self.process_full_search(rs, k, context, options = {})
  opts = {
    :positions_min_size => 3
  }.merge options
  
  result = []
  
  useful_cnt = 0

  for left in rs
    for right in rs
      delta = k - left.k - right.k
      next if not concat_condition(left, right, delta, options[:positions_min_size])

      # @@processed_path.add(key(left, right, delta))  

      lps, rps = process_common_positions(left, right, delta, context)

      if lps.size >= options[:positions_min_size]
        result << create_repetition(left, right, delta, lps, rps)
      end

      useful_cnt += 1
    end
  end

  puts "Total: #{rs.size ** 2} #{useful_cnt} #{result.size}"
  
  rs.concat(result)
end

.process_new_repetitions(new_repetitions, context) ⇒ Object



47
48
49
# File 'lib/trace_visualization/repetitions/concatenation.rb', line 47

def self.process_new_repetitions(new_repetitions, context)
  context.(new_repetitions)
end