Class: Ravensat::Node

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

Direct Known Subclasses

InitialNode, 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



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

def &(object)
  AndNode.new(self, object)
end

#clauses_sizeObject



92
93
94
95
96
97
98
99
# File 'lib/ravensat/ast/node.rb', line 92

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)


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

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

#to_dimacsObject



77
78
# File 'lib/ravensat/ast/node.rb', line 77

def to_dimacs
end

#to_sObject



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

def to_s
  self.class.name
end

#varsObject



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

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

#vars_sizeObject



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

def vars_size
  self.vars.size
end

#|(object) ⇒ Object



69
70
71
# File 'lib/ravensat/ast/node.rb', line 69

def |(object)
  OrNode.new(self, object)
end