Class: SocialFramework::NetworkHelper::Graph

Inherits:
Object
  • 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 collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#depthObject

Returns the value of attribute depth.



10
11
12
# File 'app/helpers/social_framework/network_helper.rb', line 10

def depth
  @depth
end

#networkObject

Returns the value of attribute network.



10
11
12
# File 'app/helpers/social_framework/network_helper.rb', line 10

def network
  @network
end

Class Method Details

.get_instance(id) ⇒ Object

Get graph instance to user logged

Params:
id

Integer Id of the user logged

Returns Graph object



20
21
22
23
24
25
26
27
28
# File 'app/helpers/social_framework/network_helper.rb', line 20

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

  return @@instances[id]
end

Instance Method Details

#build(root, attributes = [:username, :email, :title], 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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/social_framework/network_helper.rb', line 44

def build(root, attributes = [:username, :email, :title], relationships = "all")
  @root = root
  @network.clear
  vertices = Array.new

  attributes_hash = mount_attributes(attributes, root)
  vertices << {vertex: GraphElements::Vertex.new(@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 == SocialFramework::Event
    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



34
35
36
# File 'app/helpers/social_framework/network_helper.rb', line 34

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/social_framework/network_helper.rb', line 81

def search(map, search_in_progress = false, elements_number = SocialFramework.elements_number_to_search)
  return @users_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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/social_framework/network_helper.rb', line 106

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