pelib  2.0.0
src/ampl.cpp
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Crown.
00005 
00006  Crown is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  Crown is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Crown. If not, see <http://www.gnu.org/licenses/>.
00018 
00019 */
00020 
00021 
00022 #include <fstream>
00023 
00024 #include <pelib/Algebra.hpp>
00025 #include <pelib/AmplSolver.hpp>
00026 #include <pelib/calculus.h>
00027 
00028 #ifdef debug
00029 #undef debug
00030 #endif
00031 
00032 #define debug(var) cout << "[" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << "] " << #var << " = \"" << (var) << "\"" << endl;
00033 
00034 using namespace std;
00035 using namespace pelib;
00036 
00037 struct args
00038 {
00039         char *model, *run, *model_file;
00040         bool showError, showOutput;
00041 };
00042 typedef struct args args_t;
00043 
00044 static args_t
00045 parse(char **args)
00046 {
00047         args_t out;
00048         out.model = NULL;
00049         out.model_file = (char*)"model.mod";
00050         out.run = NULL;
00051         out.showError = false;
00052         out.showOutput = false;
00053 
00054         for(; args[0] != NULL; args++)
00055         {
00056                 if(strcmp(args[0], "--model") == 0)
00057                 {
00058                         args++;
00059                         out.model = args[0];
00060                         continue;
00061                 }
00062                 if(strcmp(args[0], "--model-name") == 0)
00063                 {
00064                         args++;
00065                         out.model_file = args[0];
00066                         continue;
00067                 }
00068                 if(strcmp(args[0], "--run") == 0)
00069                 {
00070                         args++;
00071                         out.run = args[0];
00072                         continue;
00073                 }
00074                 if(strcmp(args[0], "--show-stdout") == 0)
00075                 {
00076                         out.showOutput = true;
00077                         continue;
00078                 }
00079                 if(strcmp(args[0], "--show-stderr") == 0)
00080                 {
00081                         out.showError = true;
00082                         continue;
00083                 }
00084         }
00085 
00086         return out;
00087 }
00088 
00089 const Algebra*
00090 pelib_calculus(const map<const string, const Algebra> &data, size_t argc, char**argv, map<const string, double> &statistics)
00091 {
00092         args_t args = parse(argv);
00093         ifstream model(args.model);
00094         ifstream run(args.run);
00095         const Algebra *res = AmplSolver(model, args.model_file, run, data, args.showOutput, args.showError).solve(statistics);
00096         return res;
00097 }
00098 
00099 void
00100 pelib_delete(Algebra *data)
00101 {
00102         delete data;
00103 }
00104