18 #include <cppad/cppad.hpp>
20 #include "OSInstance.h"
30 #include "CoinError.hpp"
39 #include <CoinMpsIO.hpp>
40 #include <CoinPackedMatrix.hpp>
43 #include "CoinError.hpp"
44 #include "CoinHelperFunctions.hpp"
52 # error "don't have header file for time"
73 # error "don't have header file for stdio"
83 using std::ostringstream;
87 bool interactiveShell(std::string *schema, std::string *testFileName, std::string *outFileName,
88 bool *compress,
bool *addWhiteSpace,
bool *verifyObjects,
bool *useRandomObjects,
89 unsigned int *seed,
int *
nrep,
double *density,
bool *conformant);
94 int main(
int argC,
char* argV[])
96 WindowsErrorPopupBlocker();
105 std::string testFileName;
106 std::string outFileName;
109 ostringstream parsingTestResult;
110 ostringstream parsingTestResultFailure;
112 std::string outputString;
114 bool doOutput =
false;
115 bool compress =
true;
116 bool addWhiteSpace =
true;
117 bool verifyObjects =
true;
118 bool useRandomObjects =
false;
122 double density = 0.5;
123 bool conformant =
true;
131 if (
interactiveShell(&schema, &testFileName, &outFileName, &compress, &addWhiteSpace, &verifyObjects,
132 &useRandomObjects, &seed, &nrep, &density, &conformant) !=
true)
return 0;
135 std::cout <<
"User dialog completed " << std::endl;
160 std::cout <<
"Too Many Input Parameters" << std::endl;
163 if ( (strcmp(argV[1],
"--help") == 0) || (strcmp(argV[1],
"-h") == 0) || (strcmp(argV[1],
"-?") == 0) )
168 if ( (strcmp(argV[1],
"quit") == 0) || (strcmp(argV[1],
"exit") == 0) || (strcmp(argV[1],
"stop") == 0) )
192 std::string argument, option, value;
193 for (
int i=1; i<argC; i++)
195 if (argV[i][0] !=
'-')
196 throw ErrorClass(
"usage: -<option>=<value>; try -help for more");
197 argument = std::string(argV[i]);
198 epos = argument.find(
'=');
199 option = argument.substr(0,epos);
200 if (epos < std::string::npos)
201 value = argument.substr(epos+1);
205 std::cout <<
"option = \"" << option <<
"\"" << std::endl;
206 std::cout <<
"value = \"" << value <<
"\"" << std::endl;
208 if (option ==
"-schema")
211 throw ErrorClass(
"schema option must have a value");
214 else if (option ==
"-file")
217 throw ErrorClass(
"file option must have a value");
218 testFileName = value;
219 useRandomObjects =
false;
221 else if (option ==
"-replications")
224 throw ErrorClass(
"number of replications must be given");
225 nrep = atoi(value.c_str());
227 else if (option ==
"-seed")
230 throw ErrorClass(
"seed option must have a value");
231 seed = atoi(value.c_str());
233 else if (option ==
"-density")
236 throw ErrorClass(
"density option must have a value");
237 density =
os_strtod(value.c_str(), NULL);
239 else if (option ==
"-outfile")
242 throw ErrorClass(
"outfile option must have a value");
245 else if (option ==
"-random")
247 useRandomObjects = (value ==
"YES" || value ==
"yes" || value ==
"Y" || value ==
"Y" || value ==
"y" || value ==
"");
250 else if (option ==
"-conformant")
252 conformant = (value ==
"YES" || value ==
"yes" || value ==
"Y" || value ==
"Y" || value ==
"y" || value ==
"");
254 else if (option ==
"-compress")
256 compress = (value ==
"YES" || value ==
"yes" || value ==
"Y" || value ==
"Y" || value ==
"y" || value ==
"");
258 else if (option ==
"-whitespace")
260 addWhiteSpace = (value ==
"YES" || value ==
"yes" || value ==
"Y" || value ==
"Y" || value ==
"y" || value ==
"");
262 else if (option ==
"-verify")
265 verifyObjects =
true;
267 verifyObjects = !(value ==
"NO" || value ==
"no" || value ==
"N" || value ==
"N" || value ==
"");
271 if (outFileName ==
"") outFileName =
"junk.tmp";
274 else if (option ==
"-norandom")
276 useRandomObjects =
false;
278 else if (option ==
"-noconformant")
282 else if (option ==
"-nocompress")
286 else if (option ==
"-nowhitespace")
288 addWhiteSpace =
false;
290 else if (option ==
"-noverify")
292 verifyObjects =
false;
296 std::cout <<
"option \"" << option <<
"\" not recognized" << std::endl;
302 if (outFileName !=
"") doOutput =
true;
312 if (useRandomObjects)
314 if (schema ==
"osil")
316 for (
int irep=0; irep <
nrep; irep++)
326 else if (schema ==
"osol")
333 for (
int irep=0; irep <
nrep; irep++)
335 std::cout <<
"use random number seed: " << seed << std::endl;
339 std::cout <<
"set random osoption" << std::endl;
341 osoption->
setRandom(density, conformant);
343 std::cout <<
"write to string" << std::endl;
347 std::cout << osxl << std::endl;
350 osoption2 = osolreader->
readOSoL( osxl);
351 if (verifyObjects ==
true)
353 if (osoption->
IsEqual(osoption2) ==
false)
354 throw ErrorClass(
"Two objects are not equal!");
364 else if (schema ==
"osrl")
371 for (
int irep=0; irep <
nrep; irep++)
373 std::cout <<
"use random number seed: " << seed << std::endl;
377 std::cout <<
"set random osresult" << std::endl;
379 osresult->
setRandom(density, conformant);
381 std::cout <<
"write to string" << std::endl;
385 std::cout << osxl << std::endl;
388 osresult2 = osrlreader->
readOSrL( osxl);
389 if (verifyObjects ==
true)
391 if (osresult->
IsEqual(osresult2) ==
false)
392 throw ErrorClass(
"Two objects are not equal!");
403 else if (testFileName ==
"")
404 throw ErrorClass(
"No filename given. Nothing to parse.");
408 std::cout <<
"Processing file: " ;
409 std::cout << testFileName << std::endl;
415 std::cout <<
"Done reading the file into memory" << std::endl;
421 if (schema ==
"osil")
426 std::cout <<
"Start parsing the file" << std::endl;
428 osinstance = osilreader->
readOSiL( osxl);
431 parsingTestResult <<
"Parsed file successfully" << std::endl;
434 if (doOutput ==
true)
441 std::cout <<
"Create output string from in-memory object" << std::endl;
443 outputString = osilwriter->
writeOSiL(osinstance);
445 std::cout <<
"Created output string from in-memory object" << std::endl;
448 throw ErrorClass(
" Error while writing compressed file");
450 if (verifyObjects ==
true)
454 osinstance2 = osilreader->
readOSiL( osxl);
455 if (osinstance2->
IsEqual(osinstance) ==
false)
456 throw ErrorClass(
"Two objects are not equal!");
469 else if (schema ==
"osol")
474 std::cout <<
"Start parsing the file" << std::endl;
476 osoption = osolreader->
readOSoL( osxl);
479 parsingTestResult <<
"Parsed file successfully" << std::endl;
482 if (doOutput ==
true)
489 std::cout <<
"Create output string from in-memory object" << std::endl;
491 outputString = osolwriter->
writeOSoL(osoption);
493 std::cout <<
"Created output string from in-memory object" << std::endl;
496 throw ErrorClass(
" Error while writing compressed file");
498 if (verifyObjects ==
true)
504 osoption2 = osolreader2->
readOSoL( osxl);
506 std::cout <<
"Compare in-memory objects" << std::endl;
508 if (osoption2->
IsEqual(osoption) ==
false)
509 throw ErrorClass(
"Two objects are not equal!");
512 std::cout <<
"in-memory objects compare equal" << std::endl;
528 else if (schema ==
"osrl")
533 std::cout <<
"Start parsing the file" << std::endl;
535 osresult = osrlreader->
readOSrL( osxl);
538 parsingTestResult <<
"Parsed file successfully" << std::endl;
541 if (doOutput ==
true)
548 std::cout <<
"Create output string from in-memory object" << std::endl;
550 outputString = osrlwriter->
writeOSrL(osresult);
552 std::cout <<
"Created output string from in-memory object" << std::endl;
555 throw ErrorClass(
" Error while writing compressed file");
557 if (verifyObjects ==
true)
563 osresult2 = osrlreader2->
readOSrL( osxl);
565 std::cout <<
"Compare in-memory objects" << std::endl;
567 if (osresult2->
IsEqual(osresult) ==
false)
568 throw ErrorClass(
"Two objects are not equal!");
571 std::cout <<
"in-memory objects compare equal" << std::endl;
587 cout << endl << endl << endl;
588 if (osilreader != NULL)
delete osilreader;
590 if (osrlreader != NULL)
delete osrlreader;
592 parsingTestResultFailure << eclass.
errormsg << endl;
593 parsingTestResultFailure <<
"There was a failure in the " + schema +
" parser; ";
594 parsingTestResultFailure <<
"file " << testFileName <<
" not processed." << endl;
600 if (parsingTestResultFailure.str().length() > 0)
602 cout <<
"PARSING TEST FAILED ON THE FOLLOWING:" << endl << endl;
603 cout << parsingTestResultFailure.str() << endl << endl;
610 std::cout <<
"PARSING TEST SUCCEEDED" << std::endl;
617 bool interactiveShell(std::string *schema, std::string *testFileName, std::string *outFileName,
618 bool *compress,
bool *addWhiteSpace,
bool *verifyObjects,
bool *useRandomObjects,
619 unsigned int *seed,
int *
nrep,
double *density,
bool *conformant)
621 std::string userInput;
622 bool haveParser =
false;
623 std::cout <<
"Welcome to the parser test, which is part of the Optimization Services suite" << std::endl << std::endl;
625 std::cout <<
"This program (c) 2011 Horand Gassmann, Jun Ma and Kipp Martin" << std::endl << std::endl;
627 while (haveParser ==
false)
629 std::cout <<
"Input the parser you want to test (osil|osol|osrl)" << std::endl;
630 std::cout <<
" or \"quit\" to exit the program > ";
632 if (*schema ==
"osil")
break;
633 if (*schema ==
"osol")
break;
634 if (*schema ==
"osrl")
break;
635 if (*schema ==
"quit")
return false;
638 std::cout <<
"Input the file you want to parse or press return to generate random problems> ";
641 if (*testFileName ==
"")
643 std::cout << std::endl <<
"Random problem generation selected." << std::endl;
644 *useRandomObjects =
true;
646 std::cout << std::endl <<
"Do you want to input a random number seed? [y|N] > ";
649 if (temp ==
"Y" || temp ==
"y")
651 std::cout << std::endl <<
"Input the random number seed (must be nonnegative integer) > ";
655 std::cout <<
" Input number of replications > ";
658 std::cout <<
" Input density (0..1) > ";
659 std::cin >> *density;
661 std::cout <<
" Enforce side constraints (Y|n) > ";
663 if ( (userInput ==
"no") || (userInput ==
"NO") || (userInput ==
"No") || (userInput ==
"N") || (userInput ==
"n") )
666 std::cout <<
"Do you want to verify that input and output represents the same object? (yes|NO) > ";
668 if ( (userInput ==
"yes") || (userInput ==
"YES") || (userInput ==
"Yes")|| (userInput ==
"Y") || (userInput ==
"y") )
669 *verifyObjects =
true;
673 std::cout << std::endl <<
"The remaining arguments are optional." << std::endl;
675 std::cout <<
"Name of the output file to which you want to print the object after processing" << std::endl;
679 if (*outFileName ==
"")
680 std::cout <<
"No output generated; remaining options skipped." << std::endl;
683 std::cout <<
"Do you want to compress the output (using mult and incr attributes where possible)? (YES|no) > ";
685 if ( (userInput ==
"no") || (userInput ==
"NO") || (userInput ==
"No") || (userInput ==
"N") || (userInput ==
"n") )
688 std::cout <<
"Do you want to add whitespace (newline characters) between elements? (YES|no) > ";
691 if ( (userInput ==
"no") || (userInput ==
"NO") || (userInput ==
"No") ) *addWhiteSpace =
false;
693 std::cout <<
"Do you want to verify that input and output represents the same object? (yes|NO) > ";
695 if ( (userInput ==
"yes") || (userInput ==
"YES") || (userInput ==
"Yes")|| (userInput ==
"Y") || (userInput ==
"y") )
696 *verifyObjects =
true;
700 std::cout << std::endl <<
"echo input parameters" << std::endl;
701 std::cout <<
"schema: \"" << *schema <<
"\"" << std::endl;
702 std::cout <<
"testFileName: \"" << *testFileName <<
"\"" << std::endl;
703 std::cout <<
"outFileName: \"" << *outFileName <<
"\"" << std::endl;
704 std::cout <<
"compress: \"" << *compress <<
"\"" << std::endl;
705 std::cout <<
"addWhiteSpace: \"" << *addWhiteSpace <<
"\"" << std::endl;
706 std::cout <<
"verifyObjects: \"" << *verifyObjects <<
"\"" << std::endl;
707 std::cout <<
"replications: \"" << *nrep <<
"\"" << std::endl;
708 std::cout <<
"random seed: \"" << *seed <<
"\"" << std::endl;
709 std::cout <<
"density: \"" << *density <<
"\"" << std::endl;
710 std::cout <<
"conformant: \"" << *conformant <<
"\"" << std::endl;
717 std::cout <<
"usage: parsertest <parser> <filename>" << std::endl;
718 std::cout <<
" [<outfile> [(no)compress (no)addwhitespace (no)verify]]" << std::endl << std::endl;
720 std::cout <<
"where <parser> is \'osil\' or \'osol\' or \'osrl\'" << std::endl;
721 std::cout <<
" <filename> is the (path to the) input file you want to parse" << std::endl;
722 std::cout <<
" <outfile> is the (path to the) output file to print the object just read" << std::endl;
723 std::cout <<
" compress|nocompress controls the use of mult and incr attributes" << std::endl;
724 std::cout <<
" to compress the output file. (The default is compress)" << std::endl;
725 std::cout <<
" addwhitespace|noaddwhitespace governs the use of whitespace (linefeed" << std::endl;
726 std::cout <<
" characters between elements. (The default is addwhitespace)" << std::endl;
727 std::cout <<
" verify|noverify governs checks whether the input and output files " << std::endl;
728 std::cout <<
" represent the same in-memory object. (The default is noverify)" << std::endl;
734 std::string lineText;
736 std::string wordSep =
" ";
737 std::string optionName =
"";
738 std::string optionValue =
"";
739 std::string::size_type indexStart;
740 std::string::size_type indexEnd;
744 getline(std::cin, lineText);
745 lineText =
" " + lineText +
" ";
746 indexStart = lineText.find_first_not_of(wordSep);
747 if (indexStart != std::string::npos)
749 indexEnd = lineText.find_first_of(wordSep, indexStart + 1);
750 return lineText.substr(indexStart, indexEnd - indexStart);
752 if (defaultPresent)
return "";
753 std::cout << std::endl;
754 std::cout <<
"You did not enter a valid option. Try again." << std::endl;
double os_strtod(const char *s00, char **se)
bool m_bWhiteSpace
m_bWhiteSpace is set to true if we write white space in the file
bool writeFileFromString(char *fname, std::string thestring)
write a file from an input string.
std::string errormsg
errormsg is the error that is causing the exception to be thrown
bool interactiveShell(std::string *schema, std::string *testFileName, std::string *outFileName, bool *compress, bool *addWhiteSpace, bool *verifyObjects, bool *useRandomObjects, unsigned int *seed, int *nrep, double *density, bool *conformant)
int main(int argc, char *argv[])
OSResult * readOSrL(const std::string &posrl)
Get an OSResult object from an OSrL string.
bool setRandom(double density, bool conformant)
A function to make a random instance of this class.
bool m_bWriteBase64
m_bWriteBase64 is set to true if we encode the linear constraint coefficients in base64 binary ...
bool IsEqual(OSInstance *that)
A function to check for the equality of two objects.
Take an OSResult object and write a string that validates against OSrL.
Take an OSOption object and write a string that validates against the OSoL schema.
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
std::string writeOSrL(OSResult *theosresult)
create an osrl string from an OSResult object
bool setRandom(double density, bool conformant)
A function to make a random instance of this class.
Used to read an OSiL string.
std::string writeOSoL(OSOption *theosoption)
create an osol string from an OSOption object
OSOption * readOSoL(const std::string &osol)
parse the OSoL solver options.
bool m_bWhiteSpace
m_bWhiteSpace is set to true if we write white space in the file
bool m_bWriteBase64
m_bWriteBase64 is set to true if we encode the linear constraint coefficients in base64 binary ...
bool IsEqual(OSOption *that)
A function to check for the equality of two objects.
std::string writeOSiL(const OSInstance *theosinstance)
create an osil string from an OSInstance object
Used to read an OSoL string.
std::string getFileAsString(const char *fname)
read a file and return contents as a string.
bool m_bWhiteSpace
m_bWhiteSpace is set to true if we write white space in the file
bool IsEqual(OSResult *that)
A function to check for the equality of two objects.
The in-memory representation of an OSiL instance..
class used to make it easy to read and write files.
used for throwing exceptions.
bool m_bWriteBase64
m_bWriteBase64 is set to true if we encode the linear constraint coefficients in base64 binary ...
std::string getUserInput(bool defaultPresent)
Take an OSInstance object and write a string that validates against the OSiL schema.