Class: Wallace::Koza::KozaNode
- Inherits:
-
Object
- Object
- Wallace::Koza::KozaNode
- Defined in:
- lib/modules/koza/koza_node.rb
Overview
This class is used to represent nodes within a Koza tree.
Instance Attribute Summary collapse
-
#children ⇒ Object
We allow all attributes to be manipulated from outside but trust that all modification to trees is performed through the KozaTree interfaces (which fix depths and calculate the list of nodes in the tree).
-
#contents ⇒ Object
We allow all attributes to be manipulated from outside but trust that all modification to trees is performed through the KozaTree interfaces (which fix depths and calculate the list of nodes in the tree).
-
#depth ⇒ Object
We allow all attributes to be manipulated from outside but trust that all modification to trees is performed through the KozaTree interfaces (which fix depths and calculate the list of nodes in the tree).
-
#parent ⇒ Object
We allow all attributes to be manipulated from outside but trust that all modification to trees is performed through the KozaTree interfaces (which fix depths and calculate the list of nodes in the tree).
Instance Method Summary collapse
-
#arity ⇒ Object
Returns the arity (number of inputs) to this node.
-
#clone(opts = {}) ⇒ Object
Creates a clone of this node (and its children).
-
#ephemeral? ⇒ Boolean
Checks whether the contents of this node are an ephemeral instance.
-
#eval(args) ⇒ Object
Evaluates the S-expression given by the sub-tree at this node.
-
#initialize(contents, opts = {}) ⇒ KozaNode
constructor
Parameters: * contents, the contents of this node.
-
#label ⇒ Object
Returns the label at this node.
-
#leaf? ⇒ Boolean
Checks to see if this node is a leaf (i.e. it has no children).
-
#root? ⇒ Boolean
Checks to see if this node is a root node (i.e. it has no parent).
-
#terminal? ⇒ Boolean
Checks whether this node contains a terminal value.
-
#to_ruby ⇒ Object
Converts the sub-tree at this node into a Ruby program string.
-
#value ⇒ Object
Returns the value of this node (or the label in the case of non-terminals).
Constructor Details
#initialize(contents, opts = {}) ⇒ KozaNode
Parameters:
-
contents, the contents of this node.
-
opts, a hash of keyword options for this tree node. -> parent, the parent of this node. -> children, an array of the children of this node.
20 21 22 23 24 25 |
# File 'lib/modules/koza/koza_node.rb', line 20 def initialize(contents, opts = {}) @contents = contents @parent = opts[:parent] @children = opts[:children] || [] @depth = opts[:depth] end |
Instance Attribute Details
#children ⇒ Object
We allow all attributes to be manipulated from outside but trust that all modification to trees is performed through the KozaTree interfaces (which fix depths and calculate the list of nodes in the tree).
8 9 10 |
# File 'lib/modules/koza/koza_node.rb', line 8 def children @children end |
#contents ⇒ Object
We allow all attributes to be manipulated from outside but trust that all modification to trees is performed through the KozaTree interfaces (which fix depths and calculate the list of nodes in the tree).
8 9 10 |
# File 'lib/modules/koza/koza_node.rb', line 8 def contents @contents end |
#depth ⇒ Object
We allow all attributes to be manipulated from outside but trust that all modification to trees is performed through the KozaTree interfaces (which fix depths and calculate the list of nodes in the tree).
8 9 10 |
# File 'lib/modules/koza/koza_node.rb', line 8 def depth @depth end |
#parent ⇒ Object
We allow all attributes to be manipulated from outside but trust that all modification to trees is performed through the KozaTree interfaces (which fix depths and calculate the list of nodes in the tree).
8 9 10 |
# File 'lib/modules/koza/koza_node.rb', line 8 def parent @parent end |
Instance Method Details
#arity ⇒ Object
Returns the arity (number of inputs) to this node.
72 73 74 |
# File 'lib/modules/koza/koza_node.rb', line 72 def arity terminal? ? 0 : @contents.arity end |
#clone(opts = {}) ⇒ Object
Creates a clone of this node (and its children). Parent node is discarded in the clone (unless specified not to do so).
Parameters:
-
opts, a hash of keyword options for this method. -> keep_parent, if true maintains depth and parent information.
otherwise this information is lost.
Returns: A clone of this node (and its children).
86 87 88 89 90 91 92 93 |
# File 'lib/modules/koza/koza_node.rb', line 86 def clone(opts = {}) cloned = Wallace::Koza::KozaNode.new( @contents, parent: opts[:keep_parent] ? @parent : nil , children: @children.map { |c| c.clone }) cloned.children.each { |c| c.parent = cloned } return cloned end |
#ephemeral? ⇒ Boolean
Checks whether the contents of this node are an ephemeral instance.
Returns: True if the contents are an ephemeral instance, false if otherwise.
57 58 59 |
# File 'lib/modules/koza/koza_node.rb', line 57 def ephemeral? @contents.is_a? Wallace::Koza::EphemeralInstance end |
#eval(args) ⇒ Object
Evaluates the S-expression given by the sub-tree at this node.
Parameters:
-
args, the arguments to the function / program (as a hash).
99 100 101 |
# File 'lib/modules/koza/koza_node.rb', line 99 def eval(args) terminal? ? @contents.eval(args) : @contents.eval(@children.map { |c| c.eval(args) }) end |
#label ⇒ Object
Returns the label at this node.
62 63 64 |
# File 'lib/modules/koza/koza_node.rb', line 62 def label @contents.label end |
#leaf? ⇒ Boolean
Checks to see if this node is a leaf (i.e. it has no children).
Returns true if leaf, false if not.
44 45 46 |
# File 'lib/modules/koza/koza_node.rb', line 44 def leaf? @children.empty? end |
#root? ⇒ Boolean
Checks to see if this node is a root node (i.e. it has no parent).
36 37 38 |
# File 'lib/modules/koza/koza_node.rb', line 36 def root? @parent.nil? end |
#terminal? ⇒ Boolean
Checks whether this node contains a terminal value.
49 50 51 |
# File 'lib/modules/koza/koza_node.rb', line 49 def terminal? @contents.is_a? Wallace::Koza::Terminal end |
#to_ruby ⇒ Object
Converts the sub-tree at this node into a Ruby program string.
104 105 106 |
# File 'lib/modules/koza/koza_node.rb', line 104 def to_ruby terminal? ? @contents.value.to_s : "#{@contents.label}(#{@children.map{ |c| c.to_ruby }.join(',')})" end |
#value ⇒ Object
Returns the value of this node (or the label in the case of non-terminals).
67 68 69 |
# File 'lib/modules/koza/koza_node.rb', line 67 def value terminal? ? @contents.value : @contents.label end |