solver.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/src/model/solver.cpp $
00003   version : $LastChangedRevision: 1108 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2009-12-06 18:54:18 +0100 (Sun, 06 Dec 2009) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2009 by Johan De Taeye                                    *
00010  *                                                                         *
00011  * This library is free software; you can redistribute it and/or modify it *
00012  * under the terms of the GNU Lesser General Public License as published   *
00013  * by the Free Software Foundation; either version 2.1 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 GNU Lesser *
00019  * General Public License for more details.                                *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Lesser General Public        *
00022  * License along with this library; if not, write to the Free Software     *
00023  * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *
00024  * USA                                                                     *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 #define FREPPLE_CORE
00029 #include "frepple/model.h"
00030 
00031 namespace frepple
00032 {
00033 
00034 template<class Solver> DECLARE_EXPORT Tree utils::HasName<Solver>::st;
00035 DECLARE_EXPORT const MetaCategory* Solver::metadata;
00036 
00037 
00038 int Solver::initialize()
00039 {
00040   // Initialize the metadata
00041   metadata = new MetaCategory("solver", "solvers", reader, writer);
00042 
00043   // Initialize the Python class
00044   FreppleCategory<Solver>::getType().addMethod("solve", solve, METH_NOARGS, "run the solver");
00045   return FreppleCategory<Solver>::initialize();
00046 }
00047 
00048 
00049 DECLARE_EXPORT void Solver::writeElement
00050 (XMLOutput *o, const Keyword &tag, mode m) const
00051 {
00052   // The subclass should have written its own header
00053   assert(m == NOHEADER);
00054 
00055   // Fields
00056   if (loglevel) o->writeElement(Tags::tag_loglevel, loglevel);
00057 
00058   // End object
00059   o->EndObject(tag);
00060 }
00061 
00062 
00063 DECLARE_EXPORT void Solver::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
00064 {
00065   if (pAttr.isA(Tags::tag_loglevel))
00066   {
00067     int i = pElement.getInt();
00068     if (i<0 || i>USHRT_MAX)
00069       throw DataException("Invalid log level" + pElement.getString());
00070     setLogLevel(i);
00071   }
00072 }
00073 
00074 
00075 DECLARE_EXPORT PyObject* Solver::getattro(const Attribute& attr)
00076 {
00077   if (attr.isA(Tags::tag_name))
00078     return PythonObject(getName());
00079   if (attr.isA(Tags::tag_loglevel))
00080     return PythonObject(getLogLevel());
00081   return NULL;
00082 }
00083 
00084 
00085 DECLARE_EXPORT int Solver::setattro(const Attribute& attr, const PythonObject& field)
00086 {
00087   if (attr.isA(Tags::tag_name))
00088     setName(field.getString());
00089   else if (attr.isA(Tags::tag_loglevel))
00090     setLogLevel(field.getInt());
00091   else
00092     return -1;  // Error
00093   return 0;  // OK
00094 }
00095 
00096 
00097 DECLARE_EXPORT PyObject *Solver::solve(PyObject *self, PyObject *args)
00098 {
00099   Py_BEGIN_ALLOW_THREADS   // Free Python interpreter for other threads
00100   try
00101   {
00102     static_cast<Solver*>(self)->solve();
00103   }
00104   catch(...)
00105   {
00106     Py_BLOCK_THREADS;
00107     PythonType::evalException();
00108     return NULL;
00109   }
00110   Py_END_ALLOW_THREADS   // Reclaim Python interpreter
00111   return Py_BuildValue("");
00112 }
00113 
00114 } // end namespace

Documentation generated for frePPLe by  doxygen