CBMC
bv_dimacs.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Writing DIMACS Files
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "bv_dimacs.h"
13 
14 #include <solvers/sat/dimacs_cnf.h>
15 
16 #include <fstream> // IWYU pragma: keep
17 #include <iostream>
18 
20  const namespacet &_ns,
21  dimacs_cnft &_prop,
22  message_handlert &message_handler,
23  const std::string &_filename)
24  : bv_pointerst(_ns, _prop, message_handler),
25  filename(_filename),
26  dimacs_cnf_prop(_prop)
27 {
28 }
29 
31 {
32  if(filename.empty() || filename == "-")
33  return write_dimacs(std::cout);
34 
35  std::ofstream out(filename);
36 
37  if(!out)
38  {
39  log.error() << "failed to open " << filename << messaget::eom;
40  return false;
41  }
42 
43  return write_dimacs(out);
44 }
45 
46 bool bv_dimacst::write_dimacs(std::ostream &out)
47 {
49 
50  // we dump the mapping variable<->literals
51  for(const auto &s : get_symbols())
52  {
53  if(s.second.is_constant())
54  out << "c " << s.first << " " << (s.second.is_true() ? "TRUE" : "FALSE")
55  << "\n";
56  else
57  out << "c " << s.first << " " << s.second.dimacs() << "\n";
58  }
59 
60  // dump mapping for selected bit-vectors
61  for(const auto &m : get_map().get_mapping())
62  {
63  const auto &literal_map = m.second.literal_map;
64 
65  if(literal_map.empty())
66  continue;
67 
68  out << "c " << m.first;
69 
70  for(const auto &lit : literal_map)
71  {
72  out << ' ';
73 
74  if(lit.is_constant())
75  out << (lit.is_true() ? "TRUE" : "FALSE");
76  else
77  out << lit.dimacs();
78  }
79 
80  out << "\n";
81  }
82 
83  return false;
84 }
Writing DIMACS Files.
messaget log
Definition: arrays.h:57
const mappingt & get_mapping() const
Definition: boolbv_map.h:67
const boolbv_mapt & get_map() const
Definition: boolbv.h:97
const dimacs_cnft & dimacs_cnf_prop
Definition: bv_dimacs.h:35
bool write_dimacs()
Definition: bv_dimacs.cpp:30
const std::string filename
Definition: bv_dimacs.h:34
bv_dimacst(const namespacet &_ns, dimacs_cnft &_prop, message_handlert &message_handler, const std::string &_filename)
Definition: bv_dimacs.cpp:19
virtual void write_dimacs_cnf(std::ostream &out) const
Definition: dimacs_cnf.cpp:40
mstreamt & error() const
Definition: message.h:391
static eomt eom
Definition: message.h:289
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
const symbolst & get_symbols() const