solver.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: http://svn.code.sf.net/p/frepple/code/trunk/src/model/solver.cpp $ 00003 version : $LastChangedRevision: 1713 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2012-07-18 11:46:01 +0200 (Wed, 18 Jul 2012) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2009 by Johan De Taeye, frePPLe bvba * 00010 * * 00011 * This library is free software; you can redistribute it and/or modify it * 00012 * under the terms of the GNU Affero General Public License as published * 00013 * by the Free Software Foundation; either version 3 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 * This library is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00019 * GNU Affero General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Affero General Public * 00022 * License along with this program. * 00023 * If not, see <http://www.gnu.org/licenses/>. * 00024 * * 00025 ***************************************************************************/ 00026 00027 #define FREPPLE_CORE 00028 #include "frepple/model.h" 00029 00030 namespace frepple 00031 { 00032 00033 template<class Solver> DECLARE_EXPORT Tree utils::HasName<Solver>::st; 00034 DECLARE_EXPORT const MetaCategory* Solver::metadata; 00035 00036 00037 int Solver::initialize() 00038 { 00039 // Initialize the metadata 00040 metadata = new MetaCategory("solver", "solvers", reader, writer); 00041 00042 // Initialize the Python class 00043 FreppleCategory<Solver>::getType().addMethod("solve", solve, METH_NOARGS, "run the solver"); 00044 return FreppleCategory<Solver>::initialize(); 00045 } 00046 00047 00048 DECLARE_EXPORT void Solver::writeElement 00049 (XMLOutput *o, const Keyword &tag, mode m) const 00050 { 00051 // The subclass should have written its own header 00052 assert(m == NOHEADER); 00053 00054 // Fields 00055 if (loglevel) o->writeElement(Tags::tag_loglevel, loglevel); 00056 00057 // End object 00058 o->EndObject(tag); 00059 } 00060 00061 00062 DECLARE_EXPORT void Solver::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement) 00063 { 00064 if (pAttr.isA(Tags::tag_loglevel)) 00065 { 00066 int i = pElement.getInt(); 00067 if (i<0 || i>USHRT_MAX) 00068 throw DataException("Invalid log level" + pElement.getString()); 00069 setLogLevel(i); 00070 } 00071 } 00072 00073 00074 DECLARE_EXPORT PyObject* Solver::getattro(const Attribute& attr) 00075 { 00076 if (attr.isA(Tags::tag_name)) 00077 return PythonObject(getName()); 00078 if (attr.isA(Tags::tag_loglevel)) 00079 return PythonObject(getLogLevel()); 00080 return NULL; 00081 } 00082 00083 00084 DECLARE_EXPORT int Solver::setattro(const Attribute& attr, const PythonObject& field) 00085 { 00086 if (attr.isA(Tags::tag_name)) 00087 setName(field.getString()); 00088 else if (attr.isA(Tags::tag_loglevel)) 00089 setLogLevel(field.getInt()); 00090 else 00091 return -1; // Error 00092 return 0; // OK 00093 } 00094 00095 00096 DECLARE_EXPORT PyObject *Solver::solve(PyObject *self, PyObject *args) 00097 { 00098 Py_BEGIN_ALLOW_THREADS // Free Python interpreter for other threads 00099 try 00100 { 00101 static_cast<Solver*>(self)->solve(); 00102 } 00103 catch(...) 00104 { 00105 Py_BLOCK_THREADS; 00106 PythonType::evalException(); 00107 return NULL; 00108 } 00109 Py_END_ALLOW_THREADS // Reclaim Python interpreter 00110 return Py_BuildValue(""); 00111 } 00112 00113 } // end namespace