class MetaCL::Logic::Node

Attributes

left_child[R]
params[R]
right_child[R]
type[R]

Public Class Methods

new(type, left_child = nil, right_child = nil, params = {}) click to toggle source
# File lib/metacl/logic/node.rb, line 7
def initialize(type, left_child = nil, right_child = nil, params = {})
  @type = type
  @params = OpenStruct.new(params)
  @left_child, @right_child = left_child, right_child
end

Public Instance Methods

*(arg) click to toggle source
# File lib/metacl/logic/node.rb, line 62
def *(arg)
  Node.new :operator, self, arg.nodify, type: :*
end
+(arg) click to toggle source
# File lib/metacl/logic/node.rb, line 50
def +(arg)
  Node.new :operator, self, arg.nodify, type: :+
end
-(arg) click to toggle source
# File lib/metacl/logic/node.rb, line 54
def -(arg)
  Node.new :operator, self, arg.nodify, type: :-
end
/(arg) click to toggle source
# File lib/metacl/logic/node.rb, line 58
def /(arg)
  Node.new :operator, self, arg.nodify, type: :/
end
[](index_i, index_j = nil) click to toggle source
# File lib/metacl/logic/node.rb, line 25
def [](index_i, index_j = nil)
  params.index_i = index_i
  params.index_j = index_j
  self
end
debug(tab = 0) click to toggle source
# File lib/metacl/logic/node.rb, line 66
def debug(tab = 0)
  text = '  ' * tab << @type.to_s << ' ' << "{ #{ @params.to_h.map { |k, v| "#{k}: #{v}"}.join ', ' } }" << "\n"
  if @left_child
    text << '  ' * tab << "left: \n"
    text << @left_child.debug(tab+1)
  end
  if @right_child
    text << '  ' * tab << "right: \n"
    text << @right_child.debug(tab+1)
  end
  text
end
deep_clone() click to toggle source
# File lib/metacl/logic/node.rb, line 13
def deep_clone
  Marshal.load(Marshal.dump(self)) # TODO: write a proper solution
end
get_tree_with_substitution(subst) click to toggle source
# File lib/metacl/logic/node.rb, line 83
def get_tree_with_substitution(subst)
  new_tree = self.deep_clone
  new_tree.leaves.each do
    |leaf| leaf.params.name = subst[leaf.params.name]
  end
  new_tree
end
leaf?() click to toggle source
# File lib/metacl/logic/node.rb, line 17
def leaf?
  not (left_child or right_child)
end
leaves() click to toggle source
# File lib/metacl/logic/node.rb, line 42
def leaves
  nodes.select(&:leaf?)
end
nodes() click to toggle source
# File lib/metacl/logic/node.rb, line 37
def nodes
  result = []
  walk { |node| result << node }
end
nodify() click to toggle source
# File lib/metacl/logic/node.rb, line 21
def nodify
  self
end
objects() click to toggle source
# File lib/metacl/logic/node.rb, line 46
def objects
  leaves.map { |x| x.params.object }.compact.uniq
end
to_s() click to toggle source
# File lib/metacl/logic/node.rb, line 79
def to_s
  debug
end
walk() { |self| ... } click to toggle source
# File lib/metacl/logic/node.rb, line 31
def walk(&block)
  @left_child.walk(&block)  if @left_child
  @right_child.walk(&block) if @right_child
  yield self
end