Class: Awkward::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/awkward/visitor.rb

Defined Under Namespace

Classes: Edge, Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVisitor

Returns a new instance of Visitor.



13
14
15
16
17
18
19
# File 'lib/awkward/visitor.rb', line 13

def initialize
  @nodes     = []
  @stack     = []
  @edges     = []
  @callstack = []
  @seen      = {}
end

Instance Attribute Details

#edgesObject (readonly)

Returns the value of attribute edges.



11
12
13
# File 'lib/awkward/visitor.rb', line 11

def edges
  @edges
end

Instance Method Details

#accept(o) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/awkward/visitor.rb', line 25

def accept o
  return connect(@stack.last, @seen[o.object_id]) if @seen.key? o.object_id

  node = Node.new o

  @seen[o.object_id] = node

  connect(@stack.last, node) unless @callstack.empty?

  @stack.push node

  send "visit_#{o.class.name.gsub('::', '_')}", o

  @stack.pop
end

#nodesObject



21
22
23
# File 'lib/awkward/visitor.rb', line 21

def nodes
  @seen.values
end

#to_dotObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/awkward/visitor.rb', line 41

def to_dot
  dot = <<-eodot
digraph "Graph" {
node [width=0.375,height=0.25,shape = "record"];
  eodot

  nodes.each do |node|
    dot.concat <<-eonode
    #{node.object_id} [label="<f0> #{[node.name, node.value].compact.join('|')}"];
    eonode
  end

  edges.each do |edge|
    dot.concat <<-eoedge
    #{edge.left.object_id} -> #{edge.right.object_id} [label="#{edge.name}"];
    eoedge
  end
  dot + "}"
end