plan.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: http://svn.code.sf.net/p/frepple/code/trunk/src/model/plan.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) 2007-2012 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 
00034 DECLARE_EXPORT Plan* Plan::thePlan;
00035 DECLARE_EXPORT const MetaCategory* Plan::metadata;
00036 
00037 
00038 int Plan::initialize()
00039 {
00040   // Initialize the plan metadata.
00041   metadata = new MetaCategory("plan","");
00042 
00043   // Initialize the Python type
00044   PythonType& x = PythonExtension<Plan>::getType();
00045   x.setName("parameters");
00046   x.setDoc("frePPLe global settings");
00047   x.supportgetattro();
00048   x.supportsetattro();
00049   int tmp =x.typeReady();
00050   const_cast<MetaCategory*>(metadata)->pythonClass = x.type_object();
00051 
00052   // Create a singleton plan object
00053   // Since we can count on the initialization being executed only once, also
00054   // in a multi-threaded configuration, we don't need a more advanced mechanism
00055   // to protect the singleton plan.
00056   thePlan = new Plan();
00057 
00058   // Add access to the information with a global attribute.
00059   return PyModule_AddObject(PythonInterpreter::getModule(),
00060       "settings", &Plan::instance()) + tmp;
00061 }
00062 
00063 
00064 DECLARE_EXPORT Plan::~Plan()
00065 {
00066   // Closing the logfile
00067   Environment::setLogFile("");
00068 
00069   // Clear the pointer to this singleton object
00070   thePlan = NULL;
00071 }
00072 
00073 
00074 DECLARE_EXPORT void Plan::setCurrent (Date l)
00075 {
00076   // Update the time
00077   cur_Date = l;
00078 
00079   // Let all operationplans check for new ProblemBeforeCurrent and
00080   // ProblemBeforeFence problems.
00081   for (Operation::iterator i = Operation::begin(); i != Operation::end(); ++i)
00082     i->setChanged();
00083 }
00084 
00085 
00086 DECLARE_EXPORT void Plan::writeElement (XMLOutput *o, const Keyword& tag, mode m) const
00087 {
00088   // No references
00089   assert(m != REFERENCE);
00090 
00091   // Opening tag
00092   if (m!=NOHEADER) o->BeginObject(tag);
00093 
00094   // Write all own fields
00095   o->writeElement(Tags::tag_name, name);
00096   o->writeElement(Tags::tag_description, descr);
00097   o->writeElement(Tags::tag_current, cur_Date);
00098   Plannable::writeElement(o, tag);
00099 
00100   // Persist all categories
00101   MetaCategory::persist(o);
00102 
00103   o->EndObject(tag);
00104 }
00105 
00106 
00107 DECLARE_EXPORT void Plan::endElement (XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
00108 {
00109   if (pAttr.isA(Tags::tag_current))
00110     setCurrent(pElement.getDate());
00111   else if (pAttr.isA(Tags::tag_description))
00112     pElement >> descr;
00113   else if (pAttr.isA(Tags::tag_name))
00114     pElement >> name;
00115   else if (pAttr.isA(Tags::tag_logfile))
00116     Environment::setLogFile(pElement.getString());
00117   else
00118     Plannable::endElement(pIn, pAttr, pElement);
00119 }
00120 
00121 
00122 DECLARE_EXPORT void Plan::beginElement(XMLInput& pIn, const Attribute& pAttr)
00123 {
00124   const MetaCategory *cat = MetaCategory::findCategoryByGroupTag(pIn.getParentElement().first.getHash());
00125   if (cat)
00126   {
00127     if (cat->readFunction)
00128       // Hand over control to a registered read controller
00129       pIn.readto(cat->readFunction(cat,pIn.getAttributes()));
00130     else
00131       // There is no controller available.
00132       // This piece of code will be used to skip pieces of the XML file that
00133       // frePPLe doesn't need to be understand.
00134       pIn.IgnoreElement();
00135   }
00136 }
00137 
00138 
00139 DECLARE_EXPORT PyObject* Plan::getattro(const Attribute& attr)
00140 {
00141   if (attr.isA(Tags::tag_name))
00142     return PythonObject(Plan::instance().getName());
00143   if (attr.isA(Tags::tag_description))
00144     return PythonObject(Plan::instance().getDescription());
00145   if (attr.isA(Tags::tag_current))
00146     return PythonObject(Plan::instance().getCurrent());
00147   if (attr.isA(Tags::tag_logfile))
00148     return PythonObject(Environment::getLogFile());
00149   return NULL;
00150 }
00151 
00152 
00153 DECLARE_EXPORT int Plan::setattro(const Attribute& attr, const PythonObject& field)
00154 {
00155   if (attr.isA(Tags::tag_name))
00156     Plan::instance().setName(field.getString());
00157   else if (attr.isA(Tags::tag_description))
00158     Plan::instance().setDescription(field.getString());
00159   else if (attr.isA(Tags::tag_current))
00160     Plan::instance().setCurrent(field.getDate());
00161   else if (attr.isA(Tags::tag_logfile))
00162     Environment::setLogFile(field.getString());
00163   else
00164     return -1; // Error
00165   return 0;  // OK
00166 }
00167 
00168 }

Documentation generated for frePPLe by  doxygen