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 #define FREPPLE_CORE
00028 #include "frepple/model.h"
00029
00030 namespace frepple
00031 {
00032
00033 template<class Resource> DECLARE_EXPORT Tree utils::HasName<Resource>::st;
00034
00035
00036 DECLARE_EXPORT void Resource::setMaximum(CalendarDouble* c)
00037 {
00038
00039 if (max_cal == c) return;
00040
00041
00042 setChanged();
00043
00044
00045 if (max_cal)
00046 {
00047 for (loadplanlist::iterator oo=loadplans.begin(); oo!=loadplans.end(); )
00048 if (oo->getType() == 4)
00049 {
00050 loadplans.erase(&(*oo));
00051 delete &(*(oo++));
00052 }
00053 else ++oo;
00054 }
00055
00056
00057 if (!c) return;
00058
00059
00060 max_cal = c;
00061 double curMax = 0.0;
00062 for (CalendarDouble::EventIterator x(max_cal); x.getDate()<Date::infiniteFuture; ++x)
00063 if (curMax != x.getValue())
00064 {
00065 curMax = x.getValue();
00066 loadplanlist::EventMaxQuantity *newBucket =
00067 new loadplanlist::EventMaxQuantity(x.getDate(), curMax);
00068 loadplans.insert(newBucket);
00069 }
00070 }
00071
00072
00073 DECLARE_EXPORT void Resource::writeElement(XMLOutput *o, const Keyword& tag, mode m) const
00074 {
00075
00076 if (m == REFERENCE)
00077 {
00078 o->writeElement(tag, Tags::tag_name, getName());
00079 return;
00080 }
00081
00082
00083 if (m != NOHEADER) o->BeginObject(tag, Tags::tag_name, getName());
00084
00085
00086 HasDescription::writeElement(o, tag);
00087 HasHierarchy<Resource>::writeElement(o, tag);
00088 o->writeElement(Tags::tag_maximum, max_cal);
00089 if (getCost() != 0.0) o->writeElement(Tags::tag_cost, getCost());
00090 o->writeElement(Tags::tag_location, loc);
00091 Plannable::writeElement(o, tag);
00092
00093
00094 if (!loads.empty())
00095 {
00096 o->BeginObject (Tags::tag_loads);
00097 for (loadlist::const_iterator i = loads.begin(); i != loads.end(); ++i)
00098
00099
00100 o->writeElement(Tags::tag_load, &*i, FULL);
00101 o->EndObject (Tags::tag_loads);
00102 }
00103
00104
00105 loadplanlist::const_iterator i = loadplans.begin();
00106 if (o->getContentType() == XMLOutput::PLAN && i!=loadplans.end())
00107 {
00108 o->BeginObject(Tags::tag_loadplans);
00109 for (; i!=loadplans.end(); ++i)
00110 if (i->getType()==1)
00111 {
00112 const LoadPlan *lp = dynamic_cast<const LoadPlan*>(&*i);
00113 o->BeginObject(Tags::tag_loadplan);
00114 o->writeElement(Tags::tag_date, lp->getDate());
00115 o->writeElement(Tags::tag_quantity, lp->getQuantity());
00116 o->writeElement(Tags::tag_onhand, lp->getOnhand());
00117 o->writeElement(Tags::tag_minimum, lp->getMin());
00118 o->writeElement(Tags::tag_maximum, lp->getMax());
00119 o->writeElement(Tags::tag_operationplan, &*(lp->getOperationPlan()), FULL);
00120 o->EndObject(Tags::tag_loadplan);
00121 }
00122 o->EndObject(Tags::tag_loadplans);
00123 }
00124
00125
00126 o->EndObject(tag);
00127 }
00128
00129
00130 DECLARE_EXPORT void Resource::beginElement (XMLInput& pIn, const Attribute& pAttr)
00131 {
00132 if (pAttr.isA (Tags::tag_load)
00133 && pIn.getParentElement().first.isA(Tags::tag_loads))
00134 {
00135 Load* l = new Load();
00136 l->setResource(this);
00137 pIn.readto(&*l);
00138 }
00139 else if (pAttr.isA (Tags::tag_maximum))
00140 pIn.readto( Calendar::reader(Calendar::metadata,pIn.getAttributes()) );
00141 else if (pAttr.isA(Tags::tag_loadplans))
00142 pIn.IgnoreElement();
00143 else if (pAttr.isA(Tags::tag_location))
00144 pIn.readto( Location::reader(Location::metadata,pIn.getAttributes()) );
00145 else
00146 HasHierarchy<Resource>::beginElement(pIn, pAttr);
00147 }
00148
00149
00150 DECLARE_EXPORT void Resource::endElement (XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
00151 {
00152
00153
00154
00155 if (pAttr.isA (Tags::tag_maximum))
00156 {
00157 CalendarDouble * c = dynamic_cast<CalendarDouble*>(pIn.getPreviousObject());
00158 if (c)
00159 setMaximum(c);
00160 else
00161 {
00162 Calendar *c = dynamic_cast<Calendar*>(pIn.getPreviousObject());
00163 if (!c)
00164 throw LogicException("Incorrect object type during read operation");
00165 throw DataException("Calendar '" + c->getName() +
00166 "' has invalid type for use as resource max calendar");
00167 }
00168 }
00169 else if (pAttr.isA (Tags::tag_cost))
00170 setCost(pElement.getDouble());
00171 else if (pAttr.isA(Tags::tag_location))
00172 {
00173 Location * d = dynamic_cast<Location*>(pIn.getPreviousObject());
00174 if (d) setLocation(d);
00175 else throw LogicException("Incorrect object type during read operation");
00176 }
00177 else
00178 {
00179 Plannable::endElement(pIn, pAttr, pElement);
00180 HasDescription::endElement(pIn, pAttr, pElement);
00181 HasHierarchy<Resource>::endElement (pIn, pAttr, pElement);
00182 }
00183 }
00184
00185
00186 DECLARE_EXPORT void Resource::deleteOperationPlans(bool deleteLocked)
00187 {
00188
00189 for (loadlist::iterator i=loads.begin(); i!=loads.end(); ++i)
00190 OperationPlan::deleteOperationPlans(i->getOperation(),deleteLocked);
00191
00192
00193 setChanged();
00194 }
00195
00196
00197 DECLARE_EXPORT Resource::~Resource()
00198 {
00199
00200
00201
00202
00203 deleteOperationPlans(true);
00204
00205
00206
00207 }
00208
00209
00210 DECLARE_EXPORT void ResourceInfinite::writeElement
00211 (XMLOutput *o, const Keyword &tag, mode m) const
00212 {
00213
00214 if (m == REFERENCE)
00215 {
00216 o->writeElement
00217 (tag, Tags::tag_name, getName(), Tags::tag_type, getType().type);
00218 return;
00219 }
00220
00221
00222 if (m != NOHEADER) o->BeginObject
00223 (tag, Tags::tag_name, getName(), Tags::tag_type, getType().type);
00224
00225
00226 Resource::writeElement(o, tag, NOHEADER);
00227 }
00228
00229
00230 DECLARE_EXPORT PyObject* PythonResource::getattro(const Attribute& attr)
00231 {
00232 if (!obj) return Py_BuildValue("");
00233 if (attr.isA(Tags::tag_name))
00234 return PythonObject(obj->getName());
00235 if (attr.isA(Tags::tag_description))
00236 return PythonObject(obj->getDescription());
00237 if (attr.isA(Tags::tag_category))
00238 return PythonObject(obj->getCategory());
00239 if (attr.isA(Tags::tag_subcategory))
00240 return PythonObject(obj->getSubCategory());
00241 if (attr.isA(Tags::tag_owner))
00242 return PythonObject(obj->getOwner());
00243 if (attr.isA(Tags::tag_location))
00244 return PythonObject(obj->getLocation());
00245 if (attr.isA(Tags::tag_maximum))
00246 return PythonObject(obj->getMaximum());
00247 if (attr.isA(Tags::tag_cost))
00248 return PythonObject(obj->getCost());
00249 if (attr.isA(Tags::tag_hidden))
00250 return PythonObject(obj->getHidden());
00251 if (attr.isA(Tags::tag_loadplans))
00252 return new PythonLoadPlanIterator(obj);
00253 if (attr.isA(Tags::tag_loads))
00254 return new PythonLoadIterator(obj);
00255 if (attr.isA(Tags::tag_level))
00256 return PythonObject(obj->getLevel());
00257 if (attr.isA(Tags::tag_cluster))
00258 return PythonObject(obj->getCluster());
00259 return NULL;
00260 }
00261
00262
00263 DECLARE_EXPORT int PythonResource::setattro(const Attribute& attr, const PythonObject& field)
00264 {
00265 if (!obj) return -1;
00266 if (attr.isA(Tags::tag_name))
00267 obj->setName(field.getString());
00268 else if (attr.isA(Tags::tag_description))
00269 obj->setDescription(field.getString());
00270 else if (attr.isA(Tags::tag_category))
00271 obj->setCategory(field.getString());
00272 else if (attr.isA(Tags::tag_subcategory))
00273 obj->setSubCategory(field.getString());
00274 else if (attr.isA(Tags::tag_owner))
00275 {
00276 if (!field.check(PythonResource::getType()))
00277 {
00278 PyErr_SetString(PythonDataException, "resource owner must be of type resource");
00279 return -1;
00280 }
00281 Resource* y = static_cast<PythonResource*>(static_cast<PyObject*>(field))->obj;
00282 obj->setOwner(y);
00283 }
00284 else if (attr.isA(Tags::tag_location))
00285 {
00286 if (!field.check(PythonLocation::getType()))
00287 {
00288 PyErr_SetString(PythonDataException, "buffer location must be of type location");
00289 return -1;
00290 }
00291 Location* y = static_cast<PythonLocation*>(static_cast<PyObject*>(field))->obj;
00292 obj->setLocation(y);
00293 }
00294 else if (attr.isA(Tags::tag_maximum))
00295 {
00296 if (!field.check(PythonCalendarDouble::getType()))
00297 {
00298 PyErr_SetString(PythonDataException, "resource maximum must be of type calendar_double");
00299 return -1;
00300 }
00301 CalendarDouble* y = static_cast<PythonCalendarDouble*>(static_cast<PyObject*>(field))->obj;
00302 obj->setMaximum(y);
00303 }
00304 else if (attr.isA(Tags::tag_hidden))
00305 obj->setHidden(field.getBool());
00306 else if (attr.isA(Tags::tag_cost))
00307 obj->setCost(field.getDouble());
00308 else
00309 return -1;
00310 return 0;
00311 }
00312
00313
00314 DECLARE_EXPORT PyObject* PythonResourceDefault::getattro(const Attribute& attr)
00315 {
00316 return PythonResource(obj).getattro(attr);
00317 }
00318
00319
00320 DECLARE_EXPORT int PythonResourceDefault::setattro(const Attribute& attr, const PythonObject& field)
00321 {
00322 return PythonResource(obj).setattro(attr, field);
00323 }
00324
00325
00326 DECLARE_EXPORT PyObject* PythonResourceInfinite::getattro(const Attribute& attr)
00327 {
00328 return PythonResource(obj).getattro(attr);
00329 }
00330
00331
00332 DECLARE_EXPORT int PythonResourceInfinite::setattro(const Attribute& attr, const PythonObject& field)
00333 {
00334 return PythonResource(obj).setattro(attr, field);
00335 }
00336
00337 }