solver.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00041 metadata = new MetaCategory("solver", "solvers", reader, writer);
00042
00043
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
00053 assert(m == NOHEADER);
00054
00055
00056 if (loglevel) o->writeElement(Tags::tag_loglevel, loglevel);
00057
00058
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;
00093 return 0;
00094 }
00095
00096
00097 DECLARE_EXPORT PyObject *Solver::solve(PyObject *self, PyObject *args)
00098 {
00099 Py_BEGIN_ALLOW_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
00111 return Py_BuildValue("");
00112 }
00113
00114 }