Exemplo n.º 1
0
void retrieve(OSCommandLine *oscommandline, OSnl2OS *nl2OS)
{
    FileUtil *fileUtil = NULL;
    fileUtil = new FileUtil();
    std::string osrl = "";
    OSSolverAgent* osagent = NULL;

    try
    {
        if (oscommandline->serviceLocation != "")
        {
            if (!nl2OS->readNl(oscommandline->nlFile))
                throw ErrorClass("Error reading .nl file.");

            osagent = new OSSolverAgent(oscommandline->serviceLocation);

            if (oscommandline->osol == "")
            {
                // we need to construct the OSoL
                OSOption *osOption = NULL;
                osOption = new OSOption();
                // get a jobId if necessary
                if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
                //set the jobID
                osOption->setJobID( oscommandline->jobID);
                // now read the osOption object into a string
                OSoLWriter *osolWriter = NULL;
                osolWriter = new OSoLWriter();
                oscommandline->osol = osolWriter->writeOSoL( osOption);
                delete osOption;
                osOption = NULL;
                delete osolWriter;
                osolWriter = NULL;
            }

            osrl = osagent->retrieve(oscommandline->osol);
            reportResults(oscommandline, osrl, nl2OS);
            delete osagent;
            osagent = NULL;
        }
        else
        {
            delete osagent;
            osagent = NULL;
            throw ErrorClass("please specify service location (url)");
        }
        delete fileUtil;
        fileUtil = NULL;
    }
    catch (const ErrorClass& eclass)
    {
        reportErrors(oscommandline, eclass.errormsg, nl2OS);
        if (osagent != NULL)
            delete osagent;
        osagent = NULL;

        delete fileUtil;
        fileUtil = NULL;
    }
}//end retrieve
Exemplo n.º 2
0
void kill(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
{
    FileUtil *fileUtil = NULL;
    fileUtil = new FileUtil();
    std::string osplOutput = "";
    OSSolverAgent* osagent = NULL;
    try
    {
        if (oscommandline->serviceLocation != "")
        {
            osagent = new OSSolverAgent(oscommandline->serviceLocation);

            if (oscommandline->osol == "")
            {
                // we need to construct the OSoL
                OSOption *osOption = NULL;
                osOption = new OSOption();
                // get a jobId if necessary
                if (oscommandline->jobID == "") throw ErrorClass("there is no JobID");
                //set the jobID
                osOption->setJobID( oscommandline->jobID);
                // now read the osOption object into a string
                OSoLWriter *osolWriter = NULL;
                osolWriter = new OSoLWriter();
                oscommandline->osol = osolWriter->writeOSoL( osOption);
                delete osOption;
                osOption = NULL;
                delete osolWriter;
                osolWriter = NULL;
            }

            osplOutput = osagent->kill(oscommandline->osol);

            if (oscommandline->osplOutputFile != "")
                fileUtil->writeFileFromString(oscommandline->osplOutputFile, osplOutput);
            else
                osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_summary, osplOutput);
            delete osagent;
            osagent = NULL;
        }
        else
        {
            delete osagent;
            osagent = NULL;
            throw ErrorClass("please specify service location (url)");
        }
        delete fileUtil;
        fileUtil = NULL;
    }
    catch (const ErrorClass& eclass)
    {
        reportErrors(oscommandline, eclass.errormsg, nl2OS);
        if (osagent != NULL)
            delete osagent;
        osagent = NULL;

        delete fileUtil;
        fileUtil = NULL;
    }
}//end kill
void getOSiLFromGams(OSCommandLine *oscommandline)
{
    try
    {
#ifdef COIN_HAS_GAMSIO
        OSgams2os *gams2os = NULL;
        gams2os = new OSgams2os(oscommandline);
        gams2os->createOSObjects();
        OSiLWriter *osilwriter = NULL;
        osilwriter = new OSiLWriter();
        oscommandline->osil = osilwriter->writeOSiL(gams2os->osinstance);
		if (gams2os->osoption != NULL)
		{
	        OSoLWriter *osolwriter = NULL;
		    osolwriter = new OSoLWriter();
			oscommandline->osol = osolwriter->writeOSoL(gams2os->osoption);
	        delete osolwriter;
		    osolwriter = NULL;
		}
        delete gams2os;
        gams2os = NULL;
        delete osilwriter;
        osilwriter = NULL;
#else
        throw ErrorClass(
            "trying to convert Gams control file to osil without GAMSUTILS configured");
#endif
    }
    catch (const ErrorClass& eclass)
    {
        std::cout << eclass.errormsg << std::endl;
        throw ErrorClass(eclass.errormsg);
    }
}//getOSiLFromGams
Exemplo n.º 4
0
void makeStrings(OSCommandLine *oscommandline)
{
    // convert the osinstance and osoption objects to strings
    if (oscommandline->osil == "")
    {
        OSiLWriter *osilwriter = new OSiLWriter();
        oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
#ifndef NDEBUG
        osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, oscommandline->osil);
#endif
        delete osilwriter;
        osilwriter = NULL; 
    }

