Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
file_format_flags.h File Reference
#include <optional>
#include <ostream>
#include <string>
#include <utility>
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "ortools/math_opt/model.pb.h"
#include "ortools/math_opt/model_parameters.pb.h"

Go to the source code of this file.

Macros

#define OR_TOOLS_MATH_OPT_TOOLS_FILE_FORMAT_FLAGS_H_
 

Functions

std::ostream & operator<< (std::ostream &out, FileFormat f)
 Streams the enum value using AbslUnparseFlag().
 
bool AbslParseFlag (absl::string_view text, FileFormat *f, std::string *error)
 
std::string AbslUnparseFlag (FileFormat f)
 
absl::Span< const FileFormatAllFileFormats ()
 Returns a span of FileFormat enum values.
 
std::string OptionalFormatFlagPossibleValuesList ()
 
std::string FormatFlagPossibleValuesList ()
 
absl::flat_hash_map< absl::string_view, FileFormatExtensionToFileFormat ()
 Returns a map from file extensions to their format.
 
std::optional< FileFormatFormatFromFilePath (absl::string_view file_path)
 
std::optional< FileFormatFormatFromFlagOrFilePath (std::optional< FileFormat > format_flag_value, absl::string_view file_path)
 
absl::StatusOr< std::pair< ModelProto, std::optional< SolutionHintProto > > > ReadModel (absl::string_view file_path, FileFormat format)
 
absl::Status WriteModel (absl::string_view file_path, const ModelProto &model_proto, const std::optional< SolutionHintProto > &hint_proto, FileFormat format)
 

Variables

FileFormat
 
 kMathOptText
 
 kLinearSolverBinary
 
 kLinearSolverText
 
 kMPS
 
 kLP
 

Macro Definition Documentation

◆ OR_TOOLS_MATH_OPT_TOOLS_FILE_FORMAT_FLAGS_H_

#define OR_TOOLS_MATH_OPT_TOOLS_FILE_FORMAT_FLAGS_H_

Function Documentation

◆ AbslParseFlag()

bool AbslParseFlag ( absl::string_view text,
FileFormat * f,
std::string * error )

Used to define flags with std::optional<FileFormat> or FileFormat.

See OptionalFormatFlagPossibleValuesList() or FormatFlagPossibleValuesList() to build the related help strings.

See FormatFromFlagOrFilePath() to handle the std::nullopt case when using an optional format.

◆ AbslUnparseFlag()

std::string AbslUnparseFlag ( FileFormat f)

◆ AllFileFormats()

absl::Span< const FileFormat > AllFileFormats ( )

Returns a span of FileFormat enum values.

◆ ExtensionToFileFormat()

absl::flat_hash_map< absl::string_view, FileFormat > ExtensionToFileFormat ( )

Returns a map from file extensions to their format.

◆ FormatFlagPossibleValuesList()

std::string FormatFlagPossibleValuesList ( )

Same as OptionalFormatFlagPossibleValuesList() but for a flag of type FileFormat (i.e. with a mandatory value).

Usage example:

ABSL_FLAG( operations_research::math_opt::FileFormat, output_format, operations_research::math_opt::FileFormat::kMathOptBinary, absl::StrCat( "the format of the --output_file; possible values:", operations_research::math_opt::FormatFlagPossibleValuesList()));

◆ FormatFromFilePath()

std::optional< FileFormat > FormatFromFilePath ( absl::string_view file_path)

Uses ExtensionToFileFormat() to returns the potential format from a given file path.

Note
they may exists multiple formats for the same extension (like ".pb"). In that case an arbitrary choice is made (e.g. using MathOpt's ModelProto for ".pb").

◆ FormatFromFlagOrFilePath()

std::optional< FileFormat > FormatFromFlagOrFilePath ( std::optional< FileFormat > format_flag_value,
absl::string_view file_path )

Returns either format_flag_value if not nullopt, else returns the result of FormatFromFilePath() on the input path.

Note
in C++23 we could use std::optional<T>::or_else().

Usage example:

const FileFormat input_format = [&](){ const std::optional<FileFormat> format = FormatFromFlagOrFilePath(absl::GetFlag(FLAGS_input_format), input_file_path); if (format.has_value()) { return *format; } LOG(QFATAL) << "Can't guess the format from the file extension, please " "use --input_format to specify the file format explicitly."; }();

◆ operator<<()

std::ostream & operator<< ( std::ostream & out,
FileFormat f )

Streams the enum value using AbslUnparseFlag().

◆ OptionalFormatFlagPossibleValuesList()

std::string OptionalFormatFlagPossibleValuesList ( )

Returns a multi-line list listing all possible formats that can be used with a –format flag of type std::optional<FileFormat>. Each entry is prefixed by a '
'.

The returned string contains a multiline list of bullet.

See FormatFlagPossibleValuesList() for the alternative to use when the format value is not optional.

Usage example:

ABSL_FLAG( std::optional<operations_research::math_opt::FileFormat>, input_format, std::nullopt, absl::StrCat( "the format of the --input_file; possible values:", operations_research::math_opt::OptionalFormatFlagPossibleValuesList()));

◆ ReadModel()

absl::StatusOr< std::pair< ModelProto, std::optional< SolutionHintProto > > > ReadModel ( absl::string_view file_path,
FileFormat format )

Returns the ModelProto read from the given file. Optionally returns a SolutionHintProto for kLinearSolverXxx format as it may contain one.

◆ WriteModel()

absl::Status WriteModel ( absl::string_view file_path,
const ModelProto & model_proto,
const std::optional< SolutionHintProto > & hint_proto,
FileFormat format )

Writes the given model with the given format.

The optional hint is used when the output format supports it (e.g. MPModelProto). It is not yet implemented though; if you need it, please contact us.

Variable Documentation

◆ FileFormat

* FileFormat

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Library to handles –input_file and –format flags of a binary that reads or writes MathOpt models in various binary or text formats.

This library provides:

  • a FileFormat enum type that can be used with ABSL_FLAG, either directly or wrapped in a std::optional<FileFormat> to support guessing the file format based on the file name's extension.
  • functions that helps builds the help string of the flag using:

Definition at line 24 of file file_format_flags.h.

◆ kLinearSolverBinary

kLinearSolverBinary

Definition at line 58 of file file_format_flags.h.

◆ kLinearSolverText

kLinearSolverText

Definition at line 59 of file file_format_flags.h.

◆ kLP

kLP

Definition at line 61 of file file_format_flags.h.

◆ kMathOptText

kMathOptText

Definition at line 57 of file file_format_flags.h.

◆ kMPS

kMPS

Definition at line 60 of file file_format_flags.h.