Class: Wallace::Koza::KozaNode

Inherits:
Object
  • Object
show all
Defined in:
lib/modules/koza/koza_node.rb

Overview

This class is used to represent nodes within a Koza tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject

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

#contentsObject

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

#depthObject

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

#parentObject

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

#arityObject

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.

Returns:

  • (Boolean)


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

#labelObject

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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


49
50
51
# File 'lib/modules/koza/koza_node.rb', line 49

def terminal?
  @contents.is_a? Wallace::Koza::Terminal
end

#to_rubyObject

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

#valueObject

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