/** With the options we need to be a little more careful:
 *  Options can be in osol and jobID strings (either can be missing)
 *  or in an osoption object (which would include both of the above)
 *  or missing entirely (in which case a dummy string needs to be created)
 */
    OSoLReader *osolreader = NULL;
    if (oscommandline->osoption != NULL || oscommandline->jobID != "" || oscommandline->osol == "")
    {
        if (oscommandline->osoption == NULL)
        {
            oscommandline->osoption = new OSOption();
            if (oscommandline->osol != "")
            { 
                osolreader = new OSoLReader();
                oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
            }
            if (oscommandline->jobID != "")
                oscommandline->osoption->setJobID(oscommandline->jobID);
        }

        OSoLWriter *osolwriter = new OSoLWriter();
        oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);
#ifndef NDEBUG
        osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, oscommandline->osol);
#endif
        delete osolwriter;
        osolwriter = NULL;
    }
    if (osolreader != NULL)
    {
        delete osolreader;
        oscommandline->osoption = NULL; // no longer needed, and memory may have just been freed 
    }
    osolreader = NULL;
}
void getOSiLFromNl(OSCommandLine *oscommandline)
{
#if 0
    try
    {
#ifdef COIN_HAS_ASL
        OSnl2os *nl2os = NULL;
        nl2os = new OSnl2os(oscommandline);
        nl2os->createOSObjects();
        OSiLWriter *osilwriter = NULL;
        osilwriter = new OSiLWriter();
        oscommandline->osil = osilwriter->writeOSiL(nl2os->osinstance);
		if (nl2os->osoption != NULL)
		{
	        OSoLWriter *osolwriter = NULL;
		    osolwriter = new OSoLWriter();
			oscommandline->osol = osolwriter->writeOSoL(nl2os->osoption);
	        delete osolwriter;
		    osolwriter = NULL;
		}
        delete nl2os;
        nl2os = NULL;
        delete osilwriter;
        osilwriter = NULL;
#else
        throw ErrorClass(
            "trying to convert nl to osil without AMPL ASL configured");
#endif
    }
    catch (const ErrorClass& eclass)
    {
        std::cout << eclass.errormsg << std::endl;
        throw ErrorClass(eclass.errormsg);
    }
#endif
}//getOSiLFromNl
Exemplo n.º 6
0
int main(int argc, char **argv)
{
    WindowsErrorPopupBlocker();
    std::ostringstream outStr;
    OSnl2OS *nl2OS = new OSnl2OS();
    OSiLReader* osilreader = new OSiLReader(); 
    FileUtil *fileUtil = NULL;
    ostringstream echo_cl;

    // initialize the command line structure 

    OSCommandLine *oscommandline;// = new OSCommandLine();
    OSCommandLineReader *oscommandlinereader = new OSCommandLineReader();

    DefaultSolver *solverType  = NULL;
    char* stub = NULL;
    if (argc > 0) stub = argv[1];

    // getting the OSAmplClient_options into a std::string is a bit of a pain...
    char* temp = getenv("OSAmplClient_options");
    ostringstream temp2;
    std::string amplclient_options;
    if (temp != NULL)
    {
        temp2 << temp; 
        amplclient_options = temp2.str();
        std::cout << amplclient_options << std::endl;
    }
    else
//        amplclient_options = "";
        amplclient_options = "serviceMethod retrieve serviceLocation http://74.94.100.129:8080/OSServer/services/OSSolverService printLevel 4 jobID gus-10-apr-2013-0001";

    // this output must be held in abeyance  until the command line
    // has been processed and printLevel has been set...
#ifndef NDEBUG
    echo_cl << "HERE ARE THE AMPLCLIENT OPTIONS ";
    echo_cl << amplclient_options;
    echo_cl << endl << endl;

    echo_cl << "Try to open file ";
    echo_cl << stub << ".nl";
    echo_cl << endl;
#endif

    // Now read the command line
    try
    {
        oscommandline = oscommandlinereader->readCommandLine(amplclient_options);
    }

    catch (const ErrorClass& eclass)
    {
        reportErrors(oscommandline, eclass.errormsg, nl2OS);
        delete oscommandlinereader;
        oscommandlinereader = NULL;
        return 1;
    }

    if (stub) oscommandline->nlFile = stub;


    /** Deal with action items: -printLevel, -logFile, -filePrintLevel, --help, --version **/

    try
    {
        outStr.str("");
        outStr.clear();
        outStr << std::endl << "using print level " << oscommandline->printLevel << " for stdout" << std::endl;

        if (oscommandline->printLevel != DEFAULT_OUTPUT_LEVEL)
        {
            osoutput->SetPrintLevel("stdout", (ENUM_OUTPUT_LEVEL)oscommandline->printLevel);

#ifndef NDEBUG
            osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, echo_cl.str());
#endif
            osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info, outStr.str());
        }
