Class: Alf::Engine::ToDot

Inherits:
Object
  • Object
show all
Includes:
Support::DotUtils
Defined in:
lib/alf-engine/alf/engine/to_dot.rb

Instance Method Summary collapse

Methods included from Support::DotUtils

#dot_label

Instance Method Details

#apply(cog, buf) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/alf-engine/alf/engine/to_dot.rb', line 46

def apply(cog, buf)
  if cog.respond_to?(:operand)
    on_unary_operator(cog, buf, cog.class.name)
  elsif cog.respond_to?(:left)
    on_binary_operator(cog, buf, cog.class.name)
  elsif cog.respond_to?(:operands)
    on_nary_operator(cog, buf, cog.class.name)
  else
    on_leaf_operand(cog, buf)
  end
end

#call(cog, buf = "") ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/alf-engine/alf/engine/to_dot.rb', line 6

def call(cog, buf = "")
  buf << "/* #{cog} */\n"
  buf << "digraph \"Alf\" {\n"
  buf << "node [width=0.375,height=0.25,shape=record];\n"
  apply(cog, buf)
  buf << "}\n"
  buf
end

#on_binary_operator(cog, buf, label) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/alf-engine/alf/engine/to_dot.rb', line 21

def on_binary_operator(cog, buf, label)
  buf << "#{cog.object_id} [label=#{dot_label(label)}];\n"
  apply(cog.left, buf)
  apply(cog.right, buf)
  buf << "#{cog.object_id} -> #{cog.left.object_id} [label=\"left\"];\n"
  buf << "#{cog.object_id} -> #{cog.right.object_id} [label=\"right\"];\n"
end

#on_leaf_operand(cog, buf = "") ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/alf-engine/alf/engine/to_dot.rb', line 37

def on_leaf_operand(cog, buf = "")
  case cog
  when Relation
    buf << "#{cog.object_id} [label=\"Relation\"];\n"
  else
    buf << "#{cog.object_id} [label=#{dot_label(cog)}];\n"
  end
end

#on_nary_operator(cog, buf, label) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/alf-engine/alf/engine/to_dot.rb', line 29

def on_nary_operator(cog, buf, label)
  buf << "#{cog.object_id} [label=#{dot_label(label)}];\n"
  cog.operands.each_with_index do |op, i|
    apply(op, buf)
    buf << "#{cog.object_id} -> #{op.object_id} [label=#{dot_label(i)}];\n"
  end
end

#on_unary_operator(cog, buf, label) ⇒ Object



15
16
17
18
19
# File 'lib/alf-engine/alf/engine/to_dot.rb', line 15

def on_unary_operator(cog, buf, label)
  buf << "#{cog.object_id} [label=#{dot_label(label)}];\n"
  apply(cog.operand, buf)
  buf << "#{cog.object_id} -> #{cog.operand.object_id} [label=\"operand\"];\n"
end