00001
00018 #include <cppad/cppad.hpp>
00019 #include "OSConfig.h"
00020 #include "OSInstance.h"
00021 #include "OSOption.h"
00022 #include "OSResult.h"
00023 #include "OSiLReader.h"
00024 #include "OSiLWriter.h"
00025 #include "OSoLReader.h"
00026 #include "OSoLWriter.h"
00027 #include "OSrLReader.h"
00028 #include "OSrLWriter.h"
00029 #include "OSFileUtil.h"
00030 #include "CoinError.hpp"
00031
00032 #include "OSWSUtil.h"
00033 #include "OShL.h"
00034 #include "OSErrorClass.h"
00035 #include "OSBase64.h"
00036 #include "OSMathUtil.h"
00037
00038
00039 #include <CoinMpsIO.hpp>
00040 #include <CoinPackedMatrix.hpp>
00041
00042
00043 #include "CoinError.hpp"
00044 #include "CoinHelperFunctions.hpp"
00045
00046 #ifdef HAVE_CTIME
00047 # include <ctime>
00048 #else
00049 # ifdef HAVE_TIME_H
00050 # include <time.h>
00051 # else
00052 # error "don't have header file for time"
00053 # endif
00054 #endif
00055
00056
00057 #ifdef HAVE_CMATH
00058 # include <cmath>
00059 #else
00060 # ifdef HAVE_CMATH_H
00061 # include <cmath.h>
00062 # endif
00063 #endif
00064
00065
00066
00067 #ifdef HAVE_CSTDIO
00068 # include <cstdio>
00069 #else
00070 # ifdef HAVE_STDIO_H
00071 # include <stdio.h>
00072 # else
00073 # error "don't have header file for stdio"
00074 # endif
00075 #endif
00076
00077
00078
00079
00080
00081 using std::cout;
00082 using std::endl;
00083 using std::ostringstream;
00084
00085 #define MY_DEBUG
00086
00087 bool interactiveShell(std::string *schema, std::string *testFileName, std::string *outFileName,
00088 bool *compress, bool *addWhiteSpace, bool *verifyObjects,
00089 unsigned int *seed, int *nrep, double *density, bool *conformant);
00090 void printHelp();
00091 std::string getUserInput(bool defaultPresent);
00092
00093
00094 int main(int argC, char* argV[])
00095 {
00096 WindowsErrorPopupBlocker();
00097
00098
00099 FileUtil *fileUtil = NULL;
00100 OSiLReader *osilreader = NULL;
00101 OSoLReader *osolreader = NULL;
00102 OSrLReader *osrlreader = NULL;
00103
00104
00105 std::string testFileName;
00106 std::string outFileName;
00107 std::string osxl;
00108 std::string schema;
00109 ostringstream parsingTestResult;
00110 ostringstream parsingTestResultFailure;
00111
00112 std::string outputString;
00113
00114 bool doOutput = false;
00115 bool compress = true;
00116 bool addWhiteSpace = true;
00117 bool verifyObjects = true;
00118 bool useRandomObjects = true;
00119
00120 int nrep = 1;
00121 unsigned int seed;
00122 double density = 0.5;
00123 bool conformant = true;
00124
00125 seed = time(NULL);
00126
00127
00128
00129 if (argC == 1)
00130 {
00131 if (interactiveShell(&schema, &testFileName, &outFileName, &compress, &addWhiteSpace, &verifyObjects,
00132 &seed, &nrep, &density, &conformant) != true) return 0;
00133
00134 #ifdef MY_DEBUG
00135 std::cout << "User dialog completed " << std::endl;
00136 #endif
00137 }
00138 else
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 {
00157 int argM = 12;
00158 if( argC > argM)
00159 {
00160 std::cout << "Too Many Input Parameters" << std::endl;
00161 return 1;
00162 }
00163 if ( (strcmp(argV[1],"--help") == 0) || (strcmp(argV[1],"-h") == 0) || (strcmp(argV[1],"-?") == 0) )
00164 {
00165 printHelp();
00166 return 0;
00167 }
00168 if ( (strcmp(argV[1],"quit") == 0) || (strcmp(argV[1],"exit") == 0) || (strcmp(argV[1],"stop") == 0) )
00169 return 0;
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 size_t epos;
00192 std::string argument, option, value;
00193 for (int i=1; i<argC; i++)
00194 {
00195 if (argV[i][0] != '-')
00196 throw ErrorClass(" Error while writing compressed file");
00197 argument = std::string(argV[i]);
00198 epos = argument.find('=');
00199 option = argument.substr(0,epos);
00200 if (epos < std::string::npos)
00201 value = argument.substr(epos+1);
00202 else
00203 value = "";
00204
00205 std::cout << "option = \"" << option << "\"" << std::endl;
00206 std::cout << "value = \"" << value << "\"" << std::endl;
00207
00208 if (option == "-schema")
00209 {
00210 if (value == "")
00211 throw ErrorClass("schema option must have a value");
00212 schema = value;
00213 }
00214 else if (option == "-file")
00215 {
00216 if (value == "")
00217 throw ErrorClass("file option must have a value");
00218 testFileName = value;
00219 useRandomObjects = false;
00220 }
00221 else if (option == "-replications")
00222 {
00223 if (value == "")
00224 throw ErrorClass("number of replications must be given");
00225 nrep = atoi(value.c_str());
00226 }
00227 else if (option == "-seed")
00228 {
00229 if (value == "")
00230 throw ErrorClass("seed option must have a value");
00231 seed = atoi(value.c_str());
00232 }
00233 else if (option == "-density")
00234 {
00235 if (value == "")
00236 throw ErrorClass("density option must have a value");
00237 density = os_strtod(value.c_str(), NULL);
00238 }
00239 else if (option == "-outfile")
00240 {
00241 if (value == "")
00242 throw ErrorClass("outfile option must have a value");
00243 outFileName = value;
00244 }
00245 else if (option == "-random")
00246 {
00247 useRandomObjects = (value == "YES" || value == "yes" || value == "Y" || value == "Y" || value == "y" || value == "");
00248 testFileName = "";
00249 }
00250 else if (option == "-conformant")
00251 {
00252 conformant = (value == "YES" || value == "yes" || value == "Y" || value == "Y" || value == "y" || value == "");
00253 }
00254 else if (option == "-compress")
00255 {
00256 compress = (value == "YES" || value == "yes" || value == "Y" || value == "Y" || value == "y" || value == "");
00257 }
00258 else if (option == "-whitespace")
00259 {
00260 addWhiteSpace = (value == "YES" || value == "yes" || value == "Y" || value == "Y" || value == "y" || value == "");
00261 }
00262 else if (option == "-verify")
00263 {
00264 if (value == "")
00265 verifyObjects = true;
00266 else
00267 verifyObjects = !(value == "NO" || value == "no" || value == "N" || value == "N" || value == "");
00268
00269 if (verifyObjects)
00270 {
00271 if (outFileName == "") outFileName = "junk.tmp";
00272 }
00273 }
00274 else if (option == "-norandom")
00275 {
00276 useRandomObjects = false;
00277 }
00278 else if (option == "-noconformant")
00279 {
00280 conformant = false;
00281 }
00282 else if (option == "-nocompress")
00283 {
00284 compress = false;
00285 }
00286 else if (option == "-nowhitespace")
00287 {
00288 addWhiteSpace = false;
00289 }
00290 else if (option == "-noverify")
00291 {
00292 verifyObjects = false;
00293 }
00294 else
00295 {
00296 std::cout << "option \"" << option << "\" not recognized" << std::endl;
00297 }
00298
00299 }
00300 }
00301
00302 if (outFileName != "") doOutput = true;
00303
00304 fileUtil = new FileUtil();
00305
00306 try
00307 {
00308
00309
00310
00311
00312 if (useRandomObjects)
00313 {
00314 if (schema == "osil")
00315 {
00316 for (int irep=0; irep < nrep; irep++)
00317 {
00318
00319
00320
00321
00322
00323
00324 }
00325 }
00326 else if (schema == "osol")
00327 {
00328 OSOption *osoption, *osoption2;
00329 OSoLWriter *osolwriter;
00330 osolwriter = new OSoLWriter();
00331 OSoLReader *osolreader;
00332
00333 for (int irep=0; irep < nrep; irep++)
00334 {
00335 std::cout << "use random number seed: " << seed << std::endl;
00336
00337 srand(seed);
00338
00339 std::cout << "set random osoption" << std::endl;
00340 osoption = new OSOption();
00341 osoption->setRandom(density, conformant);
00342
00343 std::cout << "write to string" << std::endl;
00344
00345 osxl = osolwriter->writeOSoL(osoption);
00346
00347 std::cout << osxl << std::endl;
00348
00349 osolreader = new OSoLReader();
00350 osoption2 = osolreader->readOSoL( osxl);
00351 if (verifyObjects == true)
00352 {
00353 if (osoption->IsEqual(osoption2) == false)
00354 throw ErrorClass("Two objects are not equal!");
00355 }
00356 delete osoption;
00357 delete osolreader;
00358 osoption = NULL;
00359 osolreader = NULL;
00360
00361 seed = rand();
00362 }
00363 }
00364 else if (schema == "osrl")
00365 {
00366 OSResult *osresult, *osresult2;
00367 OSrLWriter *osrlwriter;
00368 osrlwriter = new OSrLWriter();
00369 OSrLReader *osrlreader;
00370
00371 for (int irep=0; irep < nrep; irep++)
00372 {
00373 std::cout << "use random number seed: " << seed << std::endl;
00374
00375 srand(seed);
00376
00377 std::cout << "set random osresult" << std::endl;
00378 osresult = new OSResult();
00379 osresult->setRandom(density, conformant);
00380
00381 std::cout << "write to string" << std::endl;
00382
00383 osxl = osrlwriter->writeOSrL(osresult);
00384
00385 std::cout << osxl << std::endl;
00386
00387 osrlreader = new OSrLReader();
00388 osresult2 = osrlreader->readOSrL( osxl);
00389 if (verifyObjects == true)
00390 {
00391 if (osresult->IsEqual(osresult2) == false)
00392 throw ErrorClass("Two objects are not equal!");
00393 }
00394 delete osresult;
00395 delete osrlreader;
00396 osresult = NULL;
00397 osrlreader = NULL;
00398
00399 seed = rand();
00400 }
00401 }
00402 }
00403 else if (testFileName == "")
00404 throw ErrorClass("No filename given. Nothing to parse.");
00405 else
00406 {
00407 #ifdef MY_DEBUG
00408 std::cout << "Processing file: " ;
00409 std::cout << testFileName << std::endl;
00410 #endif
00411
00412 osxl = fileUtil->getFileAsString( testFileName.c_str() );
00413
00414 #ifdef MY_DEBUG
00415 std::cout << "Done reading the file into memory" << std::endl;
00416 #endif
00417
00418
00419
00420
00421 if (schema == "osil")
00422 {
00423 osilreader = new OSiLReader();
00424 OSInstance *osinstance;
00425 #ifdef MY_DEBUG
00426 std::cout << "Start parsing the file" << std::endl;
00427 #endif
00428 osinstance = osilreader->readOSiL( osxl);
00429
00430 #ifdef MY_DEBUG
00431 parsingTestResult << "Parsed file successfully" << std::endl;
00432 #endif
00433
00434 if (doOutput == true)
00435 {
00436 OSiLWriter *osilwriter;
00437 osilwriter = new OSiLWriter();
00438 osilwriter->m_bWriteBase64 = compress;
00439 osilwriter->m_bWhiteSpace = addWhiteSpace;
00440 #ifdef MY_DEBUG
00441 std::cout << "Create output string from in-memory object" << std::endl;
00442 #endif
00443 outputString = osilwriter->writeOSiL(osinstance);
00444 #ifdef MY_DEBUG
00445 std::cout << "Created output string from in-memory object" << std::endl;
00446 #endif
00447 if (fileUtil->writeFileFromString( outFileName.c_str(), outputString) != true)
00448 throw ErrorClass(" Error while writing compressed file");
00449
00450 if (verifyObjects == true)
00451 {
00452 osxl = fileUtil->getFileAsString( outFileName.c_str() );
00453 OSInstance *osinstance2;
00454 osinstance2 = osilreader->readOSiL( osxl);
00455 if (osinstance2->IsEqual(osinstance) == false)
00456 throw ErrorClass("Two objects are not equal!");
00457 }
00458
00459 delete osilwriter;
00460 osilwriter = NULL;
00461 }
00462 delete osilreader;
00463 osilreader = NULL;
00464 }
00465
00466
00467
00468
00469 else if (schema == "osol")
00470 {
00471 osolreader = new OSoLReader();
00472 OSOption *osoption;
00473 #ifdef MY_DEBUG
00474 std::cout << "Start parsing the file" << std::endl;
00475 #endif
00476 osoption = osolreader->readOSoL( osxl);
00477
00478 #ifdef MY_DEBUG
00479 parsingTestResult << "Parsed file successfully" << std::endl;
00480 #endif
00481
00482 if (doOutput == true)
00483 {
00484 OSoLWriter *osolwriter;
00485 osolwriter = new OSoLWriter();
00486 osolwriter->m_bWriteBase64 = compress;
00487 osolwriter->m_bWhiteSpace = addWhiteSpace;
00488 #ifdef MY_DEBUG
00489 std::cout << "Create output string from in-memory object" << std::endl;
00490 #endif
00491 outputString = osolwriter->writeOSoL(osoption);
00492 #ifdef MY_DEBUG
00493 std::cout << "Created output string from in-memory object" << std::endl;
00494 #endif
00495 if (fileUtil->writeFileFromString( outFileName.c_str(), outputString) != true)
00496 throw ErrorClass(" Error while writing compressed file");
00497
00498 if (verifyObjects == true)
00499 {
00500 osxl = fileUtil->getFileAsString( outFileName.c_str() );
00501 OSoLReader *osolreader2;
00502 osolreader2 = new OSoLReader();
00503 OSOption *osoption2;
00504 osoption2 = osolreader2->readOSoL( osxl);
00505 #ifdef MY_DEBUG
00506 std::cout << "Compare in-memory objects" << std::endl;
00507 #endif
00508 if (osoption2->IsEqual(osoption) == false)
00509 throw ErrorClass("Two objects are not equal!");
00510 #ifdef MY_DEBUG
00511 else
00512 std::cout << "in-memory objects compare equal" << std::endl;
00513 #endif
00514 delete osolreader2;
00515 osolreader2 = NULL;
00516 }
00517
00518 delete osolwriter;
00519 osolwriter = NULL;
00520 }
00521 delete osolreader;
00522 osolreader = NULL;
00523 }
00524
00525
00526
00527
00528 else if (schema == "osrl")
00529 {
00530 osrlreader = new OSrLReader();
00531 OSResult *osresult;
00532 #ifdef MY_DEBUG
00533 std::cout << "Start parsing the file" << std::endl;
00534 #endif
00535 osresult = osrlreader->readOSrL( osxl);
00536
00537 #ifdef MY_DEBUG
00538 parsingTestResult << "Parsed file successfully" << std::endl;
00539 #endif
00540
00541 if (doOutput == true)
00542 {
00543 OSrLWriter *osrlwriter;
00544 osrlwriter = new OSrLWriter();
00545 osrlwriter->m_bWriteBase64 = compress;
00546 osrlwriter->m_bWhiteSpace = addWhiteSpace;
00547 #ifdef MY_DEBUG
00548 std::cout << "Create output string from in-memory object" << std::endl;
00549 #endif
00550 outputString = osrlwriter->writeOSrL(osresult);
00551 #ifdef MY_DEBUG
00552 std::cout << "Created output string from in-memory object" << std::endl;
00553 #endif
00554 if (fileUtil->writeFileFromString( outFileName.c_str(), outputString) != true)
00555 throw ErrorClass(" Error while writing compressed file");
00556
00557 if (verifyObjects == true)
00558 {
00559 osxl = fileUtil->getFileAsString( outFileName.c_str() );
00560 OSrLReader *osrlreader2;
00561 osrlreader2 = new OSrLReader();
00562 OSResult *osresult2;
00563 osresult2 = osrlreader2->readOSrL( osxl);
00564 #ifdef MY_DEBUG
00565 std::cout << "Compare in-memory objects" << std::endl;
00566 #endif
00567 if (osresult2->IsEqual(osresult) == false)
00568 throw ErrorClass("Two objects are not equal!");
00569 #ifdef MY_DEBUG
00570 else
00571 std::cout << "in-memory objects compare equal" << std::endl;
00572 #endif
00573 delete osrlreader2;
00574 osrlreader2 = NULL;
00575 }
00576
00577 delete osrlwriter;
00578 osrlwriter = NULL;
00579 }
00580 delete osrlreader;
00581 osrlreader = NULL;
00582 }
00583 }
00584 }
00585 catch(const ErrorClass& eclass)
00586 {
00587 cout << endl << endl << endl;
00588 if (osilreader != NULL) delete osilreader;
00589
00590 if (osrlreader != NULL) delete osrlreader;
00591
00592 parsingTestResultFailure << eclass.errormsg << endl;
00593 parsingTestResultFailure << "There was a failure in the " + schema + " parser; ";
00594 parsingTestResultFailure << "file " << testFileName << " not processed." << endl;
00595 }
00596
00597 delete fileUtil;
00598 fileUtil = NULL;
00599
00600 if (parsingTestResultFailure.str().length() > 0)
00601 {
00602 cout << "PARSING TEST FAILED ON THE FOLLOWING:" << endl << endl;
00603 cout << parsingTestResultFailure.str() << endl << endl;
00604 return 1;
00605 }
00606 else
00607 {
00608
00609 #ifdef MY_DEBUG
00610 std::cout << "PARSING TEST SUCCEEDED" << std::endl;
00611 #endif
00612
00613 return 0;
00614 }
00615 }
00616
00617 bool interactiveShell(std::string *schema, std::string *testFileName, std::string *outFileName,
00618 bool *compress, bool *addWhiteSpace, bool *verifyObjects,
00619 unsigned int *seed, int *nrep, double *density, bool *conformant)
00620 {
00621 std::string userInput;
00622 bool haveParser = false;
00623 std::cout << "Welcome to the parser test, which is part of the Optimization Services suite" << std::endl << std::endl;
00624
00625 std::cout << "This program (c) 2011 Horand Gassmann, Jun Ma and Kipp Martin" << std::endl << std::endl;
00626
00627 while (haveParser == false)
00628 {
00629 std::cout << "Input the parser you want to test (osil|osol|osrl)" << std::endl;
00630 std::cout << " or \"quit\" to exit the program > ";
00631 *schema = getUserInput(false);
00632 if (*schema == "osil") break;
00633 if (*schema == "osol") break;
00634 if (*schema == "osrl") break;
00635 if (*schema == "quit") return false;
00636 }
00637
00638 std::cout << "Input the file you want to parse or press return to generate random problems> ";
00639 *testFileName = getUserInput(true);
00640
00641 if (*testFileName == "")
00642 {
00643 std::cout << std::endl << "Random problem generation selected." << std::endl;
00644
00645 std::cout << std::endl << "Do you want to input a random number seed? [y|N] > ";
00646 std::string temp;
00647 temp = getUserInput(true);
00648 if (temp == "Y" || temp == "y")
00649 {
00650 std::cout << std::endl << "Input the random number seed (must be nonnegative integer) > ";
00651 std::cin >> *seed;
00652 }
00653
00654 std::cout << " Input number of replications > ";
00655 std::cin >> *nrep;
00656
00657 std::cout << " Input density (0..1) > ";
00658 std::cin >> *density;
00659
00660 std::cout << " Enforce side constraints (Y|n) > ";
00661 userInput = getUserInput(true);
00662 if ( (userInput == "no") || (userInput == "NO") || (userInput == "No") || (userInput == "N") || (userInput == "n") )
00663 *conformant = false;
00664
00665 std::cout << "Do you want to verify that input and output represents the same object? (yes|NO) > ";
00666 userInput = getUserInput(true);
00667 if ( (userInput == "yes") || (userInput == "YES") || (userInput == "Yes")|| (userInput == "Y") || (userInput == "y") )
00668 *verifyObjects = true;
00669 }
00670 else
00671 {
00672 std::cout << std::endl << "The remaining arguments are optional." << std::endl;
00673
00674 std::cout << "Name of the output file to which you want to print the object after processing" << std::endl;
00675 std::cout << "> ";
00676 *outFileName = getUserInput(true);
00677
00678 if (*outFileName == "")
00679 std::cout << "No output generated; remaining options skipped." << std::endl;
00680 else
00681 {
00682 std::cout << "Do you want to compress the output (using mult and incr attributes where possible)? (YES|no) > ";
00683 userInput = getUserInput(true);
00684 if ( (userInput == "no") || (userInput == "NO") || (userInput == "No") || (userInput == "N") || (userInput == "n") )
00685 *compress = false;
00686
00687 std::cout << "Do you want to add whitespace (newline characters) between elements? (YES|no) > ";
00688 userInput = getUserInput(true);
00689
00690 if ( (userInput == "no") || (userInput == "NO") || (userInput == "No") ) *addWhiteSpace = false;
00691
00692 std::cout << "Do you want to verify that input and output represents the same object? (yes|NO) > ";
00693 userInput = getUserInput(true);
00694 if ( (userInput == "yes") || (userInput == "YES") || (userInput == "Yes")|| (userInput == "Y") || (userInput == "y") )
00695 *verifyObjects = true;
00696 }
00697 }
00698
00699 std::cout << std::endl << "echo input parameters" << std::endl;
00700 std::cout << "schema: \"" << *schema << "\"" << std::endl;
00701 std::cout << "testFileName: \"" << *testFileName << "\"" << std::endl;
00702 std::cout << "outFileName: \"" << *outFileName << "\"" << std::endl;
00703 std::cout << "compress: \"" << *compress << "\"" << std::endl;
00704 std::cout << "addWhiteSpace: \"" << *addWhiteSpace << "\"" << std::endl;
00705 std::cout << "verifyObjects: \"" << *verifyObjects << "\"" << std::endl;
00706 std::cout << "replications: \"" << *nrep << "\"" << std::endl;
00707 std::cout << "random seed: \"" << *seed << "\"" << std::endl;
00708 std::cout << "density: \"" << *density << "\"" << std::endl;
00709 std::cout << "conformant: \"" << *conformant << "\"" << std::endl;
00710
00711 return true;
00712 }
00713
00714 void printHelp()
00715 {
00716 std::cout << "usage: parsertest <parser> <filename>" << std::endl;
00717 std::cout << " [<outfile> [(no)compress (no)addwhitespace (no)verify]]" << std::endl << std::endl;
00718
00719 std::cout << "where <parser> is \'osil\' or \'osol\' or \'osrl\'" << std::endl;
00720 std::cout << " <filename> is the (path to the) input file you want to parse" << std::endl;
00721 std::cout << " <outfile> is the (path to the) output file to print the object just read" << std::endl;
00722 std::cout << " compress|nocompress controls the use of mult and incr attributes" << std::endl;
00723 std::cout << " to compress the output file. (The default is compress)" << std::endl;
00724 std::cout << " addwhitespace|noaddwhitespace governs the use of whitespace (linefeed" << std::endl;
00725 std::cout << " characters between elements. (The default is addwhitespace)" << std::endl;
00726 std::cout << " verify|noverify governs checks whether the input and output files " << std::endl;
00727 std::cout << " represent the same in-memory object. (The default is noverify)" << std::endl;
00728 return;
00729 }
00730
00731 std::string getUserInput(bool defaultPresent)
00732 {
00733 std::string lineText;
00734
00735 std::string wordSep = " ";
00736 std::string optionName = "";
00737 std::string optionValue = "";
00738 std::string::size_type indexStart;
00739 std::string::size_type indexEnd;
00740
00741 for (;;)
00742 {
00743 getline(std::cin, lineText);
00744 lineText = " " + lineText + " ";
00745 indexStart = lineText.find_first_not_of(wordSep);
00746 if (indexStart != std::string::npos)
00747 {
00748 indexEnd = lineText.find_first_of(wordSep, indexStart + 1);
00749 return lineText.substr(indexStart, indexEnd - indexStart);
00750 }
00751 if (defaultPresent) return "";
00752 std::cout << std::endl;
00753 std::cout << "You did not enter a valid option. Try again." << std::endl;
00754 }
00755 return "";
00756 }