CBMC
dimacs_cnf.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #include "dimacs_cnf.h"
11 
12 #include <util/invariant.h>
13 #include <util/magic.h>
14 
15 #include <iostream>
16 
18  : cnf_clause_listt(message_handler), break_lines(false)
19 {
20 }
21 
23 {
25 }
26 
28 {
30  return false;
31 }
32 
34  std::ostream &_out,
35  message_handlert &message_handler)
36  : cnft(message_handler), out(_out)
37 {
38 }
39 
40 void dimacs_cnft::write_dimacs_cnf(std::ostream &out)
41 {
42  write_problem_line(out);
43  write_clauses(out);
44 }
45 
46 void dimacs_cnft::write_problem_line(std::ostream &out)
47 {
48  // We start counting at 1, thus there is one variable fewer.
49  out << "p cnf " << (no_variables()-1) << " "
50  << clauses.size() << "\n";
51 }
52 
54  const bvt &clause,
55  std::ostream &out,
56  bool break_lines)
57 {
58  // The DIMACS CNF format allows line breaks in clauses:
59  // "Each clauses is terminated by the value 0. Unlike many formats
60  // that represent the end of a clause by a new-line character,
61  // this format allows clauses to be on multiple lines."
62  // Some historic solvers (zchaff e.g.) have silently swallowed
63  // literals in clauses that exceed some fixed buffer size.
64 
65  // However, the SAT competition format does not allow line
66  // breaks in clauses, so we offer both options.
67 
68  for(size_t j=0; j<clause.size(); j++)
69  {
70  out << clause[j].dimacs() << " ";
71  // newline to avoid overflow in sat checkers
72  if((j&15)==0 && j!=0 && break_lines)
73  out << "\n";
74  }
75 
76  out << "0" << "\n";
77 }
78 
79 void dimacs_cnft::write_clauses(std::ostream &out)
80 {
81  std::size_t count = 0;
82  std::stringstream output_block;
83  for(clausest::const_iterator it=clauses.begin();
84  it!=clauses.end(); it++)
85  {
86  write_dimacs_clause(*it, output_block, break_lines);
87 
88  // print the block once in a while
89  if(++count % CNF_DUMP_BLOCK_SIZE == 0)
90  {
91  out << output_block.str();
92  output_block.str("");
93  }
94  }
95 
96  // make sure the final block is printed as well
97  out << output_block.str();
98 }
99 
101 {
103 }
Definition: cnf.h:18
virtual size_t no_variables() const override
Definition: cnf.h:42
std::ostream & out
Definition: dimacs_cnf.h:74
dimacs_cnf_dumpt(std::ostream &_out, message_handlert &message_handler)
Definition: dimacs_cnf.cpp:33
void lcnf(const bvt &bv) override
Definition: dimacs_cnf.cpp:100
bool is_in_conflict(literalt l) const override
Returns true if an assumption is in the final conflict.
Definition: dimacs_cnf.cpp:27
void write_clauses(std::ostream &out)
Definition: dimacs_cnf.cpp:79
dimacs_cnft(message_handlert &)
Definition: dimacs_cnf.cpp:17
void write_problem_line(std::ostream &out)
Definition: dimacs_cnf.cpp:46
static void write_dimacs_clause(const bvt &, std::ostream &, bool break_lines)
Definition: dimacs_cnf.cpp:53
bool break_lines
Definition: dimacs_cnf.h:42
void set_assignment(literalt a, bool value) override
Definition: dimacs_cnf.cpp:22
virtual void write_dimacs_cnf(std::ostream &out)
Definition: dimacs_cnf.cpp:40
std::vector< literalt > bvt
Definition: literal.h:201
Magic numbers used throughout the codebase.
const std::size_t CNF_DUMP_BLOCK_SIZE
Definition: magic.h:10
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
#define UNIMPLEMENTED
Definition: invariant.h:558