Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parser_main.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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// This binary reads an input file in the flatzinc format (see
15// http://www.minizinc.org/), parses it, and spits out the model it
16// has built.
17
18#include <cstddef>
19#include <string>
20
21#include "absl/base/log_severity.h"
22#include "absl/flags/flag.h"
23#include "absl/log/check.h"
24#include "absl/log/globals.h"
25#include "absl/strings/match.h"
27#include "ortools/base/timer.h"
31
32ABSL_FLAG(std::string, input, "", "Input file in the flatzinc format.");
33ABSL_FLAG(bool, print, false, "Print model.");
34ABSL_FLAG(bool, presolve, false, "Presolve loaded file.");
35ABSL_FLAG(bool, statistics, false, "Print model statistics");
36
37namespace operations_research {
38namespace fz {
39void ParseFile(const std::string& filename) {
40 WallTimer timer;
41 timer.Start();
42
43 SolverLogger logger;
44 logger.EnableLogging(true);
45 logger.SetLogToStdOut(true);
46
47 SOLVER_LOG(&logger, "Loading ", filename);
48
49 std::string problem_name = filename;
50 // Remove the .fzn extension.
51 CHECK(absl::EndsWith(problem_name, ".fzn"));
52 problem_name.resize(problem_name.size() - 4);
53 // Remove the leading path if present.
54 const size_t found = problem_name.find_last_of("/\\");
55 if (found != std::string::npos) {
56 problem_name = problem_name.substr(found + 1);
57 }
58 SOLVER_LOG(&logger, " - parsed in ", timer.GetInMs(), " ms");
59
60 Model model(problem_name);
61 CHECK(ParseFlatzincFile(filename, &model));
62 if (absl::GetFlag(FLAGS_statistics)) {
63 ModelStatistics stats(model, &logger);
64 stats.BuildStatistics();
65 stats.PrintStatistics();
66 }
67 if (absl::GetFlag(FLAGS_print)) {
68 SOLVER_LOG(&logger, model.DebugString());
69 }
70}
71} // namespace fz
72} // namespace operations_research
73
74int main(int argc, char** argv) {
75 const char kUsage[] =
76 "Parses a flatzinc .fzn file, optionally presolve it, and prints it in "
77 "human-readable format";
78 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
79 InitGoogle(kUsage, &argc, &argv, /*remove_flags=*/true);
80 operations_research::fz::ParseFile(absl::GetFlag(FLAGS_input));
81 return 0;
82}
int64_t GetInMs() const
Definition timer.h:45
void Start()
Definition timer.h:30
void EnableLogging(bool enable)
Definition logging.h:51
void SetLogToStdOut(bool enable)
Definition logging.h:57
std::string DebugString() const
Definition model.cc:1081
void InitGoogle(absl::string_view usage, int *argc, char ***argv, bool deprecated)
Definition init_google.h:49
void ParseFile(const std::string &filename)
bool ParseFlatzincFile(const std::string &filename, Model *model)
Definition parser.cc:42
OR-Tools root namespace.
static int input(yyscan_t yyscanner)
int main(int argc, char **argv)
ABSL_FLAG(std::string, input, "", "Input file in the flatzinc format.")
static const char kUsage[]
#define SOLVER_LOG(logger,...)
Definition logging.h:114