1 /*************************************************************
2 MiniSat       --- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 CryptoMiniSat --- Copyright (c) 2014, Mate Soos
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 ***************************************************************/
23 
24 #include "main_common.h"
25 #include "solverconf.h"
26 #include <iostream>
27 #include <fstream>
28 
29 using std::cout;
30 using std::endl;
31 
32 using namespace CMSat;
33 
handle_drat_option()34 void MainCommon::handle_drat_option()
35 {
36     if (!conf.simulate_drat) {
37         if (dratDebug) {
38             dratf = &cout;
39         } else {
40             std::ofstream* dratfTmp = new std::ofstream;
41             dratfTmp->open(dratfilname.c_str(), std::ofstream::out | std::ofstream::binary);
42             if (!*dratfTmp) {
43                 std::cerr
44                 << "ERROR: Could not open DRAT file "
45                 << dratfilname
46                 << " for writing"
47                 << endl;
48 
49                 std::exit(-1);
50             }
51             dratf = dratfTmp;
52         }
53     }
54 }
55 
print_model(CMSat::SATSolver * solver,std::ostream * os,std::vector<uint32_t> * only)56 uint32_t MainCommon::print_model(CMSat::SATSolver* solver, std::ostream* os, std::vector<uint32_t>* only)
57 {
58     *os << "v ";
59     size_t line_size = 2;
60     size_t num_undef = 0;
61 
62     auto fun = [&](uint32_t var) {
63         if (solver->get_model()[var] != CMSat::l_Undef) {
64             const bool value_is_positive = (solver->get_model()[var] == CMSat::l_True);
65             const size_t this_var_size = std::ceil(std::log10(var+1)) + 1 + !value_is_positive;
66             line_size += this_var_size;
67             if (line_size > 80) {
68                 *os << std::endl << "v ";
69                 line_size = 2 + this_var_size;
70             }
71             *os << (value_is_positive? "" : "-") << var+1 << " ";
72         } else {
73             num_undef++;
74         }
75     };
76 
77     if (only == NULL) {
78         for (uint32_t var = 0; var < solver->nVars(); var++) {
79             fun(var);
80         }
81     } else {
82         for(uint32_t var: *only) {
83             fun(var);
84         }
85     }
86     *os << "0" << std::endl;
87     return num_undef;
88 }
89