Module: Annotated

Defined in:
lib/rbbt/annotations.rb,
lib/rbbt/annotations/util.rb

Overview

{{{ ANNOTATED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#containerObject

Returns the value of attribute container.



9
10
11
# File 'lib/rbbt/annotations.rb', line 9

def container
  @container
end

#container_indexObject

Returns the value of attribute container_index.



9
10
11
# File 'lib/rbbt/annotations.rb', line 9

def container_index
  @container_index
end

Class Method Details

.flatten(array) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rbbt/annotations/util.rb', line 5

def self.flatten(array)
  return array unless Array === array and not array.empty?
  array.extend AnnotatedArray if Annotated === array
  return array.flatten if AnnotatedArray === array
  begin
    return array if array.compact.collect{|e| e.info }.uniq.length > 1
  rescue
    return array
  end
  array.compact.first.annotate(array.flatten).tap{|a| a.extend AnnotatedArray }
end

.load_entity(object, info) ⇒ Object



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
# File 'lib/rbbt/annotations/util.rb', line 17

def self.load_entity(object, info)
  annotation_types = info.delete(:annotation_types) || info.delete("annotation_types") || []
  annotation_types = annotation_types.split("|") if String === annotation_types

  return object if annotation_types.nil? or annotation_types.empty?

  annotated_array = false
  annotated_array = true if (info.delete(:annotated_array) || info.delete("annotated_array")).to_s == "true"
  entity_id = info.delete(:entity_id) || info.delete("entity_id")

  annotation_types.each do |mod|
    begin
      mod = Misc.string2const(mod) if String === mod
      object.extend mod unless mod === object
    rescue Exception
      Log.warn "Exception loading annotation into object: #{$!.message}"
    end
  end

  object.instance_variable_set(:@annotation_values, info) 

  object.instance_variable_set(:@id, entity_id) if entity_id

  object.extend AnnotatedArray if annotated_array and Array === object

  object
end

.load_hash(hash) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rbbt/annotations/util.rb', line 239

def self.load_hash(hash)
  if Array === hash
    hash.collect{|h| load_hash(h) }
  else
    literal = hash[:literal]
    info = hash[:info]
    info[:annotation_types].each do |type|
      type = Kernel.const_get(type) if String === type
      type.setup(literal, info) 
    end
    literal
  end
end

.load_info(fields, values) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbbt/annotations/util.rb', line 53

def self.load_info(fields, values)
  info = {}
  fields.each_with_index do |field,i|
    next if field == "literal"
    case field
    when "JSON"
      JSON.parse(values[i]).each do |key, value|
        info[key.to_sym] = value
      end
    when nil
      next
    else
      info[field.to_sym] = resolve_array(values[i])
    end
  end
  info
end

.load_tsv(tsv) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rbbt/annotations/util.rb', line 97

def self.load_tsv(tsv)
  tsv.with_unnamed do
    annotated_entities = tsv.collect do |id, values|
      Annotated.load_tsv_values(id, values, tsv.fields)
    end

    case tsv.key_field 
    when "List"
      annotated_entities.first
    else
      annotated_entities
    end
  end
end

.load_tsv_values(id, values, *fields) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rbbt/annotations/util.rb', line 71

def self.load_tsv_values(id, values, *fields)
  fields = fields.flatten
  literal_pos = fields.index "literal"

  object = case
           when literal_pos
             values[literal_pos].tap{|o| o.force_encoding(Encoding.default_external)}
           else
             id.dup
           end

  object = resolve_array(object)

  if Array === values.first
    Misc.zip_fields(values).collect do |v|
      info = load_info(fields, v)
    end
  else
    info = load_info(fields, values)
  end

  self.load_entity(object, info)

  object
end

.purge(object) ⇒ Object



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
# File 'lib/rbbt/annotations.rb', line 155

def self.purge(object)
  case object
  when Path
    object
  when String
    object.respond_to?(:clean_annotations) ?
      object.clean_annotations : 
      object
  when Array
    object.respond_to?(:clean_annotations) ?
      object.clean_annotations(true) : 
      object.inject([]){|acc,e| acc << Annotated.purge(e); acc}
  when TSV
    object.unnamed = true
    object
  when Hash
    new = {}
    object.each do |key, value|
      Annotated.purge key
      new[key] = Annotated.purge value
    end
    new
  else
    object
  end
end

.resolve_array(entry) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/rbbt/annotations/util.rb', line 45

def self.resolve_array(entry)
  if String === entry && entry =~ /^Array:/
    entry["Array:".length..-1].split("|")
  else
    entry
  end
end

.to_hash(e) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rbbt/annotations/util.rb', line 220

def self.to_hash(e)
  hash = {}
  if Array === e && AnnotatedArray === e
    hash[:literal] = Annotated.purge(e)
    hash[:info] = e.info
  elsif Array === e
    hash = e.collect do |_e|
      _hash = {}
      _hash[:literal] = _e.dup
      _hash[:info] = _e.info if _e.respond_to?(:info)
      _hash
    end
  else
    hash[:literal] = e.dup
    hash[:info] = e.info
  end
  hash
end

.tsv(annotations, *fields) ⇒ Object



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
# File 'lib/rbbt/annotations/util.rb', line 113

def self.tsv(annotations, *fields)
  return nil if annotations.nil?

  fields = case

           when ((fields.compact.empty?) && ! annotations.empty?)
             fields = AnnotatedArray === annotations ? annotations.annotations : annotations.compact.first.annotations
             fields << :annotation_types

           when (fields == [:literal] and ! annotations.compact.empty?)
             fields << :literal

           when (fields == [:all] && Annotated === annotations)
             annotation_names = annotations.annotations
             annotation_names += annotations.first.annotations if Annotated === annotations.first
             fields = [:annotation_types] + annotation_names.uniq
             fields << :annotated_array if AnnotatedArray === annotations
             fields << :literal

           when (fields == [:all] && ! annotations.compact.empty?)
             raise "Input array must be annotated or its elements must be" if not Annotated === annotations.compact.first and not Array === annotations.compact.first
             raise "Input array must be annotated or its elements must be. No double arrays of singly annotated entities." if not Annotated === annotations.compact.first and Array === annotations.compact.first
             fields = [:annotation_types] + (Annotated === annotations ? 
                                             annotations.annotations: 
                                             annotations.compact.first.annotations)
             fields << :literal

           when annotations.empty?
             [:annotation_types, :literal]

           else
             fields.flatten

           end

  fields = fields.collect{|f| f.to_s}.uniq

  case
  when (Annotated === annotations and not (AnnotatedArray === annotations and annotations.double_array))
    tsv = TSV.setup({}, :key_field => "List", :fields => fields, :type => :list, :unnamed => true)

    annot_id = annotations.id
    annot_id = annot_id * "," if Array === annot_id
    tsv[annot_id] = annotations.tsv_values(*fields).dup

  when Array === annotations 
    tsv = TSV.setup({}, :key_field => "ID", :fields => fields, :type => :list, :unnamed => true)

    annotations.compact.each_with_index do |annotation,i|
      tsv[annotation.id + "#" << i.to_s] = annotation.tsv_values(*fields).dup
    end

  else
    raise "Annotations need to be an Array to create TSV"
  end

  tsv
end

Instance Method Details

#annotate(object) ⇒ Object



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
# File 'lib/rbbt/annotations.rb', line 110

def annotate(object)

  return object unless Array === object or String === object

  annotation_types.each do |annotation|
    object.extend annotation unless annotation === object
  end

  object.instance_variable_set(:@annotation_types, nil)

  if object.instance_variables.include?(:@annotation_values)
    hash = {}
    object.instance_variable_get(:@annotation_values).each{|k,v| hash[k] = v}
    self.annotation_values.each{|k,v| hash[k] = v unless v.nil?}

    object.instance_variable_set(:@annotation_values, hash)
    object.instance_variable_set(:@shared_annotations,  false)
  else
    object.instance_variable_set(:@annotation_values,  self.annotation_values)
    object.instance_variable_set(:@shared_annotations,  true)
    @shared_annotations = true
  end

  object.extend AnnotatedArray if AnnotatedArray === self and Array === object

  object
end

#annotation_md5Object



93
94
95
96
97
98
# File 'lib/rbbt/annotations.rb', line 93

def annotation_md5
  if annotation_values.instance_variable_get(:@annotation_md5).nil?
    annotation_values.instance_variable_set(:@annotation_md5, Misc.hash2md5(annotation_values))
  end
  annotation_values.instance_variable_get(:@annotation_md5)
end

#annotation_typesObject



28
29
30
31
32
33
34
35
# File 'lib/rbbt/annotations.rb', line 28

def annotation_types

  @annotation_types ||= class << self; self; end.
    included_modules.
    select{|m| 
      Annotation === m
    }
end

#annotation_valuesObject



11
12
13
# File 'lib/rbbt/annotations.rb', line 11

def annotation_values
  @annotation_values ||= {}
end

#annotationsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbbt/annotations.rb', line 37

def annotations
  if @annotations.nil? 
    @annotations = []

    annotation_types.each do |annotation_type|
      @annotations.concat annotation_type.annotations
    end

    @annotations
  else
    @annotations
  end
end

#clean_annotations(recursive = false) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rbbt/annotations.rb', line 138

def clean_annotations(recursive = false)
  case
  when self.nil?
    nil
  when Array === self
    if recursive
      [].concat self.collect{|e| e.respond_to?(:clean_annotations)? e.clean_annotations : e}
    else
      [].concat self
    end
  when String === self
    "" << self
  else
    self.dup
  end
end

#detach_annotationsObject



15
16
17
18
19
# File 'lib/rbbt/annotations.rb', line 15

def detach_annotations
  @annotation_values = @annotation_values.dup
  @annotation_values.instance_variable_set(:@annotation_md5, nil)
  @shared_annotations = false
end

#idObject

ToDo This does not make much sense, why not change :id directly



105
106
107
108
# File 'lib/rbbt/annotations.rb', line 105

def id
  @id ||= self.respond_to?(:annotation_id) ? 
    annotation_id : self_md5
end

#info(masked = false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbbt/annotations.rb', line 69

def info(masked = false)

  if @info.nil?
    annotation_values = self.annotation_values
    annotation_values = annotation_values.dup unless annotation_values.nil?
    info = annotation_values
    info[:annotation_types] = annotation_types
    info[:annotated_array] = true if AnnotatedArray === self
    @info = info
  end

  if masked 
    if @masked_info.nil?
      @masked_info = @info.dup
      masked_annotations.each do |annotation|
        @masked_info.delete annotation
      end
    end
    @masked_info
  else
    @info
  end
end

#make_listObject



182
183
184
185
186
187
# File 'lib/rbbt/annotations.rb', line 182

def make_list
  new = [self]
  self.annotate(new)
  new.extend AnnotatedArray
  new
end

#marshal_dumpObject



253
254
255
# File 'lib/rbbt/annotations/util.rb', line 253

def marshal_dump
  Annotated.to_hash(self)
end

#masked_annotationsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbbt/annotations.rb', line 51

def masked_annotations
  if @masked_annotations.nil? 
    @masked_annotations = []

    annotation_types.each do |annotation_type|
      @masked_annotations.concat annotation_type.masked_annotations
    end

    @masked_annotations
  else
    @masked_annotations
  end
end

#resetObject



21
22
23
24
25
26
# File 'lib/rbbt/annotations.rb', line 21

def reset
  @info = nil
  @id = nil
  @self_md5 = nil
  annotation_values.instance_variable_set(:@annotation_md5, nil)
end

#self_md5Object



100
101
102
# File 'lib/rbbt/annotations.rb', line 100

def self_md5
  @self_md5 ||= Misc.digest(annotation_md5 + self.to_s)
end

#tsv_values(*fields) ⇒ Object



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
# File 'lib/rbbt/annotations/util.rb', line 172

def tsv_values(*fields)
  if Array === self and (not AnnotatedArray === self or self.double_array)
    Misc.zip_fields(self.compact.collect{|e| e.tsv_values(fields)})
  else
    fields = fields.flatten

    info = self.info

    values = []

    fields.each do |field|
      values << case

      when Proc === field
        field.call(self)

      when field == "JSON"
        if AnnotatedArray === self
          info.merge(:annotated_array => true).to_json
        else
          info.to_json
        end

      when field == "annotation_types"
        annotation_types.collect{|t| t.to_s} * "|"

      when field == "annotated_array"
        AnnotatedArray === self

      when field == "literal"
        (Array === self ? "Array:" << self * "|" : self).gsub(/\n|\t/, ' ')

      when info.include?(field.to_sym)
        res = info[field.to_sym]
        Array === res ? "Array:" << res * "|" : res

      when self.respond_to?(field)
        res = self.send(field)
        Array === res ? "Array:"<< res * "|" : res

      end
    end


    values
  end
end

#unmasked_annotationsObject



65
66
67
# File 'lib/rbbt/annotations.rb', line 65

def unmasked_annotations
  @unmasked_annotations ||= annotations - masked_annotations
end