Class: Coopy::Alignment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAlignment

Returns a new instance of Alignment.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/coopy/alignment.rb', line 7

def initialize
  @map_a2b = {} # Map<Int,Int>
  @map_b2a = {} # Map<Int,Int>
  @ha = @hb = 0
  @map_count = 0
  @reference = nil
  @meta = nil
  @order_cache_has_reference = false
  @ia = 0
  @ib = 0
end

Instance Attribute Details

#metaObject

Alignment



5
6
7
# File 'lib/coopy/alignment.rb', line 5

def meta
  @meta
end

#referenceObject

Alignment



4
5
6
# File 'lib/coopy/alignment.rb', line 4

def reference
  @reference
end

Instance Method Details

#a2b(a) ⇒ Object



58
59
60
# File 'lib/coopy/alignment.rb', line 58

def a2b(a)
  @map_a2b[a]
end

#add_index_columns(unit) ⇒ Object



47
48
49
50
51
52
# File 'lib/coopy/alignment.rb', line 47

def add_index_columns(unit)
  if @index_columns.nil?
    @index_columns = []
  end
  @index_columns << unit
end

#b2a(b) ⇒ Object



62
63
64
# File 'lib/coopy/alignment.rb', line 62

def b2a(b)
  @map_b2a[b]
end

#countObject



66
67
68
# File 'lib/coopy/alignment.rb', line 66

def count
  @map_count
end

#get_index_columnsObject



54
55
56
# File 'lib/coopy/alignment.rb', line 54

def get_index_columns
  @index_columns
end

#get_sourceObject



87
88
89
# File 'lib/coopy/alignment.rb', line 87

def get_source
  @ta
end

#get_source_headerObject



95
96
97
# File 'lib/coopy/alignment.rb', line 95

def get_source_header
  @ia
end

#get_targetObject



91
92
93
# File 'lib/coopy/alignment.rb', line 91

def get_target
  @tb
end

#get_target_headerObject



99
100
101
# File 'lib/coopy/alignment.rb', line 99

def get_target_header
  @ib
end

#hbObject



37
38
39
# File 'lib/coopy/alignment.rb', line 37

def hb
  @hb
end

#headers(ia, ib) ⇒ Object



29
30
31
32
# File 'lib/coopy/alignment.rb', line 29

def headers(ia, ib)
  @ia = ia
  @ib = ib
end


41
42
43
44
45
# File 'lib/coopy/alignment.rb', line 41

def link(a, b)
  @map_a2b[a] = b
  @map_b2a[b] = a
  @map_count+=1
end

#range(ha, hb) ⇒ Object



19
20
21
22
# File 'lib/coopy/alignment.rb', line 19

def range(ha, hb)
  @ha = ha
  @hb = hb
end

#set_rowlike(flag) ⇒ Object



34
35
# File 'lib/coopy/alignment.rb', line 34

def set_rowlike(flag)
end

#tables(ta, tb) ⇒ Object



24
25
26
27
# File 'lib/coopy/alignment.rb', line 24

def tables(ta, tb)
  @ta = ta
  @tb = tb
end

#to_orderObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/coopy/alignment.rb', line 74

def to_order
  if @order_cache
    if @reference
      if !@order_cache_has_reference
        @order_cache = nil
      end
    end
  end
  @order_cache = to_order_3 if @order_cache.nil?
  @order_cache_has_reference = true if reference
  @order_cache
end

#to_order_3Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/coopy/alignment.rb', line 103

def to_order_3
  ref = @reference
  if ref.nil?
    ref = Coopy::Alignment.new
    ref.range(@ha,@ha)
    ref.tables(@ta,@ta)
    (0...@ha).each do |i|
      ref.link(i,i)
    end
  end
  order = Coopy::Ordering.new
  if @reference.nil?
    order.ignore_parent
  end
  xp = 0
  xl = 0
  xr = 0
  hp = @ha
  hl = ref.hb
  hr = @hb
  vp = {}
  vl = {}
  vr = {}
  (0...hp).each { |i| vp[i] = i }
  (0...hl).each { |i| vl[i] = i }
  (0...hr).each { |i| vr[i] = i }
  ct_vp = hp
  ct_vl = hl
  ct_vr = hr
  prev  = -1
  ct    = 0
  max_ct = (hp+hl+hr)*10
  while (ct_vp>0 || 
         ct_vl>0 || 
         ct_vr>0) do
    ct+=1
    if ct>max_ct
      puts("Ordering took too long, something went wrong")
      break
    end
    xp = 0 if (xp>=hp)
    xl = 0 if (xl>=hl)
    xr = 0 if (xr>=hr)
    if xp<hp && ct_vp>0
      if a2b(xp).nil? && ref.a2b(xp).nil?
        if vp.has_key?(xp)
          order.add(-1,-1,xp)
          prev = xp
          vp.remove(xp)
          ct_vp-=1
        end
        xp+=1
        next
      end
    end
    zl = nil
    zr = nil
    if xl<hl && ct_vl>0
      zl = ref.b2a(xl)
      if zl.nil?
        if vl.has_key?(xl)
          order.add(xl,-1,-1)
          vl.remove(xl)
          ct_vl-=1
        end
        xl+=1
        next
      end
    end
    if xr<hr && ct_vr>0
      zr = b2a(xr)
      if zr.nil?
        if vr.has_key?(xr)
          order.add(-1,xr,-1)
          vr.delete(xr)
           ct_vr-=1
        end
        xr+=1
        next
      end
    end
    if zl
      if a2b(zl).nil?
        # row deleted in remote
        if vl.has_key?(xl)
          order.add(xl,-1,zl)
          prev = zl
          vp.delete(zl)
          ct_vp-=1
          vl.delete(xl)
          ct_vl-=1
          xp = zl+1
        end
        xl+=1
        next
      end
    end
    if zr
      if ref.a2b(zr).nil?
        # row deleted in local
        if vr.has_key?(xr)
          order.add(-1,xr,zr)
          prev = zr
          vp.remove(zr)
          ct_vp-=1
          vr.remove(xr)
          ct_vr-=1
          xp = zr+1
        end
        xr+=1
        next
      end
    end
    if zl && zr && a2b(zl) && ref.a2b(zr)
      # we have a choice of order
      # local thinks zl should come next
      # remote thinks zr should come next
      if zl==prev+1 || zr!=prev+1
        if vr.has_key?(xr)
          order.add(ref.a2b(zr),xr,zr)
          prev = zr
          vp.delete(zr)
          ct_vp-=1
          vl.delete(ref.a2b(zr))
          ct_vl-=1
          vr.delete(xr)
          ct_vr-=1
          xp = zr+1
          xl = ref.a2b(zr)+1
        end
        xr+=1
        next
      else
        if vl.has_key?(xl)
          order.add(xl,a2b(zl),zl)
          prev = zl
          vp.remove(zl)
          ct_vp-=1
          vl.remove(xl)
          ct_vl-=1
          vr.remove(a2b(zl))
          ct_vr-=1
          xp = zl+1
          xr = a2b(zl)+1
        end
        xl+=1
        next
      end
    end
    xp+=1
    xl+=1
    xr+=1
  end
  return order
end

#to_sObject



70
71
72
# File 'lib/coopy/alignment.rb', line 70

def to_s
  map_a2b.to_s
end