Class: ForeachNode

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

Instance Attribute Summary

Attributes inherited from Node

#after_list, #before_list, #code_block, #commands, #exec_block, #id, #is_concurrently, #is_safely, #name, #node_list, #parent

Instance Method Summary collapse

Methods inherited from Node

#after_exec, #before_exec, #draw_after_blocks, #draw_before_blocks, #draw_child_nodes, #init_time, #paint, #print_tree

Constructor Details

#initialize(parent, block) ⇒ ForeachNode

Returns a new instance of ForeachNode.



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

def initialize (parent, block)
    super(parent, block)
    self.name = "[Foreach]\n"
end

Instance Method Details

#draw_block(graph) ⇒ Object



35
36
37
38
# File 'lib/tree_building/foreach_node.rb', line 35

def draw_block(graph)
    graph.hexagon << graph.node(id)
    graph.node(id).label(name + 'Concurrently: ' + is_concurrently.to_s + '\nSafely: ' + is_safely.to_s)
end

#run(instance) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tree_building/foreach_node.rb', line 9

def run(instance)
    init_time
    before_exec(instance, @total_time)
    begin
        if is_concurrently
            pids = []
            node_list.each do |node|
                pids << fork do
                    @total_time[:exec].push(node.run(instance))
                end
            end
            pids.each{|pid| Process.waitpid(pid)}
        else
            node_list.each do |node|
                @total_time[:exec].push(node.run(@instance))
            end
        end
    rescue
        if is_safely
            raise "An error has occured"
        end
    end
    after_exec(@instance, @total_time)
    @total_time
end