Class: TreeDiff::DotVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_diff/dot_visitor.rb

Constant Summary collapse

COLORS =
{
  1 => 'blue',
  2 => 'red',
  3 => 'green',
}

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ DotVisitor

Returns a new instance of DotVisitor.



9
10
11
12
13
# File 'lib/tree_diff/dot_visitor.rb', line 9

def initialize root
  @root  = root
  @nodes = []
  @edges = []
end

Instance Method Details

#accept(target) ⇒ Object



15
16
17
# File 'lib/tree_diff/dot_visitor.rb', line 15

def accept target
  target.accept(self)
end

#to_sObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tree_diff/dot_visitor.rb', line 47

def to_s
  String.new("    digraph g {\n      node [\n        fontsize = \"16\"\n        shape = \"record\"\n        style = filled\n      ];\n      \#{@nodes.join + @edges.join}\n    }\n  eograph\nend\n")

#visit(node) ⇒ Object



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
# File 'lib/tree_diff/dot_visitor.rb', line 19

def visit node
  if @root.source == node.source
    @nodes << String.new("      \"\#{node.object_id}\" [\n        label = \"<f0> (\#{node.name})\"\n      ];\n    eonode\n  else\n    i = 0\n    @nodes << String.new(<<-eonode)\n      \"\#{node.object_id}\" [\n        label = \"{<f0> (\#{node.name}) | \#{node.source.map { |src|\n          \"<f\#{i}> \#{src}\"\n        }.join(\" | \")}}\"\n        color = blue\n      ];\n    eonode\n  end\n  node.children.each { |child|\n    @edges << String.new(<<-eoedge)\n      \"\#{node.object_id}\" -> \"\#{child.object_id}\":f0 [\n        id = \#{@edges.length}\n      ];\n    eoedge\n  }\n  node.children.each { |c| c.accept(self) }\nend\n")