OSAmplClient2.cpp
Go to the documentation of this file.
1 
60 #include "OSConfig.h"
61 #include "OSnl2OS.h"
62 #include "OSosrl2ampl.h"
63 #include "OSiLReader.h"
64 #include "OSiLWriter.h"
65 #include "OSoLReader.h"
66 #include "OSoLWriter.h"
67 #include "OSrLReader.h"
68 #include "OSrLWriter.h"
69 #include "OSInstance.h"
70 #include "OSOption.h"
71 #include "OSResult.h"
72 #include "OSOutput.h"
73 
74 #include "OSDefaultSolver.h"
75 #include "OSCoinSolver.h"
76 
77 #ifndef COIN_HAS_ASL
78 # error do not have ASL
79 #endif
80 
81 #ifdef COIN_HAS_LINDO
82 # include "OSLindoSolver.h"
83 #endif
84 
85 #ifdef COIN_HAS_IPOPT
86 # include "OSIpoptSolver.h"
87 #endif
88 
89 #ifdef COIN_HAS_BONMIN
90 # include "OSBonminSolver.h"
91 #endif
92 
93 #ifdef COIN_HAS_COUENNE
94 # include "OSCouenneSolver.h"
95 #endif
96 
97 #include "OSFileUtil.h"
98 #include "OSSolverAgent.h"
99 #include "OShL.h"
100 #include "OSErrorClass.h"
101 #include "CoinError.hpp"
102 #include "OSCommandLine.h"
103 #include "OSCommandLineReader.h"
104 #include "OSRunSolver.h"
105 #include <sstream>
106 
107 #ifdef HAVE_CSTRING
108 # include <cstring>
109 #else
110 # ifdef HAVE_STRING_H
111 # include <string.h>
112 # else
113 # error "don't have header file for string"
114 # endif
115 #endif
116 
117 #include "CoinError.hpp"
118 #include "CoinHelperFunctions.hpp"
119 
120 
121 #include <iostream>
122 //AMPL includes must be last.
123 #include <asl.h>
124 
125 
126 
128 YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner );
129 //void osssset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
130 void setyyextra(OSCommandLine *oscommandline, void* scanner);
131 int ossslex(void* scanner );
132 int ossslex_init(void** ptr);
133 int ossslex_destroy (void* scanner );
134 
135 
136 using std::cerr;
137 using std::cout;
138 using std::endl;
139 using std::ostringstream;
140 
141 std::string get_help();
142 void list_options(OSCommandLine *oscommandline);
143 
144 // the serviceMethods
145 void solve(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
146 void getJobID(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
147 void send(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
148 void kill(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
149 void retrieve(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
150 void knock(OSCommandLine *oscommandline, OSnl2OS *osnl2os);
151 
152 // additional methods
153 bool findInstance( OSCommandLine *oscommandline, OSnl2OS *osnl2os);
154 void makeStrings( OSCommandLine *oscommandline);
155 void doPrintRow(OSInstance *osinstance, std::string rownumberstring);
156 void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS *osnl2OS);
157 void reportErrors(OSCommandLine *oscommandline, std::string errMsg, OSnl2OS *osnl2OS);
158 
159 extern const SmartPtr</*const*/ OSOutput> osoutput;
160 
161 
162 int main(int argc, char **argv)
163 {
164  WindowsErrorPopupBlocker();
165  std::ostringstream outStr;
166  OSnl2OS *nl2OS = new OSnl2OS();
167  OSiLReader* osilreader = new OSiLReader();
168  FileUtil *fileUtil = NULL;
169  ostringstream echo_cl;
170 
171  // initialize the command line structure
172 
173  OSCommandLine *oscommandline;// = new OSCommandLine();
174  OSCommandLineReader *oscommandlinereader = new OSCommandLineReader();
175 
176  DefaultSolver *solverType = NULL;
177  char* stub = NULL;
178  if (argc > 0) stub = argv[1];
179 
180  // getting the OSAmplClient_options into a std::string is a bit of a pain...
181  char* temp = getenv("OSAmplClient_options");
182  ostringstream temp2;
183  std::string amplclient_options;
184  if (temp != NULL)
185  {
186  temp2 << temp;
187  amplclient_options = temp2.str();
188  std::cout << amplclient_options << std::endl;
189  }
190  else
191 // amplclient_options = "";
192  amplclient_options = "serviceMethod retrieve serviceLocation http://74.94.100.129:8080/OSServer/services/OSSolverService printLevel 4 jobID gus-10-apr-2013-0001";
193 
194  // this output must be held in abeyance until the command line
195  // has been processed and printLevel has been set...
196 #ifndef NDEBUG
197  echo_cl << "HERE ARE THE AMPLCLIENT OPTIONS ";
198  echo_cl << amplclient_options;
199  echo_cl << endl << endl;
200 
201  echo_cl << "Try to open file ";
202  echo_cl << stub << ".nl";
203  echo_cl << endl;
204 #endif
205 
206  // Now read the command line
207  try
208  {
209  oscommandline = oscommandlinereader->readCommandLine(amplclient_options);
210  }
211 
212  catch (const ErrorClass& eclass)
213  {
214  reportErrors(oscommandline, eclass.errormsg, nl2OS);
215  delete oscommandlinereader;
216  oscommandlinereader = NULL;
217  return 1;
218  }
219 
220  if (stub) oscommandline->nlFile = stub;
221 
222 
225  try
226  {
227  outStr.str("");
228  outStr.clear();
229  outStr << std::endl << "using print level " << oscommandline->printLevel << " for stdout" << std::endl;
230 
231  if (oscommandline->printLevel != DEFAULT_OUTPUT_LEVEL)
232  {
233  osoutput->SetPrintLevel("stdout", (ENUM_OUTPUT_LEVEL)oscommandline->printLevel);
234 
235 #ifndef NDEBUG
236  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, echo_cl.str());
237 #endif
238  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
239  }
240 #ifndef NDEBUG
241  else
242  {
243  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, echo_cl.str());
244  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
245  }
246 #endif
247 
248  if (oscommandline->logFile != "")
249  {
250  int status = osoutput->AddChannel(oscommandline->logFile);
251 
252  switch(status)
253  {
254  case 0:
256  "Added channel " + oscommandline->logFile);
257  break;
258  case 1:
260  "Output channel " + oscommandline->logFile + " previously defined");
261  break;
262  default:
263  throw ErrorClass("Could not add output channel " + oscommandline->logFile);
264  }//end switch
265 
266 
267  outStr.str("");
268  outStr.clear();
269  outStr << std::endl << "using print level " << oscommandline->filePrintLevel;
270  outStr << " for " << oscommandline->logFile << std::endl;
271 
272  if (oscommandline->filePrintLevel != DEFAULT_OUTPUT_LEVEL)
273  {
274  osoutput->SetPrintLevel(oscommandline->logFile, (ENUM_OUTPUT_LEVEL)oscommandline->filePrintLevel);
275  }
276  else
277  {
278  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
279  }
280  }
281 
282  if (oscommandline->invokeHelp == true)
283  {
284  outStr.str("");
285  outStr.clear();
286  outStr << std::endl << std::endl << get_help() << std::endl;
288 
289  delete oscommandlinereader;
290  oscommandlinereader = NULL;
291  return 0;
292  }
293 
294  if (oscommandline->writeVersion == true)
295  {
296  outStr.str("");
297  outStr.clear();
298  outStr << std::endl << std::endl << OSgetVersionInfo() << std::endl;
300 
301  delete oscommandlinereader;
302  oscommandlinereader = NULL;
303  return 0;
304  }
305  }
306  catch (const ErrorClass& eclass)
307  {
308  reportErrors(oscommandline, eclass.errormsg, nl2OS);
309  delete oscommandlinereader;
310  oscommandlinereader = NULL;
311  return 1;
312  }
313 
314 #ifndef NDEBUG
315  outStr.str("");
316  outStr.clear();
317 
318  outStr << "HERE ARE THE OPTION VALUES:" << endl;
319  if(oscommandline->configFile != "") outStr << "Config file = " << oscommandline->configFile << endl;
320  if(oscommandline->osilFile != "") outStr << "OSiL file = " << oscommandline->osilFile << endl;
321  if(oscommandline->osolFile != "") outStr << "OSoL file = " << oscommandline->osolFile << endl;
322  if(oscommandline->osrlFile != "") outStr << "OSrL file = " << oscommandline->osrlFile << endl;
323  //if(oscommandline->insListFile != "") outStr << "Instruction List file = " << oscommandline->insListFile << endl;
324  if(oscommandline->osplInputFile != "") outStr << "OSpL Input file = " << oscommandline->osplInputFile << endl;
325  if(oscommandline->serviceMethod != "") outStr << "Service Method = " << oscommandline->serviceMethod << endl;
326  if(oscommandline->mpsFile != "") outStr << "MPS File Name = " << oscommandline->mpsFile << endl;
327  if(oscommandline->nlFile != "") outStr << "NL File Name = " << oscommandline->nlFile << endl;
328  if(oscommandline->gamsControlFile != "") outStr << "gams Control File Name = " << oscommandline->gamsControlFile << endl;
329  if(oscommandline->browser != "") outStr << "Browser Value = " << oscommandline->browser << endl;
330  if(oscommandline->solverName != "") outStr << "Selected Solver = " << oscommandline->solverName << endl;
331  if(oscommandline->serviceLocation != "") outStr << "Service Location = " << oscommandline->serviceLocation << endl;
332  if(oscommandline->jobID != "") outStr << "Job ID = " << oscommandline->jobID << endl;
333  if(oscommandline->printModel) outStr << "print model prior to solve/send" << endl;
334  if(oscommandline->printRowNumberAsString != "") outStr << "print model row " << oscommandline->printRowNumberAsString << " prior to solve/send" << endl;
335  outStr << "print level for stdout: " << oscommandline->printLevel << endl;
336  if(oscommandline->logFile != "")
337  {
338  outStr << "also send output to " << oscommandline->logFile << endl;
339  outStr << "print level for file output: " << oscommandline->filePrintLevel << endl;
340  }
341 
342  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
343 #endif
344 
345 
346  //convert solver name to lower case so there is no ambiguity
347  unsigned int k;
348  for (k = 0; k < oscommandline->solverName.length(); k++)
349  {
350  oscommandline->solverName[k] = (char)tolower(oscommandline->solverName[k]);
351  }
352 
353  // now call the correct serviceMethod
354  // solve is the default
355  if (oscommandline->serviceMethod == "") oscommandline->serviceMethod = "solve";
356 
357  try
358  {
359  // for local use only the solve method is available
360  if (oscommandline->serviceLocation == "")
361  if (oscommandline->serviceMethod != "solve")
362  throw ErrorClass("No serviceLocation given. Only \'solve\' is available locally.");
363 
364  if (oscommandline->serviceMethod[0] == 's')
365  {
366  // for solve or send commands we must have an instance
367  if (oscommandline->osil == "")
368  {
369  if (!findInstance(oscommandline, nl2OS))
370  throw ErrorClass("No instance could be found");
371  }
372 
373  // write out the model or portion thereof, if directed by the user
374  if (oscommandline->printModel == true)
375  {
376  if (oscommandline->osinstance == NULL)
377  {
378  oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
379  }
381  oscommandline->osinstance->printModel());
382  }
383  else if (oscommandline->printRowNumberAsString != "")
384  {
385  if (oscommandline->osinstance == NULL)
386  {
387  oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
388  }
389  doPrintRow(oscommandline->osinstance, oscommandline->printRowNumberAsString);
390  }
391 
392  // write the instance to an OSiL file, if directed by the user
393  if (oscommandline->osilOutputFile != "")
394  {
395  fileUtil = new FileUtil();
396  if (oscommandline->osil == "")
397  {
398  OSiLWriter* osilwriter = new OSiLWriter();
399  oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
400  delete osilwriter;
401  osilwriter = NULL;
402  }
403  fileUtil->writeFileFromString(oscommandline->osilOutputFile, oscommandline->osil);
404  delete fileUtil;
405  fileUtil = NULL;
406  }
407 
408  // write the solver options to an OSoL file, if directed by the user
409  if (oscommandline->osolOutputFile != "")
410  {
411  fileUtil = new FileUtil();
412  if (oscommandline->osol != "" && oscommandline->osoption == NULL)
413  fileUtil->writeFileFromString(oscommandline->osolOutputFile, oscommandline->osol);
414  else
415  {
416  if (oscommandline->osoption == NULL)
417  oscommandline->osoption = new OSOption();
418  OSoLWriter* osolwriter = new OSoLWriter();
419  fileUtil->writeFileFromString(oscommandline->osolOutputFile,
420  osolwriter->writeOSoL(oscommandline->osoption));
421  delete osolwriter;
422  osolwriter = NULL;
423  }
424  delete fileUtil;
425  fileUtil = NULL;
426  }
427 
428  // if no serviceLocation was given, do a local solve
429  if (oscommandline->serviceLocation == "")
430  {
431  std::string osrl;
432  if (oscommandline->osinstance != NULL)
433  if (oscommandline->osoption != NULL)
434  osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osinstance);
435  else
436  osrl = runSolver(oscommandline->solverName, oscommandline->osol, oscommandline->osinstance);
437  else
438  if (oscommandline->osoption != NULL)
439  osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osil);
440  else
441  osrl = runSolver(oscommandline->solverName, oscommandline->osol, oscommandline->osil);
442  reportResults(oscommandline, osrl, nl2OS);
443  }
444 
445  // remote solve or send
446  else
447  {
448  if (oscommandline->serviceMethod[1] == 'e')
449  send(oscommandline, nl2OS);
450  else
451  solve(oscommandline, nl2OS);
452  }
453  }
454 
455  else //call one of the other methods
456  {
457  switch (oscommandline->serviceMethod[0])
458  {
459  case 'g':
460  getJobID(oscommandline, nl2OS);
461  break;
462  case 'r':
463  retrieve(oscommandline, nl2OS);
464  break;
465  case 'k':
466  if (oscommandline->serviceMethod[1] == 'i')
467  kill(oscommandline, nl2OS);
468  else
469  knock(oscommandline, nl2OS);
470  break;
471  default:
472  break;
473  }
474  }
475 
476  // garbage collection
477  if (osilreader != NULL)
478  delete osilreader;
479  oscommandline->osinstance = NULL;
480  osilreader = NULL;
481  delete oscommandlinereader;
482  oscommandlinereader = NULL;
483  delete fileUtil;
484  fileUtil = NULL;
485 
486  if (nl2OS != NULL)
487  delete nl2OS;
488  nl2OS = NULL;
489 
490  return 0;
491  }
492 
493  catch (const ErrorClass& eclass)
494  {
495  reportErrors(oscommandline, eclass.errormsg, nl2OS);
496  if (osilreader != NULL)
497  delete osilreader;
498  oscommandline->osinstance = NULL;
499  osilreader = NULL;
500  delete oscommandlinereader;
501  oscommandlinereader = NULL;
502  delete fileUtil;
503  fileUtil = NULL;
504 
505  if (nl2OS != NULL)
506  delete nl2OS;
507  nl2OS = NULL;
508  return 1;
509  }
510 }// end of main()
511 
512 
519 bool findInstance(OSCommandLine *oscommandline, OSnl2OS *nl2os)
520 {
521 // FileUtil *fileUtil = NULL;
522 // fileUtil = new FileUtil();
523 
524  try
525  {
526 // nl2os = new OSnl2OS();
527  if (!nl2os->readNl(oscommandline->nlFile))
528  throw ErrorClass("Error reading .nl file.");
529  nl2os->setOsol(oscommandline->osol);
530  nl2os->setJobID(oscommandline->jobID);
531  nl2os->createOSObjects() ;
532  oscommandline->osinstance = nl2os->osinstance;
533  return true;
534  }
535  catch (const ErrorClass& eclass)
536  {
538  //throw ErrorClass(eclass.errormsg);
539  return false;
540  }
541 }//findInstance
542 
543 void makeStrings(OSCommandLine *oscommandline)
544 {
545  // convert the osinstance and osoption objects to strings
546  if (oscommandline->osil == "")
547  {
548  OSiLWriter *osilwriter = new OSiLWriter();
549  oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
550 #ifndef NDEBUG
551  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, oscommandline->osil);
552 #endif
553  delete osilwriter;
554  osilwriter = NULL;
555  }
556 
562  OSoLReader *osolreader = NULL;
563  if (oscommandline->osoption != NULL || oscommandline->jobID != "" || oscommandline->osol == "")
564  {
565  if (oscommandline->osoption == NULL)
566  {
567  oscommandline->osoption = new OSOption();
568  if (oscommandline->osol != "")
569  {
570  osolreader = new OSoLReader();
571  oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
572  }
573  if (oscommandline->jobID != "")
574  oscommandline->osoption->setJobID(oscommandline->jobID);
575  }
576 
577  OSoLWriter *osolwriter = new OSoLWriter();
578  oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);
579 #ifndef NDEBUG
580  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, oscommandline->osol);
581 #endif
582  delete osolwriter;
583  osolwriter = NULL;
584  }
585  if (osolreader != NULL)
586  {
587  delete osolreader;
588  oscommandline->osoption = NULL; // no longer needed, and memory may have just been freed
589  }
590  osolreader = NULL;
591 }
592 
598 void solve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
599 {
600  std::string osrl = "";
601  OSSolverAgent* osagent = NULL;
602  FileUtil *fileUtil = NULL;
603  fileUtil = new FileUtil();
604 
605  try
606  {
607  // place a remote call
608  osagent = new OSSolverAgent(oscommandline->serviceLocation);
609 
610  //no need to worry about jobID, but make sure we have strings
611  oscommandline->jobID = "";
612  makeStrings(oscommandline);
613 
614  osrl = osagent->solve(oscommandline->osil, oscommandline->osol);
615  delete osagent;
616  osagent = NULL;
617 
618  reportResults(oscommandline, osrl, nl2OS);
619 
620  //garbage collection
621  delete fileUtil;
622  fileUtil = NULL;
623 
624  }//end try
625  catch (const ErrorClass& eclass)
626  {
627  reportErrors(oscommandline, eclass.errormsg, nl2OS);
628 
629  //garbage collection
630  delete fileUtil;
631  fileUtil = NULL;
632  }//end local catch
633 
634 }//end solve
635 
636 
637 void send(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
638 {
639  bool bSend = false;
640  bool always_print = false;
641  OSSolverAgent* osagent = NULL;
642  ostringstream outStr;
643 
644  try
645  {
646  osagent = new OSSolverAgent(oscommandline->serviceLocation);
647 
648 
649  // get a job ID if necessary
650  if (oscommandline->jobID == "NEW")
651  {
652  always_print = true;
653  oscommandline->jobID = osagent->getJobID("");
654  if (oscommandline->osoption != NULL)
655  oscommandline->osoption->setJobID(oscommandline->jobID);
656  }
657 
658  makeStrings(oscommandline);
659 
660  outStr.str("");
661  outStr.clear();
662  outStr << "Submitting Job " << oscommandline->jobID << std::endl;
663  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
664 
665  bSend = osagent->send(oscommandline->osil, oscommandline->osol);
666 
667  outStr.str("");
668  outStr.clear();
669  outStr << "Job " << oscommandline->jobID;
670 
671  if (bSend == true)
672  {
673  outStr << " successfully submitted." << std::endl;
674  if (always_print)
676  else
677  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
678  }
679  else
680  {
681  outStr << ": send failed." << std::endl;
682  outStr << "Check to make sure you sent a jobID not on the system." << std::endl;
683  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, outStr.str());
684  }
685 
686  delete osagent;
687  osagent = NULL;
688  }
689 
690  catch (const ErrorClass& eclass)
691  {
692  reportErrors(oscommandline, eclass.errormsg, nl2OS);
693  if (osagent != NULL)
694  delete osagent;
695  osagent = NULL;
696  }
697 }//end send
698 
699 void getJobID(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
700 {
701  OSSolverAgent* osagent = NULL;
702  try
703  {
704  if (oscommandline->serviceLocation != "")
705  {
706  osagent = new OSSolverAgent(oscommandline->serviceLocation);
707  oscommandline->jobID = osagent->getJobID(oscommandline->osol);
708  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, oscommandline->jobID);
709  delete osagent;
710  osagent = NULL;
711  }
712  else
713  {
714  delete osagent;
715  osagent = NULL;
716  throw ErrorClass("please specify service location (url)");
717  }
718  }
719 
720  catch (const ErrorClass& eclass)
721  {
722  reportErrors(oscommandline, eclass.errormsg, nl2OS);
723  if (osagent != NULL)
724  delete osagent;
725  osagent = NULL;
726  }
727 }//end getJobID
728 
729 
730 void knock(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
731 {
732  std::string osplOutput = "";
733  OSSolverAgent* osagent = NULL;
734  FileUtil *fileUtil = NULL;
735  fileUtil = new FileUtil();
736  try
737  {
738  if (oscommandline->serviceLocation != "")
739  {
740  osagent = new OSSolverAgent(oscommandline->serviceLocation);
741 
742  if (oscommandline->osol == "")
743  {
744  // we need to construct the OSoL
745  OSOption *osOption = NULL;
746  osOption = new OSOption();
747  //set the jobID if there is one
748  if(oscommandline->jobID == "") osOption->setJobID( oscommandline->jobID);
749  // now read the osOption object into a string
750  OSoLWriter *osolWriter = NULL;
751  osolWriter = new OSoLWriter();
752  oscommandline->osol = osolWriter->writeOSoL( osOption);
753  delete osOption;
754  osOption = NULL;
755  delete osolWriter;
756  osolWriter = NULL;
757  }
758 
759  osplOutput = osagent->knock(oscommandline->osplInput, oscommandline->osol);
760  if (oscommandline->osplOutputFile != "")
761  fileUtil->writeFileFromString(oscommandline->osplOutputFile,
762  osplOutput);
763  else
765  delete osagent;
766  }
767  else
768  {
769  delete osagent;
770  throw ErrorClass("please specify service location (url)");
771  }
772  delete fileUtil;
773  fileUtil = NULL;
774  }
775  catch (const ErrorClass& eclass)
776  {
777  reportErrors(oscommandline, eclass.errormsg, nl2OS);
778  if (osagent != NULL)
779  delete osagent;
780  osagent = NULL;
781 
782  delete fileUtil;
783  fileUtil = NULL;
784  }
785 }//end knock
786 
787 void retrieve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
788 {
789  FileUtil *fileUtil = NULL;
790  fileUtil = new FileUtil();
791  std::string osrl = "";
792  OSSolverAgent* osagent = NULL;
793 
794  try
795  {
796  if (oscommandline->serviceLocation != "")
797  {
798  if (!nl2OS->readNl(oscommandline->nlFile))
799  throw ErrorClass("Error reading .nl file.");
800 
801  osagent = new OSSolverAgent(oscommandline->serviceLocation);
802 
803  if (oscommandline->osol == "")
804  {
805  // we need to construct the OSoL
806  OSOption *osOption = NULL;
807  osOption = new OSOption();
808  // get a jobId if necessary
809  if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
810  //set the jobID
811  osOption->setJobID( oscommandline->jobID);
812  // now read the osOption object into a string
813  OSoLWriter *osolWriter = NULL;
814  osolWriter = new OSoLWriter();
815  oscommandline->osol = osolWriter->writeOSoL( osOption);
816  delete osOption;
817  osOption = NULL;
818  delete osolWriter;
819  osolWriter = NULL;
820  }
821 
822  osrl = osagent->retrieve(oscommandline->osol);
823  reportResults(oscommandline, osrl, nl2OS);
824  delete osagent;
825  osagent = NULL;
826  }
827  else
828  {
829  delete osagent;
830  osagent = NULL;
831  throw ErrorClass("please specify service location (url)");
832  }
833  delete fileUtil;
834  fileUtil = NULL;
835  }
836  catch (const ErrorClass& eclass)
837  {
838  reportErrors(oscommandline, eclass.errormsg, nl2OS);
839  if (osagent != NULL)
840  delete osagent;
841  osagent = NULL;
842 
843  delete fileUtil;
844  fileUtil = NULL;
845  }
846 }//end retrieve
847 
848 void kill(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
849 {
850  FileUtil *fileUtil = NULL;
851  fileUtil = new FileUtil();
852  std::string osplOutput = "";
853  OSSolverAgent* osagent = NULL;
854  try
855  {
856  if (oscommandline->serviceLocation != "")
857  {
858  osagent = new OSSolverAgent(oscommandline->serviceLocation);
859 
860  if (oscommandline->osol == "")
861  {
862  // we need to construct the OSoL
863  OSOption *osOption = NULL;
864  osOption = new OSOption();
865  // get a jobId if necessary
866  if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
867  //set the jobID
868  osOption->setJobID( oscommandline->jobID);
869  // now read the osOption object into a string
870  OSoLWriter *osolWriter = NULL;
871  osolWriter = new OSoLWriter();
872  oscommandline->osol = osolWriter->writeOSoL( osOption);
873  delete osOption;
874  osOption = NULL;
875  delete osolWriter;
876  osolWriter = NULL;
877  }
878 
879  osplOutput = osagent->kill(oscommandline->osol);
880 
881  if (oscommandline->osplOutputFile != "")
882  fileUtil->writeFileFromString(oscommandline->osplOutputFile, osplOutput);
883  else
885  delete osagent;
886  osagent = NULL;
887  }
888  else
889  {
890  delete osagent;
891  osagent = NULL;
892  throw ErrorClass("please specify service location (url)");
893  }
894  delete fileUtil;
895  fileUtil = NULL;
896  }
897  catch (const ErrorClass& eclass)
898  {
899  reportErrors(oscommandline, eclass.errormsg, nl2OS);
900  if (osagent != NULL)
901  delete osagent;
902  osagent = NULL;
903 
904  delete fileUtil;
905  fileUtil = NULL;
906  }
907 }//end kill
908 
909 
910 
913 void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS* nl2OS)
914 {
915  ostringstream outStr;
916 
917  if (oscommandline->osrlFile == "")
918  oscommandline->osrlFile = oscommandline->nlFile + ".osrl";
919 
920  FileUtil* fileUtil = new FileUtil();
921  fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
922 
923  if (oscommandline->browser != "")
924  {
925  std::string str = oscommandline->browser + " "
926  + oscommandline->osrlFile;
927  const char *ch = &str[0];
928  std::system(ch);
929  }
930 
931  // now put solution back to ampl
932 
933  OSosrl2ampl *solWriter = new OSosrl2ampl();
934 
935  try
936  {
937 std::cout << osrl << std::endl << std::endl;
938 // fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
939  std::string::size_type pos1 = osrl.find( "error");
940  if(pos1 == std::string::npos)
941  {
942  OSrLReader *osrlreader = new OSrLReader();
943  OSResult *osresult = osrlreader->readOSrL( osrl);
944  solWriter->writeSolFile(osrl, nl2OS->getASL("asl"), oscommandline->nlFile + ".sol");
945 
946  delete osrlreader;
947  osrlreader = NULL;
948  }
949  else // there was an error
950  {
952  solWriter->writeSolFile(osrl, nl2OS->getASL("asl"), oscommandline->nlFile + ".sol");
953  }
954  if (fileUtil != NULL)
955  delete fileUtil;
956  fileUtil = NULL;
957  }
958  catch(const ErrorClass& eclass)
959  {
960  outStr.str("");
961  outStr.clear();
962  outStr << "There was an error parsing the OSrL string" << endl << eclass.errormsg << endl << endl;
963  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
964  if (fileUtil != NULL)
965  delete fileUtil;
966  fileUtil = NULL;
967  }
968 
969 }// reportResults
970 
973 void reportErrors(OSCommandLine *oscommandline, std::string errormsg, OSnl2OS* nl2OS)
974 {
975  std::string osrl = "";
976  OSResult *osresult = NULL;
977  OSrLWriter *osrlwriter = NULL;
978  //first check to see if we already have OSrL,
979  //if so don't create a new osresult object
980  std::string::size_type pos1 = errormsg.find( "<osrl");
981  if(pos1 == std::string::npos)
982  {
983  osrlwriter = new OSrLWriter();
984  osresult = new OSResult();
985  osresult->setGeneralMessage(errormsg);
986  osresult->setGeneralStatusType("error");
987  osrl = osrlwriter->writeOSrL(osresult);
988  }
989  else
990  {
991  osrl = errormsg;
992  }
993  reportResults(oscommandline, osrl, nl2OS);
994 }// reportErrors
995 
996 
997 void doPrintRow(OSInstance *osinstance, std::string rownumberstring)
998 {
999  ostringstream outStr;
1000  int rownumber;
1001  if (rownumberstring == "")
1003  "no row number given; print command ignored");
1004  else
1005  {
1006  try
1007  {
1008  rownumber = atoi((rownumberstring).c_str());
1009  }
1010  catch (const ErrorClass& eclass)
1011  {
1013  "invalid row number; print command ignored");
1014  }
1015 
1016  if (osinstance == NULL)
1017  {
1019  "no instance defined; print command ignored");
1020  }
1021  else
1022  {
1023  outStr << std::endl << "Row " << rownumber << ":" << std::endl << std::endl;
1024  outStr << osinstance->printModel(rownumber) << std::endl;
1025  osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());
1026  }
1027  }
1028 }// doPrintRow(OSInstance *osinstance, std::string rownumberstring)
1029 
1030 
1031 std::string get_help()
1032 {
1033  std::ostringstream helpMsg;
1034 
1035  helpMsg << "************************* HELP *************************"
1036  << endl << endl;
1037  helpMsg
1038  << "In this HELP file we assume that the solve service method is used and "
1039  << endl;
1040  helpMsg
1041  << "that we are solving problems locally, that is the solver is on the "
1042  << endl;
1043  helpMsg
1044  << "machine running this OSSolverService. See Section 10.3 of the User\'s "
1045  << endl;
1046  helpMsg
1047  << "Manual for other service methods or calling a server remotely. "
1048  << endl;
1049  helpMsg << "The OSSolverService takes the parameters listed below. "
1050  << endl;
1051  helpMsg
1052  << "The order of the parameters is irrelevant. Not all the parameters "
1053  << endl;
1054  helpMsg << "are required. However, the location of an instance file is "
1055  << endl;
1056  helpMsg
1057  << "required when using the solve service method. The location of the "
1058  << endl;
1059  helpMsg << "instance file is specified using the osil option. " << endl;
1060 
1061  helpMsg << endl;
1062 
1063  helpMsg
1064  << "-osil xxx.osil this is the name of the file that contains the "
1065  << endl;
1066  helpMsg << "optimization instance in OSiL format. This option may be "
1067  << endl;
1068  helpMsg << "specified in the OSoL solver options file. " << endl;
1069 
1070  helpMsg << endl;
1071 
1072  helpMsg
1073  << "-osol xxx.osol this is the name of the file that contains the solver options. "
1074  << endl;
1075  helpMsg << "It is not necessary to specify this option. " << endl;
1076 
1077  helpMsg << endl;
1078 
1079  helpMsg
1080  << "-osrl xxx.osrl this is the name of the file to which the solver solution is written. "
1081  << endl;
1082  helpMsg
1083  << "It is not necessary to specify this option. If this option is not specified, "
1084  << endl;
1085  helpMsg << "the result will be printed to standard out. " << endl;
1086 
1087  helpMsg << endl;
1088 
1089  helpMsg
1090  << "-osplInput xxx.ospl this is the name of an input file in the OS Process"
1091  << endl;
1092  helpMsg << " Language (OSpL), this is used as input to the knock method."
1093  << endl;
1094 
1095  helpMsg << endl;
1096 
1097  helpMsg
1098  << "-osplOutput xxx.ospl this is the name of an output file in the OS Process"
1099  << endl;
1100  helpMsg
1101  << "Language (OSpL), this the output string from the knock and kill methods."
1102  << endl;
1103 
1104  helpMsg << endl;
1105 
1106  helpMsg << "-serviceLocation url is the URL of the solver service. "
1107  << endl;
1108  helpMsg
1109  << "This is not required, and if not specified it is assumed that "
1110  << endl;
1111  helpMsg << "the problem is solved locally. " << endl;
1112 
1113  helpMsg << endl;
1114 
1115  helpMsg
1116  << "-serviceMethod methodName this is the method on the solver service to be invoked. "
1117  << endl;
1118  helpMsg
1119  << "The options are solve, send, kill, knock, getJobID, and retrieve. "
1120  << endl;
1121  helpMsg
1122  << "This option is not required, and the default value is solve. "
1123  << endl;
1124 
1125  helpMsg << endl;
1126 
1127  helpMsg
1128  << "-mps xxx.mps this is the name of the mps file if the problem instance "
1129  << endl;
1130  helpMsg
1131  << "is in mps format. The default file format is OSiL so this option is not required. "
1132  << endl;
1133 
1134  helpMsg << endl;
1135 
1136  helpMsg
1137  << "-nl xxx.nl this is the name of the AMPL nl file if the problem "
1138  << endl;
1139  helpMsg
1140  << "instance is in AMPL nl format. The default file format is OSiL "
1141  << endl;
1142  helpMsg << "so this option is not required. " << endl;
1143 
1144  helpMsg << endl;
1145 
1146  helpMsg
1147  << "-solver solverName Possible values for default OS installation "
1148  << endl;
1149  helpMsg
1150  << "are bonmin(COIN-OR Bonmin), couenne (COIN-OR Couenne), clp (COIN-OR Clp),"
1151  << endl;
1152  helpMsg << "cbc (COIN-OR Cbc), dylp (COIN-OR DyLP), ipopt (COIN-OR Ipopt),"
1153  << endl;
1154  helpMsg << "and symphony (COIN-OR SYMPHONY). Other solvers supported"
1155  << endl;
1156  helpMsg
1157  << "(if the necessary libraries are present) are cplex (Cplex through COIN-OR Osi),"
1158  << endl;
1159  helpMsg
1160  << "glpk (glpk through COIN-OR Osi), knitro (Knitro), and lindo (LINDO)."
1161  << endl;
1162  helpMsg << "If no value is specified for this parameter," << endl;
1163  helpMsg << "then cbc is the default value of this parameter." << endl;
1164 
1165  helpMsg << endl;
1166 
1167  helpMsg
1168  << "-browser browserName this paramater is a path to the browser on the "
1169  << endl;
1170  helpMsg
1171  << "local machine. If this optional parameter is specified then the "
1172  << endl;
1173  helpMsg << "solver result in OSrL format is transformed using XSLT into "
1174  << endl;
1175  helpMsg << "HTML and displayed in the browser. " << endl;
1176 
1177  helpMsg << endl;
1178 
1179  helpMsg
1180  << "-config pathToConfigureFile this parameter specifies a path on "
1181 
1182  << endl;
1183  helpMsg
1184  << "the local machine to a text file containing values for the input parameters. "
1185  << endl;
1186  helpMsg
1187  << "This is convenient for the user not wishing to constantly retype parameter values. "
1188  << endl;
1189  helpMsg
1190  << "This configure file can contain values for all of the other parameters. "
1191  << endl;
1192 
1193  helpMsg << endl;
1194 
1195  helpMsg << "--version or -v get the current version of this executable "
1196  << endl;
1197 
1198  helpMsg << endl;
1199 
1200  helpMsg << "--help or -h to get this help file " << endl;
1201 
1202  helpMsg << endl;
1203 
1204  helpMsg
1205  << "Note: If you specify a configure file by using the -config option, you can "
1206  << endl;
1207  helpMsg
1208  << "override the values of the options in the configure file by putting them in "
1209  << endl;
1210  helpMsg << "at the command line. " << endl << endl;
1211 
1212  helpMsg
1213  << "See the OS User\' Manual: http://www.coin-or.org/OS/doc/osUsersManual.pdf"
1214  << endl;
1215  helpMsg << "for more detail on how to use the OS project. " << endl;
1216 
1217  helpMsg << endl;
1218  helpMsg << "********************************************************"
1219  << endl << endl;
1220 
1221  return helpMsg.str();
1222 }// get help
1223 
1224 
void getJobID(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
std::string OSgetVersionInfo()
int printLevel
this parameter controls the amount of output to print the higher the number, the more output is gener...
bool invokeHelp
if this parameter is true we print the contents of the file help.txt and return
Used by a client to invoke a remote solver.
Definition: OSSolverAgent.h:41
#define DEFAULT_OUTPUT_LEVEL
Definition: OSParameters.h:121
void send(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
bool writeFileFromString(char *fname, std::string thestring)
write a file from an input string.
Definition: OSFileUtil.cpp:116
std::string retrieve(std::string osol)
implement the retrieve() method which is a virtual function in OShL
const OSSmartPtr< OSOutput > osoutput
Definition: OSOutput.cpp:39
bool writeVersion
if this parameter is true we print the current version of the OS project
void makeStrings(OSCommandLine *oscommandline)
void reportResults(OSCommandLine *oscommandline, std::string osrl, OSnl2OS *osnl2OS)
Deal with the OSrL output generated by the call to one of the service methods.
std::string osplInput
osplInput is the content of the osplInputFile
std::string serviceLocation
serviceLocation is the URL of the remote solver when a local solver is not used
Definition: OSCommandLine.h:52
The OSCommandLineReader Class.
std::string printModel()
Print the infix representation of the problem.
std::string osilOutputFile
osilOutputFile is the name of the file to which the instance can be written in OSiL format...
Definition: OSCommandLine.h:84
bool send(std::string osil, std::string osol)
implement the send() method which is a virtual function in OShL
std::string jobID
the JobID
std::string osil
osil is the content of the osilFile
Definition: OSCommandLine.h:77
#define scanner
std::string errormsg
errormsg is the error that is causing the exception to be thrown
Definition: OSErrorClass.h:42
std::string nlFile
the name of the file that holds an instance in AMPL nl format
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
int ossslex_init(void **ptr)
OSResult * readOSrL(const std::string &posrl)
Get an OSResult object from an OSrL string.
Definition: OSrLReader.cpp:97
The Result Class.
Definition: OSResult.h:2548
bool readNl(std::string stub)
read the nl file
Definition: OSnl2OS.cpp:109
bool createOSObjects()
create an OSInstance and OSOption representation from the AMPL nl content (Some of the information in...
Definition: OSnl2OS.cpp:504
void setOsol(std::string osol)
set the osol string
Definition: OSnl2OS.cpp:99
std::string solve(std::string osil, std::string osol)
implement the solve() method which is a virtual function in OShL, this is synchronous ...
void knock(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
Take an OSResult object and write a string that validates against OSrL.
Definition: OSrLWriter.h:30
This class handles all the output from OSSolverService, OSAmplClient and other executables derived fr...
Definition: OSOutput.h:146
Take an OSOption object and write a string that validates against the OSoL schema.
Definition: OSoLWriter.h:29
std::string browser
this parameter is a path to the browser on the local machine.
OSCommandLine * readCommandLine(const std::string &osss)
Get an OSCommandLine object from a command line string.
void reportErrors(OSCommandLine *oscommandline, std::string errMsg, OSnl2OS *osnl2OS)
Deal with any error messages generated by the call to one of the service methods. ...
std::string osplInputFile
name of an input file with xml in OS process language format, used for example to knock on a server...
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
Definition: OSiLReader.cpp:53
int ossslex_destroy(void *scanner)
std::string writeOSrL(OSResult *theosresult)
create an osrl string from an OSResult object
Definition: OSrLWriter.cpp:45
std::string osrlFile
osrlFile is the name of the file where the solver should write the result (in OSrL format) ...
The Option Class.
Definition: OSOption.h:3564
std::string gamsControlFile
the name of the file that holds the GAMS control parameters
std::string logFile
this optional parameter contains the path to a logfile that can be used as an alternate output stream...
struct yy_buffer_state * YY_BUFFER_STATE
OSInstance * osinstance
osinstance is a representation of the instance in OSInstance format
Definition: OSCommandLine.h:42
std::string osilFile
osilFile is the name of the file that holds the model instance in OSiL format
Definition: OSCommandLine.h:73
std::string osplOutputFile
name of an output file where the solver should write the result of a knock or kill service request ...
void solve(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
Next we have implementations of the six remote service methods solve, send, retrieve, knock, kill, getJobID (Do not bother with local solve; use runSolver instead)
bool printModel
if this parameter is true we print the current instance as read from an osil, nl or mps file ...
Used to read an OSiL string.
Definition: OSiLReader.h:37
std::string osolOutputFile
osolOutputFile is the name of the file to which the solver options can be written in OSoL format...
bool writeSolFile(std::string osrl, ASL *asl, std::string filename)
Convert the solution to AMPL .sol format.
Definition: OSosrl2ampl.cpp:71
std::string printRowNumberAsString
this parameter contains a string representation (!) of the row number if only a single row (constrain...
std::string configFile
configFile is the name of the file that holds the configuration options if the OSSolverService reads ...
Definition: OSCommandLine.h:68
std::string writeOSoL(OSOption *theosoption)
create an osol string from an OSOption object
Definition: OSoLWriter.cpp:45
std::string mpsFile
the name of the file that holds an instance in MPS format
ENUM_OUTPUT_LEVEL
Enumeration for the different verbosity levels that can be used in producing output.
Definition: OSParameters.h:107
OSResult * osresult
The Default Solver Class.
void fint fint * k
std::string runSolver(std::string solverName, std::string osol, OSInstance *osinstance)
This class is used to invoke a solver locally.
Definition: OSRunSolver.cpp:65
void retrieve(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
int filePrintLevel
this parameter controls the amount of output to send to the log file (if used) the higher the number...
void doPrintRow(OSInstance *osinstance, std::string rownumberstring)
void setyyextra(OSCommandLine *oscommandline, void *scanner)
The OSosrl2ampl Class.
Definition: OSosrl2ampl.h:44
OSOption * readOSoL(const std::string &osol)
parse the OSoL solver options.
Definition: OSoLReader.cpp:76
bool setGeneralMessage(std::string message)
Set the general message.
std::string serviceMethod
the service method the OSSolverService should execute, i.e.
Definition: OSCommandLine.h:57
bool setGeneralStatusType(std::string type)
Set the general status type, which can be: success, error, warning.
std::string osol
osol is the content of the osolFile
Definition: OSCommandLine.h:93
std::string knock(std::string ospl, std::string osol)
implement the knock() method which is a virtual function in OShL
std::string writeOSiL(const OSInstance *theosinstance)
create an osil string from an OSInstance object
Definition: OSiLWriter.cpp:40
Used to read an OSoL string.
Definition: OSoLReader.h:37
std::string get_help()
std::string osolFile
osolFile is the name of the file that holds the solver options in OSoL format
Definition: OSCommandLine.h:89
ASL * getASL(std::string name)
return a pointer to an ASL object
Definition: OSnl2OS.cpp:87
The OSrLReader Class.
Definition: OSrLReader.h:42
bool findInstance(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
This method tries to find the instance by reading the .nl file.
bool setJobID(std::string jobID)
Set the job ID.
Definition: OSOption.cpp:7613
void list_options(OSCommandLine *oscommandline)
void setJobID(std::string jobID)
set the job ID
Definition: OSnl2OS.cpp:104
std::string kill(std::string osol)
implement the kill() method which is a virtual function in OShL
std::string getJobID(std::string osol)
implement the getJobID() method which is a virtual function in OShL
int ossslex(void *scanner)
This class is used to store command line options for the OSSolverService executable and to provide me...
Definition: OSCommandLine.h:36
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
OSInstance * osinstance
class used to make it easy to read and write files.
Definition: OSFileUtil.h:37
The OSnl2OS Class.
Definition: OSnl2OS.h:78
OSInstance * osinstance
osinstance is a pointer to the OSInstance object that gets created from the information in the nl fil...
Definition: OSnl2OS.h:172
void kill(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
used for throwing exceptions.
Definition: OSErrorClass.h:31
std::string solverName
the name of the solver to be invoked locally, e.g -solver Ipopt
Definition: OSCommandLine.h:62
OSOption * osoption
osoption is a representation of the solver options in OSOption format
Definition: OSCommandLine.h:47
Take an OSInstance object and write a string that validates against the OSiL schema.
Definition: OSiLWriter.h:29
YY_BUFFER_STATE osss_scan_string(const char *osss, void *scanner)