Class: SocialFramework::NetworkHelper::Graph
- Inherits:
-
Object
- Object
- SocialFramework::NetworkHelper::Graph
- Defined in:
- app/helpers/social_framework/network_helper.rb
Overview
Represent the network on a Graph, with Vertices and Edges
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#network ⇒ Object
Returns the value of attribute network.
Class Method Summary collapse
-
.get_instance(id) ⇒ Object
- Get graph instance to user logged ====== Params:
id -
IntegerId of the user logged Returns Graph object.
- Get graph instance to user logged ====== Params:
Instance Method Summary collapse
-
#build(root, attributes = [:username, :email, :title], relationships = "all") ⇒ Object
- Mount a graph from an user ====== Params:
root UserRoot user to mount graphattributesArrayAttributes will be added in vertexrelationships-
Arraylabels 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.
- Mount a graph from an user ====== Params:
-
#destroy(id) ⇒ Object
- Destroy graph instance with id passed ====== Params:
id -
IntegerId of the user logged Returns Graph instance removed.
- Destroy graph instance with id passed ====== Params:
-
#search(map, search_in_progress = false, elements_number = SocialFramework.elements_number_to_search) ⇒ Object
- Search users with values specified in a map ====== Params:
map Hashwith keys and values to comparesearch_in_progressBooleanto continue if true or start a new searchelements_number-
Integerto limit max search result Returns Set with users found.
- Search users with values specified in a map ====== Params:
-
#suggest_relationships(type_relationships = SocialFramework.relationship_type_to_suggest, amount_relationships = SocialFramework.amount_relationship_to_suggest) ⇒ Object
- Suggest relationships to root ====== Params:
type_relationships Arraylabels to find relationships, can be multiple in array or just one in a simple Stringamount_relationships-
Integerquantity of relationships to suggest a new relationship ReturnsArraywith the vertices to suggestions.
- Suggest relationships to root ====== Params:
Instance Attribute Details
#depth ⇒ Object
Returns the value of attribute depth.
10 11 12 |
# File 'app/helpers/social_framework/network_helper.rb', line 10 def depth @depth end |
#network ⇒ Object
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-
IntegerId 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-
UserRoot user to mount graph attributes-
ArrayAttributes will be added in vertex relationships-
Arraylabels 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-
IntegerId 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-
Hashwith keys and values to compare search_in_progress-
Booleanto continue if true or start a new search elements_number-
Integerto 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-
Arraylabels to find relationships, can be multiple in array or just one in a simple String amount_relationships-
Integerquantity 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 |