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

Instance Method Summary collapse

Methods inherited from GraphStrategy

#destroy, get_instance

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/helpers/social_framework/network_helper.rb', line 92

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

#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 Set with users found



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/social_framework/network_helper.rb', line 129

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 the vertices to suggestions



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/helpers/social_framework/network_helper.rb', line 154

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