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
# 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|
    mod = Misc.string2const(mod) if String === mod
    object.extend mod unless mod === object
  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_info(fields, values) ⇒ Object



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

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rbbt/annotations/util.rb', line 93

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



67
68
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/util.rb', line 67

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

  object = case
           when literal_pos
             values[literal_pos]
           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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rbbt/annotations.rb', line 153

def self.purge(object)
  case object
  when String
    object.respond_to?(:clean_annotations) ?
      object.clean_annotations : 
      object
  when Array
    object.respond_to?(:clean_annotations) ?
      object.clean_annotations : 
      object.inject([]){|acc,e| acc << Annotated.purge(e); acc}
  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



41
42
43
44
45
46
47
# File 'lib/rbbt/annotations/util.rb', line 41

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

.tsv(annotations, *fields) ⇒ Object



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

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

  fields = case

           when ((fields.compact.empty?) and not annotations.empty?)
             fields = AnnotatedArray === annotations ? annotations.annotations : annotations.compact.first.annotations
             fields << :annotation_types

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

           when (fields == [:all] and Annotated === annotations)
             fields = [:annotation_types] + annotations.annotations 
             fields << :annotated_array if AnnotatedArray === annotations
             fields << :literal

           when (fields == [:all] and not 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}

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



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

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}

    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



91
92
93
94
95
96
# File 'lib/rbbt/annotations.rb', line 91

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



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

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



103
104
105
106
# File 'lib/rbbt/annotations.rb', line 103

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

def info(masked = false)

  if @info.nil?
    info = annotation_values.dup
    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



175
176
177
178
179
180
# File 'lib/rbbt/annotations.rb', line 175

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

#marshal_dumpObject



215
216
217
# File 'lib/rbbt/annotations/util.rb', line 215

def marshal_dump
  Annotated.purge(self).to_sym.to_s
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



98
99
100
# File 'lib/rbbt/annotations.rb', line 98

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

#tsv_values(*fields) ⇒ Object



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

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