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