Module: Pacer::Utils::GraphAnalysis

Defined in:
lib/pacer/utils/graph_analysis.rb

Defined Under Namespace

Modules: Edges, Properties, Route, Vertices

Class Method Summary (collapse)

Class Method Details

+ (Object) structure(graph, type_field = :type)

Returns a TinkerGraph representing the number of each type of node and how many edges (by label) point to each other type of node.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pacer/utils/graph_analysis.rb', line 8

def structure(graph, type_field = :type)
  result = Pacer.tg
  result.vertex_name = proc do |v|
    if v[:element_type] == 'vertex'
      "vertex '#{v[:type]}' (#{v[:count]})"
    elsif v[:element_type] == 'edge'
      "edge :#{v[:label]} (#{v[:count]})"
    elsif v[:element_type] == 'property keys'
      "properties #{v[:number]} keys (#{v[:count]})"
    end
  end
  graph.v[type_field].fast_group_count.to_h.each do |type, count|
    result.create_vertex :element_type => 'vertex', :type_field => type_field, :type => type, :count => count
  end
  graph.e.labels.fast_group_count.to_h.each do |label, count|
    result.create_vertex :element_type => 'edge', :label => label, :count => count
  end
  result.v(:element_type => 'vertex').each do |type_node|
    puts "vertices of type #{ type_node[:type] }: #{ type_node[:count] }"
    graph.v(self, :type => type_node[:type]).property_variations result, type_node
  end
  result.v(:element_type => 'edge').each do |edge_node|
    puts "edges with label #{ edge_node[:label] }: #{ edge_node[:count] }"
    edge_route = graph.e(self, edge_node[:label])
    edge_route.property_variations result, edge_node
  end
  result.v.each do |type_node|
    begin
      edges = graph.v(type_field => type_node[:type]).out_e
      edge_types = edges.group_count do |e|
        [e.label, e.in_vertex[type_field]]
      end
      edge_types.each do |(label, type), count|
        puts "edges labelled #{ label } from #{ type_node[:type] } to #{ type }: #{ count }"
        type_node.add_edges_to(label, result.v(:type => type), :count => count)
      end
    rescue => e
      puts e.message
    end
  end
  result
end