plan.cpp

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