OR-Tools  8.1
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 
21 #include "ortools/base/logging.h"
22 #include "ortools/base/mathutil.h"
23 #include "ortools/base/stl_util.h"
24 #include "ortools/flatzinc/model.h"
25 #include "ortools/flatzinc/parser.tab.hh"
27 
28 extern int orfz_lex(YYSTYPE*, void* scanner);
29 extern int orfz_get_lineno(void* scanner);
30 extern 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 
39 namespace operations_research {
40 namespace fz {
41 // Whether the given list of annotations contains the given identifier
42 // (or function call).
43 bool 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 
56 bool 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.
74 template <class T>
75 const 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
integral_types.h
orfz_debug
int orfz_debug
operations_research::fz::ConvertAsIntegerOrDie
int64 ConvertAsIntegerOrDie(double d)
Definition: parser_util.cc:65
LOG
#define LOG(severity)
Definition: base/logging.h:420
ERROR
const int ERROR
Definition: log_severity.h:32
CHECK_GE
#define CHECK_GE(val1, val2)
Definition: base/logging.h:701
model.h
logging.h
operations_research::fz::Lookup
const T & Lookup(const std::vector< T > &v, int index)
Definition: parser_util.cc:75
operations_research::fz::Model
Definition: flatzinc/model.h:315
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
int64
int64_t int64
Definition: integral_types.h:34
YYSTYPE
#define YYSTYPE
Definition: parser.tab.cc:80
index
int index
Definition: pack.cc:508
context
GurobiMPCallbackContext * context
Definition: gurobi_interface.cc:509
operations_research::fz::Annotation::FUNCTION_CALL
@ FUNCTION_CALL
Definition: flatzinc/model.h:242
string_array.h
operations_research::fz::ParserContext
Definition: parser_util.h:29
operations_research::fz::AllDomainsHaveOneValue
bool AllDomainsHaveOneValue(const std::vector< Domain > &domains)
Definition: parser_util.cc:56
mathutil.h
CHECK_LE
#define CHECK_LE(val1, val2)
Definition: base/logging.h:699
operations_research::fz::ContainsId
bool ContainsId(std::vector< Annotation > *annotations, const std::string &id)
Definition: parser_util.cc:43
model
GRBmodel * model
Definition: gurobi_interface.cc:269
parser_util.h
orfz_error
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
stl_util.h
orfz_get_lineno
int orfz_get_lineno(void *scanner)
orfz_lex
int orfz_lex(YYSTYPE *, void *scanner)
operations_research::fz::Annotation::IDENTIFIER
@ IDENTIFIER
Definition: flatzinc/model.h:241