/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OS/OS/src/OSSolverService.cpp

Go to the documentation of this file.
00001 
00070 #include "OSCoinSolver.h"
00071 #include "OSResult.h" 
00072 #include "OSiLReader.h"        
00073 #include "OSiLWriter.h" 
00074 #include "OSrLReader.h"        
00075 #include "OSrLWriter.h"      
00076 #include "OSInstance.h"  
00077 #include "OSFileUtil.h"  
00078 #include "OSConfig.h"  
00079 #include "OSDefaultSolver.h"  
00080 #include "OSWSUtil.h" 
00081 #include "OSSolverAgent.h"   
00082 #include "OShL.h"     
00083 #include "OSErrorClass.h"
00084 #include "OSmps2osil.h"   
00085 #include "OSBase64.h"
00086 #include "OSCommonUtil.h"
00087 
00088 
00089 
00090 #ifdef COIN_HAS_KNITRO    
00091 #include "OSKnitroSolver.h"
00092 #endif 
00093 
00094 
00095 
00096 
00097 
00098 #ifdef COIN_HAS_LINDO    
00099 #include "OSLindoSolver.h"
00100 #endif 
00101 
00102 /*
00103 #ifdef COIN_HAS_IPOPT  
00104         #ifndef COIN_HAS_ASL
00105                 #include "OSIpoptSolver.h"
00106                 #undef COIN_HAS_ASL
00107         #else
00108                 #include "OSIpoptSolver.h"
00109 #endif
00110 #endif 
00111 */
00112 
00113 #ifdef COIN_HAS_ASL
00114 #include "OSnl2osil.h"
00115 #endif
00116 
00117 
00118 
00119 //#ifdef COIN_HAS_IPOPT  
00120 //#include "OSIpoptSolver.h"
00121 //#endif
00122 
00123 #ifdef COIN_HAS_IPOPT  
00124         #ifndef COIN_HAS_ASL
00125                 #include "OSIpoptSolver.h"
00126                 #undef COIN_HAS_ASL
00127         #else
00128                 #include "OSIpoptSolver.h"
00129         #endif
00130 #endif
00131 
00132 
00133 
00134 
00135 
00136 
00137 #include "OSOptionsStruc.h"  
00138 
00139 
00140 using std::cout;
00141 using std::endl;
00142 using std::ostringstream;
00143 using std::string;
00144 
00145     
00146 
00147 #define MAXCHARS 5000 
00148 
00149 typedef struct yy_buffer_state *YY_BUFFER_STATE;
00150 YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner ); 
00151 //void osssset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
00152 void setyyextra(osOptionsStruc *osoptions, void* scanner);
00153 int ossslex(void* scanner );
00154 int ossslex_init(void** ptr);
00155 int ossslex_destroy (void* scanner );
00156 
00157 
00158 
00159 
00160  
00161 // the serviceMethods  
00162 void solve();  
00163 void getJobID(); 
00164 void send();
00165 void kill();
00166 void retrieve(); 
00167 void knock();
00168 
00169 // additional methods
00170 void getOSiLFromNl(); 
00171 void getOSiLFromMps();
00172 string getServiceURI( std::string osol);
00173 string getInstanceLocation( std::string osol);
00174 string getSolverName( std::string osol);
00175 
00176 //options structure
00177 // this is the only global variable but 
00178 // this is not a problem since this is a main routine
00179 osOptionsStruc *osoptions; 
00180 
00181 
00182 int main(int argC, const char* argV[])
00183 {       
00184         void* scanner;
00185         FileUtil *fileUtil = NULL;
00186         FileUtil *inputFileUtil = NULL; 
00187         char osss[MAXCHARS] = " ";
00188         const char *space = " "; 
00189         //char *config = "-config";
00190         std::string configFileName = "";
00191         int i;
00192 
00193         // initialize the OS options structure
00194 
00195         osoptions = new osOptionsStruc();
00196         osoptions->configFile = ""; 
00197         osoptions->osilFile = ""; 
00198         osoptions->osil = ""; 
00199         osoptions->osolFile = ""; 
00200         osoptions->osol = "";  
00201         osoptions->osrlFile = ""; 
00202         osoptions->osrl = ""; 
00203         osoptions->insListFile = ""; 
00204         osoptions->insList = ""; 
00205         osoptions->serviceLocation = ""; 
00206         osoptions->serviceMethod = ""; 
00207         osoptions->osplInputFile = ""; 
00208         osoptions->osplOutputFile = ""; 
00209         osoptions->mpsFile = ""; 
00210         osoptions->nlFile = ""; 
00211         osoptions->solverName = ""; 
00212         osoptions->browser = ""; 
00213         osoptions->invokeHelp = false;
00214         osoptions->writeVersion = false;
00215         try{
00216                 if(argC < 2){
00217                         std::cout << "There must be at least one command line argument" << std::endl;
00218                         std::cout << "Try -help or --help" << std::endl;
00219                         delete osoptions;
00220                         return 1;
00221                 }
00222                 // see if the first argument is a file name
00223                 i = 1;
00224                 while(i < argC) {
00225                         if(strlen(osss) + strlen(argV[ i]) + 1 > MAXCHARS) throw ErrorClass( "the command line has too many arguments");
00226                         strcat(osss, argV[ i]);
00227                         strcat(osss, space);
00228                         i++;
00229                 }
00230                 cout << "Input String = "  << osss << endl;
00231                 ossslex_init( &scanner);
00232                 //std::cout << "Call Text Extra" << std::endl;
00233                 setyyextra( osoptions, scanner);
00234                 //std::cout << "Call scan string " << std::endl;
00235                 osss_scan_string( osss, scanner); 
00236                 std::cout << "call ossslex" << std::endl;
00237                 ossslex( scanner);
00238                 ossslex_destroy( scanner);
00239                 std::cout << "done with call to ossslex" << std::endl;
00240                 // if there is a config file, get those options
00241                 if(osoptions->configFile != ""){
00242                         ossslex_init( &scanner);
00243                         configFileName = osoptions->configFile;
00244                         cout << "configFileName = " << configFileName << endl;
00245                         std::string osolfileOptions = fileUtil->getFileAsString( configFileName.c_str() );
00246                         std::cout << "Call Text Extra" << std::endl;
00247                         setyyextra( osoptions, scanner);
00248                         std::cout << "Done with call Text Extra" << std::endl;
00249                         osss_scan_string( osolfileOptions.c_str() , scanner);
00250                         ossslex(scanner );      
00251                         ossslex_destroy( scanner);
00252                 }
00253         }
00254                 catch(const ErrorClass& eclass){
00255                 cout << eclass.errormsg <<  endl;
00256                 cout << "try -help or --help" <<  endl;
00257                 ossslex_destroy( scanner);
00258                 delete fileUtil;
00259                 delete osoptions;
00260                 return 1;
00261         } 
00262                 try{
00263                         if(osoptions->invokeHelp == true){ 
00264                                 inputFileUtil = new FileUtil();
00265                                 std::string helpTxt = inputFileUtil->getFileAsString( "help.txt" );
00266                                 std::cout << std::endl << std::endl;
00267                                 std::cout << helpTxt << std::endl;
00268                                 delete  osoptions;
00269                                 osoptions = NULL;       
00270                                 delete inputFileUtil;
00271                                 inputFileUtil = NULL;
00272                                 return 0;
00273                         }
00274                         if(osoptions->writeVersion == true){ 
00275                                 inputFileUtil = new FileUtil();
00276                                 std::string writeTxt = "OS Version 1.0\n";
00277                                 writeTxt += inputFileUtil->getFileAsString( "version.txt" );
00278                                 std::cout << std::endl << std::endl;
00279                                 std::cout << writeTxt << std::endl;
00280                                 delete  osoptions;
00281                                 osoptions = NULL;       
00282                                 delete inputFileUtil;
00283                                 inputFileUtil = NULL;
00284                                 return 0;
00285                         }
00286                 }
00287                 catch(const ErrorClass& eclass){
00288                         cout << eclass.errormsg <<  endl;
00289                         cout << "try -help or --help" <<  endl;
00290                         delete  osoptions;
00291                         osoptions = NULL;       
00292                         delete inputFileUtil;
00293                         inputFileUtil  = NULL;
00294                         return 1;
00295                 } 
00296                 cout << "HERE ARE THE OPTION VALUES:" << endl;
00297                 if(osoptions->configFile != "") cout << "Config file = " << osoptions->configFile << endl;
00298                 if(osoptions->osilFile != "") cout << "OSiL file = " << osoptions->osilFile << endl;
00299                 if(osoptions->osolFile != "") cout << "OSoL file = " << osoptions->osolFile << endl;
00300                 if(osoptions->osrlFile != "") cout << "OSrL file = " << osoptions->osrlFile << endl;
00301                 if(osoptions->insListFile != "") cout << "Instruction List file = " << osoptions->insListFile << endl;
00302                 if(osoptions->osplInputFile != "") cout << "OSpL Input file = " << osoptions->osplInputFile << endl;
00303                 if(osoptions->serviceMethod != "") cout << "Service Method = " << osoptions->serviceMethod << endl;
00304                 if(osoptions->mpsFile != "") cout << "MPS File Name = " << osoptions->mpsFile << endl;
00305                 if(osoptions->nlFile != "") cout << "NL File Name = " << osoptions->nlFile << endl;
00306                 if(osoptions->browser != "") cout << "Browser Value = " << osoptions->browser << endl;
00307                 // get the data from the files
00308                 fileUtil = new FileUtil();
00309                 try{    
00310                         if(osoptions->insListFile != "") osoptions->insList = fileUtil->getFileAsChar( (osoptions->insListFile).c_str() );
00311                         if(osoptions->osolFile != ""){
00312                                 osoptions->osol = fileUtil->getFileAsString( (osoptions->osolFile).c_str() );
00313                         }
00314                         if(osoptions->serviceLocation != ""){
00315                                  cout << "Service Location = " << osoptions->serviceLocation << endl;
00316                         }
00317                         else{
00318                                 //if( getServiceURI( osoptions->osol) != ""){
00319                                 //      osoptions->serviceLocation = &getServiceURI( osoptions->osol)[0];
00320                                 //      cout << "Service Location = " << osoptions->serviceLocation << endl;
00321                                 //}
00322                         }
00323                         if(osoptions->osilFile != ""){
00324                                 //this takes precedence over what is in the OSoL file
00325                                  osoptions->osil = fileUtil->getFileAsString( (osoptions->osilFile).c_str()   );
00326                         }
00327                         else{//get <instanceLocation if we are doing a local solve
00328                                 // make sure we don't have a service URI in the file
00329                                         if( (osoptions->osol != "") &&  (osoptions->serviceLocation == "")  &&  (getServiceURI( osoptions->osol) == "") ) 
00330                                                 osoptions->osil = fileUtil->getFileAsString( getInstanceLocation( osoptions->osol).c_str()  );
00331                         }
00332                         // see if there is a solver specified
00333                         if(osoptions->solverName != ""){ 
00334                                 cout << "Solver Name = " << osoptions->solverName << endl;
00335                         }
00336                         else{
00337                                 if(osoptions->osol != ""){
00338                                 osoptions->solverName  =    getSolverName( osoptions->osolFile.c_str()  );
00339                                 }
00340                         }
00341                         //if(osoptions->osplInputFile != "") osoptions->osplInput = fileUtil->getFileAsChar( (osoptions->osplInputFile).c_str()  );
00342                         if(osoptions->osplInputFile != "") osoptions->osplInput = fileUtil->getFileAsString( (osoptions->osplInputFile).c_str() );
00343                         //if(osoptions->osplOutputFile != "") osoptions->osplOutput = fileUtil->getFileAsChar( (osoptions->osplOutputFile).c_str() );
00344                         if(osoptions->osplOutputFile != "") osoptions->osplOutput = fileUtil->getFileAsString( (osoptions->osplOutputFile).c_str() );
00345                 }
00346                 catch(const ErrorClass& eclass){
00347                         //cout << eclass.errormsg <<  endl;
00348                         cout << "could not open file properly" << endl;
00349                         cout << "try -help or --help" <<  endl;
00350                         delete  osoptions;
00351                         osoptions = NULL;       
00352                         delete fileUtil;
00353                         fileUtil = NULL;
00354                         return 1;
00355                 }       
00356                 // now call the correct serviceMethod
00357                 // solve is the default
00358                 if( osoptions->serviceMethod == "") solve();
00359                 else{
00360                         switch(osoptions->serviceMethod[ 0]){
00361                                 case 'g': 
00362                                         getJobID();
00363                                         break;
00364                                 case 'r':
00365                                         retrieve();
00366                                         break;
00367                                 case 's':
00368                                         if( osoptions->serviceMethod[ 1] == 'e') send();
00369                                         else solve();
00370                                         break;
00371                                 case 'k':
00372                                         if(osoptions->serviceMethod[ 1] == 'i') kill();
00373                                         else knock();
00374                                         break;
00375                                 default:
00376                                         break;
00377                         }
00378                 }
00379         delete  osoptions;
00380         osoptions = NULL;       
00381         delete fileUtil;
00382         fileUtil = NULL;
00383         return 0;
00384 }
00385 
00386 void solve(){
00387         std::string osrl = "";
00388         OSiLReader *osilreader = NULL; 
00389         OSSolverAgent* osagent = NULL;
00390         DefaultSolver *solverType  = NULL;
00391         FileUtil *fileUtil = NULL;
00392         fileUtil = new FileUtil();
00393         try{
00394                 // now solve either remotely or locally
00395                 if( osoptions->serviceLocation != "" ){
00396                         // call a method here to get OSiL if we have an nl or mps file
00397                         if(osoptions->osil == ""){
00398                                 //we better have an nl file present or mps file or osol file
00399                                 if(osoptions->nlFile != ""){
00400                                         getOSiLFromNl();
00401                                 }
00402                                 else{
00403                                         if(osoptions->mpsFile != ""){
00404                                                 getOSiLFromMps();
00405                                         }
00406                                         else{// need an osol file with an instanceLocation specified
00407                                                 if( osoptions->osol.find( "<instanceLocation") == std::string::npos){
00408                                                         throw ErrorClass("solve called and no osil, osol with osil specified, nl, or mps file given");
00409                                                 }
00410                                         }
00411                                 }
00412                         }
00413                         // place a remote call
00414                         osagent = new OSSolverAgent( osoptions->serviceLocation );
00415                         if(osoptions->solverName != ""){
00416                                 string::size_type iStringpos;
00417                                 //see if there is an osol file
00418                                 if(osoptions->osol != ""){// we have an osol string
00419                                         // see if a solver is listed, if so don't do anything
00420                                         iStringpos = osoptions->osol.find("os_solver");
00421                                         if(iStringpos == std::string::npos) { //don't have a solver specify, we must do so
00422                                                 iStringpos = osoptions->osol.find("</osol");
00423                                                         osoptions->osol.insert(iStringpos, "<other name=\"os_solver\">"
00424                                                         + osoptions->solverName  + "</other>");
00425                                         }
00426                                 }
00427                                 else{// no osol string
00428                                         osoptions->osol = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <osol xmlns=\"os.optimizationservices.org\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"os.optimizationservices.org http://www.optimizationservices.org/schemas/OSoL.xsd\"><other> </other></osol>";       
00429                                         iStringpos = osoptions->osol.find("</osol");
00430                                         osoptions->osol.insert(iStringpos, "<other name=\"os_solver\">"
00431                                                         + osoptions->solverName  + "</other>");
00432                                 }
00433                         }
00434                         std::cout  << std::endl;
00435                         if(osoptions->osol.length() > 0){
00436                                 std::cout << "HERE IS THE OSoL FILE" << std::endl;
00437                                 std::cout << osoptions->osol << std::endl << std::endl;
00438                         }
00439                         osrl = osagent->solve(osoptions->osil  , osoptions->osol);
00440                         if(osoptions->osrlFile != ""){
00441                                 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00442                                 //const char *ch1 = "/Applications/Firefox.app/Contents/MacOS/firefox  ";
00443                                 if(osoptions->browser != ""){
00444                                         std::string str = osoptions->browser + "  " +  osoptions->osrlFile;
00445                                         const char *ch = &str[ 0];
00446                                         std::system( ch );
00447                                 }
00448                         }
00449                         else cout << osrl << endl;
00450                         delete osagent;
00451                         osagent = NULL;
00452                 }
00453                 else{
00454                         // solve locally
00455                         if(osoptions->solverName == "" ){
00456                                 string sSolverName = "cbc";
00457                                 osoptions->solverName = sSolverName;
00458                         }
00459                         if( osoptions->solverName.find( "ipopt") != std::string::npos) {
00460                                 // we are requesting the Ipopt solver
00461                                 bool bIpoptIsPresent = false;
00462                                 #ifdef COIN_HAS_IPOPT
00463                                 bIpoptIsPresent = true;
00464                                 //IpoptSolver *ipoptSolver  = new IpoptSolver();        
00465                                 //ipoptSolver->osol = osoptions->osol;
00466                                 //ipoptSolver->osil = osoptions->osil;
00467                                 //ipoptSolver->osinstance = NULL;
00468                                 //ipoptSolver->solve();
00469                                 //osrl = ipoptSolver->osrl ;
00470                                 solverType = new IpoptSolver(); 
00471                                 //solverType->osol = osoptions->osol;
00472                                 //solverType->osil = osoptions->osil;
00473                                 //solverType->osinstance = NULL;
00474                                 //solverType->solve();
00475                                 //osrl = solverType->osrl ;
00476                                 #endif
00477                                 if(bIpoptIsPresent == false) throw ErrorClass( "the Ipopt solver requested is not present");
00478                         }
00479                         else{
00480                                 if( osoptions->solverName.find( "lindo") != std::string::npos) {
00481                                         // we are requesting the Lindo solver
00482                                         bool bLindoIsPresent = false;
00483                                         #ifdef COIN_HAS_LINDO
00484                                         bLindoIsPresent = true;
00485                                         std::cout << "calling the LINDO Solver " << std::endl;
00486                                         solverType = new LindoSolver();
00487                                         std::cout << "DONE calling the LINDO Solver " << std::endl;
00488                                         #endif
00489                                         if(bLindoIsPresent == false) throw ErrorClass( "the Lindo solver requested is not present");
00490                                 }
00491                                 else{ 
00492                                         if( osoptions->solverName.find( "clp") != std::string::npos){
00493                                                 solverType = new CoinSolver();
00494                                                 solverType->sSolverName = "clp";
00495                                         }
00496                                         else{
00497                                                 if( osoptions->solverName.find( "cplex") != std::string::npos){
00498                                                         solverType = new CoinSolver();
00499                                                         solverType->sSolverName = "cplex";
00500                                                 }
00501                                                 else{
00502                                                         if( osoptions->solverName.find( "glpk") != std::string::npos){
00503                                                                 solverType = new CoinSolver();
00504                                                                 solverType->sSolverName = "glpk";
00505                                                         }
00506                                                         else{
00507                                                                 if( osoptions->solverName.find( "dylp") != std::string::npos){
00508                                                                         bool bDYlPIsPresent  = false;
00509                                                                         #ifdef COIN_HAS_DYLP
00510                                                                         solverType = new CoinSolver();
00511                                                                         solverType->sSolverName = "dylp";
00512                                                                         bDYlPIsPresent = true;
00513                                                                         #endif
00514                                                                         if(bDYlPIsPresent == false) throw ErrorClass( "the DyLP solver requested is not present");
00515                                                                 }
00516                                                                 else{
00517                                                                         if( osoptions->solverName.find( "symphony") != std::string::npos){
00518                                                                                 solverType = new CoinSolver();
00519                                                                                 solverType->sSolverName = "symphony";
00520                                                                         }
00521                                                                         else{
00522                                                                                 if( osoptions->solverName.find( "knitro") != std::string::npos){
00523                                                                                         bool bKnitroIsPresent = false;
00524                                                                                         #ifdef COIN_HAS_KNITRO
00525                                                                                         bKnitroIsPresent = true;
00526                                                                                         std::cout << "calling the KNITRO Solver " << std::endl;
00527                                                                                         solverType = new KnitroSolver();
00528                                                                                         std::cout << "DONE calling the KNITRO Solver " << std::endl;
00529                                                                                         #endif
00530                                                                                         if(bKnitroIsPresent == false) throw ErrorClass( "the Knitro solver requested is not present");          
00531                                                                                 }
00532                                                                                 else{ 
00533                                                                                         if( osoptions->solverName.find( "vol") != std::string::npos){
00534                                                                                                 solverType = new CoinSolver();
00535                                                                                                 solverType->sSolverName = "vol";
00536                                                                                         }
00537                                                                                         else{ //cbc is the default
00538                                                                                                 solverType = new CoinSolver();
00539                                                                                                 solverType->sSolverName = "cbc";
00540                                                                                         }
00541                                                                                 }                                                                       
00542                                                                         }
00543                                                                 }
00544                                                         }
00545                                                 }
00546                                         }
00547                                 }
00548                         }
00549                         std::cout << "CALL SOLVE" << std::endl;
00550                         solverType->osol = osoptions->osol;
00551                         if(osoptions->osil != ""){
00552                                 osilreader = new OSiLReader();
00553                                 std::cout << "CREATING AN OSINSTANCE FROM AN OSIL FILE" << std::endl;
00554                                 solverType->osinstance = osilreader->readOSiL( osoptions->osil );
00555                                 solverType->buildSolverInstance();
00556                                 solverType->solve();
00557                                 osrl = solverType->osrl;
00558                         }
00559                         else{
00560                                 //we better have an nl file present or mps file or osol file
00561                                 if(osoptions->nlFile != ""){
00562                                         #ifdef COIN_HAS_ASL
00563                                                 std::cout << "CREATING AN OSINSTANCE FROM AN NL FILE" << std::endl;
00564                                                 OSnl2osil *nl2osil = new OSnl2osil( osoptions->nlFile); 
00565                                                 nl2osil->createOSInstance() ;
00566                                                 solverType->osinstance = nl2osil->osinstance;
00567                                                 solverType->buildSolverInstance();
00568                                                 solverType->solve();
00569                                                 osrl = solverType->osrl;
00570                                                 delete nl2osil;
00571                                         #else
00572                                                 throw ErrorClass("nlFile specified locally but ASL not present");
00573                                         #endif
00574                                 }
00575                                 else{
00576                                         if(osoptions->mpsFile != ""){
00577                                                 std::cout << "CREATING AN OSINSTANCE FROM AN MPS FILE" << std::endl;
00578                                                 OSmps2osil *mps2osil = new OSmps2osil( osoptions->mpsFile);
00579                                                 mps2osil->createOSInstance() ;
00580                                                 solverType->osinstance = mps2osil->osinstance;
00581                                                 solverType->buildSolverInstance();
00582                                                 solverType->solve();
00583                                                 osrl = solverType->osrl;
00584                                                 
00585                                                 delete mps2osil;
00586                                         }
00587                                         else{// need an osol file with an instanceLocation specified
00588                                                 if( osoptions->osol.find( "<instanceLocation") == std::string::npos){
00589                                                         throw ErrorClass("solve called and no osil, osol with osil specified, nl, or mps file given");
00590                                                 }
00591                                         }
00592                                 }
00593                         }
00594                         //delete fileUtil;
00595                         if(osoptions->osrlFile != ""){
00596                                 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00597                                 //const char *ch1 = "/Applications/Firefox.app/Contents/MacOS/firefox  ";
00598                                 if(osoptions->browser != ""){
00599                                         std::string str = osoptions->browser + "  " +  osoptions->osrlFile;
00600                                         const char *ch = &str[ 0];
00601                                         std::system( ch );
00602                                 }
00603                         }
00604                         else cout << osrl << endl;
00605                 }
00606         }//end try
00607         catch(const ErrorClass& eclass){
00608                 if(osoptions->osrlFile != ""){
00609                         fileUtil->writeFileFromString(osoptions->osrlFile, eclass.errormsg);
00610                         if(osoptions->browser != ""){
00611                                 std::string str = osoptions->browser + "  " +  osoptions->osrlFile;
00612                                 const char *ch = &str[ 0];
00613                                 std::system( ch );
00614                         }
00615                 }
00616                 else{
00617                         OSResult *osresult = NULL;
00618                         OSrLWriter *osrlwriter = NULL;
00619                         osrlwriter = new OSrLWriter();
00620                         osresult = new OSResult();
00621                         osresult->setGeneralMessage( eclass.errormsg);
00622                         osresult->setGeneralStatusType( "error");
00623                         std::string osrl = osrlwriter->writeOSrL( osresult);
00624                         std::cout << osrl << std::endl;
00625                         delete osresult;
00626                         delete osrlwriter;
00627                 }
00628         }       
00629         if(osilreader != NULL) delete osilreader;
00630         if(solverType != NULL) delete solverType;
00631         delete fileUtil;
00632         fileUtil = NULL;
00633 }//end solve
00634 
00635 void getJobID(){
00636         std::string jobID = "";
00637         OSSolverAgent* osagent = NULL;
00638         try{
00639                 if(osoptions->serviceLocation != ""){
00640                         osagent = new OSSolverAgent( osoptions->serviceLocation );
00641                         jobID = osagent->getJobID( osoptions->osol);
00642                         cout << jobID << endl;
00643                         delete osagent;
00644                         osagent = NULL;
00645                 }
00646                 else{
00647                         delete osagent;
00648                         osagent = NULL;
00649                         throw ErrorClass("please specify service location (url)");
00650                 }
00651         }
00652         catch(const ErrorClass& eclass){
00653                 FileUtil *fileUtil = NULL;
00654                 fileUtil = new FileUtil();
00655                 OSResult *osresult = NULL;
00656                 OSrLWriter *osrlwriter = NULL;
00657                 osrlwriter = new OSrLWriter();
00658                 osresult = new OSResult();
00659                 osresult->setGeneralMessage( eclass.errormsg);
00660                 osresult->setGeneralStatusType( "error");
00661                 std::string osrl = osrlwriter->writeOSrL( osresult);
00662                 if(osoptions->osrlFile != "") fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00663                 else cout << osrl << endl;
00664                 delete osresult;
00665                 osresult = NULL;
00666                 delete osrlwriter;
00667                 osrlwriter = NULL;
00668                 delete fileUtil;
00669                 fileUtil = NULL;
00670         }       
00671 }//end getJobID
00672  
00673 
00674 void knock(){
00675         std::string osplOutput = "";
00676         OSSolverAgent* osagent = NULL;
00677         FileUtil *fileUtil = NULL;
00678         fileUtil = new FileUtil();
00679         try{
00680                 if(osoptions->serviceLocation != ""){
00681                         osagent = new OSSolverAgent( osoptions->serviceLocation );
00682                         osplOutput = osagent->knock(osoptions->osplInput,  osoptions->osol);
00683                         if(osoptions->osplOutputFile != "") fileUtil->writeFileFromString(osoptions->osplOutputFile, osplOutput);
00684                         else cout << osplOutput << endl;
00685                         delete osagent;
00686                 }
00687                 else{
00688                         delete osagent;
00689                         throw ErrorClass( "please specify service location (url)" );
00690                 }
00691                 delete fileUtil;
00692                 fileUtil = NULL;
00693         }
00694         catch(const ErrorClass& eclass){
00695                 OSResult *osresult = NULL;
00696                 OSrLWriter *osrlwriter = NULL;
00697                 osrlwriter = new OSrLWriter();
00698                 osresult = new OSResult();
00699                 osresult->setGeneralMessage( eclass.errormsg);
00700                 osresult->setGeneralStatusType( "error");
00701                 std::string osrl = osrlwriter->writeOSrL( osresult);
00702                 if(osoptions->osrlFile != "") fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00703                 else cout << osrl << endl;
00704                 delete osresult;
00705                 osresult = NULL;
00706                 delete osrlwriter;
00707                 osrlwriter = NULL;
00708                 delete fileUtil;
00709                 fileUtil = NULL;
00710         }       
00711 }//end knock
00712 
00713 
00714 void send(){
00715         bool bSend = false;
00716         std::string jobID = "";
00717         OSSolverAgent* osagent = NULL;
00718         // first get the jobID
00719         std::string sOSoL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <osol xmlns=\"os.optimizationservices.org\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"os.optimizationservices.org http://www.optimizationservices.org/schemas/OSoL.xsd\"><general> </general></osol>";
00720         string::size_type iStringpos;
00721         try{
00722                 // call a method here to get OSiL if we have an nl or mps file
00723                 if(osoptions->osil == ""){
00724                         //we better have an nl file present or mps file
00725                         if(osoptions->nlFile != ""){
00726                                 getOSiLFromNl();
00727                         }
00728                         else{
00729                                 if(osoptions->mpsFile != ""){
00730                                         getOSiLFromMps();
00731                                 }
00732                                 else{
00733                                         throw ErrorClass("solve called and no osil, nl, or mps file given");
00734                                 }
00735                         }
00736                 }
00737                 if(osoptions->serviceLocation != ""){
00738                         osagent = new OSSolverAgent( osoptions->serviceLocation );
00739                         // check to see if there is an osol 
00740                         if(osoptions->osolFile == ""){
00741                                 // get a jobID
00742                                 jobID =  osagent->getJobID( sOSoL) ;
00743                                 // insert the jobID into the default osol
00744                                 iStringpos = sOSoL.find("</general");
00745                                 cout << "startel ==  " << iStringpos << endl;
00746                                 if(iStringpos != std::string::npos) sOSoL.insert(iStringpos, "<jobID>" + jobID+ "</jobID>");
00747                         }
00748                         else{
00749                                 // we have an osol file use it for the sOSoL
00750                                 sOSoL = osoptions->osol;
00751                                 // see if we have a jobID
00752                                 iStringpos = sOSoL.find("<jobID");
00753                                 // if we have a jobId send out the osil and osol files
00754                                 if(iStringpos == std::string::npos){
00755                                         // get a jobID and insert it
00756                                         jobID =  osagent->getJobID( osoptions->osol) ;
00757                                         iStringpos = sOSoL.find("</general");
00758                                         if(iStringpos != std::string::npos) sOSoL.insert(iStringpos, "<jobID>" + jobID+ "</jobID>");
00759                                 }
00760                         }
00761                         cout << sOSoL << endl;
00762                         bSend = osagent->send(osoptions->osil, sOSoL);
00763                         if(bSend == true) cout << "send is true" << endl;
00764                         else cout << "send is false" << endl;
00765                         delete  osagent;
00766                 }
00767                 else{
00768                         delete  osagent;
00769                         throw ErrorClass( "please specify service location (url)" );
00770                 }
00771         }
00772         catch(const ErrorClass& eclass){
00773                 FileUtil *fileUtil = NULL;
00774                 fileUtil = new FileUtil();
00775                 OSResult *osresult = NULL;
00776                 OSrLWriter *osrlwriter = NULL;
00777                 osrlwriter = new OSrLWriter();
00778                 osresult = new OSResult();
00779                 osresult->setGeneralMessage( eclass.errormsg);
00780                 osresult->setGeneralStatusType( "error");
00781                 std::string osrl = osrlwriter->writeOSrL( osresult);
00782                 if(osoptions->osrlFile != "") fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00783                 else cout << osrl << endl;
00784                 delete osresult;
00785                 osresult = NULL;
00786                 delete osrlwriter;
00787                 osrlwriter = NULL;
00788                 delete fileUtil;
00789                 fileUtil = NULL;
00790         }       
00791 }//end send
00792 
00793 void retrieve(){
00794         FileUtil *fileUtil = NULL;
00795         fileUtil = new FileUtil();
00796         std::string osrl = "";
00797         OSSolverAgent* osagent = NULL;
00798         try{
00799                 if(osoptions->serviceLocation != ""){
00800                         osagent = new OSSolverAgent( osoptions->serviceLocation );
00801                         std::cout << "HERE ARE THE OSOL OPTIONS " <<  osoptions->osol << std::endl;
00802                         osrl = osagent->retrieve( osoptions->osol);
00803                         if(osoptions->osrlFile != "") {
00804                                 fileUtil->writeFileFromString(osoptions->osrlFile, osrl); 
00805                                 if(osoptions->browser != ""){
00806                                         std::string str = osoptions->browser + "  " + osoptions->osrlFile ;
00807                                         const char *ch = &str[ 0];
00808                                         std::system( ch );
00809                                 }
00810                         }
00811                         else cout << osrl << endl;
00812                         delete osagent;
00813                         osagent = NULL;
00814                 }
00815                 else{
00816                         delete osagent;
00817                         osagent = NULL;
00818                         throw ErrorClass( "please specify service location (url)" );
00819                 }
00820                 delete fileUtil;
00821                 fileUtil = NULL;
00822         }
00823         catch(const ErrorClass& eclass){
00824                 OSResult *osresult = NULL;
00825                 OSrLWriter *osrlwriter = NULL;
00826                 osrlwriter = new OSrLWriter();
00827                 osresult = new OSResult();
00828                 osresult->setGeneralMessage( eclass.errormsg);
00829                 osresult->setGeneralStatusType( "error");
00830                 std::string osrl = osrlwriter->writeOSrL( osresult);
00831                 if(osoptions->osrlFile != "") fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00832                 else cout << osrl << endl;
00833                 delete osresult;
00834                 osresult = NULL;
00835                 delete osrlwriter;
00836                 osrlwriter = NULL;
00837                 delete fileUtil;
00838                 fileUtil = NULL;
00839         }       
00840 }//end retrieve
00841 
00842 void kill(){
00843         FileUtil *fileUtil = NULL;
00844         fileUtil = new FileUtil();
00845         std::string osplOutput = "";
00846         OSSolverAgent* osagent = NULL;
00847         try{
00848                 if(osoptions->serviceLocation != ""){
00849                         osagent = new OSSolverAgent( osoptions->serviceLocation );
00850                         osplOutput = osagent->kill( osoptions->osol);
00851                         if(osoptions->osplOutputFile != "") fileUtil->writeFileFromString(osoptions->osplOutputFile, osplOutput);
00852                         else cout << osplOutput << endl;
00853                         delete osagent;
00854                         osagent = NULL;
00855                 }
00856                 else{
00857                         delete osagent;
00858                         osagent = NULL;
00859                         throw ErrorClass( "please specify service location (url)" );
00860                 }
00861                 delete fileUtil;
00862                 fileUtil = NULL;
00863         }
00864         catch(const ErrorClass& eclass){
00865                 OSResult *osresult = NULL;
00866                 OSrLWriter *osrlwriter = NULL;
00867                 osrlwriter = new OSrLWriter();
00868                 osresult = new OSResult();
00869                 osresult->setGeneralMessage( eclass.errormsg);
00870                 osresult->setGeneralStatusType( "error");
00871                 std::string osrl = osrlwriter->writeOSrL( osresult);
00872                 if(osoptions->osrlFile != "") fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00873                 else cout << osrl << endl;
00874                 delete osresult;
00875                 osresult = NULL;
00876                 delete osrlwriter;
00877                 osrlwriter = NULL;
00878                 delete fileUtil;
00879                 fileUtil = NULL;
00880         }       
00881 }//end kill
00882 
00883 void getOSiLFromNl(){
00884         try{
00885                 #ifdef COIN_HAS_ASL
00886                 OSnl2osil *nl2osil = NULL;
00887                 nl2osil = new OSnl2osil( osoptions->nlFile);
00888                 nl2osil->createOSInstance() ;
00889                 OSiLWriter *osilwriter = NULL;
00890                 osilwriter = new OSiLWriter();
00891                 std::string osil;
00892                 osil = osilwriter->writeOSiL(  nl2osil->osinstance) ;
00893                 osoptions->osil = osil;
00894                 delete nl2osil;
00895                 nl2osil = NULL;
00896                 delete osilwriter;
00897                 osilwriter = NULL;      
00898                 #else
00899                 throw ErrorClass("trying to convert nl to osil without AMPL ASL configured");
00900                 #endif
00901         }
00902         catch(const ErrorClass& eclass){
00903                 std::cout << eclass.errormsg <<  std::endl;
00904                 throw ErrorClass( eclass.errormsg) ;
00905         }       
00906 }//getOSiLFromNl
00907 
00908 
00909 void getOSiLFromMps(){
00910         try{
00911                 OSmps2osil *mps2osil = NULL;
00912                 mps2osil = new OSmps2osil( osoptions->mpsFile);
00913                 mps2osil->createOSInstance() ;
00914                 OSiLWriter *osilwriter = NULL;
00915                 osilwriter = new OSiLWriter();
00916                 std::string osil;
00917                 osil = osilwriter->writeOSiL(  mps2osil->osinstance) ;
00918                 osoptions->osil = osil;
00919                 delete mps2osil;
00920                 mps2osil = NULL;
00921                 delete osilwriter;
00922                 osilwriter = NULL;      
00923         }
00924         catch(const ErrorClass& eclass){
00925                 std::cout << eclass.errormsg <<  std::endl;
00926                 throw ErrorClass( eclass.errormsg) ;
00927         }       
00928         
00929 }//getOSiLFromMps
00930 
00931 string getServiceURI( std::string osol){
00932         if(osol == "") return osol;
00933         string::size_type pos2;
00934         string::size_type  pos1 = osol.find( "<serviceURI");
00935         if(pos1 != std::string::npos){
00936                 // get the end of the serviceURI start tag
00937                 pos1 = osol.find(">", pos1 + 1);
00938                 if(pos1 != std::string::npos){
00939                         // get the start of serviceURI end tag
00940                         pos2 = osol.find( "</serviceURI", pos1 + 1);
00941                         if( pos2 != std::string::npos){
00942                                 // get the substring
00943                                 return osol.substr( pos1 + 1, pos2 - pos1 - 1); 
00944                         }
00945                         else return "";
00946                 }
00947                 else return "";
00948         }
00949         else return "";
00950 }//getServiceURI
00951 
00952 string getInstanceLocation( std::string osol){
00953         if(osol == "") return osol;
00954         string::size_type pos2;
00955         string::size_type pos1 = osol.find( "<instanceLocation");
00956         if(pos1 != std::string::npos){
00957                 // get the end of the instanceLocation start tag
00958                 pos1 = osol.find(">", pos1 + 1);
00959                 if(pos1 != std::string::npos){
00960                         // get the start of instanceLocation end tag
00961                         pos2 = osol.find( "</instanceLocation", pos1 + 1);
00962                         if( pos2 != std::string::npos){
00963                                 // get the substring
00964                                 return osol.substr( pos1 + 1, pos2 - pos1 - 1); 
00965                         }
00966                         else return "";
00967                 }
00968                 else return "";
00969         }
00970         else return "";
00971 }//getInstanceLocation
00972 
00973 string getSolverName( std::string osol){
00974         if(osol == "") return osol;
00975         string::size_type pos2;
00976         string::size_type pos1 = osol.find( "os_solver");
00977         if(pos1 != std::string::npos){
00978                 // get the end of the instanceLocation start tag
00979                 pos1 = osol.find(">", pos1 + 1);
00980                 if(pos1 != std::string::npos){
00981                         // get the start of instanceLocation end tag
00982                         pos2 = osol.find( "</other", pos1 + 1);
00983                         if( pos2 != std::string::npos){
00984                                 // get the substring
00985                                 return osol.substr( pos1 + 1, pos2 - pos1 - 1); 
00986                         }
00987                         else return "";
00988                 }
00989                 else return "";
00990         }
00991         else return "";
00992 }//getSolverName
00993 
00994 
00995 
00996 
00997 

Generated on Sat Mar 29 22:38:03 2008 by  doxygen 1.5.3