#ifndef NDEBUG
        else
        {
            osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_trace, echo_cl.str());
            osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());            
        }
#endif

        if (oscommandline->logFile != "")
        {
            int status = osoutput->AddChannel(oscommandline->logFile);
 
            switch(status)
            {
                case 0:
                    osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info,
                        "Added channel " + oscommandline->logFile);
                    break;          
                case 1:
                    osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_info,
                        "Output channel " + oscommandline->logFile + " previously defined");
                    break;
                default:
                    throw ErrorClass("Could not add output channel " + oscommandline->logFile);
            }//end switch
            

            outStr.str("");
            outStr.clear();
            outStr << std::endl << "using print level " << oscommandline->filePrintLevel;
            outStr << " for " << oscommandline->logFile << std::endl;

            if (oscommandline->filePrintLevel != DEFAULT_OUTPUT_LEVEL)
            {
                osoutput->SetPrintLevel(oscommandline->logFile, (ENUM_OUTPUT_LEVEL)oscommandline->filePrintLevel);
            }
            else
            {
                osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());            
            }
        }

        if (oscommandline->invokeHelp == true)
        {
            outStr.str("");
            outStr.clear();
            outStr << std::endl << std::endl << get_help() << std::endl;
            osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());

            delete oscommandlinereader;
            oscommandlinereader = NULL;
            return 0;
        }

        if (oscommandline->writeVersion == true)
        {
            outStr.str("");
            outStr.clear();
            outStr << std::endl << std::endl << OSgetVersionInfo() << std::endl;
            osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, outStr.str());

            delete oscommandlinereader;
            oscommandlinereader = NULL;
            return 0;
        }
    }
    catch (const ErrorClass& eclass)
    {
        reportErrors(oscommandline, eclass.errormsg, nl2OS);
        delete oscommandlinereader;
        oscommandlinereader = NULL;
        return 1;
    }

