Class: Ravensat::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ravensat/ast/node.rb

Direct Known Subclasses

InitialNode, NilNode, OprNode, VarNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



6
7
8
# File 'lib/ravensat/ast/node.rb', line 6

def initialize
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/ravensat/ast/node.rb', line 5

def children
  @children
end

Instance Method Details

#&(object) ⇒ Object

Raises:

  • (TypeError)


65
66
67
68
69
70
# File 'lib/ravensat/ast/node.rb', line 65

def &(object)
  raise TypeError.new("#{object.class} can't be coerced into Ravensat::Node") unless object.is_a? Node
  return self if object.is_a? NilNode

  AndNode.new(self, object)
end

#clauses_sizeObject



101
102
103
104
105
106
107
108
# File 'lib/ravensat/ast/node.rb', line 101

def clauses_size
  cnt = 0
  self.each_by_descriptive do |node|
    cnt+=1 if node.is_a? AndNode
  end
  cnt
  # self.count{|node| node.is_a? AndNode} + 1
end

#cnf?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ravensat/ast/node.rb', line 86

def cnf?
  @children.map(&:cnf?).reduce(:&)
end

#eachObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ravensat/ast/node.rb', line 49

def each
  return to_enum unless block_given?
  node_stack = [self]

  until node_stack.empty?
    current_node = node_stack.shift
    next unless current_node

    yield current_node

    node_stack = node_stack.concat(current_node.children)
  end

  self if block_given?
end

#each_by_descriptiveObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ravensat/ast/node.rb', line 10

def each_by_descriptive
  node_stack = [[self, self.children.clone]] #[[parent, children], ...]

  until node_stack.empty?
    current_parent, current_children = node_stack.pop
    current_node = current_children.shift

    and_node_obj = AndNode.new

    case current_node
    when AndNode
      node_stack.push [current_parent, current_children.clone]
      node_stack.push [current_node, current_node.children.clone]
    when OrNode
      node_stack.push [current_parent, current_children.clone]
      node_stack.push [current_node, current_node.children.clone]
    when NotNode
      yield(current_node)
      yield(current_node.children.first)

      if current_children.empty?
        yield(and_node_obj)
      else
        yield(current_parent)
        node_stack.push [current_parent, current_children.clone]
      end
    when VarNode, Extension::BooleanVariable
      yield(current_node)

      if current_children.empty?
        yield(and_node_obj)
      else
        yield(current_parent)
        node_stack.push [current_parent, current_children.clone]
      end
    end
  end
end

#evalObject



90
91
# File 'lib/ravensat/ast/node.rb', line 90

def eval
end

#to_dimacsObject



83
84
# File 'lib/ravensat/ast/node.rb', line 83

def to_dimacs
end

#to_sObject



79
80
81
# File 'lib/ravensat/ast/node.rb', line 79

def to_s
  self.class.name
end

#varsObject



93
94
95
# File 'lib/ravensat/ast/node.rb', line 93

def vars
  self.select{|node| node.is_a? VarNode}.uniq
end

#vars_sizeObject



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

def vars_size
  self.vars.size
end

#|(object) ⇒ Object

Raises:

  • (TypeError)


72
73
74
75
76
77
# File 'lib/ravensat/ast/node.rb', line 72

def |(object)
  raise TypeError.new("#{object.class} can't be coerced into Ravensat::Node") unless object.is_a? Node
  return self if object.is_a? NilNode

  OrNode.new(self, object)
end