Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
file_format_flags.h
Go to the documentation of this file.
1// Copyright 2010-2024 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
14// Library to handles --input_file and --format flags of a binary that reads or
15// writes MathOpt models in various binary or text formats.
16//
17// This library provides:
18//
19// - a FileFormat enum type that can be used with ABSL_FLAG, either directly
20// or wrapped in a std::optional<FileFormat> to support guessing the file
21// format based on the file name's extension.
22//
23// - functions that helps builds the help string of the flag using:
24// * FileFormat: FormatFlagPossibleValuesList()
25// * std::optional<FileFormat>: OptionalFormatFlagPossibleValuesList()
26//
27// - a FormatFromFlagOrFilePath() function to handle the std::nullopt case
28// when using std::optional<FileFormat> for a flag.
29//
30// - functions ReadModel() and WriteModel() that take the FileFormat and
31// read/write ModelProto.
32//
33#ifndef OR_TOOLS_MATH_OPT_TOOLS_FILE_FORMAT_FLAGS_H_
34#define OR_TOOLS_MATH_OPT_TOOLS_FILE_FORMAT_FLAGS_H_
35
36#include <optional>
37#include <ostream>
38#include <string>
39#include <utility>
40
41#include "absl/container/flat_hash_map.h"
42#include "absl/status/status.h"
43#include "absl/status/statusor.h"
44#include "absl/strings/string_view.h"
45#include "absl/types/span.h"
46#include "ortools/math_opt/model.pb.h"
47#include "ortools/math_opt/model_parameters.pb.h"
48
50
51// The supported file formats.
52//
53// The --format flag could be std::optional<FileFormat> to support automatic
54// guessing of the format based on the input file extension.
55enum class FileFormat {
56 kMathOptBinary,
62};
63
64// Streams the enum value using AbslUnparseFlag().
65std::ostream& operator<<(std::ostream& out, FileFormat f);
66
67// Used to define flags with std::optional<FileFormat> or FileFormat.
68//
69// See OptionalFormatFlagPossibleValuesList() or FormatFlagPossibleValuesList()
70// to build the related help strings.
71//
72// See FormatFromFlagOrFilePath() to handle the std::nullopt case when using an
73// optional format.
74bool AbslParseFlag(absl::string_view text, FileFormat* f, std::string* error);
76
77// Returns a span of FileFormat enum values.
78absl::Span<const FileFormat> AllFileFormats();
79
80// Returns a multi-line list listing all possible formats that can be used with
81// a --format flag of type std::optional<FileFormat>. Each entry is prefixed by
82// a '\n'.
83//
84// The returned string contains a multiline list of bullet.
85//
86// See FormatFlagPossibleValuesList() for the alternative to use when the format
87// value is not optional.
88//
89// Usage example:
90//
91// ABSL_FLAG(
92// std::optional<operations_research::math_opt::FileFormat>, input_format,
93// std::nullopt,
94// absl::StrCat(
95// "the format of the --input_file; possible values:",
96// operations_research::math_opt::OptionalFormatFlagPossibleValuesList()));
97//
99
100// Same as OptionalFormatFlagPossibleValuesList() but for a flag of type
101// FileFormat (i.e. with a mandatory value).
102//
103// Usage example:
104//
105// ABSL_FLAG(
106// operations_research::math_opt::FileFormat, output_format,
107// operations_research::math_opt::FileFormat::kMathOptBinary,
108// absl::StrCat(
109// "the format of the --output_file; possible values:",
110// operations_research::math_opt::FormatFlagPossibleValuesList()));
111//
113
114// Returns a map from file extensions to their format.
115absl::flat_hash_map<absl::string_view, FileFormat> ExtensionToFileFormat();
116
117// Uses ExtensionToFileFormat() to returns the potential format from a given
118// file path.
119//
120// Note that they may exists multiple formats for the same extension (like
121// ".pb"). In that case an arbitrary choice is made (e.g. using MathOpt's
122// ModelProto for ".pb").
123std::optional<FileFormat> FormatFromFilePath(absl::string_view file_path);
124
125// Returns either format_flag_value if not nullopt, else returns the result of
126// FormatFromFilePath() on the input path.
127//
128// Note that in C++23 we could use std::optional<T>::or_else().
129//
130// Usage example:
131//
132// const FileFormat input_format = [&](){
133// const std::optional<FileFormat> format =
134// FormatFromFlagOrFilePath(absl::GetFlag(FLAGS_input_format),
135// input_file_path);
136// if (format.has_value()) {
137// return *format;
138// }
139// LOG(QFATAL)
140// << "Can't guess the format from the file extension, please "
141// "use --input_format to specify the file format explicitly.";
142// }();
143//
144std::optional<FileFormat> FormatFromFlagOrFilePath(
145 std::optional<FileFormat> format_flag_value, absl::string_view file_path);
146
147// Returns the ModelProto read from the given file. Optionally returns a
148// SolutionHintProto for kLinearSolverXxx format as it may contain one.
149absl::StatusOr<std::pair<ModelProto, std::optional<SolutionHintProto>>>
150ReadModel(absl::string_view file_path, FileFormat format);
151
152// Writes the given model with the given format.
153//
154// The optional hint is used when the output format supports it
155// (e.g. MPModelProto). **It is not yet implemented though**; if you need it,
156// please contact us.
157absl::Status WriteModel(absl::string_view file_path,
158 const ModelProto& model_proto,
159 const std::optional<SolutionHintProto>& hint_proto,
160 FileFormat format);
161
162} // namespace operations_research::math_opt
163
164#endif // OR_TOOLS_MATH_OPT_TOOLS_FILE_FORMAT_FLAGS_H_
kLinearSolverBinary
* FileFormat
kMathOptText
kLinearSolverText
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::optional< FileFormat > FormatFromFlagOrFilePath(const std::optional< FileFormat > format_flag_value, const absl::string_view file_path)
bool AbslParseFlag(const absl::string_view text, SolverType *const value, std::string *const error)
absl::flat_hash_map< absl::string_view, FileFormat > ExtensionToFileFormat()
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
std::string OptionalFormatFlagPossibleValuesList()
absl::Status WriteModel(const absl::string_view file_path, const ModelProto &model_proto, const std::optional< SolutionHintProto > &hint_proto, const FileFormat format)
absl::StatusOr< std::pair< ModelProto, std::optional< SolutionHintProto > > > ReadModel(const absl::string_view file_path, const FileFormat format)
std::optional< FileFormat > FormatFromFilePath(absl::string_view file_path)
std::string AbslUnparseFlag(const SolverType value)
absl::Span< const FileFormat > AllFileFormats()