Class: Node

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_building/node.rb

Direct Known Subclasses

ConcurrentNode, ExecutionNode, ForeachNode, OnNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, block) ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tree_building/node.rb', line 9

def initialize(parent, block)
  self.parent = parent
  self.code_block = block
  self.node_list = Array.new
  self.before_list = Array.new
  self.after_list = Array.new
  self.is_safely = false
  self.is_concurrently = false
  self.commands = Array.new
  self.id = self.to_s
  self.name = '[Node]'
end

Instance Attribute Details

#after_listObject

Returns the value of attribute after_list.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def after_list
  @after_list
end

#before_listObject

Returns the value of attribute before_list.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def before_list
  @before_list
end

#code_blockObject

Returns the value of attribute code_block.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def code_block
  @code_block
end

#commandsObject

Returns the value of attribute commands.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def commands
  @commands
end

#exec_blockObject

Returns the value of attribute exec_block.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def exec_block
  @exec_block
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def id
  @id
end

#is_concurrentlyObject

Returns the value of attribute is_concurrently.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def is_concurrently
  @is_concurrently
end

#is_safelyObject

Returns the value of attribute is_safely.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def is_safely
  @is_safely
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def name
  @name
end

#node_listObject

Returns the value of attribute node_list.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def node_list
  @node_list
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/tree_building/node.rb', line 5

def parent
  @parent
end

Instance Method Details

#after_exec(instance, total_time) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tree_building/node.rb', line 55

def after_exec(instance, total_time)
  start = Time.now
  after_list.each do |after_command|
    if instance.nil?
      puts `#{after_command}`
    else
      $virtual_machines[instance].invoke [[after_command]]
    end
  end
  finish = Time.now
  total_time[:after] = finish - start
end

#before_exec(instance, total_time) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tree_building/node.rb', line 41

def before_exec(instance, total_time)
  start = Time.now
  before_list.each do |before_command|
    if instance.nil?
      puts `#{before_command}`
    else
      $virtual_machines[instance].invoke [[before_command]]
    end
  end
  finish = Time.now

  total_time[:before] = finish - start
end

#draw_after_blocks(node, graph) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tree_building/node.rb', line 86

def draw_after_blocks(node, graph)
  if node.after_list.length > 0
    id = Random.rand(2**64)
    name = "[AFTER]\n"
    node.after_list.each do |after|
      name += ">#{after}\n"
    end
    graph.rect << graph.node(id)
    graph.red << graph.edge(node.id, id)
    graph.node(id).label(name)
  end
end

#draw_before_blocks(node, graph) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tree_building/node.rb', line 73

def draw_before_blocks(node, graph)
  if node.before_list.length > 0
    id = Random.rand(2**64)
    name = "[BEFORE]\n"
    node.before_list.each do |before|
      name += ">#{before}\n"
    end
    graph.rect << graph.node(id)
    graph.green << graph.edge(node.id, id)
    graph.node(id).label(name)
  end
end

#draw_block(graph) ⇒ Object



68
69
70
71
# File 'lib/tree_building/node.rb', line 68

def draw_block(graph)
  graph.rect << graph.node(self.id)
  graph.node(self.id).label(name)
end

#draw_child_nodes(node, graph) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/tree_building/node.rb', line 99

def draw_child_nodes(node, graph)
  node.node_list.each do |child_node|
    child_node.commands.each do |command|
      child_node.name += "\n >" +command.to_s
    end
    graph.edge(node.id, child_node.id)
    paint(child_node, graph)
  end
end

#init_timeObject



22
23
24
25
26
27
28
# File 'lib/tree_building/node.rb', line 22

def init_time
  @total_time =
      {
          :name => @name,
          :exec => []
      }
end

#paint(node, graph) ⇒ Object



109
110
111
112
113
114
# File 'lib/tree_building/node.rb', line 109

def paint(node, graph)
  node.draw_block(graph)
  draw_before_blocks(node, graph)
  draw_child_nodes(node, graph)
  draw_after_blocks(node, graph)
end


116
117
118
119
120
121
122
# File 'lib/tree_building/node.rb', line 116

def print_tree(path)
  node = self
  digraph do
    node.paint(node, self)
    save path, 'png'
  end
end

#run(instance) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/tree_building/node.rb', line 30

def run(instance)
  init_time
  before_exec(instance, @total_time)
  node_list.each do |node|
    @total_time[:exec].push(node.run(instance))
  end
  after_exec(instance, @total_time)

  return @total_time
end