solver.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2009 by Johan De Taeye, frePPLe bvba *
4  * *
5  * This library is free software; you can redistribute it and/or modify it *
6  * under the terms of the GNU Affero General Public License as published *
7  * by the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU Affero General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Affero General Public *
16  * License along with this program. *
17  * If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  ***************************************************************************/
20 
21 #define FREPPLE_CORE
22 #include "frepple/model.h"
23 
24 namespace frepple
25 {
26 
27 template<class Solver> DECLARE_EXPORT Tree utils::HasName<Solver>::st;
29 
30 
32 {
33  // Initialize the metadata
34  metadata = new MetaCategory("solver", "solvers", reader, writer);
35 
36  // Initialize the Python class
37  FreppleCategory<Solver>::getType().addMethod("solve", solve, METH_NOARGS, "run the solver");
39 }
40 
41 
43 (XMLOutput *o, const Keyword &tag, mode m) const
44 {
45  // The subclass should have written its own header
46  assert(m == NOHEADER);
47 
48  // Fields
49  if (loglevel) o->writeElement(Tags::tag_loglevel, loglevel);
50 
51  // End object
52  o->EndObject(tag);
53 }
54 
55 
56 DECLARE_EXPORT void Solver::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
57 {
58  if (pAttr.isA(Tags::tag_loglevel))
59  {
60  int i = pElement.getInt();
61  if (i<0 || i>USHRT_MAX)
62  throw DataException("Invalid log level" + pElement.getString());
63  setLogLevel(i);
64  }
65 }
66 
67 
69 {
70  if (attr.isA(Tags::tag_name))
71  return PythonObject(getName());
72  if (attr.isA(Tags::tag_loglevel))
73  return PythonObject(getLogLevel());
74  return NULL;
75 }
76 
77 
78 DECLARE_EXPORT int Solver::setattro(const Attribute& attr, const PythonObject& field)
79 {
80  if (attr.isA(Tags::tag_name))
81  setName(field.getString());
82  else if (attr.isA(Tags::tag_loglevel))
83  setLogLevel(field.getInt());
84  else
85  return -1; // Error
86  return 0; // OK
87 }
88 
89 
90 DECLARE_EXPORT PyObject *Solver::solve(PyObject *self, PyObject *args)
91 {
92  Py_BEGIN_ALLOW_THREADS // Free Python interpreter for other threads
93  try
94  {
95  static_cast<Solver*>(self)->solve();
96  }
97  catch(...)
98  {
99  Py_BLOCK_THREADS;
100  PythonType::evalException();
101  return NULL;
102  }
103  Py_END_ALLOW_THREADS // Reclaim Python interpreter
104  return Py_BuildValue("");
105 }
106 
107 } // end namespace