OR-Tools  9.6
drat_writer.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <string>
17 
18 #include "ortools/base/logging.h"
19 #if !defined(__PORTABLE_PLATFORM__)
20 #include "ortools/base/file.h"
21 #include "ortools/base/helpers.h"
22 #include "ortools/base/options.h"
23 #endif // !__PORTABLE_PLATFORM__
24 #include "absl/status/status.h"
25 #include "absl/strings/str_format.h"
26 #include "absl/types/span.h"
27 #include "ortools/sat/sat_base.h"
28 
29 namespace operations_research {
30 namespace sat {
31 
33  if (output_ != nullptr) {
34 #if !defined(__PORTABLE_PLATFORM__)
35  CHECK_OK(file::WriteString(output_, buffer_, file::Defaults()));
36  CHECK_OK(output_->Close(file::Defaults()));
37 #endif // !__PORTABLE_PLATFORM__
38  }
39 }
40 
41 void DratWriter::AddClause(absl::Span<const Literal> clause) {
42  WriteClause(clause);
43 }
44 
45 void DratWriter::DeleteClause(absl::Span<const Literal> clause) {
46  buffer_ += "d ";
47  WriteClause(clause);
48 }
49 
50 void DratWriter::WriteClause(absl::Span<const Literal> clause) {
51  for (const Literal literal : clause) {
52  absl::StrAppendFormat(&buffer_, "%d ", literal.SignedValue());
53  }
54  buffer_ += "0\n";
55  if (buffer_.size() > 10000) {
56 #if !defined(__PORTABLE_PLATFORM__)
57  CHECK_OK(file::WriteString(output_, buffer_, file::Defaults()));
58 #endif // !__PORTABLE_PLATFORM__
59  buffer_.clear();
60  }
61 }
62 
63 } // namespace sat
64 } // namespace operations_research
bool Close()
Definition: base/file.cc:49
void DeleteClause(absl::Span< const Literal > clause)
Definition: drat_writer.cc:45
void AddClause(absl::Span< const Literal > clause)
Definition: drat_writer.cc:41
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
Definition: base/file.cc:194
Options Defaults()
Definition: base/file.h:123
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:88