OR-Tools  8.2
parser_util.cc
Go to the documentation of this file.
1// Copyright 2010-2018 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// Utility functions used by the code in parser.yy
15// Included in parser.tab.cc.
17
18#include <string>
19
25#include "ortools/flatzinc/parser.tab.hh"
27
28extern int orfz_lex(YYSTYPE*, void* scanner);
29extern int orfz_get_lineno(void* scanner);
30extern int orfz_debug;
31
33 operations_research::fz::Model* model, bool* ok, void* scanner,
34 const char* str) {
35 LOG(ERROR) << "Error: " << str << " in line no. " << orfz_get_lineno(scanner);
36 *ok = false;
37}
38
39namespace operations_research {
40namespace fz {
41// Whether the given list of annotations contains the given identifier
42// (or function call).
43bool ContainsId(std::vector<Annotation>* annotations, const std::string& id) {
44 if (annotations != nullptr) {
45 for (int i = 0; i < annotations->size(); ++i) {
46 if (((*annotations)[i].type == Annotation::IDENTIFIER ||
47 (*annotations)[i].type == Annotation::FUNCTION_CALL) &&
48 (*annotations)[i].id == id) {
49 return true;
50 }
51 }
52 }
53 return false;
54}
55
56bool AllDomainsHaveOneValue(const std::vector<Domain>& domains) {
57 for (int i = 0; i < domains.size(); ++i) {
58 if (!domains[i].HasOneValue()) {
59 return false;
60 }
61 }
62 return true;
63}
64
66 const double rounded = std::round(d);
67 const int64 i = static_cast<int64>(rounded);
68 CHECK_LE(std::abs(static_cast<double>(i) - rounded), 1e-9);
69 return i;
70}
71
72// Array in flatzinc are 1 based. We use this trivial wrapper for all flatzinc
73// arrays.
74template <class T>
75const T& Lookup(const std::vector<T>& v, int index) {
76 // TODO(user): replace this by a macro for better logging.
77 CHECK_GE(index, 1);
78 CHECK_LE(index, v.size());
79 return v[index - 1];
80}
81} // namespace fz
82} // namespace operations_research
#define CHECK_GE(val1, val2)
Definition: base/logging.h:701
#define LOG(severity)
Definition: base/logging.h:420
#define CHECK_LE(val1, val2)
Definition: base/logging.h:699
GRBmodel * model
GurobiMPCallbackContext * context
int64_t int64
const int ERROR
Definition: log_severity.h:32
int64 ConvertAsIntegerOrDie(double d)
Definition: parser_util.cc:65
bool ContainsId(std::vector< Annotation > *annotations, const std::string &id)
Definition: parser_util.cc:43
const T & Lookup(const std::vector< T > &v, int index)
Definition: parser_util.cc:75
bool AllDomainsHaveOneValue(const std::vector< Domain > &domains)
Definition: parser_util.cc:56
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
int index
Definition: pack.cc:508
#define YYSTYPE
Definition: parser.tab.cc:80
int orfz_lex(YYSTYPE *, void *scanner)
int orfz_debug
int orfz_get_lineno(void *scanner)
void orfz_error(operations_research::fz::ParserContext *context, operations_research::fz::Model *model, bool *ok, void *scanner, const char *str)
Definition: parser_util.cc:32