Class: Flock::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/flock/mock_service.rb

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



192
193
194
# File 'lib/flock/mock_service.rb', line 192

def initialize
  @by_pair, @by_source, @by_destination = {}, Hash.new { |h, k| h[k] = [] }, Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#add_edge(state, source, dest, time, pos) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/flock/mock_service.rb', line 196

def add_edge(state, source, dest, time, pos)
  if existing_edge = @by_pair[[source, dest]]
    if ![Edges::EdgeState::Positive, Edges::EdgeState::Archived].include?(existing_edge.state_id) && state == Edges::EdgeState::Positive
      existing_edge.position = pos
    end
    existing_edge.state_id = state
    existing_edge.updated_at = time.to_i
  else
    edge = make_edge(state, source, dest, time, pos)
    @by_pair[[source, dest]] = edge
    @by_source[source] << edge
    @by_destination[dest] << edge
  end
end

#contains?(source, dest, state) ⇒ Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/flock/mock_service.rb', line 243

def contains?(source, dest, state)
  select_edges(source, dest, state).any?
end

#select_edges(source_ids, destination_ids, states = []) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/flock/mock_service.rb', line 211

def select_edges(source_ids, destination_ids, states = [])
  source_ids, destination_ids, states = Array(source_ids), Array(destination_ids), Array(states)
  result = []
  if source_ids.empty?
    destination_ids.each do |destination_id|
      @by_destination[destination_id].each do |edge|
        next unless states.empty? || states.include?(edge.state_id)

        result << edge
      end
    end
  elsif destination_ids.empty?
    source_ids.each do |source_id|
      @by_source[source_id].each do |edge|
        next unless states.empty? || states.include?(edge.state_id)

        result << edge
      end
    end
  else
    source_ids.each do |source_id|
      destination_ids.each do |destination_id|
        next unless existing_edge = @by_pair[[source_id, destination_id]]
        next unless states.empty? || states.include?(existing_edge.state_id)

        result << existing_edge
      end
    end
  end
  result
end