00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <stddef.h>
00015 #include <string.h>
00016 #include "base/hash.h"
00017 #include "base/hash.h"
00018 #include <string>
00019 #include <vector>
00020
00021 #include "base/integral_types.h"
00022 #include "base/logging.h"
00023 #include "base/macros.h"
00024 #include "base/scoped_ptr.h"
00025 #include "base/concise_iterator.h"
00026 #include "base/map-util.h"
00027 #include "base/stl_util.h"
00028 #include "constraint_solver/constraint_solver.h"
00029 #include "constraint_solver/constraint_solveri.h"
00030
00031 namespace operations_research {
00032
00033
00034 const string& ArgumentHolder::TypeName() const {
00035 return type_name_;
00036 }
00037
00038 void ArgumentHolder::SetTypeName(const string& type_name) {
00039 type_name_ = type_name;
00040 }
00041
00042 void ArgumentHolder::SetIntegerArgument(const string& arg_name, int64 value) {
00043 integer_argument_[arg_name] = value;
00044 }
00045
00046 void ArgumentHolder::SetIntegerArrayArgument(
00047 const string& arg_name,
00048 const int64* const values,
00049 int size) {
00050 for (int i = 0; i < size; ++i) {
00051 integer_array_argument_[arg_name].push_back(values[i]);
00052 }
00053 }
00054
00055 void ArgumentHolder::SetIntegerMatrixArgument(
00056 const string& arg_name,
00057 const IntTupleSet& values) {
00058 std::pair<string, IntTupleSet> to_insert= std::make_pair(arg_name, values);
00059 matrix_argument_.insert(to_insert);
00060 }
00061
00062 void ArgumentHolder::SetIntegerExpressionArgument(
00063 const string& arg_name,
00064 const IntExpr* const expr) {
00065 integer_expression_argument_[arg_name] = expr;
00066 }
00067
00068 void ArgumentHolder::SetIntegerVariableArrayArgument(
00069 const string& arg_name,
00070 const IntVar* const * const vars,
00071 int size) {
00072 for (int i = 0; i < size; ++i) {
00073 integer_variable_array_argument_[arg_name].push_back(vars[i]);
00074 }
00075 }
00076
00077 void ArgumentHolder::SetIntervalArgument(
00078 const string& arg_name,
00079 const IntervalVar* const var) {
00080 interval_argument_[arg_name] = var;
00081 }
00082
00083 void ArgumentHolder::SetIntervalArrayArgument(
00084 const string& arg_name,
00085 const IntervalVar* const * const vars,
00086 int size) {
00087 for (int i = 0; i < size; ++i) {
00088 interval_array_argument_[arg_name].push_back(vars[i]);
00089 }
00090 }
00091
00092 void ArgumentHolder::SetSequenceArgument(
00093 const string& arg_name,
00094 const SequenceVar* const var) {
00095 sequence_argument_[arg_name] = var;
00096 }
00097
00098 void ArgumentHolder::SetSequenceArrayArgument(
00099 const string& arg_name,
00100 const SequenceVar* const * const vars,
00101 int size) {
00102 for (int i = 0; i < size; ++i) {
00103 sequence_array_argument_[arg_name].push_back(vars[i]);
00104 }
00105 }
00106
00107 bool ArgumentHolder::HasIntegerExpressionArgument(
00108 const string& arg_name) const {
00109 return ContainsKey(integer_expression_argument_, arg_name);
00110 }
00111
00112 bool ArgumentHolder::HasIntegerVariableArrayArgument(
00113 const string& arg_name) const {
00114 return ContainsKey(integer_variable_array_argument_, arg_name);
00115 }
00116
00117
00118 int64 ArgumentHolder::FindIntegerArgumentWithDefault(
00119 const string& arg_name,
00120 int64 def) const {
00121 return FindWithDefault(integer_argument_, arg_name, def);
00122 }
00123
00124 int64 ArgumentHolder::FindIntegerArgumentOrDie(const string& arg_name) const {
00125 return FindOrDie(integer_argument_, arg_name);
00126 }
00127
00128 const std::vector<int64>& ArgumentHolder::FindIntegerArrayArgumentOrDie(
00129 const string& arg_name) const {
00130 return FindOrDie(integer_array_argument_, arg_name);
00131 }
00132
00133
00134 const IntExpr* ArgumentHolder::FindIntegerExpressionArgumentOrDie(
00135 const string& arg_name) const {
00136 return FindOrDie(integer_expression_argument_, arg_name);
00137 }
00138
00139 const std::vector<const IntVar*>&
00140 ArgumentHolder::FindIntegerVariableArrayArgumentOrDie(
00141 const string& arg_name) const {
00142 return FindOrDie(integer_variable_array_argument_, arg_name);
00143 }
00144
00145 const IntTupleSet& ArgumentHolder::FindIntegerMatrixArgumentOrDie(
00146 const string& arg_name) const {
00147 return FindOrDie(matrix_argument_, arg_name);
00148 }
00149
00150
00151
00152 ModelParser::ModelParser() {}
00153
00154 ModelParser::~ModelParser() {
00155 CHECK(holders_.empty());
00156 }
00157
00158 void ModelParser::BeginVisitModel(const string& solver_name) {
00159 PushArgumentHolder();
00160 }
00161
00162 void ModelParser::EndVisitModel(const string& solver_name) {
00163 PopArgumentHolder();
00164 }
00165
00166 void ModelParser::BeginVisitConstraint(const string& type_name,
00167 const Constraint* const constraint) {
00168 PushArgumentHolder();
00169 }
00170
00171 void ModelParser::EndVisitConstraint(const string& type_name,
00172 const Constraint* const constraint) {
00173
00174 PopArgumentHolder();
00175 }
00176
00177 void ModelParser::BeginVisitIntegerExpression(const string& type_name,
00178 const IntExpr* const expr) {
00179 PushArgumentHolder();
00180 }
00181
00182 void ModelParser::EndVisitIntegerExpression(const string& type_name,
00183 const IntExpr* const expr) {
00184
00185 PopArgumentHolder();
00186 }
00187
00188 void ModelParser::VisitIntegerVariable(const IntVar* const variable,
00189 const IntExpr* const delegate) {
00190
00191 }
00192
00193 void ModelParser::VisitIntegerVariable(const IntVar* const variable,
00194 const string& operation,
00195 int64 value,
00196 const IntVar* const delegate) {
00197 delegate->Accept(this);
00198
00199 }
00200
00201 void ModelParser::VisitIntervalVariable(const IntervalVar* const variable,
00202 const string& operation,
00203 const IntervalVar* const delegate) {
00204 if (delegate != NULL) {
00205 delegate->Accept(this);
00206 }
00207
00208 }
00209
00210 void ModelParser::VisitIntervalVariable(const IntervalVar* const variable,
00211 const string& operation,
00212 const IntervalVar* const * delegates,
00213 int size) {
00214 for (int i = 0; i < size; ++i) {
00215 delegates[i]->Accept(this);
00216 }
00217
00218 }
00219
00220 void ModelParser::VisitSequenceVariable(const SequenceVar* const variable) {
00221
00222 }
00223
00224
00225 void ModelParser::VisitIntegerArgument(const string& arg_name, int64 value) {
00226 Top()->SetIntegerArgument(arg_name, value);
00227 }
00228
00229 void ModelParser::VisitIntegerArrayArgument(const string& arg_name,
00230 const int64* const values,
00231 int size) {
00232 Top()->SetIntegerArrayArgument(arg_name, values, size);
00233 }
00234
00235 void ModelParser::VisitIntegerMatrixArgument(const string& arg_name,
00236 const IntTupleSet& values) {
00237 Top()->SetIntegerMatrixArgument(arg_name, values);
00238 }
00239
00240
00241 void ModelParser::VisitIntegerExpressionArgument(
00242 const string& arg_name,
00243 const IntExpr* const argument) {
00244 Top()->SetIntegerExpressionArgument(arg_name, argument);
00245 argument->Accept(this);
00246 }
00247
00248 void ModelParser::VisitIntegerVariableArrayArgument(
00249 const string& arg_name,
00250 const IntVar* const * arguments,
00251 int size) {
00252 Top()->SetIntegerVariableArrayArgument(arg_name, arguments, size);
00253 for (int i = 0; i < size; ++i) {
00254 arguments[i]->Accept(this);
00255 }
00256 }
00257
00258
00259 void ModelParser::VisitIntervalArgument(
00260 const string& arg_name,
00261 const IntervalVar* const argument) {
00262 Top()->SetIntervalArgument(arg_name, argument);
00263 argument->Accept(this);
00264 }
00265
00266 void ModelParser::VisitIntervalArrayArgument(
00267 const string& arg_name,
00268 const IntervalVar* const * arguments,
00269 int size) {
00270 Top()->SetIntervalArrayArgument(arg_name, arguments, size);
00271 for (int i = 0; i < size; ++i) {
00272 arguments[i]->Accept(this);
00273 }
00274 }
00275
00276
00277 void ModelParser::VisitSequenceArgument(
00278 const string& arg_name,
00279 const SequenceVar* const argument) {
00280 Top()->SetSequenceArgument(arg_name, argument);
00281 argument->Accept(this);
00282 }
00283
00284 void ModelParser::VisitSequenceArrayArgument(
00285 const string& arg_name,
00286 const SequenceVar* const * arguments,
00287 int size) {
00288 Top()->SetSequenceArrayArgument(arg_name, arguments, size);
00289 for (int i = 0; i < size; ++i) {
00290 arguments[i]->Accept(this);
00291 }
00292 }
00293
00294 void ModelParser::PushArgumentHolder() {
00295 holders_.push_back(new ArgumentHolder);
00296 }
00297
00298 void ModelParser::PopArgumentHolder() {
00299 CHECK(!holders_.empty());
00300 delete holders_.back();
00301 holders_.pop_back();
00302 }
00303
00304 ArgumentHolder* ModelParser::Top() const {
00305 CHECK(!holders_.empty());
00306 return holders_.back();
00307 }
00308 }