00001
00060 #include "OSConfig.h"
00061 #include "OSnl2OS.h"
00062 #include "OSosrl2ampl.h"
00063 #include "OSiLReader.h"
00064 #include "OSiLWriter.h"
00065 #include "OSoLReader.h"
00066 #include "OSoLWriter.h"
00067 #include "OSrLReader.h"
00068 #include "OSrLWriter.h"
00069 #include "OSInstance.h"
00070 #include "OSOption.h"
00071 #include "OSResult.h"
00072 #include "OSOutput.h"
00073
00074 #include "OSDefaultSolver.h"
00075 #include "OSCoinSolver.h"
00076
00077 #ifndef COIN_HAS_ASL
00078 # error do not have ASL
00079 #endif
00080
00081 #ifdef COIN_HAS_LINDO
00082 # include "OSLindoSolver.h"
00083 #endif
00084
00085 #ifdef COIN_HAS_IPOPT
00086 # include "OSIpoptSolver.h"
00087 #endif
00088
00089 #ifdef COIN_HAS_BONMIN
00090 # include "OSBonminSolver.h"
00091 #endif
00092
00093 #ifdef COIN_HAS_COUENNE
00094 # include "OSCouenneSolver.h"
00095 #endif
00096
00097 #include "OSFileUtil.h"
00098 #include "OSSolverAgent.h"
00099 #include "OShL.h"
00100 #include "OSErrorClass.h"
00101 #include "CoinError.hpp"
00102 #include "OSCommandLine.h"
00103 #include "OSCommandLineReader.h"
00104 #include "OSRunSolver.h"
00105 #include <sstream>
00106
00107 #ifdef HAVE_CSTRING
00108 # include <cstring>
00109 #else
00110 # ifdef HAVE_STRING_H
00111 # include <string.h>
00112 # else
00113 # error "don't have header file for string"
00114 # endif
00115 #endif
00116
00117 #include "CoinError.hpp"
00118 #include "CoinHelperFunctions.hpp"
00119
00120
00121 #include <iostream>
00122
00123 #include <asl.h>
00124
00125
00126
00127 typedef struct yy_buffer_state *YY_BUFFER_STATE;
00128 YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner );
00129
00130 void setyyextra(OSCommandLine *oscommandline, void* scanner);
00131 int ossslex(void* scanner );
00132 int ossslex_init(void** ptr);
00133 int ossslex_destroy (void* scanner );
00134
00135
00136 using std::cerr;
00137 using std::cout;
00138 using std::endl;
00139 using std::ostringstream;
00140
00141 std::string get_help();
00142 void list_options(OSCommandLine *oscommandline);
00143
00144
00145 void solve(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00146 void getJobID(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00147 void send(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00148 void kill(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00149 void retrieve(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00150 void knock(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00151
00152
00153 bool findInstance( OSCommandLine *oscommandline, OSnl2OS *osnl2os);
00154 void makeStrings( OSCommandLine *oscommandline);
00155 void doPrintRow(OSInstance *osinstance, std::string rownumberstring);
00156 void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS *osnl2OS);
00157 void reportErrors(OSCommandLine *oscommandline, std::string errMsg, OSnl2OS *osnl2OS);
00158
00159 extern const OSSmartPtr<OSOutput> osoutput;
00160
00161
00162 int main(int argc, char **argv)
00163 {
00164 WindowsErrorPopupBlocker();
00165 std::ostringstream outStr;
00166 OSnl2OS *nl2OS = new OSnl2OS();
00167 OSiLReader* osilreader = new OSiLReader();
00168 FileUtil *fileUtil = NULL;
00169 ostringstream echo_cl;
00170
00171
00172
00173 OSCommandLine *oscommandline = NULL;
00174 OSCommandLineReader *oscommandlinereader = new OSCommandLineReader();
00175
00176
00177 char* stub = NULL;
00178 if (argc > 0) stub = argv[1];
00179
00184 ostringstream temp2;
00185 std::string amplclient_options = "";
00186 if (argc > 2)
00187 {
00188 if (strncmp(argv[2],"-AMPL",5) == 0)
00189 {
00190 char* temp = getenv("OSAmplClient_options");
00191 if (temp != NULL)
00192 {
00193 temp2 << temp;
00194 }
00195 }
00196 else
00197 {
00198 for (int i=2; i<argc; i++)
00199 temp2 << argv[i] << " ";
00200 }
00201 amplclient_options = temp2.str();
00202 }
00203
00204
00205
00206
00207
00208
00209 #ifndef NDEBUG
00210 echo_cl << "HERE ARE THE AMPLCLIENT OPTIONS ";
00211 echo_cl << amplclient_options;
00212 echo_cl << endl << endl;
00213
00214 echo_cl << "Try to open file ";
00215 echo_cl << stub << ".nl";
00216 echo_cl << endl;
00217 #endif
00218
00219
00220 try
00221 {
00222 oscommandline = oscommandlinereader->readCommandLine(amplclient_options);
00223 }
00224
00225 catch (const ErrorClass& eclass)
00226 {
00227 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00228 delete oscommandlinereader;
00229 oscommandlinereader = NULL;
00230 return 1;
00231 }
00232
00233 if (stub) oscommandline->nlFile = stub;
00234
00235
00238 try
00239 {
00240 outStr.str("");
00241 outStr.clear();
00242 outStr << std::endl << "using print level " << oscommandline->printLevel << " for stdout" << std::endl;
00243
00244 if (oscommandline->printLevel != DEFAULT_OUTPUT_LEVEL)
00245 {
00246 osoutput->SetPrintLevel("stdout", (ENUM_OUTPUT_LEVEL)oscommandline->printLevel);
00247
00248 #ifndef NDEBUG
00249 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, echo_cl.str());
00250 #endif
00251 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
00252 }
00253 #ifndef NDEBUG
00254 else
00255 {
00256 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, echo_cl.str());
00257 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00258 }
00259 #endif
00260
00261 if (oscommandline->logFile != "")
00262 {
00263 int status = osoutput->AddChannel(oscommandline->logFile);
00264
00265 switch(status)
00266 {
00267 case 0:
00268 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info,
00269 "Added channel " + oscommandline->logFile);
00270 break;
00271 case 1:
00272 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info,
00273 "Output channel " + oscommandline->logFile + " previously defined");
00274 break;
00275 default:
00276 throw ErrorClass("Could not add output channel " + oscommandline->logFile);
00277 }
00278
00279
00280 outStr.str("");
00281 outStr.clear();
00282 outStr << std::endl << "using print level " << oscommandline->filePrintLevel;
00283 outStr << " for " << oscommandline->logFile << std::endl;
00284
00285 if (oscommandline->filePrintLevel != DEFAULT_OUTPUT_LEVEL)
00286 {
00287 osoutput->SetPrintLevel(oscommandline->logFile, (ENUM_OUTPUT_LEVEL)oscommandline->filePrintLevel);
00288 }
00289 else
00290 {
00291 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00292 }
00293 }
00294
00295 if (oscommandline->invokeHelp == true)
00296 {
00297 outStr.str("");
00298 outStr.clear();
00299 outStr << std::endl << std::endl << get_help() << std::endl;
00300 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
00301
00302 delete oscommandlinereader;
00303 oscommandlinereader = NULL;
00304 return 0;
00305 }
00306
00307 if (oscommandline->writeVersion == true)
00308 {
00309 outStr.str("");
00310 outStr.clear();
00311 outStr << std::endl << std::endl << OSgetVersionInfo() << std::endl;
00312 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
00313
00314 delete oscommandlinereader;
00315 oscommandlinereader = NULL;
00316 return 0;
00317 }
00318 }
00319 catch (const ErrorClass& eclass)
00320 {
00321 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00322 delete oscommandlinereader;
00323 oscommandlinereader = NULL;
00324 return 1;
00325 }
00326
00327 #ifndef NDEBUG
00328 outStr.str("");
00329 outStr.clear();
00330
00331 outStr << "HERE ARE THE OPTION VALUES:" << endl;
00332 if(oscommandline->configFile != "") outStr << "Config file = " << oscommandline->configFile << endl;
00333 if(oscommandline->osilFile != "") outStr << "OSiL file = " << oscommandline->osilFile << endl;
00334 if(oscommandline->osolFile != "") outStr << "OSoL file = " << oscommandline->osolFile << endl;
00335 if(oscommandline->osrlFile != "") outStr << "OSrL file = " << oscommandline->osrlFile << endl;
00336
00337 if(oscommandline->osplInputFile != "") outStr << "OSpL Input file = " << oscommandline->osplInputFile << endl;
00338 if(oscommandline->serviceMethod != "") outStr << "Service Method = " << oscommandline->serviceMethod << endl;
00339 if(oscommandline->mpsFile != "") outStr << "MPS File Name = " << oscommandline->mpsFile << endl;
00340 if(oscommandline->nlFile != "") outStr << "NL File Name = " << oscommandline->nlFile << endl;
00341 if(oscommandline->gamsControlFile != "") outStr << "gams Control File Name = " << oscommandline->gamsControlFile << endl;
00342 if(oscommandline->browser != "") outStr << "Browser Value = " << oscommandline->browser << endl;
00343 if(oscommandline->solverName != "") outStr << "Selected Solver = " << oscommandline->solverName << endl;
00344 if(oscommandline->serviceLocation != "") outStr << "Service Location = " << oscommandline->serviceLocation << endl;
00345 if(oscommandline->jobID != "") outStr << "Job ID = " << oscommandline->jobID << endl;
00346 if(oscommandline->printModel) outStr << "print model prior to solve/send" << endl;
00347 if(oscommandline->printRowNumberAsString != "") outStr << "print model row " << oscommandline->printRowNumberAsString << " prior to solve/send" << endl;
00348 outStr << "print level for stdout: " << oscommandline->printLevel << endl;
00349 if(oscommandline->logFile != "")
00350 {
00351 outStr << "also send output to " << oscommandline->logFile << endl;
00352 outStr << "print level for file output: " << oscommandline->filePrintLevel << endl;
00353 }
00354
00355 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00356 #endif
00357
00358
00359
00360 unsigned int k;
00361 for (k = 0; k < oscommandline->solverName.length(); k++)
00362 {
00363 oscommandline->solverName[k] = (char)tolower(oscommandline->solverName[k]);
00364 }
00365
00366
00367
00368 if (oscommandline->serviceMethod == "") oscommandline->serviceMethod = "solve";
00369
00370 try
00371 {
00372
00373 if (oscommandline->serviceLocation == "")
00374 if (oscommandline->serviceMethod != "solve")
00375 throw ErrorClass("No serviceLocation given. Only \'solve\' is available locally.");
00376
00377 if (oscommandline->serviceMethod[0] == 's')
00378 {
00379
00380 if (oscommandline->osil == "")
00381 {
00382 if (!findInstance(oscommandline, nl2OS))
00383 throw ErrorClass("No instance could be found");
00384 }
00385
00386
00387 if (oscommandline->printModel == true)
00388 {
00389 if (oscommandline->osinstance == NULL)
00390 {
00391 oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
00392 }
00393 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
00394 oscommandline->osinstance->printModel());
00395 }
00396 else if (oscommandline->printRowNumberAsString != "")
00397 {
00398 if (oscommandline->osinstance == NULL)
00399 {
00400 oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
00401 }
00402 doPrintRow(oscommandline->osinstance, oscommandline->printRowNumberAsString);
00403 }
00404
00405
00406 if (oscommandline->osilOutputFile != "")
00407 {
00408 fileUtil = new FileUtil();
00409 if (oscommandline->osil == "")
00410 {
00411 OSiLWriter* osilwriter = new OSiLWriter();
00412 oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
00413 delete osilwriter;
00414 osilwriter = NULL;
00415 }
00416 fileUtil->writeFileFromString(oscommandline->osilOutputFile, oscommandline->osil);
00417 delete fileUtil;
00418 fileUtil = NULL;
00419 }
00420
00421
00422 if (oscommandline->osolOutputFile != "")
00423 {
00424 fileUtil = new FileUtil();
00425 if (oscommandline->osol != "" && oscommandline->osoption == NULL)
00426 fileUtil->writeFileFromString(oscommandline->osolOutputFile, oscommandline->osol);
00427 else
00428 {
00429 if (oscommandline->osoption == NULL)
00430 oscommandline->osoption = new OSOption();
00431 OSoLWriter* osolwriter = new OSoLWriter();
00432 fileUtil->writeFileFromString(oscommandline->osolOutputFile,
00433 osolwriter->writeOSoL(oscommandline->osoption));
00434 delete osolwriter;
00435 osolwriter = NULL;
00436 }
00437 delete fileUtil;
00438 fileUtil = NULL;
00439 }
00440
00441
00442 if (oscommandline->serviceLocation == "")
00443 {
00444 std::string osrl;
00445 if (oscommandline->osinstance != NULL)
00446 if (oscommandline->osoption != NULL)
00447 osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osinstance);
00448 else
00449 osrl = runSolver(oscommandline->solverName, oscommandline->osol, oscommandline->osinstance);
00450 else
00451 if (oscommandline->osoption != NULL)
00452 osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osil);
00453 else
00454 osrl = runSolver(oscommandline->solverName, oscommandline->osol, oscommandline->osil);
00455 reportResults(oscommandline, osrl, nl2OS);
00456 }
00457
00458
00459 else
00460 {
00461 if (oscommandline->serviceMethod[1] == 'e')
00462 send(oscommandline, nl2OS);
00463 else
00464 solve(oscommandline, nl2OS);
00465 }
00466 }
00467
00468 else
00469 {
00470 switch (oscommandline->serviceMethod[0])
00471 {
00472 case 'g':
00473 getJobID(oscommandline, nl2OS);
00474 break;
00475 case 'r':
00476 retrieve(oscommandline, nl2OS);
00477 break;
00478 case 'k':
00479 if (oscommandline->serviceMethod[1] == 'i')
00480 kill(oscommandline, nl2OS);
00481 else
00482 knock(oscommandline, nl2OS);
00483 break;
00484 default:
00485 break;
00486 }
00487 }
00488
00489
00490 if (osilreader != NULL)
00491 delete osilreader;
00492 oscommandline->osinstance = NULL;
00493 osilreader = NULL;
00494 delete oscommandlinereader;
00495 oscommandlinereader = NULL;
00496 delete fileUtil;
00497 fileUtil = NULL;
00498
00499 if (nl2OS != NULL)
00500 delete nl2OS;
00501 nl2OS = NULL;
00502
00503 return 0;
00504 }
00505
00506 catch (const ErrorClass& eclass)
00507 {
00508 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00509 if (osilreader != NULL)
00510 delete osilreader;
00511 oscommandline->osinstance = NULL;
00512 osilreader = NULL;
00513 delete oscommandlinereader;
00514 oscommandlinereader = NULL;
00515 delete fileUtil;
00516 fileUtil = NULL;
00517
00518 if (nl2OS != NULL)
00519 delete nl2OS;
00520 nl2OS = NULL;
00521 if ( (argc > 2) && (strncmp(argv[2],"-AMPL",5) == 0) )
00522 return 0;
00523 else
00524 return 1;
00525 }
00526 }
00527
00528
00535 bool findInstance(OSCommandLine *oscommandline, OSnl2OS *nl2os)
00536 {
00537
00538
00539
00540 try
00541 {
00542
00543 if (!nl2os->readNl(oscommandline->nlFile))
00544 throw ErrorClass("Error reading .nl file.");
00545 nl2os->setOsol(oscommandline->osol);
00546 nl2os->setJobID(oscommandline->jobID);
00547 nl2os->createOSObjects() ;
00548 oscommandline->osinstance = nl2os->osinstance;
00549 return true;
00550 }
00551 catch (const ErrorClass& eclass)
00552 {
00553 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, eclass.errormsg);
00554
00555 return false;
00556 }
00557 }
00558
00559 void makeStrings(OSCommandLine *oscommandline)
00560 {
00561
00562 if (oscommandline->osil == "")
00563 {
00564 OSiLWriter *osilwriter = new OSiLWriter();
00565 oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
00566 #ifndef NDEBUG
00567 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, oscommandline->osil);
00568 #endif
00569 delete osilwriter;
00570 osilwriter = NULL;
00571 }
00572
00578 OSoLReader *osolreader = NULL;
00579 if (oscommandline->osoption != NULL || oscommandline->jobID != "" || oscommandline->osol == "")
00580 {
00581 if (oscommandline->osoption == NULL)
00582 {
00583 oscommandline->osoption = new OSOption();
00584 if (oscommandline->osol != "")
00585 {
00586 osolreader = new OSoLReader();
00587 oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
00588 }
00589 if (oscommandline->jobID != "")
00590 oscommandline->osoption->setJobID(oscommandline->jobID);
00591 }
00592
00593 OSoLWriter *osolwriter = new OSoLWriter();
00594 oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);
00595 #ifndef NDEBUG
00596 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, oscommandline->osol);
00597 #endif
00598 delete osolwriter;
00599 osolwriter = NULL;
00600 }
00601 if (osolreader != NULL)
00602 {
00603 delete osolreader;
00604 oscommandline->osoption = NULL;
00605 }
00606 osolreader = NULL;
00607 }
00608
00614 void solve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
00615 {
00616 std::string osrl = "";
00617 OSSolverAgent* osagent = NULL;
00618 FileUtil *fileUtil = NULL;
00619 fileUtil = new FileUtil();
00620
00621 try
00622 {
00623
00624 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00625
00626
00627 oscommandline->jobID = "";
00628 makeStrings(oscommandline);
00629
00630 osrl = osagent->solve(oscommandline->osil, oscommandline->osol);
00631 delete osagent;
00632 osagent = NULL;
00633
00634 reportResults(oscommandline, osrl, nl2OS);
00635
00636
00637 delete fileUtil;
00638 fileUtil = NULL;
00639
00640 }
00641 catch (const ErrorClass& eclass)
00642 {
00643 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00644
00645
00646 delete fileUtil;
00647 fileUtil = NULL;
00648 }
00649
00650 }
00651
00652
00653 void send(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
00654 {
00655 bool bSend = false;
00656 bool always_print = false;
00657 OSSolverAgent* osagent = NULL;
00658 ostringstream outStr;
00659
00660 try
00661 {
00662 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00663
00664
00665
00666 if (oscommandline->jobID == "NEW")
00667 {
00668 always_print = true;
00669 oscommandline->jobID = osagent->getJobID("");
00670 if (oscommandline->osoption != NULL)
00671 oscommandline->osoption->setJobID(oscommandline->jobID);
00672 }
00673
00674 makeStrings(oscommandline);
00675
00676 outStr.str("");
00677 outStr.clear();
00678 outStr << "Submitting Job " << oscommandline->jobID << std::endl;
00679 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
00680
00681 bSend = osagent->send(oscommandline->osil, oscommandline->osol);
00682
00683 outStr.str("");
00684 outStr.clear();
00685 outStr << "Job " << oscommandline->jobID;
00686
00687 if (bSend == true)
00688 {
00689 outStr << " successfully submitted." << std::endl;
00690 if (always_print)
00691 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
00692 else
00693 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
00694 }
00695 else
00696 {
00697
00698 outStr << ": send failed." << std::endl;
00699 outStr << "Check to make sure you sent a jobID not on the system." << std::endl;
00700 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, outStr.str());
00701 }
00702
00703 delete osagent;
00704 osagent = NULL;
00705 }
00706
00707 catch (const ErrorClass& eclass)
00708 {
00709 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00710 if (osagent != NULL)
00711 delete osagent;
00712 osagent = NULL;
00713 }
00714 }
00715
00716 void getJobID(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
00717 {
00718 OSSolverAgent* osagent = NULL;
00719 try
00720 {
00721 if (oscommandline->serviceLocation != "")
00722 {
00723 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00724 oscommandline->jobID = osagent->getJobID(oscommandline->osol);
00725 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, oscommandline->jobID);
00726 delete osagent;
00727 osagent = NULL;
00728 }
00729 else
00730 {
00731 delete osagent;
00732 osagent = NULL;
00733 throw ErrorClass("please specify service location (url)");
00734 }
00735 }
00736
00737 catch (const ErrorClass& eclass)
00738 {
00739 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00740 if (osagent != NULL)
00741 delete osagent;
00742 osagent = NULL;
00743 }
00744 }
00745
00746
00747 void knock(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
00748 {
00749 std::string osplOutput = "";
00750 OSSolverAgent* osagent = NULL;
00751 FileUtil *fileUtil = NULL;
00752 fileUtil = new FileUtil();
00753 try
00754 {
00755 if (oscommandline->serviceLocation != "")
00756 {
00757 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00758
00759
00760 if (oscommandline->osplInput == "")
00761 {
00762 std::ostringstream temp;
00763 temp << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" <<
00764 "<ospl xmlns=\"os.optimizationservices.org\"\n" <<
00765 " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" <<
00766 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" <<
00767 " xsi:schemaLocation=\"os.optimizationservices.org\n" <<
00768 " http://www.optimizationservices.org/schemas/OSpL.xsd\">\n"<<
00769 " <processHeader>\n" <<
00770 " <request action=\"getAll\"/>\n" <<
00771 " </processHeader>\n" <<
00772 " <processData/>\n" <<
00773 "</ospl>\n";
00774 oscommandline->osplInput = temp.str();
00775 }
00776
00777
00778 if(oscommandline->jobID != "")
00779 {
00780 OSOption *osOption = NULL;
00781 if (oscommandline->osol == "")
00782 {
00783 osOption = new OSOption();
00784 }
00785 else
00786 {
00787 OSoLReader *osolReader = new OSoLReader();
00788 try
00789 {
00790 osOption = osolReader->readOSoL(oscommandline->osol);
00791 delete osolReader;
00792 osolReader = NULL;
00793 }
00794 catch (const ErrorClass& eclass)
00795 {
00796 if (osolReader != NULL) delete osolReader;
00797 osolReader = NULL;
00798 throw ErrorClass(eclass.errormsg);
00799 }
00800 }
00801 osOption->setJobID( oscommandline->jobID);
00802 OSoLWriter *osolWriter = new OSoLWriter();
00803 oscommandline->osol = osolWriter->writeOSoL( osOption);
00804 delete osOption;
00805 osOption = NULL;
00806 delete osolWriter;
00807 osolWriter = NULL;
00808 }
00809
00810 osplOutput = osagent->knock(oscommandline->osplInput, oscommandline->osol);
00811 if (oscommandline->osplOutputFile != "")
00812 fileUtil->writeFileFromString(oscommandline->osplOutputFile,
00813 osplOutput);
00814 else
00815 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, osplOutput);
00816 delete osagent;
00817 }
00818 else
00819 {
00820 delete osagent;
00821 throw ErrorClass("please specify service location (url)");
00822 }
00823 delete fileUtil;
00824 fileUtil = NULL;
00825 }
00826 catch (const ErrorClass& eclass)
00827 {
00828 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00829 if (osagent != NULL)
00830 delete osagent;
00831 osagent = NULL;
00832
00833 delete fileUtil;
00834 fileUtil = NULL;
00835 }
00836 }
00837
00838 void retrieve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
00839 {
00840 FileUtil *fileUtil = NULL;
00841 fileUtil = new FileUtil();
00842 std::string osrl = "";
00843 OSSolverAgent* osagent = NULL;
00844
00845 try
00846 {
00847 if (oscommandline->serviceLocation != "")
00848 {
00849 if (!nl2OS->readNl(oscommandline->nlFile))
00850 throw ErrorClass("Error reading .nl file.");
00851
00852 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00853
00854 if (oscommandline->osol == "")
00855 {
00856
00857 OSOption *osOption = NULL;
00858 osOption = new OSOption();
00859
00860 if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
00861
00862 osOption->setJobID( oscommandline->jobID);
00863
00864 OSoLWriter *osolWriter = NULL;
00865 osolWriter = new OSoLWriter();
00866 oscommandline->osol = osolWriter->writeOSoL( osOption);
00867 delete osOption;
00868 osOption = NULL;
00869 delete osolWriter;
00870 osolWriter = NULL;
00871 }
00872
00873 osrl = osagent->retrieve(oscommandline->osol);
00874 reportResults(oscommandline, osrl, nl2OS);
00875 delete osagent;
00876 osagent = NULL;
00877 }
00878 else
00879 {
00880 delete osagent;
00881 osagent = NULL;
00882 throw ErrorClass("please specify service location (url)");
00883 }
00884 delete fileUtil;
00885 fileUtil = NULL;
00886 }
00887 catch (const ErrorClass& eclass)
00888 {
00889 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00890 if (osagent != NULL)
00891 delete osagent;
00892 osagent = NULL;
00893
00894 delete fileUtil;
00895 fileUtil = NULL;
00896 }
00897 }
00898
00899 void kill(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
00900 {
00901 FileUtil *fileUtil = NULL;
00902 fileUtil = new FileUtil();
00903 std::string osplOutput = "";
00904 OSSolverAgent* osagent = NULL;
00905 try
00906 {
00907 if (oscommandline->serviceLocation != "")
00908 {
00909 osagent = new OSSolverAgent(oscommandline->serviceLocation);
00910
00911 if (oscommandline->osol == "")
00912 {
00913
00914 OSOption *osOption = NULL;
00915 osOption = new OSOption();
00916
00917 if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
00918
00919 osOption->setJobID( oscommandline->jobID);
00920
00921 OSoLWriter *osolWriter = NULL;
00922 osolWriter = new OSoLWriter();
00923 oscommandline->osol = osolWriter->writeOSoL( osOption);
00924 delete osOption;
00925 osOption = NULL;
00926 delete osolWriter;
00927 osolWriter = NULL;
00928 }
00929
00930 osplOutput = osagent->kill(oscommandline->osol);
00931
00932 if (oscommandline->osplOutputFile != "")
00933 {
00934 fileUtil->writeFileFromString(oscommandline->osplOutputFile, osplOutput);
00935 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
00936 "Results written to file " + oscommandline->osplOutputFile);
00937 }
00938 else
00939 {
00940 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, "kill command executed\n");
00941 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_summary, osplOutput);
00942 }
00943 delete osagent;
00944 osagent = NULL;
00945 }
00946 else
00947 {
00948 delete osagent;
00949 osagent = NULL;
00950 throw ErrorClass("please specify service location (url)");
00951 }
00952 delete fileUtil;
00953 fileUtil = NULL;
00954 }
00955 catch (const ErrorClass& eclass)
00956 {
00957 reportErrors(oscommandline, eclass.errormsg, nl2OS);
00958 if (osagent != NULL)
00959 delete osagent;
00960 osagent = NULL;
00961
00962 delete fileUtil;
00963 fileUtil = NULL;
00964 }
00965 }
00966
00967
00968
00971 void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS* nl2OS)
00972 {
00973 ostringstream outStr;
00974
00975 if (oscommandline->osrlFile == "")
00976 oscommandline->osrlFile = oscommandline->nlFile + ".osrl";
00977
00978 FileUtil* fileUtil = new FileUtil();
00979 fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
00980
00981 if (oscommandline->browser != "")
00982 {
00983 std::string str = oscommandline->browser + " "
00984 + oscommandline->osrlFile;
00985 const char *ch = &str[0];
00986 std::system(ch);
00987 }
00988
00989
00990
00991 OSosrl2ampl *solWriter = new OSosrl2ampl();
00992
00993 try
00994 {
00995 std::string::size_type pos1 = osrl.find( "error");
00996 if(pos1 == std::string::npos)
00997 {
00998
00999
01000 solWriter->writeSolFile(osrl, nl2OS->getASL("asl"), oscommandline->nlFile + ".sol");
01001
01002
01003
01004 }
01005 else
01006 {
01007 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, osrl);
01008 solWriter->writeSolFile(osrl, nl2OS->getASL("asl"), oscommandline->nlFile + ".sol");
01009 }
01010 if (fileUtil != NULL)
01011 delete fileUtil;
01012 fileUtil = NULL;
01013 }
01014 catch(const ErrorClass& eclass)
01015 {
01016 outStr.str("");
01017 outStr.clear();
01018 outStr << "There was an error parsing the OSrL string" << endl << eclass.errormsg << endl << endl;
01019 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
01020 if (fileUtil != NULL)
01021 delete fileUtil;
01022 fileUtil = NULL;
01023 }
01024
01025 }
01026
01029 void reportErrors(OSCommandLine *oscommandline, std::string errormsg, OSnl2OS* nl2OS)
01030 {
01031 std::string osrl = "";
01032 OSResult *osresult = NULL;
01033 OSrLWriter *osrlwriter = NULL;
01034
01035
01036 std::string::size_type pos1 = errormsg.find( "<osrl");
01037 if(pos1 == std::string::npos)
01038 {
01039 osrlwriter = new OSrLWriter();
01040 osresult = new OSResult();
01041 osresult->setGeneralMessage(errormsg);
01042 osresult->setGeneralStatusType("error");
01043 osrl = osrlwriter->writeOSrL(osresult);
01044 }
01045 else
01046 {
01047 osrl = errormsg;
01048 }
01049 reportResults(oscommandline, osrl, nl2OS);
01050 }
01051
01052
01053 void doPrintRow(OSInstance *osinstance, std::string rownumberstring)
01054 {
01055 ostringstream outStr;
01056 int rownumber;
01057 if (rownumberstring == "")
01058 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
01059 "no row number given; print command ignored");
01060 else
01061 {
01062 try
01063 {
01064 rownumber = atoi((rownumberstring).c_str());
01065 }
01066 catch (const ErrorClass& eclass)
01067 {
01068 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
01069 "invalid row number; print command ignored");
01070 }
01071
01072 if (osinstance == NULL)
01073 {
01074 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always,
01075 "no instance defined; print command ignored");
01076 }
01077 else
01078 {
01079 outStr << std::endl << "Row " << rownumber << ":" << std::endl << std::endl;
01080 outStr << osinstance->printModel(rownumber) << std::endl;
01081 osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
01082 }
01083 }
01084 }
01085
01086
01087 std::string get_help()
01088 {
01089 std::ostringstream helpMsg;
01090
01091 helpMsg << "************************* HELP *************************"
01092 << endl << endl;
01093 helpMsg
01094 << "In this HELP file we assume that the solve service method is used and "
01095 << endl;
01096 helpMsg
01097 << "that we are solving problems locally, that is the solver is on the "
01098 << endl;
01099 helpMsg
01100 << "machine running this OSSolverService. See Section 10.3 of the User\'s "
01101 << endl;
01102 helpMsg
01103 << "Manual for other service methods or calling a server remotely. "
01104 << endl;
01105 helpMsg << "The OSSolverService takes the parameters listed below. "
01106 << endl;
01107 helpMsg
01108 << "The order of the parameters is irrelevant. Not all the parameters "
01109 << endl;
01110 helpMsg << "are required. However, the location of an instance file is "
01111 << endl;
01112 helpMsg
01113 << "required when using the solve service method. The location of the "
01114 << endl;
01115 helpMsg << "instance file is specified using the osil option. " << endl;
01116
01117 helpMsg << endl;
01118
01119 helpMsg
01120 << "-osil xxx.osil this is the name of the file that contains the "
01121 << endl;
01122 helpMsg << "optimization instance in OSiL format. This option may be "
01123 << endl;
01124 helpMsg << "specified in the OSoL solver options file. " << endl;
01125
01126 helpMsg << endl;
01127
01128 helpMsg
01129 << "-osol xxx.osol this is the name of the file that contains the solver options. "
01130 << endl;
01131 helpMsg << "It is not necessary to specify this option. " << endl;
01132
01133 helpMsg << endl;
01134
01135 helpMsg
01136 << "-osrl xxx.osrl this is the name of the file to which the solver solution is written. "
01137 << endl;
01138 helpMsg
01139 << "It is not necessary to specify this option. If this option is not specified, "
01140 << endl;
01141 helpMsg << "the result will be printed to standard out. " << endl;
01142
01143 helpMsg << endl;
01144
01145 helpMsg
01146 << "-osplInput xxx.ospl this is the name of an input file in the OS Process"
01147 << endl;
01148 helpMsg << " Language (OSpL), this is used as input to the knock method."
01149 << endl;
01150
01151 helpMsg << endl;
01152
01153 helpMsg
01154 << "-osplOutput xxx.ospl this is the name of an output file in the OS Process"
01155 << endl;
01156 helpMsg
01157 << "Language (OSpL), this the output string from the knock and kill methods."
01158 << endl;
01159
01160 helpMsg << endl;
01161
01162 helpMsg << "-serviceLocation url is the URL of the solver service. "
01163 << endl;
01164 helpMsg
01165 << "This is not required, and if not specified it is assumed that "
01166 << endl;
01167 helpMsg << "the problem is solved locally. " << endl;
01168
01169 helpMsg << endl;
01170
01171 helpMsg
01172 << "-serviceMethod methodName this is the method on the solver service to be invoked. "
01173 << endl;
01174 helpMsg
01175 << "The options are solve, send, kill, knock, getJobID, and retrieve. "
01176 << endl;
01177 helpMsg
01178 << "This option is not required, and the default value is solve. "
01179 << endl;
01180
01181 helpMsg << endl;
01182
01183 helpMsg
01184 << "-mps xxx.mps this is the name of the mps file if the problem instance "
01185 << endl;
01186 helpMsg
01187 << "is in mps format. The default file format is OSiL so this option is not required. "
01188 << endl;
01189
01190 helpMsg << endl;
01191
01192 helpMsg
01193 << "-nl xxx.nl this is the name of the AMPL nl file if the problem "
01194 << endl;
01195 helpMsg
01196 << "instance is in AMPL nl format. The default file format is OSiL "
01197 << endl;
01198 helpMsg << "so this option is not required. " << endl;
01199
01200 helpMsg << endl;
01201
01202 helpMsg
01203 << "-solver solverName Possible values for default OS installation "
01204 << endl;
01205 helpMsg
01206 << "are bonmin(COIN-OR Bonmin), couenne (COIN-OR Couenne), clp (COIN-OR Clp),"
01207 << endl;
01208 helpMsg << "cbc (COIN-OR Cbc), dylp (COIN-OR DyLP), ipopt (COIN-OR Ipopt),"
01209 << endl;
01210 helpMsg << "and symphony (COIN-OR SYMPHONY). Other solvers supported"
01211 << endl;
01212 helpMsg
01213 << "(if the necessary libraries are present) are cplex (Cplex through COIN-OR Osi),"
01214 << endl;
01215 helpMsg
01216 << "glpk (glpk through COIN-OR Osi), knitro (Knitro), and lindo (LINDO)."
01217 << endl;
01218 helpMsg << "If no value is specified for this parameter," << endl;
01219 helpMsg << "then cbc is the default value of this parameter." << endl;
01220
01221 helpMsg << endl;
01222
01223 helpMsg
01224 << "-browser browserName this paramater is a path to the browser on the "
01225 << endl;
01226 helpMsg
01227 << "local machine. If this optional parameter is specified then the "
01228 << endl;
01229 helpMsg << "solver result in OSrL format is transformed using XSLT into "
01230 << endl;
01231 helpMsg << "HTML and displayed in the browser. " << endl;
01232
01233 helpMsg << endl;
01234
01235 helpMsg
01236 << "-config pathToConfigureFile this parameter specifies a path on "
01237
01238 << endl;
01239 helpMsg
01240 << "the local machine to a text file containing values for the input parameters. "
01241 << endl;
01242 helpMsg
01243 << "This is convenient for the user not wishing to constantly retype parameter values. "
01244 << endl;
01245 helpMsg
01246 << "This configure file can contain values for all of the other parameters. "
01247 << endl;
01248
01249 helpMsg << endl;
01250
01251 helpMsg << "--version or -v get the current version of this executable "
01252 << endl;
01253
01254 helpMsg << endl;
01255
01256 helpMsg << "--help or -h to get this help file " << endl;
01257
01258 helpMsg << endl;
01259
01260 helpMsg
01261 << "Note: If you specify a configure file by using the -config option, you can "
01262 << endl;
01263 helpMsg
01264 << "override the values of the options in the configure file by putting them in "
01265 << endl;
01266 helpMsg << "at the command line. " << endl << endl;
01267
01268 helpMsg
01269 << "See the OS User\' Manual: http://www.coin-or.org/OS/doc/osUsersManual.pdf"
01270 << endl;
01271 helpMsg << "for more detail on how to use the OS project. " << endl;
01272
01273 helpMsg << endl;
01274 helpMsg << "********************************************************"
01275 << endl << endl;
01276
01277 return helpMsg.str();
01278 }
01279
01280