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



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rbbt/association/item.rb', line 170

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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rbbt/association/item.rb', line 158

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



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

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rbbt/association/item.rb', line 183

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