Class: CompTree::Node

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

Overview

Base class for nodes in the computation tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject

Returns the value of attribute children.



9
10
11
# File 'lib/comp_tree/node.rb', line 9

def children
  @children
end

#computedObject

Returns the value of attribute computed.



9
10
11
# File 'lib/comp_tree/node.rb', line 9

def computed
  @computed
end

#functionObject

Returns the value of attribute function.



9
10
11
# File 'lib/comp_tree/node.rb', line 9

def function
  @function
end

#lock_levelObject

Returns the value of attribute lock_level.



9
10
11
# File 'lib/comp_tree/node.rb', line 9

def lock_level
  @lock_level
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/comp_tree/node.rb', line 7

def name
  @name
end

#parentsObject

Returns the value of attribute parents.



9
10
11
# File 'lib/comp_tree/node.rb', line 9

def parents
  @parents
end

#resultObject

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_resultsObject

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

#computeObject

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_nowObject

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_childObject



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

#lockObject



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

Returns:

  • (Boolean)


119
120
121
# File 'lib/comp_tree/node.rb', line 119

def locked?
  @lock_level != 0
end

#resetObject

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_selfObject

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

#unlockObject



129
130
131
132
133
# File 'lib/comp_tree/node.rb', line 129

def unlock
  each_upward { |node|
    node.lock_level -= 1
  }
end