COPASI API  4.16.103
CExperimentObjectMap.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <vector>
16 #include <limits>
17 #include <cmath>
18 #include <stdlib.h>
19 
20 #include "copasi.h"
21 
22 #include "CExperimentObjectMap.h"
23 
24 #include "utilities/utility.h"
26 
28  const CCopasiContainer * pParent):
29  CCopasiParameterGroup(name, pParent),
30  mObjects(0),
31  mLastColumn(0)
33 
35  const CCopasiContainer * pParent):
36  CCopasiParameterGroup(src, pParent),
37  mObjects(src.mObjects),
38  mLastColumn(src.mLastColumn)
40 
42  const CCopasiContainer * pParent):
43  CCopasiParameterGroup(group, pParent),
44  mObjects(0),
45  mLastColumn(0)
47 
49 
51 {
53 }
54 
56 {
57  bool success = true;
58 
59  std::vector<CCopasiParameter *>::iterator itColumn = mValue.pGROUP->begin();
60  std::vector<CCopasiParameter *>::iterator endColumn = mValue.pGROUP->end();
61 
62  if (itColumn != endColumn &&
63  dynamic_cast< CCopasiParameterGroup * >(*itColumn) == NULL) // We have an old data format.
64  {
66 
67  for (; itColumn != endColumn; ++itColumn)
68  {
69  CCopasiParameterGroup * pGroup = New.assertGroup((*itColumn)->getObjectName());
70  pGroup->assertParameter("Object CN", CCopasiParameter::CN, *(*itColumn)->getValue().pCN);
71  }
72 
73  clear();
74  *this = New;
75  }
76 
77  for (itColumn = mValue.pGROUP->begin(); itColumn != endColumn; ++itColumn)
78  if (((*itColumn) = elevate<CDataColumn, CCopasiParameterGroup>(*itColumn)) == NULL)
79  success = false;
80 
81  return success;
82 }
83 
84 bool CExperimentObjectMap::setNumCols(const size_t & numCols)
85 {
86  if (numCols == mValue.pGROUP->size())
87  return true;
88 
89  // We only clear the vector of parameter. We do not destroy the parameter they are still
90  // accessible through CCopasiContainer::mObjects and thus will be automatically destroyed.
91  mValue.pGROUP->clear();
92 
93  bool success = true;
94 
95  for (size_t col = 0; col < numCols; col++)
96  {
97  CCopasiParameterGroup * pGrp = assertGroup(StringPrint("%d", col));
98 
99  // assertGroup() adds only newly created groups to mValue.pGROUP. We need to add the existing
100  // ones.
101  if (mValue.pGROUP->size() < col + 1)
102  mValue.pGROUP->push_back(pGrp);
103 
104  success &= (elevate<CDataColumn, CCopasiParameterGroup>(pGrp) != NULL);
105  }
106 
107  return success;
108 }
109 
111 {
112  std::vector<CCopasiParameter *>::iterator itColumn = mValue.pGROUP->begin();
113  std::vector<CCopasiParameter *>::iterator endColumn = mValue.pGROUP->end();
114 
115  C_INT32 LastNotIgnored = -1;
116 
117  for (; itColumn != endColumn; ++itColumn)
118  if (static_cast< CDataColumn * >(*itColumn)->getRole() != CExperiment::ignore)
119  {
120  C_INT32 index = strtol(static_cast< CDataColumn * >(*itColumn)->getObjectName().c_str(), NULL, 10);
121 
122  if (index > LastNotIgnored) LastNotIgnored = index;
123  }
124 
125  return LastNotIgnored;
126 }
127 
128 // virtual
129 const std::string & CExperimentObjectMap::getName(const size_t & index) const
130 {
131  static const std::string NoName("");
132 
133  const CDataColumn * pColumn =
134  dynamic_cast< const CDataColumn * >(getGroup(StringPrint("%d", index)));
135 
136  if (pColumn)
137  return pColumn->getObjectName();
138  else
139  return NoName;
140 }
141 
142 bool CExperimentObjectMap::setRole(const size_t & index,
143  const CExperiment::Type & role)
144 {
145  CDataColumn * pColumn =
146  dynamic_cast< CDataColumn * >(getGroup(StringPrint("%d", index)));
147 
148  if (pColumn)
149  return pColumn->setRole(role);
150  else
151  return false;
152 }
153 
155 {
156  const CDataColumn * pColumn =
157  dynamic_cast< const CDataColumn * >(getGroup(StringPrint("%d", index)));
158 
159  if (pColumn)
160  return pColumn->getRole();
161  else
162  return CExperiment::ignore;
163 }
164 
165 bool CExperimentObjectMap::setObjectCN(const size_t & index,
166  const std::string & CN)
167 {
168  CDataColumn * pColumn =
169  dynamic_cast< CDataColumn * >(getGroup(StringPrint("%d", index)));
170 
171  if (pColumn)
172  return pColumn->setObjectCN((CCopasiObjectName) CN);
173  else
174  return false;
175 }
176 
177 std::string CExperimentObjectMap::getObjectCN(const size_t & index) const
178 {
179  const CDataColumn * pColumn =
180  dynamic_cast< const CDataColumn * >(getGroup(StringPrint("%d", index)));
181 
182  if (pColumn)
183  return pColumn->getObjectCN();
184  else
185  return "";
186 }
187 
188 bool CExperimentObjectMap::setScale(const size_t & index,
189  const C_FLOAT64 & weight)
190 {
191  CDataColumn * pColumn =
192  dynamic_cast< CDataColumn * >(getGroup(StringPrint("%d", index)));
193 
194  if (pColumn)
195  return pColumn->setScale(weight);
196  else
197  return false;
198 }
199 
200 C_FLOAT64 CExperimentObjectMap::getScale(const size_t & index) const
201 {
202  const CDataColumn * pColumn =
203  dynamic_cast< const CDataColumn * >(getGroup(StringPrint("%d", index)));
204 
205  if (pColumn)
206  return pColumn->getScale();
207  else
208  return std::numeric_limits<C_FLOAT64>::quiet_NaN();
209 }
210 
212 {
213  const CDataColumn * pColumn =
214  dynamic_cast< const CDataColumn * >(getGroup(StringPrint("%d", index)));
215 
216  if (pColumn)
217  return pColumn->getDefaultScale();
218  else
219  return std::numeric_limits<C_FLOAT64>::quiet_NaN();
220 }
221 
222 bool CExperimentObjectMap::compile(const std::vector< CCopasiContainer * > listOfContainer)
223 {
224  size_t i, imax = size();
225  size_t Column;
226 
227  // We need to find out the size of the object map
228  if (imax == 0)
230  else
231  {
232  mLastColumn = strtoul(getName(0).c_str(), NULL, 0);
233 
234  for (i = 1; i < imax; i++)
235  {
236  Column = strtoul(getName(i).c_str(), NULL, 0);
237 
238  if (mLastColumn < Column)
239  mLastColumn = Column;
240  }
241  }
242 
244  mObjects = NULL;
245 
246  CCopasiObject * pObject = NULL;
247  std::string CN;
248 
249  for (i = 0; i < imax; i++)
250  {
251  if ((CN = getObjectCN(i)) == "") continue;
252 
253  if ((pObject =
254  getObjectDataModel()->ObjectFromName(listOfContainer, CN)) != NULL &&
255  pObject->isValueDbl())
256  {
257  Column = strtoul(getName(i).c_str(), NULL, 0);
258  mObjects[Column] = pObject;
259  }
260  else
261  return false;
262  }
263 
264  return true;
265 }
266 
268 {return mObjects;}
269 
271 {return mLastColumn;}
272 
274 {
277 
278  for (; it != end; ++it)
279  {
280  CDataColumn * pColumn = dynamic_cast< CDataColumn * >(*it);
281 
282  if (pColumn != NULL)
283  {
284  pColumn->fixBuild55();
285  }
286  }
287 }
288 
290  const CCopasiContainer * pParent) :
291  CCopasiParameterGroup(name, pParent),
292  mpRole(NULL),
293  mpObjectCN(NULL),
294  mpScale(NULL)
295 {
297 }
298 
300  const CCopasiContainer * pParent) :
301  CCopasiParameterGroup(src, pParent),
302  mpRole(NULL),
303  mpObjectCN(NULL),
304  mpScale(NULL)
305 {
307 }
308 
310  const CCopasiContainer * pParent) :
311  CCopasiParameterGroup(group, pParent),
312  mpRole(NULL),
313  mpObjectCN(NULL),
314  mpScale(NULL)
315 {
317 }
318 
320 {}
321 
323 {
324  mpRole = (CExperiment::Type *)
326 
327  CCopasiParameter * pParm = getParameter("Object CN");
328 
329  if (pParm != NULL)
330  mpObjectCN = pParm->getValue().pCN;
331 
332  pParm = getParameter("Weight");
333 
334  if (pParm != NULL)
335  mpScale = pParm->getValue().pUDOUBLE;
336 
337  elevateChildren();
338 }
339 
341 {
342  *mpRole = *rhs.mpRole;
343  setObjectCN(rhs.getObjectCN());
344  setScale(rhs.getScale());
345 
346  return *this;
347 }
348 
350 {return true;}
351 
353 {
354  *mpRole = role;
355  return true;
356 }
357 
359 {return *mpRole;}
360 
361 bool CExperimentObjectMap::CDataColumn::setObjectCN(const std::string & objectCN)
362 {
363  if (objectCN == "")
364  {
365  if (mpObjectCN != NULL)
366  {
367  removeParameter("Object CN");
368  mpObjectCN = NULL;
369  }
370  }
371  else
372  {
373  if (mpObjectCN != NULL)
374  *mpObjectCN = objectCN;
375  else
376  mpObjectCN =
378  }
379 
380  return true;
381 }
382 
384 {
385  if (mpObjectCN != NULL)
386  return *mpObjectCN;
387  else
388  return "";
389 }
390 
392 {
393  if (isnan(weight))
394  {
395  if (mpScale != NULL)
396  {
397  removeParameter("Weight");
398  mpScale = NULL;
399  }
400 
401  return true;
402  }
403 
404  C_FLOAT64 DefaultWeight = getDefaultScale();
405 
406  if (weight != DefaultWeight || isnan(DefaultWeight))
407  {
408  if (mpScale != NULL)
409  *mpScale = weight;
410  else
411  mpScale = assertParameter("Weight", CCopasiParameter::UDOUBLE, weight)->getValue().pUDOUBLE;
412 
413  return true;
414  }
415 
416  if (mpScale != NULL)
417  {
418  removeParameter("Weight");
419  mpScale = NULL;
420  }
421 
422  return true;
423 }
424 
426 {
427  if (mpScale == NULL)
428  return getDefaultScale();
429  else
430  return *mpScale;
431 }
432 
434 {
435  if (mpObjectCN == NULL)
436  return std::numeric_limits<C_FLOAT64>::quiet_NaN();
437 
438  CCopasiParameterGroup *pGroup =
439  dynamic_cast< CCopasiParameterGroup * >(getObjectParent());
440 
441  if (pGroup == NULL)
442  return std::numeric_limits<C_FLOAT64>::quiet_NaN();
443 
444  const CExperiment *pExperiment =
445  dynamic_cast<const CExperiment * >(pGroup->getObjectParent());
446 
447  if (pExperiment == NULL)
448  return std::numeric_limits<C_FLOAT64>::quiet_NaN();
449 
450  const CCopasiDataModel* pDataModel = getObjectDataModel();
451  assert(pDataModel != NULL);
452  const CCopasiObject * pObject = pDataModel->getDataObject(*mpObjectCN);
453 
454  if (pObject == NULL)
455  return std::numeric_limits<C_FLOAT64>::quiet_NaN();
456 
457  return pExperiment->getDefaultScale(pObject);
458 }
459 
461 {
462  if (mpScale != NULL)
463  {
464  *mpScale *= *mpScale;
465  }
466 }
CCopasiDataModel * getObjectDataModel()
CCopasiObject * getDataObject(const CCopasiObjectName &CN) const
C_FLOAT64 getDefaultScale(const CCopasiObject *const &pObject) const
C_FLOAT64 getScale(const size_t &index) const
bool setScale(const size_t &index, const C_FLOAT64 &scale)
const std::string & getObjectName() const
bool compile(const std::vector< CCopasiContainer * > listOfContainer=CCopasiContainer::EmptyList)
CExperiment::Type getRole(const size_t &index) const
C_FLOAT64 getDefaultScale(const size_t &index) const
std::vector< CCopasiParameter * > * pGROUP
const CExperiment::Type & getRole() const
CExperimentObjectMap(const std::string &name="Object Map", const CCopasiContainer *pParent=NULL)
virtual const std::string & getName(const size_t &index) const
index_iterator endIndex() const
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
#define C_INVALID_INDEX
Definition: copasi.h:222
CDataColumn(const std::string &name="Object Map", const CCopasiContainer *pParent=NULL)
#define C_INT32
Definition: copasi.h:90
CRegisteredObjectName * pCN
bool removeParameter(const std::string &name)
const CVector< CCopasiObject * > & getMappedObjects() const
bool setRole(const CExperiment::Type &role)
const size_t & getLastColumn() const
index_iterator beginIndex() const
bool setRole(const size_t &index, const CExperiment::Type &role)
const Value & getValue() const
unsigned C_INT32 * pUINT
CCopasiParameterGroup * assertGroup(const std::string &name)
CCopasiParameter * getParameter(const std::string &name)
CDataColumn & operator=(const CDataColumn &rhs)
#define C_FLOAT64
Definition: copasi.h:92
CVector< CCopasiObject * > mObjects
std::string StringPrint(const char *format,...)
Definition: utility.cpp:87
bool setObjectCN(const std::string &objectCN)
bool isValueDbl() const
CCopasiParameter * assertParameter(const std::string &name, const CCopasiParameter::Type type, const CType &defaultValue)
bool setScale(const C_FLOAT64 &scale)
std::string getObjectCN(const size_t &index) const
CCopasiParameterGroup * getGroup(const std::string &name)
bool setNumCols(const size_t &numCols)
parameterGroup::iterator index_iterator
bool setObjectCN(const size_t &index, const std::string &objectCN)
CCopasiContainer * getObjectParent() const
size_t getLastNotIgnoredColumn() const