#ifndef NDEBUG
    outStr.str("");
    outStr.clear();
    
    outStr << "HERE ARE THE OPTION VALUES:" << endl;
    if(oscommandline->configFile != "") outStr << "Config file = " << oscommandline->configFile << endl;
    if(oscommandline->osilFile != "") outStr << "OSiL file = " << oscommandline->osilFile << endl;
    if(oscommandline->osolFile != "") outStr << "OSoL file = " << oscommandline->osolFile << endl;
    if(oscommandline->osrlFile != "") outStr << "OSrL file = " << oscommandline->osrlFile << endl;
    //if(oscommandline->insListFile != "") outStr << "Instruction List file = " << oscommandline->insListFile << endl;
    if(oscommandline->osplInputFile != "") outStr << "OSpL Input file = " << oscommandline->osplInputFile << endl;
    if(oscommandline->serviceMethod != "") outStr << "Service Method = " << oscommandline->serviceMethod << endl;
    if(oscommandline->mpsFile != "") outStr << "MPS File Name = " << oscommandline->mpsFile << endl;
    if(oscommandline->nlFile != "") outStr << "NL File Name = " << oscommandline->nlFile << endl;
    if(oscommandline->gamsControlFile != "") outStr << "gams Control File Name = " << oscommandline->gamsControlFile << endl;
    if(oscommandline->browser != "") outStr << "Browser Value = " << oscommandline->browser << endl;
    if(oscommandline->solverName != "") outStr << "Selected Solver = " << oscommandline->solverName << endl;
    if(oscommandline->serviceLocation != "") outStr << "Service Location = " << oscommandline->serviceLocation << endl;
    if(oscommandline->jobID != "") outStr << "Job ID = " << oscommandline->jobID << endl;
    if(oscommandline->printModel) outStr << "print model prior to solve/send" << endl;
    if(oscommandline->printRowNumberAsString != "") outStr << "print model row " << oscommandline->printRowNumberAsString << " prior to solve/send" << endl;
    outStr << "print level for stdout: " << oscommandline->printLevel << endl;
    if(oscommandline->logFile != "") 
    {
        outStr << "also send output to " << oscommandline->logFile << endl;
        outStr << "print level for file output: " << oscommandline->filePrintLevel << endl;
    }

    osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_debug, outStr.str());
