00001 00023 #include "OSrLWriter.h" 00024 #include "OSResult.h" 00025 #include "OSParameters.h" 00026 #include "OSCommonUtil.h" 00027 #include "OSConfig.h" 00028 #include <sstream> 00029 #include <iostream> 00030 00031 using std::cout; 00032 using std::endl; 00033 using std::ostringstream; 00034 00035 OSrLWriter::OSrLWriter( ) { 00036 } 00037 00038 OSrLWriter::~OSrLWriter(){ 00039 } 00040 00041 00042 /*char* OSrLWriter::writeOSrLWrap( OSResult *theosresult){ 00043 std::string sTmp = writeOSrL( theosresult); 00044 char *ch; 00045 ch = new char[sTmp.size() + 1]; 00046 strcpy(ch, sTmp.c_str()); 00047 return ch; 00048 } 00049 */ 00050 00051 00052 00053 00054 00055 std::string OSrLWriter::writeOSrL( OSResult *theosresult){ 00056 m_OSResult = theosresult; 00057 std::ostringstream outStr; 00058 #ifdef WIN_ 00059 const char dirsep='\\'; 00060 #else 00061 const char dirsep='/'; 00062 #endif 00063 // Set directory containing stylesheet files. 00064 std::string xsltDir; 00065 xsltDir = dirsep == '/' ? "../stylesheets/" : "..\\stylesheets\\"; 00066 // always go with '/' -- it is a hypertext reference 00067 xsltDir = "../stylesheets/"; 00068 int i, j; 00069 if(m_OSResult == NULL) return outStr.str(); 00070 outStr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ; 00071 outStr << "<?xml-stylesheet type = \"text/xsl\" href = \""; 00072 outStr << xsltDir; 00073 outStr << "OSrL.xslt\"?>"; 00074 outStr << "<osrl xmlns=\"os.optimizationservices.org\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"os.optimizationservices.org http://www.optimizationservices.org/schemas/OSrL.xsd\" >" ; 00075 outStr << endl; 00076 outStr << "<resultHeader>" << endl; 00077 if(m_OSResult->resultHeader != NULL){ 00078 if(m_OSResult->resultHeader->generalStatus != NULL){ 00079 outStr << "<generalStatus " ; 00080 if(m_OSResult->resultHeader->generalStatus->type.length() > 0){ 00081 outStr << "type=\""; 00082 outStr << m_OSResult->resultHeader->generalStatus->type ; 00083 outStr << "\""; 00084 } 00085 if(m_OSResult->resultHeader->generalStatus->description.length() > 0){ 00086 outStr << "type=\""; 00087 outStr << m_OSResult->resultHeader->generalStatus->description ; 00088 outStr << "\""; 00089 } 00090 outStr << "/>" << endl; 00091 } 00092 00093 if(m_OSResult->resultHeader->serviceURI.length() > 0){ 00094 outStr << "<serviceURI>" + m_OSResult->resultHeader->serviceURI + "</serviceURI>" << endl; 00095 } 00096 00097 if(m_OSResult->resultHeader->serviceName.length() > 0){ 00098 outStr << "<serviceName>" + m_OSResult->resultHeader->serviceName + "</serviceName>" << endl; 00099 } 00100 if(m_OSResult->resultHeader->instanceName.length() > 0){ 00101 outStr << "<instanceName>" + m_OSResult->resultHeader->instanceName + "</instanceName>" << endl; 00102 } 00103 if(m_OSResult->resultHeader->jobID.length() > 0){ 00104 outStr << "<jobID>" + m_OSResult->resultHeader->jobID + "</jobID>" << endl; 00105 } 00106 if(m_OSResult->resultHeader->time.length() > 0){ 00107 outStr << "<time>" + m_OSResult->resultHeader->time + "</time>" << endl; 00108 } 00109 if(m_OSResult->resultHeader->message.length() > 0){ 00110 outStr << "<message>" + m_OSResult->resultHeader->message + "</message>" << endl; 00111 } 00112 } 00113 outStr << "</resultHeader>" << endl; 00114 if(m_OSResult->resultData != NULL){ 00115 outStr << "<resultData>" << endl; 00116 if(m_OSResult->resultData->optimization != NULL && m_OSResult->resultData->optimization->numberOfSolutions > 0){ 00117 outStr << "<optimization " ; 00118 outStr << "numberOfSolutions=\""; 00119 outStr << m_OSResult->resultData->optimization->numberOfSolutions ; 00120 outStr << "\""; 00121 outStr << " numberOfVariables=\""; 00122 outStr << m_OSResult->resultData->optimization->numberOfVariables ; 00123 outStr << "\""; 00124 outStr << " numberOfConstraints=\""; 00125 outStr << m_OSResult->resultData->optimization->numberOfConstraints ; 00126 outStr << "\""; 00127 outStr << " numberOfObjectives=\""; 00128 outStr << m_OSResult->resultData->optimization->numberOfObjectives ; 00129 outStr << "\"" ; 00130 outStr << ">" << endl; 00131 // get solution information 00132 for(i = 0; i < m_OSResult->resultData->optimization->numberOfSolutions; i++){ 00133 if(m_OSResult->resultData->optimization->solution[i] != NULL){ 00134 outStr << "<solution" ; 00135 outStr << " objectiveIdx=\""; 00136 outStr << m_OSResult->resultData->optimization->solution[i]->objectiveIdx ; 00137 outStr << "\"" ; 00138 outStr << ">" << endl; 00139 if(m_OSResult->resultData->optimization->solution[i]->status != NULL){ 00140 outStr << "<status"; 00141 if(m_OSResult->resultData->optimization->solution[i]->status->type.length() > 0){ 00142 outStr << " type=\""; 00143 outStr << m_OSResult->resultData->optimization->solution[i]->status->type; 00144 outStr << "\""; 00145 } 00146 if(m_OSResult->resultData->optimization->solution[i]->status->description.length() > 0){ 00147 outStr << " description=\""; 00148 outStr << m_OSResult->resultData->optimization->solution[i]->status->description; 00149 outStr << "\"" ; 00150 } 00151 outStr << "/>" << endl; 00152 } 00153 if(m_OSResult->resultData->optimization->solution[i]->message != ""){ 00154 outStr << "<message>" << endl; 00155 outStr << m_OSResult->resultData->optimization->solution[i]->message << endl; 00156 outStr << "</message>" << endl; 00157 } 00158 if(m_OSResult->resultData->optimization->solution[i]->variables != NULL){ 00159 outStr << "<variables>" << endl; 00160 if(m_OSResult->resultData->optimization->solution[i]->variables->values != NULL){ 00161 outStr << "<values>" << endl; 00162 for(j = 0; j < m_OSResult->resultData->optimization->numberOfVariables; j++){ 00163 if(m_OSResult->resultData->optimization->solution[i]->variables->values->var[j] != NULL){ 00164 outStr << "<var"; 00165 outStr << " idx=\""; 00166 outStr << j ; 00167 outStr << "\">"; 00168 outStr << m_OSResult->resultData->optimization->solution[i]->variables->values->var[j]->value; 00169 outStr << "</var>" << endl; 00170 } 00171 } 00172 outStr << "</values>" << endl; 00173 } 00174 if(m_OSResult->resultData->optimization->solution[i]->variables->other != NULL){ 00175 if(m_OSResult->resultData->optimization->solution[i]->variables->numberOfOtherVariableResult > 0){ 00176 for(int k = 0; k < m_OSResult->resultData->optimization->solution[i]->variables->numberOfOtherVariableResult; k++){ 00177 outStr << "<other" ; 00178 outStr << " name=\""; 00179 outStr << m_OSResult->resultData->optimization->solution[i]->variables->other[k]->name; 00180 outStr << "\"" ; 00181 outStr << " description=\""; 00182 outStr << m_OSResult->resultData->optimization->solution[i]->variables->other[k]->description; 00183 outStr << "\"" ; 00184 outStr << ">" << endl; 00185 if(m_OSResult->resultData->optimization->solution[i]->variables->other[k]->var.size() > 0){ 00186 for(j = 0; j < m_OSResult->resultData->optimization->numberOfVariables; j++){ 00187 if(m_OSResult->resultData->optimization->solution[i]->variables->other[k]->var.size() > 0){ 00188 outStr << "<var"; 00189 outStr << " idx=\""; 00190 outStr << j ; 00191 outStr << "\">"; 00192 outStr << m_OSResult->resultData->optimization->solution[i]->variables->other[k]->var[j]->value; 00193 outStr << "</var>" << endl; 00194 } 00195 } 00196 } 00197 outStr << "</other>" << endl; 00198 } 00199 } 00200 } // end of if on other variables 00201 outStr << "</variables>" << endl; 00202 } 00203 // 00204 // 00205 // 00206 if(m_OSResult->resultData->optimization->solution[i]->objectives != NULL){ 00207 outStr << "<objectives>" << endl; 00208 if(m_OSResult->resultData->optimization->solution[i]->objectives->values != NULL){ 00209 outStr << "<values>" << endl; 00210 for(j = 0; j < m_OSResult->resultData->optimization->numberOfObjectives; j++){ 00211 if(m_OSResult->resultData->optimization->solution[i]->objectives->values->obj[j] != NULL){ 00212 outStr << "<obj"; 00213 outStr << " idx=\""; 00214 outStr << -(1 + j) ; 00215 outStr << "\">"; 00216 outStr << m_OSResult->resultData->optimization->solution[i]->objectives->values->obj[j]->value; 00217 outStr << "</obj>" << endl; 00218 } 00219 } 00220 outStr << "</values>" << endl; 00221 } 00222 if(m_OSResult->resultData->optimization->solution[i]->objectives->other != NULL){ 00223 if(m_OSResult->resultData->optimization->solution[i]->objectives->numberOfOtherObjectiveResult > 0){ 00224 for(int k = 0; k < m_OSResult->resultData->optimization->solution[i]->objectives->numberOfOtherObjectiveResult; k++){ 00225 outStr << "<other" ; 00226 outStr << " name=\"";\ 00227 outStr << m_OSResult->resultData->optimization->solution[i]->objectives->other[k]->name; 00228 outStr << "\"" ; 00229 outStr << " description=\""; 00230 outStr << m_OSResult->resultData->optimization->solution[i]->objectives->other[k]->description; 00231 outStr << "\"" ; 00232 outStr << ">" << endl; 00233 if(m_OSResult->resultData->optimization->solution[i]->objectives->other[k]->obj.size() > 0){ 00234 for(j = 0; j < m_OSResult->resultData->optimization->numberOfObjectives; j++){ 00235 if(m_OSResult->resultData->optimization->solution[i]->objectives->other[k]->obj.size() > 0){ 00236 outStr << "<obj"; 00237 outStr << " idx=\""; 00238 outStr << j ; 00239 outStr << "\">"; 00240 outStr << m_OSResult->resultData->optimization->solution[i]->objectives->other[k]->obj[j]->value; 00241 outStr << "</obj>" << endl; 00242 } 00243 } 00244 } 00245 outStr << "</other>" << endl; 00246 } 00247 } 00248 } // end of if on other objectives 00249 outStr << "</objectives>" << endl; 00250 } 00251 if(m_OSResult->resultData->optimization->solution[i]->constraints != NULL){ 00252 outStr << "<constraints>" << endl; 00253 if(m_OSResult->resultData->optimization->solution[i]->constraints->values != NULL){ 00254 outStr << "<values>" << endl; 00255 for(j = 0; j < m_OSResult->resultData->optimization->numberOfConstraints; j++){ 00256 if(m_OSResult->resultData->optimization->solution[i]->constraints->values->con[j] != NULL){ 00257 outStr << "<con"; 00258 outStr << " idx=\""; 00259 outStr << j ; 00260 outStr << "\">"; 00261 outStr << m_OSResult->resultData->optimization->solution[i]->constraints->values->con[j]->value; 00262 outStr << "</con>" << endl; 00263 } 00264 } 00265 outStr << "</values>" << endl; 00266 } 00267 if(m_OSResult->resultData->optimization->solution[i]->constraints->dualValues != NULL){ 00268 outStr << "<dualValues>" << endl; 00269 for(j = 0; j < m_OSResult->resultData->optimization->numberOfConstraints; j++){ 00270 if(m_OSResult->resultData->optimization->solution[i]->constraints->dualValues->con[j] != NULL){ 00271 outStr << "<con"; 00272 outStr << " idx=\""; 00273 outStr << j ; 00274 outStr << "\">"; 00275 outStr << m_OSResult->resultData->optimization->solution[i]->constraints->dualValues->con[j]->value; 00276 outStr << "</con>" << endl; 00277 } 00278 } 00279 outStr << "</dualValues>" << endl; 00280 } 00281 if(m_OSResult->resultData->optimization->solution[i]->constraints->other != NULL){ 00282 if(m_OSResult->resultData->optimization->solution[i]->constraints->numberOfOtherConstraintResult > 0){ 00283 for(int k = 0; k < m_OSResult->resultData->optimization->solution[i]->constraints->numberOfOtherConstraintResult; k++){ 00284 outStr << "<other" ; 00285 outStr << " name=\"";\ 00286 outStr << m_OSResult->resultData->optimization->solution[i]->constraints->other[k]->name; 00287 outStr << "\"" ; 00288 outStr << " description=\""; 00289 outStr << m_OSResult->resultData->optimization->solution[i]->constraints->other[k]->description; 00290 outStr << "\"" ; 00291 outStr << ">" << endl; 00292 if(m_OSResult->resultData->optimization->solution[i]->constraints->other[k]->con.size() > 0){ 00293 for(j = 0; j < m_OSResult->resultData->optimization->numberOfConstraints; j++){ 00294 if(m_OSResult->resultData->optimization->solution[i]->constraints->other[k]->con.size() > 0){ 00295 outStr << "<con"; 00296 outStr << " idx=\""; 00297 outStr << j ; 00298 outStr << "\">"; 00299 outStr << m_OSResult->resultData->optimization->solution[i]->constraints->other[k]->con[j]->value; 00300 outStr << "</con>" << endl; 00301 } 00302 } 00303 } 00304 outStr << "</other>" << endl; 00305 } 00306 } 00307 } // end of if on other constraints 00308 outStr << "</constraints>" << endl; 00309 } 00310 outStr << "</solution>" << endl; 00311 } 00312 } // end the solution for loop 00313 outStr << "</optimization>" << endl; 00314 } 00315 outStr << "</resultData>" << endl ; 00316 } // end instanceData if 00317 outStr << "</osrl>" << endl; 00318 return outStr.str(); 00319 }// end writeOSrL 00320 00321 //the following is copied from CoinHelperFunctions 00322 00323