Class: MetaCL::Logic::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, left_child = nil, right_child = nil, params = {}) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
# 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

Instance Attribute Details

#left_childObject (readonly)

Returns the value of attribute left_child.



5
6
7
# File 'lib/metacl/logic/node.rb', line 5

def left_child
  @left_child
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/metacl/logic/node.rb', line 5

def params
  @params
end

#right_childObject (readonly)

Returns the value of attribute right_child.



5
6
7
# File 'lib/metacl/logic/node.rb', line 5

def right_child
  @right_child
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/metacl/logic/node.rb', line 5

def type
  @type
end

Instance Method Details

#*(arg) ⇒ Object



62
63
64
# File 'lib/metacl/logic/node.rb', line 62

def *(arg)
  Node.new :operator, self, arg.nodify, type: :*
end

#+(arg) ⇒ Object



50
51
52
# File 'lib/metacl/logic/node.rb', line 50

def +(arg)
  Node.new :operator, self, arg.nodify, type: :+
end

#-(arg) ⇒ Object



54
55
56
# File 'lib/metacl/logic/node.rb', line 54

def -(arg)
  Node.new :operator, self, arg.nodify, type: :-
end

#/(arg) ⇒ Object



58
59
60
# File 'lib/metacl/logic/node.rb', line 58

def /(arg)
  Node.new :operator, self, arg.nodify, type: :/
end

#[](index_i, index_j = nil) ⇒ Object



25
26
27
28
29
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# 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_cloneObject



13
14
15
# 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) ⇒ Object



83
84
85
86
87
88
89
# 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?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/metacl/logic/node.rb', line 17

def leaf?
  not (left_child or right_child)
end

#leavesObject



42
43
44
# File 'lib/metacl/logic/node.rb', line 42

def leaves
  nodes.select(&:leaf?)
end

#nodesObject



37
38
39
40
# File 'lib/metacl/logic/node.rb', line 37

def nodes
  result = []
  walk { |node| result << node }
end

#nodifyObject



21
22
23
# File 'lib/metacl/logic/node.rb', line 21

def nodify
  self
end

#objectsObject



46
47
48
# File 'lib/metacl/logic/node.rb', line 46

def objects
  leaves.map { |x| x.params.object }.compact.uniq
end

#to_sObject



79
80
81
# File 'lib/metacl/logic/node.rb', line 79

def to_s
  debug
end

#walk {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



31
32
33
34
35
# 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