#endif


    //convert solver name to lower case so there is no ambiguity
    unsigned int k;
    for (k = 0; k < oscommandline->solverName.length(); k++)
    {
        oscommandline->solverName[k] = (char)tolower(oscommandline->solverName[k]);
    }

    // now call the correct serviceMethod
    // solve is the default
    if (oscommandline->serviceMethod == "") oscommandline->serviceMethod = "solve";

    try
    {
        // for local use only the solve method is available
        if (oscommandline->serviceLocation == "")
            if (oscommandline->serviceMethod != "solve") 
                throw ErrorClass("No serviceLocation given. Only \'solve\' is available locally.");

        if (oscommandline->serviceMethod[0] == 's')
        {
            // for solve or send commands we must have an instance
            if (oscommandline->osil == "")
            {
                if (!findInstance(oscommandline, nl2OS))
                    throw ErrorClass("No instance could be found");
            }

            // write out the model or portion thereof, if directed by the user
            if (oscommandline->printModel == true)
            {
                if (oscommandline->osinstance == NULL)
                {
                    oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
                }
                osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_always, 
                    oscommandline->osinstance->printModel());
            }
            else if (oscommandline->printRowNumberAsString != "")
            {
                if (oscommandline->osinstance == NULL)
                {
                    oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
                }
                doPrintRow(oscommandline->osinstance, oscommandline->printRowNumberAsString);
            }

            // write the instance to an OSiL file, if directed by the user
            if (oscommandline->osilOutputFile != "")
            {
                fileUtil = new FileUtil();
                if (oscommandline->osil == "")
                {
                    OSiLWriter* osilwriter = new OSiLWriter(); 
                    oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
                    delete osilwriter;
                    osilwriter = NULL;
                }
                fileUtil->writeFileFromString(oscommandline->osilOutputFile, oscommandline->osil);
                delete fileUtil;
                fileUtil = NULL;
            }

            // write the solver options to an OSoL file, if directed by the user
            if (oscommandline->osolOutputFile != "")
            {
                fileUtil = new FileUtil();
                if (oscommandline->osol != "" && oscommandline->osoption == NULL)
                    fileUtil->writeFileFromString(oscommandline->osolOutputFile, oscommandline->osol);
                else
                {
                    if (oscommandline->osoption == NULL)
                        oscommandline->osoption = new OSOption();
                    OSoLWriter* osolwriter = new OSoLWriter(); 
                    fileUtil->writeFileFromString(oscommandline->osolOutputFile, 
                        osolwriter->writeOSoL(oscommandline->osoption));
                    delete osolwriter;
                    osolwriter = NULL;
                }
                delete fileUtil;
                fileUtil = NULL;
            }

            // if no serviceLocation was given, do a local solve
            if (oscommandline->serviceLocation == "")
            {
                std::string osrl;
                if (oscommandline->osinstance != NULL)
                    if (oscommandline->osoption != NULL)
                        osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osinstance);
                    else
                        osrl = runSolver(oscommandline->solverName, oscommandline->osol,     oscommandline->osinstance);
                else
                    if (oscommandline->osoption != NULL)
                        osrl = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osil);
                    else
                        osrl = runSolver(oscommandline->solverName, oscommandline->osol,     oscommandline->osil);
                reportResults(oscommandline, osrl, nl2OS);
            }
    
            // remote solve or send
            else
            {
                if (oscommandline->serviceMethod[1] == 'e')
                    send(oscommandline, nl2OS);
                else
                    solve(oscommandline, nl2OS);
            }
        }

        else //call one of the other methods
        {
            switch (oscommandline->serviceMethod[0])
            {
            case 'g':
                getJobID(oscommandline, nl2OS);
                break;
            case 'r':
                retrieve(oscommandline, nl2OS);
                break;
            case 'k':
                if (oscommandline->serviceMethod[1] == 'i')
                    kill(oscommandline, nl2OS);
                else
                    knock(oscommandline, nl2OS);
                break;
            default:
                break;
            }
        }

        // garbage collection
        if (osilreader != NULL)
            delete osilreader;
            oscommandline->osinstance = NULL;
        osilreader = NULL;
        delete oscommandlinereader;
        oscommandlinereader = NULL;
        delete fileUtil;
        fileUtil = NULL;

        if (nl2OS != NULL)
            delete nl2OS;
        nl2OS = NULL;

        return 0;
    }

    catch (const ErrorClass& eclass)
    {
        reportErrors(oscommandline, eclass.errormsg, nl2OS);
        if (osilreader != NULL)
            delete osilreader;
            oscommandline->osinstance = NULL;
        osilreader = NULL;
        delete oscommandlinereader;
        oscommandlinereader = NULL;
        delete fileUtil;
        fileUtil = NULL;

        if (nl2OS != NULL)
            delete nl2OS;
        nl2OS = NULL;
        return 1;
    }
}// end of main()
Exemplo n.º 7
0
void knock(OSCommandLine *oscommandline, OSnl2OS* nl2OS)
{
    std::string osplOutput = "";
    OSSolverAgent* osagent = NULL;
    FileUtil *fileUtil = NULL;
    fileUtil = new FileUtil();
    try
    {
        if (oscommandline->serviceLocation != "")
        {
            osagent = new OSSolverAgent(oscommandline->serviceLocation);

            // if no OSpL file was given, make a default one
            if (oscommandline->osplInput == "")
            {
                std::ostringstream temp;
                temp << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" << 
                    "<ospl xmlns=\"os.optimizationservices.org\"\n" <<
                    "        xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" <<
                    "        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" <<
                    "        xsi:schemaLocation=\"os.optimizationservices.org\n" <<
                    "        http://www.optimizationservices.org/schemas/OSpL.xsd\">\n"<<
                    "     <processHeader>\n" <<
                    "          <request action=\"getAll\"/>\n" <<
                    "     </processHeader>\n" <<
                    "     <processData/>\n" <<
                    "</ospl>\n";              
                oscommandline->osplInput = temp.str();
           }

            // if a jobID was given on the command line, use it
            if(oscommandline->jobID != "") 
            {
                OSOption *osOption = NULL;
                if (oscommandline->osol == "")
                {
                    osOption = new OSOption();
                }
                else
                {
                    OSoLReader *osolReader = new OSoLReader();
                    try
                    {
                        osOption = osolReader->readOSoL(oscommandline->osol);
                        delete osolReader;
                        osolReader = NULL;
                    }
                    catch (const ErrorClass& eclass)
                    {
                        if (osolReader != NULL) delete osolReader;
                        osolReader = NULL;
                        throw ErrorClass(eclass.errormsg);
                    }
                }
                osOption->setJobID( oscommandline->jobID);
                OSoLWriter *osolWriter = new OSoLWriter();
                oscommandline->osol = osolWriter->writeOSoL( osOption);
                delete osOption;
                osOption = NULL;
                delete osolWriter;
                osolWriter = NULL;
            }

            osplOutput = osagent->knock(oscommandline->osplInput, oscommandline->osol);
            if (oscommandline->osplOutputFile != "")
                fileUtil->writeFileFromString(oscommandline->osplOutputFile,
                                              osplOutput);
            else
                osoutput->OSPrint(ENUM_OUTPUT_AREA_main, ENUM_OUTPUT_LEVEL_error, osplOutput);
            delete osagent;
        }
        else
        {
            delete osagent;
            throw ErrorClass("please specify service location (url)");
        }
        delete fileUtil;
        fileUtil = NULL;
    }
    catch (const ErrorClass& eclass)
    {
         reportErrors(oscommandline, eclass.errormsg, nl2OS);
        if (osagent != NULL)
            delete osagent;
        osagent = NULL;

        delete fileUtil;
        fileUtil = NULL;
    }
}//end knock
bool OSServiceMethods::executeServiceMethod(OSCommandLine *oscommandline)
{
    OSSolverAgent* osagent = NULL;
	/**
	 *  The required file conversions are all assumed 
	 *  to have been taken care of in the nonstandard constructor,
	 *  so this wrapper merely directs traffic
	 */
    try
    {
    	
    	
		/** the only local service method is solve() */
		if	(oscommandline->serviceLocation == "")
			if (oscommandline->serviceMethod == "solve")
				//resultString = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osinstance);
				int junk;
			else
				throw ErrorClass("No service location (URL) specified. Only \"solve\" is available locally");

		/** Here we have a remote call --- reuse as much code as possible */
		else
		{
            osagent = new OSSolverAgent(oscommandline->serviceLocation);
//solve
			if (oscommandline->serviceMethod == "solve")
	            resultString = osagent->solve(oscommandline->osil, oscommandline->osol);
//send --- first check that there is a jobID. If not, get one
			else if (oscommandline->serviceMethod == "send")
			{
                if (oscommandline->osol.find( "<jobID") == std::string::npos)
				{
	                OSoLReader *osolreader = NULL;
		            osolreader = new OSoLReader();
	                OSoLWriter *osolwriter = NULL;
		            osolwriter = new OSoLWriter();
					try
					{

						if (oscommandline->osol != "")
							oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
			            oscommandline->osoption->setJobID(osagent->getJobID(""));

						// now write the osOption object into a string
				        oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);

		                delete osolreader;
			            osolreader = NULL;
		                delete osolwriter;
			            osolwriter = NULL;
					}
					catch (const ErrorClass& eclass)
					{
						delete osolreader;
			            osolreader = NULL;
		                delete osolwriter;
			            osolwriter = NULL;
						throw ErrorClass(eclass.errormsg);
					}
				}
				bool sendResult;
				sendResult = osagent->send(oscommandline->osil, oscommandline->osol);
				if (sendResult == false)
					throw ErrorClass("send() method failed");
            }
//retrieve
			else if (oscommandline->serviceMethod == "retrieve")
			{
	            if (oscommandline->osol.find( "<jobID") == std::string::npos)
		            throw ErrorClass("there is no JobID to retrieve");
			    resultString = osagent->retrieve(oscommandline->osol);
			}
//getJobID
			else if (oscommandline->serviceMethod == "getJobID")
	            resultString = osagent->getJobID(oscommandline->osol);
//knock
			else if (oscommandline->serviceMethod == "knock")
	            resultString = osagent->knock(oscommandline->osplInput, oscommandline->osol);
//kill
			else if (oscommandline->serviceMethod == "kill")
			{
	            if (oscommandline->osol.find( "<jobID") == std::string::npos)
		            throw ErrorClass("there is no JobID to kill");
			    resultString = osagent->kill(oscommandline->osol);
			}
			else
		            throw ErrorClass("serviceMethod not recognized");

			delete osagent;
            osagent = NULL;
		}
		return true;
    }
    catch (const ErrorClass& eclass)
    {
        OSResult *osresult = NULL;
        OSrLWriter *osrlwriter = NULL;
        osrlwriter = new OSrLWriter();
        osresult = new OSResult();
        osresult->setGeneralMessage(eclass.errormsg);
        osresult->setGeneralStatusType("error");
        resultString = osrlwriter->writeOSrL(osresult);
        //catch garbage collection
        delete osresult;
        osresult = NULL;
        delete osrlwriter;
        osrlwriter = NULL;

		if (osagent != NULL) delete osagent;
            osagent = NULL;
        return false;
    }
    
 
}//executeServiceMethod
OSServiceMethods::OSServiceMethods(OSCommandLine *oscommandline): resultString("") 
{
#ifdef DEBUG_OSSERVICEMETHODS
    cout << "Inside the OSServiceMethods Constructor" << endl;
#endif

    FileUtil   *fileUtil   = NULL;
	OSiLReader *osilreader = NULL;
	OSoLReader *osolreader = NULL;
	OSiLWriter *osilwriter = NULL;
	OSoLWriter *osolwriter = NULL;
	OSmps2osil *mps2osil   = NULL;

#ifdef COIN_HAS_ASL
//    OSnl2os *nl2os = NULL;
    OSnl2osil *nl2osil = NULL;
#endif

#ifdef COIN_HAS_GAMSUTILS
    OSgams2os *gams2os = NULL;
#endif

    try
	{
	    fileUtil = new FileUtil();

		/** Prepare the OSInstance and OSOption objects if needed.
		 *  The objects are needed if
		 *  1. any output is based on the instance ((i.e, printModel, 
		 *     printRow or osilOutputFile is specified) OR
		 *  2. a solve/send command is based on input other than OSiL format
		 */
		if ( (oscommandline->printModel || oscommandline->printRowNumberAsString != "" || oscommandline->osilOutputFile != "") ||
			((oscommandline->serviceMethod[0] = 's') && (oscommandline->osilFile == "")))
		{
		/** Search for an instance in the following order
		 *  1. osil file
		 *  2. non-proprietary formats (only MPS for now)
		 *  3. proprietary formats (AMPL nl, GAMS dat, etc.)
		 */
			if (oscommandline->osilFile != "") 
			{
				osilreader = new OSiLReader();			
	            oscommandline->osil = fileUtil->getFileAsString(
                                  (oscommandline->osilFile).c_str());
				oscommandline->osinstance = osilreader->readOSiL(oscommandline->osil);
				if (oscommandline->osolFile != "") 
				{
		            oscommandline->osol = fileUtil->getFileAsString(
			                      (oscommandline->osolFile).c_str());
					osolreader = new OSoLReader();			
					oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
				}
			}
		    else if (oscommandline->mpsFile != "") 
			{
                mps2osil = new OSmps2osil(oscommandline->mpsFile);
                mps2osil->createOSInstance();
                oscommandline->osinstance = mps2osil->osinstance;
				if (oscommandline->osolFile != "") 
				{
		            oscommandline->osol = fileUtil->getFileAsString(
			                      (oscommandline->osolFile).c_str());
					osolreader = new OSoLReader();			
					oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
				}
			}
		    else if (oscommandline->nlFile != "")
			{
#ifdef COIN_HAS_ASL
//                nl2os = new OSnl2os(oscommandline);
//                nl2os->createOSObjects();
//                osinstance = nl2os->osinstance;
//                osoption   = nl2os->osoption;
                nl2osil = new OSnl2osil(oscommandline->nlFile);
                nl2osil->createOSInstance();
                oscommandline->osinstance = nl2osil->osinstance;
#else
                throw ErrorClass(
                    "nl file specified locally but ASL not present");
#endif
			}
		    else if (oscommandline->gamsControlFile != "")
			{
#ifdef COIN_HAS_GAMSUTILS
                gams2os = new OSgams2os(oscommandline);
                gams2os->createOSObjects();
                osinstance = gams2os->osinstance;
                osoption   = gams2os->osoption;
#else
                throw ErrorClass(
                    "a Gams Control specified locally but GAMSIP not present");
#endif
			}
			else
			{
				if (oscommandline->osolFile != "" && oscommandline->serviceLocation != "") 
				{
		            oscommandline->osol = fileUtil->getFileAsString(
                                  (oscommandline->osolFile).c_str());
					osolreader = new OSoLReader();			
					oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
					if (oscommandline->solverName != "")
						oscommandline->osoption->setSolverToInvoke(oscommandline->solverName);
				}
                if (oscommandline->osol.find( "<instanceLocation") == std::string::npos)
                    throw ErrorClass(
                        "Error: no optimization instance found");
			}

			// Make sure the solver name is recorded properly
			if (oscommandline->solverName != "") 
			{	
				if (oscommandline->osol == "" && oscommandline->osolFile != "") 
		            oscommandline->osol = fileUtil->getFileAsString(
                                  (oscommandline->osolFile).c_str());
				oscommandline->osoption->setSolverToInvoke(oscommandline->solverName);
			}	

			// convert OS objects to strings if necessary
			if (oscommandline->serviceLocation != "" && oscommandline->osil == "") 
			{	
				osilwriter= new OSiLWriter();
				oscommandline->osil = osilwriter->writeOSiL(oscommandline->osinstance);
			}	
			if (oscommandline->serviceLocation != "" && oscommandline->osoption != NULL) 
			{	
				osolwriter= new OSoLWriter();
				oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);
			}	
		}

		// cleanup
	    if (fileUtil != NULL) delete fileUtil;
		fileUtil = NULL;
	    if (osilreader != NULL) delete osilreader;
		osilreader = NULL;
	    if (osolreader != NULL) delete osolreader;
		osolreader = NULL;
	    if (osilwriter != NULL) delete osilwriter;
		osilwriter = NULL;
	    if (osolwriter != NULL) delete osolwriter;
		osolwriter = NULL;
	    if (mps2osil != NULL) delete mps2osil;
		mps2osil = NULL;

#ifdef COIN_HAS_ASL
//	    if (nl2os != NULL) delete nl2os;
//	    nl2os = NULL;
	    if (nl2osil != NULL) delete nl2osil;
	    nl2osil = NULL;
#endif

#ifdef COIN_HAS_GAMSUTILS
	    if (gams2os != NULL) delete gams2os;
		gams2os = NULL;
#endif
	}
	catch(const ErrorClass& eclass)
	{
	    if (fileUtil != NULL) delete fileUtil;
		fileUtil = NULL;
	    if (osilreader != NULL) delete osilreader;
		osilreader = NULL;
	    if (osolreader != NULL) delete osolreader;
		osolreader = NULL;
	    if (osilwriter != NULL) delete osilwriter;
		osilwriter = NULL;
	    if (osolwriter != NULL) delete osolwriter;
		osolwriter = NULL;
	    if (mps2osil != NULL) delete mps2osil;
		mps2osil = NULL;

#ifdef COIN_HAS_ASL
	    if (nl2osil != NULL) delete nl2osil;
	    nl2osil = NULL;
#endif

#ifdef COIN_HAS_GAMSUTILS
	    if (gams2os != NULL) delete gams2os;
		gams2os = NULL;
#endif

		throw ErrorClass(eclass.errormsg);
	}
}//end nonstandard OSServiceMethods constructor