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



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rbbt/association/item.rb', line 179

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



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rbbt/association/item.rb', line 167

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



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/association/item.rb', line 136

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rbbt/association/item.rb', line 192

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