Class: Ravensat::DimacsEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/ravensat/dimacs/dimacs_encoder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDimacsEncoder

Returns a new instance of DimacsEncoder.



4
5
6
# File 'lib/ravensat/dimacs/dimacs_encoder.rb', line 4

def initialize
  @name_table = {}
end

Instance Attribute Details

#name_tableObject (readonly)

Returns the value of attribute name_table.



3
4
5
# File 'lib/ravensat/dimacs/dimacs_encoder.rb', line 3

def name_table
  @name_table
end

Instance Method Details

#set_dimacs_name(formula) ⇒ Object



22
23
24
25
26
# File 'lib/ravensat/dimacs/dimacs_encoder.rb', line 22

def set_dimacs_name(formula)
  formula.vars.each_with_index do |node, i|
    node.dimacs_name = (i+1).to_s
  end
end

#to_dimacs(formula) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ravensat/dimacs/dimacs_encoder.rb', line 8

def to_dimacs(formula)
  dimacs_header = "p cnf #{formula.vars_size} #{formula.clauses_size}\n"
  dimacs_body = ""
  return nil unless formula.cnf?

  set_dimacs_name(formula)

  formula.each_by_descriptive do |node|
    dimacs_body << node.to_dimacs
  end

  dimacs_header + dimacs_body
end