plan.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2007-2013 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 
28 DECLARE_EXPORT Plan* Plan::thePlan;
30 
31 
33 {
34  // Initialize the plan metadata.
35  metadata = new MetaCategory("plan","");
36 
37  // Initialize the Python type
39  x.setName("parameters");
40  x.setDoc("frePPLe global settings");
41  x.supportgetattro();
42  x.supportsetattro();
43  int tmp = x.typeReady();
44  const_cast<MetaCategory*>(metadata)->pythonClass = x.type_object();
45 
46  // Create a singleton plan object
47  // Since we can count on the initialization being executed only once, also
48  // in a multi-threaded configuration, we don't need a more advanced mechanism
49  // to protect the singleton plan.
50  thePlan = new Plan();
51 
52  // Add access to the information with a global attribute.
53  PythonInterpreter::registerGlobalObject("settings", &Plan::instance());
54  return tmp;
55 }
56 
57 
59 {
60  // Closing the logfile
61  Environment::setLogFile("");
62 
63  // Clear the pointer to this singleton object
64  thePlan = NULL;
65 }
66 
67 
69 {
70  // Update the time
71  cur_Date = l;
72 
73  // Let all operationplans check for new ProblemBeforeCurrent and
74  // ProblemBeforeFence problems.
75  for (Operation::iterator i = Operation::begin(); i != Operation::end(); ++i)
76  i->setChanged();
77 }
78 
79 
80 DECLARE_EXPORT void Plan::writeElement (XMLOutput *o, const Keyword& tag, mode m) const
81 {
82  // No references
83  assert(m != REFERENCE);
84 
85  // Write the head
86  if (m != NOHEAD && m != NOHEADTAIL) o->BeginObject(tag);
87 
88  // Write all own fields
89  o->writeElement(Tags::tag_name, name);
91  o->writeElement(Tags::tag_current, cur_Date);
93 
94  // Persist all categories
95  MetaCategory::persist(o);
96 
97  // Write the tail
98  if (m != NOHEADTAIL && m != NOTAIL) o->EndObject(tag);
99 }
100 
101 
102 DECLARE_EXPORT void Plan::endElement (XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
103 {
104  if (pAttr.isA(Tags::tag_current))
105  setCurrent(pElement.getDate());
106  else if (pAttr.isA(Tags::tag_description))
107  pElement >> descr;
108  else if (pAttr.isA(Tags::tag_name))
109  pElement >> name;
110  else if (pAttr.isA(Tags::tag_logfile))
111  Environment::setLogFile(pElement.getString());
112  else
113  Plannable::endElement(pIn, pAttr, pElement);
114 }
115 
116 
118 {
119  const MetaCategory *cat = MetaCategory::findCategoryByGroupTag(pIn.getParentElement().first.getHash());
120  if (cat)
121  {
122  if (cat->readFunction)
123  // Hand over control to a registered read controller
124  pIn.readto(cat->readFunction(cat,pIn.getAttributes()));
125  else
126  // There is no controller available.
127  // This piece of code will be used to skip pieces of the XML file that
128  // frePPLe doesn't need to be understand.
129  pIn.IgnoreElement();
130  }
131 }
132 
133 
135 {
136  if (attr.isA(Tags::tag_name))
137  return PythonObject(Plan::instance().getName());
138  if (attr.isA(Tags::tag_description))
140  if (attr.isA(Tags::tag_current))
142  if (attr.isA(Tags::tag_logfile))
143  return PythonObject(Environment::getLogFile());
144  return NULL;
145 }
146 
147 
148 DECLARE_EXPORT int Plan::setattro(const Attribute& attr, const PythonObject& field)
149 {
150  if (attr.isA(Tags::tag_name))
151  Plan::instance().setName(field.getString());
152  else if (attr.isA(Tags::tag_description))
154  else if (attr.isA(Tags::tag_current))
155  Plan::instance().setCurrent(field.getDate());
156  else if (attr.isA(Tags::tag_logfile))
157  Environment::setLogFile(field.getString());
158  else
159  return -1; // Error
160  return 0; // OK
161 }
162 
163 }