Module: AssociationItem

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

Constant Summary

Constants included from Entity

Entity::UNPERSISTED_PREFIX

Instance Attribute Summary

Attributes included from Entity

#all_formats

Class Method Summary collapse

Methods included from Entity

add_identifiers, extended, identifier_files

Class Method Details

._select_match(orig, elem) ⇒ Object



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

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



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rbbt/association/item.rb', line 148

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



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

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rbbt/association/item.rb', line 173

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