00001
00020 #include <cppad/cppad.hpp>
00021 #include "OSConfig.h"
00022 #include "OSInstance.h"
00023
00024 #include "OSResult.h"
00025 #include "OSiLReader.h"
00026 #include "OSiLWriter.h"
00027
00028
00029 #include "OSrLReader.h"
00030 #include "OSrLWriter.h"
00031 #include "OSFileUtil.h"
00032 #include "CoinError.hpp"
00033
00034 #include "OSWSUtil.h"
00035 #include "OShL.h"
00036 #include "OSErrorClass.h"
00037 #include "OSBase64.h"
00038 #include "OSMathUtil.h"
00039
00040
00041 #include <CoinMpsIO.hpp>
00042 #include <CoinPackedMatrix.hpp>
00043
00044
00045 #include "CoinError.hpp"
00046 #include "CoinHelperFunctions.hpp"
00047
00048 #ifdef HAVE_CTIME
00049 # include <ctime>
00050 #else
00051 # ifdef HAVE_TIME_H
00052 # include <time.h>
00053 # else
00054 # error "don't have header file for time"
00055 # endif
00056 #endif
00057
00058
00059 #ifdef HAVE_CMATH
00060 # include <cmath>
00061 #else
00062 # ifdef HAVE_CMATH_H
00063 # include <cmath.h>
00064 # endif
00065 #endif
00066
00067
00068
00069 #ifdef HAVE_CSTDIO
00070 # include <cstdio>
00071 #else
00072 # ifdef HAVE_STDIO_H
00073 # include <stdio.h>
00074 # else
00075 # error "don't have header file for stdio"
00076 # endif
00077 #endif
00078
00079
00080
00081
00082
00083 using std::cout;
00084 using std::endl;
00085 using std::ostringstream;
00086
00087
00088
00089 bool interactiveShell(std::string *schema, std::string *testFileName, std::string *outFileName,
00090 bool *compress, bool *addWhiteSpace, bool *verifyObjects);
00091 void printHelp();
00092 std::string getUserInput(bool defaultPresent);
00093
00094
00095 int main(int argC, char* argV[])
00096 {
00097 WindowsErrorPopupBlocker();
00098
00099
00100 FileUtil *fileUtil = NULL;
00101 OSiLReader *osilreader = NULL;
00102
00103 OSrLReader *osrlreader = NULL;
00104
00105
00106 std::string testFileName;
00107 std::string outFileName;
00108 std::string osxl;
00109 std::string schema;
00110 ostringstream parsingTestResult;
00111 ostringstream parsingTestResultFailure;
00112
00113 std::string outputString;
00114
00115 bool ok;
00116 bool doOutput = false;
00117 bool compress = true;
00118 bool addWhiteSpace = true;
00119 bool verifyObjects = false;
00120 double check;
00121
00122
00123
00124 if (argC == 1)
00125 {
00126 if (interactiveShell(&schema, &testFileName, &outFileName, &compress, &addWhiteSpace, &verifyObjects) != true) return 0;
00127
00128 if (outFileName != "") doOutput = true;
00129
00130 #ifdef MY_DEBUG
00131 std::cout << "User dialog completed " << std::endl;
00132 #endif
00133 }
00134 else
00135 {
00136 if( argC > 7)
00137 {
00138 std::cout << "Too Many Input Parameters" << std::endl;
00139 return 1;
00140 }
00141 if ( (strcmp(argV[1],"--help") == 0) || (strcmp(argV[1],"-h") == 0) || (strcmp(argV[1],"-?") == 0) )
00142 {
00143 printHelp();
00144 return 0;
00145 }
00146 if ( (strcmp(argV[1],"quit") == 0) || (strcmp(argV[1],"exit") == 0) || (strcmp(argV[1],"stop") == 0) )
00147 return 0;
00148 if (argC < 3 || ((strcmp(argV[1],"osil") != 0) && (strcmp(argV[1],"osol") != 0) && (strcmp(argV[1],"osrl") != 0)))
00149 {
00150 std::cout << "usage: parsingtest <parser> <filename> [<outputFile> [(no)compress (no)addWhiteSpace (noverify]]" << std::endl;
00151 std::cout << "type \"parsingtest --help\" for more information" << std::endl;
00152 return 1;
00153 }
00154 schema = argV[1];
00155 testFileName = argV[2];
00156 if (argC > 3)
00157 {
00158 outFileName = argV[3];
00159 doOutput = true;
00160 }
00161 if (argC > 4)
00162 compress = (strcmp(argV[4],"nocompress") != 0);
00163 if (argC > 5)
00164 addWhiteSpace = (strcmp(argV[5],"noaddwhitespace") != 0);
00165 if (argC == 7)
00166 verifyObjects = (strcmp(argV[6],"verifyobjects") == 0);
00167 }
00168
00169 fileUtil = new FileUtil();
00170
00171 try
00172 {
00173
00174 #ifdef MY_DEBUG
00175 std::cout << "Processing file: " ;
00176 std::cout << testFileName << std::endl;
00177 #endif
00178
00179 osxl = fileUtil->getFileAsString( testFileName.c_str() );
00180
00181 #ifdef MY_DEBUG
00182 std::cout << "Done reading the file" << std::endl;
00183 #endif
00184
00185 if (schema == "osil")
00186 {
00187 osilreader = new OSiLReader();
00188 OSInstance *osinstance;
00189 osinstance = osilreader->readOSiL( osxl);
00190
00191 #ifdef MY_DEBUG
00192 parsingTestResult << "Reading files successfully" << std::endl;
00193 #endif
00194
00195 if (doOutput == true)
00196 {
00197 OSiLWriter *osilwriter;
00198 osilwriter = new OSiLWriter();
00199 osilwriter->m_bWriteBase64 = compress;
00200 osilwriter->m_bWhiteSpace = addWhiteSpace;
00201 #ifdef MY_DEBUG
00202 std::cout << "Create output string from in-memory object" << std::endl;
00203 #endif
00204 outputString = osilwriter->writeOSiL(osinstance);
00205 #ifdef MY_DEBUG
00206 std::cout << "Created output string from in-memory object" << std::endl;
00207 #endif
00208 if (fileUtil->writeFileFromString( outFileName.c_str(), outputString) != true)
00209 throw ErrorClass(" Error while writing compressed file");
00210
00211 if (verifyObjects == true)
00212 {
00213 osxl = fileUtil->getFileAsString( outFileName.c_str() );
00214 OSInstance *osinstance2;
00215 osinstance2 = osilreader->readOSiL( osxl);
00216 if (osinstance2->IsEqual(osinstance) == false)
00217 throw ErrorClass("Two objects are not equal!");
00218 }
00219
00220 delete osilwriter;
00221 osilwriter = NULL;
00222 }
00223 delete osilreader;
00224 osilreader = NULL;
00225 }
00226 else if (schema == "osol")
00227 {
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 }
00268 else if (schema == "osrl")
00269 {
00270 osrlreader = new OSrLReader();
00271 OSResult *osresult;
00272 osresult = osrlreader->readOSrL( osxl);
00273
00274 #ifdef MY_DEBUG
00275 parsingTestResult << "Reading files successfully" << std::endl;
00276 #endif
00277
00278 if (doOutput == true)
00279 {
00280 OSrLWriter *osrlwriter;
00281 osrlwriter = new OSrLWriter();
00282 osrlwriter->m_bWriteBase64 = compress;
00283 osrlwriter->m_bWhiteSpace = addWhiteSpace;
00284 #ifdef MY_DEBUG
00285 std::cout << "Create output string from in-memory object" << std::endl;
00286 #endif
00287 outputString = osrlwriter->writeOSrL(osresult);
00288 #ifdef MY_DEBUG
00289 std::cout << "Created output string from in-memory object" << std::endl;
00290 #endif
00291 if (fileUtil->writeFileFromString( outFileName.c_str(), outputString) != true)
00292 throw ErrorClass(" Error while writing compressed file");
00293
00294 if (verifyObjects == true)
00295 {
00296 osxl = fileUtil->getFileAsString( outFileName.c_str() );
00297 OSrLReader *osrlreader2;
00298 osrlreader2 = new OSrLReader();
00299 OSResult *osresult2;
00300 osresult2 = osrlreader2->readOSrL( osxl);
00301 #ifdef MY_DEBUG
00302 std::cout << "Compare in-memory objects" << std::endl;
00303 #endif
00304 if (osresult2->IsEqual(osresult) == false)
00305 throw ErrorClass("Two objects are not equal!");
00306 #ifdef MY_DEBUG
00307 else
00308 std::cout << "in-memory objects compare equal" << std::endl;
00309 #endif
00310 delete osrlreader2;
00311 osrlreader2 = NULL;
00312 }
00313
00314 delete osrlwriter;
00315 osrlwriter = NULL;
00316 }
00317 delete osrlreader;
00318 osrlreader = NULL;
00319 }
00320 }
00321 catch(const ErrorClass& eclass)
00322 {
00323 cout << endl << endl << endl;
00324 if (osilreader != NULL) delete osilreader;
00325
00326 if (osrlreader != NULL) delete osrlreader;
00327
00328 parsingTestResultFailure << eclass.errormsg << endl;
00329 parsingTestResultFailure << "There was a failure in the " + schema + " parser; ";
00330 parsingTestResultFailure << "file " << testFileName << " not processed." << endl;
00331 }
00332
00333 delete fileUtil;
00334 fileUtil = NULL;
00335
00336 if (parsingTestResultFailure.str().length() > 0)
00337 {
00338 cout << "PARSING TEST FAILED ON THE FOLLOWING:" << endl << endl;
00339 cout << parsingTestResultFailure.str() << endl << endl;
00340 return 1;
00341 }
00342 else
00343 {
00344
00345 #ifdef MY_DEBUG
00346 std::cout << "PARSING TEST SUCCEEDED" << std::endl;
00347 #endif
00348
00349 return 0;
00350 }
00351 }
00352
00353 bool interactiveShell(std::string *schema, std::string *testFileName, std::string *outFileName,
00354 bool *compress, bool *addWhiteSpace, bool *verifyObjects)
00355 {
00356 std::string userInput;
00357 bool haveParser = false;
00358 std::cout << "Welcome to the parser test, which is part of the Optimization Services suite" << std::endl << std::endl;
00359
00360 std::cout << "This program (c) 2010 Horand Gassmann, Jun Ma and Kipp Martin" << std::endl << std::endl;
00361
00362 while (haveParser == false)
00363 {
00364 std::cout << "Input the parser you want to test (osil|osol|osrl)" << std::endl;
00365 std::cout << " or \"quit\" to exit the program > ";
00366 *schema = getUserInput(false);
00367 if (*schema == "osil") break;
00368 if (*schema == "osol") break;
00369 if (*schema == "osrl") break;
00370 if (*schema == "quit") return false;
00371 }
00372
00373 std::cout << "Input the file you want to parse > ";
00374 *testFileName = getUserInput(false);
00375
00376 std::cout << std::endl << "The remaining arguments are optional." << std::endl;
00377
00378 std::cout << "Name of the output file to which you want to print the object after processing" << std::endl;
00379 std::cout << "> ";
00380 *outFileName = getUserInput(true);
00381
00382 if (*outFileName == "")
00383 std::cout << "No output generated; remaining options skipped." << std::endl;
00384 else
00385 {
00386 std::cout << "Do you want to compress the output (using mult and incr attributes where possible)? (YES|no) > ";
00387 userInput = getUserInput(true);
00388 if ( (userInput == "no") || (userInput == "NO") || (userInput == "No") || (userInput == "N") || (userInput == "n") )
00389 *compress = false;
00390
00391 std::cout << "Do you want to add whitespace (newline characters) between elements? (YES|no) > ";
00392 userInput = getUserInput(true);
00393 if ( (userInput == "no") || (userInput == "NO") || (userInput == "No") ) *addWhiteSpace = false;
00394
00395 std::cout << "Do you want to verify that input and output represents the same object? (yes|NO) > ";
00396 userInput = getUserInput(true);
00397 if ( (userInput == "yes") || (userInput == "YES") || (userInput == "Yes")|| (userInput == "Y") || (userInput == "y") )
00398 *verifyObjects = true;
00399 }
00400 std::cout << std::endl << "echo input parameters" << std::endl;
00401 std::cout << "schema: \"" << *schema << "\"" << std::endl;
00402 std::cout << "testFileName: \"" << *testFileName << "\"" << std::endl;
00403 std::cout << "outFileName: \"" << *outFileName << "\"" << std::endl;
00404 std::cout << "compress: \"" << *compress << "\"" << std::endl;
00405 std::cout << "addWhiteSpace: \"" << *addWhiteSpace << "\"" << std::endl;
00406 std::cout << "verifyObjects: \"" << *verifyObjects << "\"" << std::endl;
00407 return true;
00408 }
00409
00410 void printHelp()
00411 {
00412 std::cout << "usage: parsertest <parser> <filename>" << std::endl;
00413 std::cout << " [<outfile> [(no)compress (no)addwhitespace (no)verify]]" << std::endl << std::endl;
00414
00415 std::cout << "where <parser> is \'osil\' or \'osol\' or \'osrl\'" << std::endl;
00416 std::cout << " <filename> is the (path to the) input file you want to parse" << std::endl;
00417 std::cout << " <outfile> is the (path to the) output file to print the object just read" << std::endl;
00418 std::cout << " compress|nocompress controls the use of mult and incr attributes" << std::endl;
00419 std::cout << " to compress the output file. (The default is compress)" << std::endl;
00420 std::cout << " addwhitespace|noaddwhitespace governs the use of whitespace (linefeed" << std::endl;
00421 std::cout << " characters between elements. (The default is addwhitespace)" << std::endl;
00422 std::cout << " verify|noverify governs checks whether the input and output files " << std::endl;
00423 std::cout << " represent the same in-memory object. (The default is noverify)" << std::endl;
00424 return;
00425 }
00426
00427 std::string getUserInput(bool defaultPresent)
00428 {
00429 std::string lineText;
00430 bool validName;
00431
00432 std::string wordSep = " ";
00433 std::string optionName = "";
00434 std::string optionValue = "";
00435 std::string::size_type indexStart;
00436 std::string::size_type indexEnd;
00437
00438 for (;;)
00439 {
00440 getline(std::cin, lineText);
00441 lineText = " " + lineText + " ";
00442 indexStart = lineText.find_first_not_of(wordSep);
00443 if (indexStart != string::npos)
00444 {
00445 indexEnd = lineText.find_first_of(wordSep, indexStart + 1);
00446 return lineText.substr(indexStart, indexEnd - indexStart);
00447 }
00448 if (defaultPresent) return "";
00449 std::cout << std::endl;
00450 std::cout << "You did not enter a valid option." << std::endl;
00451 }
00452 return "";
00453 }