- Main Page
- Classes
- Files

- File List

# OptFrame/Modules/ExportModule.hpp

    00001 #ifndef EXPORT_HPP_
    00002 #define EXPORT_HPP_
    00003
    00004 #include "../OptFrameModule.hpp"
    00005
    00006 #include <stdio.h>
    00007
    00008 template<class R, class M>
    00009 class ExportModule: public OptFrameModule<R, M>
    00010 {
    00011 public:
    00012         string id()
    00013         {
    00014                 return "export";
    00015         }
    00016
    00017         string usage()
    00018         {
    00019                 string out = "\texport initsol id output_filename\n";
    00020                 out += "\texport loadsol id output_filename\nWHERE:\n";
    00021                 out += "\tid can assume two different type values:\n";
    00022                 out += "\t\t1 - an specific solution id,\n";
    00023                 // out += "\t\t2 - a set of solutions id's placed between the left ([) and right (]) brackets,\n"; // TODO
    00024                 out     += "\t\t2 - the special character (*) meaning that all solutions will be saved.\n";
    00025
    00026                 return out;
    00027         }
    00028
    00029         void run(vector<OptFrameModule<R, M>*> all_modules,
    00030                         HeuristicFactory<R, M>* factory, map<string, string>* dictionary,
    00031                         string input)
    00032         {
    00033                 //cout << "export: " << input << endl;
    00034                 Scanner scanner(input);
    00035
    00036                 if (!scanner.hasNext())
    00037                 {
    00038                         cout << "Usage: " << usage() << endl;
    00039                         return;
    00040                 }
    00041
    00042                 string sol = scanner.next();
    00043
    00044                 if (sol != "initsol" && sol != "loadsol")
    00045                 {
    00046                         cout << "First parameter must be either 'initsol' or 'loadsol'!"
    00047                                         << endl;
    00048                         cout << "Usage: " << usage() << endl;
    00049                         return;
    00050                 }
    00051
    00052                 string id = scanner.next();
    00053
    00054                 string filename = scanner.next();
    00055
    00056                 FILE * pFile;
    00057
    00058                 pFile = fopen(filename.c_str(), "w");
    00059
    00060                 if (id == "*") // Saving all solutions
    00061                 {
    00062                         if (sol == "initsol")
    00063                         {
    00064                                 for (int i = 0; i < (factory->initsol_size()); i++)
    00065                                 {
    00066                                         stringstream i_temp;
    00067                                         i_temp << i;
    00068
    00069                                         Scanner s2((sol + " " + i_temp.str()));
    00070
    00071                                         Solution<R>* s =
    00072                                                         &(factory->read_initsol(&s2))->generateSolution();
    00073
    00074                                         stringstream stream;
    00075
    00076                                         stream << s->getR() << endl;
    00077
    00078                                         fprintf(pFile, "%s", (stream.str()).c_str());
    00079                                 }
    00080                         }
    00081                         else
    00082                         {
    00083                                 for (int i = 0; i < (factory->loadsol_size()); i++)
    00084                                 {
    00085                                         stringstream i_temp;
    00086                                         i_temp << i;
    00087
    00088                                         Scanner s2((sol + " " + i_temp.str()));
    00089
    00090                                         Solution<R>* s = factory->read_loadsol(&s2);
    00091
    00092                                         stringstream stream;
    00093
    00094                                         stream << s->getR() << endl;
    00095
    00096                                         fprintf(pFile, "%s", (stream.str()).c_str());
    00097                                 }
    00098                         }
    00099                 }
    00100                 /*
    00101                 else if (id == "[") // TODO
    00102                 {
    00103                         bool right_bracket = false;
    00104
    00105                         vector<string> solutions;
    00106
    00107                         while (scanner.hasNext())
    00108                         {
    00109                                 string temp = scanner.next();
    00110
    00111                                 if (temp == "]")
    00112                                 {
    00113                                         right_bracket = true;
    00114                                         break;
    00115                                 }
    00116                                 else
    00117                                 {
    00118                                         solutions.push_back(scanner.next());
    00119                                 }
    00120                         }
    00121
    00122                         if (right_bracket)
    00123                         {
    00124                                 if (sol == "initsol")
    00125                                 {
    00126                                         for (int i = 0; i < (solutions.size()); i++)
    00127                                         {
    00128                                                 Scanner s2((sol + " " + solutions[i]));
    00129
    00130                                                 Solution<R>
    00131                                                                 * s =
    00132                                                                                 &(factory->read_initsol(&s2))->generateSolution();
    00133
    00134                                                 stringstream stream;
    00135
    00136                                                 stream << s->getR() << endl;
    00137
    00138                                                 fprintf(pFile, "%s", (stream.str()).c_str());
    00139                                         }
    00140                                 }
    00141                                 else
    00142                                 {
    00143                                         for (int i = 0; i < (solutions.size()); i++)
    00144                                         {
    00145                                                 Scanner s2((sol + " " + solutions[i]));
    00146
    00147                                                 Solution<R>* s = factory->read_loadsol(&s2);
    00148
    00149                                                 stringstream stream;
    00150
    00151                                                 stream << s->getR() << endl;
    00152
    00153                                                 fprintf(pFile, "%s", (stream.str()).c_str());
    00154                                         }
    00155                                 }
    00156                         }
    00157                         else
    00158                         {
    00159                                 cerr << "Right Bracket \"]\" not found." << endl;
    00160                                 //exit(1);
    00161                                 return;
    00162                         }
    00163                 }
    00164                 */
    00165                 else // Saving only solution
    00166                 {
    00167                         Scanner s2(sol + " " + id);
    00168
    00169                         Solution<R>* s;
    00170
    00171                         if (sol == "initsol")
    00172                                 s = &(factory->read_initsol(&s2))->generateSolution();
    00173                         else
    00174                                 s = factory->read_loadsol(&s2);
    00175
    00176                         stringstream stream;
    00177
    00178                         stream << s->getR() << endl;
    00179
    00180                         fprintf(pFile, "%s", (stream.str()).c_str());
    00181
    00182                 }
    00183
    00184                 fclose(pFile);
    00185         }
    00186 };
    00187
    00188 #endif /* EXPORT_HPP_ */

All Classes Functions