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



20
21
22
23
24
25
26
# File 'lib/comp_tree/node.rb', line 20

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

#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.



71
72
73
74
75
76
77
78
# File 'lib/comp_tree/node.rb', line 71

def children_results
  @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.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/comp_tree/node.rb', line 84

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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/comp_tree/node.rb', line 52

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

#free?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/comp_tree/node.rb', line 97

def free?
  @lock_level.zero?
end

#lockObject



101
102
103
104
105
106
# File 'lib/comp_tree/node.rb', line 101

def lock
  @lock_level = @lock_level.succ
  @parents.each { |parent|
    parent.lock
  }
end

#resetObject

Reset the computation for this node and all children.



41
42
43
44
45
46
# File 'lib/comp_tree/node.rb', line 41

def reset
  reset_self
  @children.each { |child|
    child.reset
  }
end

#reset_selfObject

Reset the computation for this node.



31
32
33
34
35
36
# File 'lib/comp_tree/node.rb', line 31

def reset_self
  @result = nil
  @computed = nil
  @lock_level = 0
  @children_results = nil
end

#unlockObject



108
109
110
111
112
113
# File 'lib/comp_tree/node.rb', line 108

def unlock
  @lock_level -= 1  # Fixnum#pred not in 1.8.6
  @parents.each { |parent|
    parent.unlock
  }
end