Module: AssociationItem

Extended by:
Object::Entity
Defined in:
lib/rbbt/association/item.rb

Class Method Summary collapse

Class Method Details

._select_match(orig, elem) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rbbt/association/item.rb', line 187

def self._select_match(orig, elem)
  if Array === orig and Array === elem
    (orig & elem).any?
  elsif Array === orig
    orig.include? elem
  elsif Array === elem
    elem.include? orig
  else
    elem === orif
  end
  false
end

.adjacency(pairs, key_field = nil, &block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rbbt/association/item.rb', line 175

def self.adjacency(pairs, key_field = nil, &block)
  incidence = incidence(pairs, key_field, &block)

  targets = incidence.fields
  adjacency = TSV.setup({}, :key_field => incidence.key_field, :fields => ["Target"], :type => :double)
  TSV.traverse incidence, :into => adjacency, :unnamed => true do |k,values|
    target_values = targets.zip(values).reject{|t,v| v.nil? }.collect{|t,v| [t,v]}
    next if target_values.empty?
    [k, Misc.zip_fields(target_values)]
  end
end

.incidence(pairs, key_field = nil, &block) ⇒ Object



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
# File 'lib/rbbt/association/item.rb', line 144

def self.incidence(pairs, key_field = nil, &block)
  matrix = {}
  targets = []
  sources = []
  matches = {}

  pairs.inject([]){|acc,m| acc << m; acc << m.invert if m.respond_to?(:undirected) and m.undirected; acc  }.each do |p|
    s, sep, t = p.partition "~"

    sources << s
    targets << t
    if block_given?
      matches[s] ||= Hash.new{nil}
      value = block.call p
      matches[s][t] = value unless value.nil? or (mv = matches[s][t] and value > mv)
    else
      matches[s] ||= Hash.new{false}
      matches[s][t] ||= true 
    end
  end

  sources.uniq!
  targets = targets.uniq.sort

  matches.each do |s,hash|
    matrix[s] = hash.values_at(*targets)
  end

  defined?(TSV)? TSV.setup(matrix, :key_field => (key_field || "Source") , :fields => targets, :type => :list) : matrix
end

.select(list, method = nil, &block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rbbt/association/item.rb', line 200

def self.select(list, method = nil, &block)
  if method and method.any?
    list.select do |item|
      method.collect do |key,value|
        case key
        when :target
          _select_match item.target, value
        when :source
          _select_match item.source, value
        else
          orig = item.info[key]
          orig = orig.split(";;") if String orig
          _select_match orig, value 
        end
      end
    end
  else
    list.select(&block)
  end
end