Class: Gremlin2Dot::GraphBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gremlin2dot/graph_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphBuilder

Returns a new instance of GraphBuilder.



11
12
13
14
# File 'lib/gremlin2dot/graph_builder.rb', line 11

def initialize
  @seen = Set.new
  @g = GraphViz.new("G")
end

Class Method Details

.build(results) ⇒ Object



7
8
9
# File 'lib/gremlin2dot/graph_builder.rb', line 7

def self.build(results)
  self.new().build(results)
end

Instance Method Details

#add_thing(thing) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gremlin2dot/graph_builder.rb', line 28

def add_thing(thing)
  case thing
  when Vertex
    node = thing
    unless @seen.include? node.id
      label = node.label + node.properties.entries.sort_by(&:first).map {|k, v| "\n#{k}=#{v.first}" }.join("")
      @g.add_nodes(node.id, label: label)
      @seen << node.id
    end
  when Edge
    edge = thing
    unless @seen.include? edge.id
      label = edge.label + edge.properties.entries.sort_by(&:first).map {|k, v| "\n#{k}=#{v}" }.join("")
      @g.add_edges(edge.outV, edge.inV, label: label)
      @seen << edge.id
    end
  when Tree
    add_thing thing.key
    thing.items.each {|child| add_thing child}
  else
    $stderr.puts "Not adding #{thing.class} to the graph"
  end
end

#build(unmangled_data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gremlin2dot/graph_builder.rb', line 16

def build(unmangled_data)
  trees = unmangled_data['result']['data']

  trees.each do |t0|
    t0.each do |t1|
      add_thing t1
    end
  end

  @g
end