Class: CompTree::Node
- Inherits:
-
Object
- Object
- CompTree::Node
- Defined in:
- lib/comp_tree/node.rb
Overview
Base class for nodes in the computation tree.
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#computed ⇒ Object
Returns the value of attribute computed.
-
#function ⇒ Object
Returns the value of attribute function.
-
#lock_level ⇒ Object
Returns the value of attribute lock_level.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parents ⇒ Object
Returns the value of attribute parents.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
-
#children_results ⇒ Object
If all children have been computed, return their results; otherwise return nil.
-
#compute ⇒ Object
Compute this node; children must be computed and lock must be already acquired.
-
#compute_now ⇒ Object
Force all children and self to be computed; no locking required.
- #each_child ⇒ Object
- #each_downward(&block) ⇒ Object
- #each_upward(&block) ⇒ Object
-
#initialize(name) ⇒ Node
constructor
Create a node.
- #lock ⇒ Object
- #locked? ⇒ Boolean
-
#reset ⇒ Object
Reset the computation for this node and all children.
-
#reset_self ⇒ Object
Reset the computation for this node.
- #unlock ⇒ Object
Constructor Details
#initialize(name) ⇒ Node
Create a node
21 22 23 24 25 26 27 |
# File 'lib/comp_tree/node.rb', line 21 def initialize(name) @name = name @parents = [] @children = [] @function = nil reset_self end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def children @children end |
#computed ⇒ Object
Returns the value of attribute computed.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def computed @computed end |
#function ⇒ Object
Returns the value of attribute function.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def function @function end |
#lock_level ⇒ Object
Returns the value of attribute lock_level.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def lock_level @lock_level end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/comp_tree/node.rb', line 7 def name @name end |
#parents ⇒ Object
Returns the value of attribute parents.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def parents @parents end |
#result ⇒ Object
Returns the value of attribute result.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def result @result end |
Instance Method Details
#children_results ⇒ Object
If all children have been computed, return their results; otherwise return nil.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/comp_tree/node.rb', line 91 def children_results @children_results or ( @children_results = @children.map { |child| unless child.computed return nil end child.result } ) end |
#compute ⇒ Object
Compute this node; children must be computed and lock must be already acquired.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/comp_tree/node.rb', line 106 def compute begin unless @function raise NoFunctionError.new(@name) end @result = @function.call(*@children_results) @computed = true rescue Exception => exception @computed = exception end @result end |
#compute_now ⇒ Object
Force all children and self to be computed; no locking required. Intended to be used outside of parallel computations.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/comp_tree/node.rb', line 72 def compute_now unless @computed unless @children_results @children_results = @children.map { |child| child.compute_now } end compute if @computed.is_a? Exception raise @computed end end @result end |
#each_child ⇒ Object
62 63 64 65 66 |
# File 'lib/comp_tree/node.rb', line 62 def each_child @children.each { |child| yield(child) } end |
#each_downward(&block) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/comp_tree/node.rb', line 48 def each_downward(&block) block.call(self) @children.each { |child| child.each_downward(&block) } end |
#each_upward(&block) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/comp_tree/node.rb', line 55 def each_upward(&block) block.call(self) @parents.each { |parent| parent.each_upward(&block) } end |
#lock ⇒ Object
123 124 125 126 127 |
# File 'lib/comp_tree/node.rb', line 123 def lock each_upward { |node| node.lock_level += 1 } end |
#locked? ⇒ Boolean
119 120 121 |
# File 'lib/comp_tree/node.rb', line 119 def locked? @lock_level != 0 end |
#reset ⇒ Object
Reset the computation for this node and all children.
42 43 44 45 46 |
# File 'lib/comp_tree/node.rb', line 42 def reset each_downward { |node| node.reset_self } end |
#reset_self ⇒ Object
Reset the computation for this node.
32 33 34 35 36 37 |
# File 'lib/comp_tree/node.rb', line 32 def reset_self @result = nil @computed = nil @lock_level = 0 @children_results = nil end |
#unlock ⇒ Object
129 130 131 132 133 |
# File 'lib/comp_tree/node.rb', line 129 def unlock each_upward { |node| node.lock_level -= 1 } end |