Class: SocialFramework::NetworkHelper::GraphStrategyDefault

Inherits:
GraphStrategy
  • Object
show all
Defined in:
app/helpers/social_framework/network_helper.rb

Overview

Represent the network on a Graph, with Vertices and Edges

Instance Attribute Summary

Attributes inherited from GraphStrategy

#depth, #network

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_instance(id, elements_factory) ⇒ Object

Get graph instance to user logged

Params:
id

Integer Id of the user logged

elements_factory

String Represent the factory class name to build

Returns Graph object



85
86
87
88
89
90
91
92
93
# File 'app/helpers/social_framework/network_helper.rb', line 85

def self.get_instance(id, elements_factory)
  @@instances ||= {}
  
  if @@instances[id].nil?
    @@instances[id] = GraphStrategyDefault.new elements_factory
  end

  return @@instances[id]
end

Instance Method Details

#build(root, attributes = SocialFramework.attributes_to_build_graph, relationships = "all") ⇒ Object

Mount a graph from an user

Params:
root

User Root user to mount graph

attributes

Array Attributes will be added in vertex

relationships

Array labels to find relationships, can be multiple in array or just one in a simple String, default is “all” thats represents all relationships existing

Returns The graph mounted



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/helpers/social_framework/network_helper.rb', line 109

def build(root, attributes = SocialFramework.attributes_to_build_graph, relationships = "all")
  @root = root
  @network.clear
  vertices = Array.new

  attributes_hash = mount_attributes(attributes, root)
  vertices << {vertex: @elements_factory.create_vertex(@root.id, @root.class, attributes_hash), depth: 1}

  until vertices.empty?
    pair = vertices.shift
    current_vertex = pair[:vertex]
    @network << current_vertex

    next if pair[:depth] == @depth or current_vertex.type == ModelFabric.get_class(SocialFramework.event_class)
    new_depth = pair[:depth] + 1

    edges = get_edges(current_vertex.id, relationships)

    edges.each do |edge|
      user = (edge.origin.id == current_vertex.id) ? edge.destiny : edge.origin

      add_vertex(vertices, current_vertex, new_depth, user, attributes, edge.label, edge.bidirectional)
    end

    events = get_events(current_vertex.id)
    events.each do |event|
      add_vertex(vertices, current_vertex, new_depth, event, attributes, "event", false)
    end
  end
end

#destroy(id) ⇒ Object

Destroy graph instance with id passed

Params:
id

Integer Id of the user logged

Returns Graph instance removed



99
100
101
# File 'app/helpers/social_framework/network_helper.rb', line 99

def destroy(id)
  @@instances.delete(id)
end

#search(map, search_in_progress = false, elements_number = SocialFramework.elements_number_to_search) ⇒ Object

Search users with values specified in a map

Params:
map

Hash with keys and values to compare

search_in_progress

Boolean to continue if true or start a new search

elements_number

Integer to limit max search result

Returns Hash with users and events found



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/helpers/social_framework/network_helper.rb', line 146

def search(map, search_in_progress = false, elements_number = SocialFramework.elements_number_to_search)
  return {users: @users_found, events: @events_found} if @finished_search and search_in_progress == true

  unless search_in_progress
    clean_vertices
    @network.first.color = :gray
    @queue << @network.first
  end

  if block_given? and search_in_progress
    @elements_number = yield @elements_number
  else
    @elements_number += elements_number
  end

  search_visit(map) unless @finished_search_in_graph
  search_in_database(map) if (@users_found.size + @events_found.size) < @elements_number and @finished_search_in_graph
  return {users: @users_found, events: @events_found}
end

#suggest_relationships(type_relationships = SocialFramework.relationship_type_to_suggest, amount_relationships = SocialFramework.amount_relationship_to_suggest) ⇒ Object

Suggest relationships to root

Params:
type_relationships

Array labels to find relationships, can be multiple in array or just one in a simple String

amount_relationships

Integer quantity of relationships to suggest a new relationship

Returns Array with users to suggestions



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/helpers/social_framework/network_helper.rb', line 171

def suggest_relationships(type_relationships = SocialFramework.relationship_type_to_suggest,
  amount_relationships = SocialFramework.amount_relationship_to_suggest)

  travel_in_third_depth(type_relationships) do |destiny_edge|
    destiny_edge.destiny.visits = 0
  end

  suggestions = Array.new

  travel_in_third_depth(type_relationships) do |destiny_edge|
    destiny_edge.destiny.visits = destiny_edge.destiny.visits + 1

    if(destiny_edge.destiny.visits == amount_relationships and
      destiny_edge.destiny.id != @root.id and
      @network.first.edges.select { |e| e.destiny == destiny_edge.destiny }.empty?)
      
      suggestions << @root.class.find(destiny_edge.destiny.id)
    end
  end
  return suggestions
end