COPASI API  4.16.103
CCopasiParameterGroup.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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) 2003 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 /**
16  * CCopasiParameterGroup class.
17  * This class is used to describe parameters. This class is intended
18  * to be used with integration or optimization methods or reactions.
19  *
20  * Created for COPASI by Stefan Hoops 2002
21  */
22 
23 #include <sstream>
24 
25 #include "copasi.h"
26 
27 #include "CCopasiParameterGroup.h"
28 #include "CCopasiMessage.h"
29 
30 #include "utilities/utility.h"
31 
33  CCopasiParameter("NoName", GROUP)
34 {}
35 
37  const CCopasiContainer * pParent):
38  CCopasiParameter(src, pParent)
39 {
40  *this = src;
41 }
42 
44  const CCopasiContainer * pParent,
45  const std::string & objectType):
46  CCopasiParameter(name, CCopasiParameter::GROUP, NULL, pParent, objectType)
47 {}
48 
50 {
51  clear();
52 }
53 
54 // virtual
56 {
57  const CObjectInterface * pObjectInterface = CCopasiContainer::getObject(cn);
58 
59  if (pObjectInterface != NULL)
60  {
61  return pObjectInterface;
62  }
63 
64  std::string UniqueName = cn.getObjectName();
65 
66  std::string::size_type pos = UniqueName.find_last_of('[');
67  std::string Name = UniqueName.substr(0, pos);
68  size_t Index = strToUnsignedInt(UniqueName.substr(pos + 1).c_str());
69  size_t counter = C_INVALID_INDEX;
70 
71  index_iterator it = mValue.pGROUP->begin();
72  index_iterator end = mValue.pGROUP->end();
73 
74  for (; it != end; ++it)
75  {
76  if ((*it)->getObjectName() == Name)
77  {
78  counter++;
79 
80  if (counter == Index)
81  {
82  return (*it)->getObject(cn.getRemainder());
83  }
84  }
85  }
86 
87  return NULL;
88 }
89 
91 
93 {
94  if (getObjectName() != rhs.getObjectName())
96 
97  name_iterator itRHS = rhs.beginName();
98  name_iterator endRHS = rhs.endName();
99 
100  name_iterator itLHS = beginName();
101  name_iterator endLHS = endName();
102 
103  std::vector< std::string > ToBeRemoved;
104  std::vector< CCopasiParameter * > ToBeAdded;
105 
106  CCopasiParameter * pLHS;
107  CCopasiParameter * pRHS;
108 
109  while (itRHS != endRHS && itLHS != endLHS)
110  {
111  // We only assign parameters
112  if ((pRHS = dynamic_cast< CCopasiParameter * >(itRHS->second)) == NULL)
113  {
114  ++itRHS;
115  continue;
116  }
117 
118  // We only assign parameters
119  if ((pLHS = dynamic_cast< CCopasiParameter * >(itLHS->second)) == NULL)
120  {
121  ++itLHS;
122  continue;
123  }
124 
125  const std::string & NameLHS = pLHS->getObjectName();
126 
127  const std::string & NameRHS = pRHS->getObjectName();
128 
129  // The LHS parameter is missing on the RHS thus we need to remove it
130  if (NameLHS < NameRHS)
131  {
132  ToBeRemoved.push_back(NameLHS);
133  ++itLHS;
134  continue;
135  }
136 
137  // The RHS parameter is missing on the LHS thus we need to add it
138  if (NameLHS > NameRHS)
139  {
140  ToBeAdded.push_back(pRHS);
141  ++itRHS;
142  continue;
143  }
144 
145  // The names are equal it suffices to use the assignment operator of the parameter
146  *pLHS = *pRHS;
147  ++itLHS;
148  ++itRHS;
149  }
150 
151  // All remaining parameters of the LHS need to be removed
152  while (itLHS != endLHS)
153  {
154  // We only assign parameters
155  if ((pLHS = dynamic_cast< CCopasiParameter * >(itLHS->second)) != NULL)
156  ToBeRemoved.push_back(pLHS->getObjectName());
157 
158  ++itLHS;
159  }
160 
161  // All remaining paramter of the RHS need to be added
162  while (itRHS != endRHS)
163  {
164  // We only assign parameters
165  if ((pRHS = dynamic_cast< CCopasiParameter * >(itRHS->second)) != NULL)
166  ToBeAdded.push_back(pRHS);
167 
168  ++itRHS;
169  }
170 
171  // We remove the parameters
172  std::vector< std::string >::const_iterator itToBeRemoved = ToBeRemoved.begin();
173  std::vector< std::string >::const_iterator endToBeRemoved = ToBeRemoved.end();
174 
175  for (; itToBeRemoved != endToBeRemoved; ++itToBeRemoved)
176  this->removeParameter(*itToBeRemoved);
177 
178  // We add the missing parameters
179  CCopasiParameter * pParameter;
180  std::vector< CCopasiParameter * >::const_iterator itToBeAdded = ToBeAdded.begin();
181  std::vector< CCopasiParameter * >::const_iterator endToBeAdded = ToBeAdded.end();
182 
183  for (; itToBeAdded != endToBeAdded; ++itToBeAdded)
184  {
185  if ((*itToBeAdded)->getType() == GROUP)
186  pParameter = new CCopasiParameterGroup(* static_cast< CCopasiParameterGroup * >(*itToBeAdded));
187  else
188  pParameter = new CCopasiParameter(**itToBeAdded);
189 
190  addParameter(pParameter);
191  }
192 
193  return *this;
194 }
195 
196 void CCopasiParameterGroup::print(std::ostream * ostream) const
197 {*ostream << *this;}
198 
199 std::ostream &operator<<(std::ostream &os, const CCopasiParameterGroup & o)
200 {
201  os << "<<< Parameter Group: " << o.getObjectName() << std::endl;
202 
203  CCopasiParameterGroup::parameterGroup::const_iterator it =
204  o.CCopasiParameter::getValue().pGROUP->begin();
205  CCopasiParameterGroup::parameterGroup::const_iterator end =
206  o.CCopasiParameter::getValue().pGROUP->end();
207 
208  for (; it != end; ++it)
209  {
210  (*it)->print(&os);
211  os << std::endl;
212  }
213 
214  os << ">>> Parameter Group: " << o.getObjectName() << std::endl;
215  return os;
216 }
217 
219  const CCopasiParameterGroup & rhs)
220 {
221  if (lhs.getObjectName() != rhs.getObjectName()) return false;
222 
223  if (lhs.mValue.pGROUP->size() != rhs.mValue.pGROUP->size()) return false;
224 
225  CCopasiParameterGroup::parameterGroup::const_iterator itLhs =
226  lhs.mValue.pGROUP->begin();
227  CCopasiParameterGroup::parameterGroup::const_iterator endLhs =
228  lhs.mValue.pGROUP->end();
229 
230  CCopasiParameterGroup::parameterGroup::const_iterator itRhs =
231  rhs.mValue.pGROUP->begin();
232 
233  for (; itLhs != endLhs; ++itLhs, ++itRhs)
234  if (!(**itLhs == **itRhs)) return false;
235 
236  return true;
237 }
238 
240 {
241  if (parameter.getType() == CCopasiParameter::GROUP)
242  {
243  CCopasiParameterGroup * pGroup =
244  new CCopasiParameterGroup(*dynamic_cast<const CCopasiParameterGroup *>(&parameter));
245  addParameter(pGroup);
246  }
247  else
248  {
249  CCopasiParameter * pParameter = new CCopasiParameter(parameter);
250  addParameter(pParameter);
251  }
252 
253  return true;
254 }
255 
257 {
258  if (pParameter == NULL) return;
259 
260  CCopasiContainer::add(pParameter, true);
261  mValue.pGROUP->push_back(pParameter);
262 }
263 
265 {return const_cast< CCopasiContainer::objectMap * >(&getObjects())->begin();}
266 
268 {return const_cast< CCopasiContainer::objectMap * >(&getObjects())->end();}
269 
271 {return mValue.pGROUP->begin();}
272 
274 {return mValue.pGROUP->end();}
275 
276 bool CCopasiParameterGroup::addParameter(const std::string & name,
277  const CCopasiParameter::Type type)
278 {
279  CCopasiParameter * pParameter;
280 
281  if (type == GROUP)
282  pParameter = new CCopasiParameterGroup(name);
283  else
284  pParameter = new CCopasiParameter(name, type);
285 
286  addParameter(pParameter);
287 
288  return true;
289 }
290 
291 bool CCopasiParameterGroup::addGroup(const std::string & name)
292 {
294  return true;
295 }
296 
298 {
299  CCopasiParameterGroup * pGrp = getGroup(name);
300 
301  if (pGrp) return pGrp;
302 
303  removeParameter(name);
304 
305  addGroup(name);
306  return getGroup(name);
307 }
308 
309 bool CCopasiParameterGroup::removeParameter(const std::string & name)
310 {
311  size_t index = getIndex(name);
312 
313  if (index != C_INVALID_INDEX)
314  {
315  index_iterator it =
316  mValue.pGROUP->begin() + index;
317 
318  pdelete(*it);
319  mValue.pGROUP->erase(it, it + 1);
320 
321  return true;
322  }
323 
324  return false;
325 }
326 
327 bool CCopasiParameterGroup::removeParameter(const size_t & index)
328 {
329  if (index < size())
330  {
331  index_iterator it =
332  mValue.pGROUP->begin() + index;
333 
334  pdelete(*it);
335  mValue.pGROUP->erase(it, it + 1);
336 
337  return true;
338  }
339 
340  return false;
341 }
342 
344 {
345  std::pair < CCopasiContainer::objectMap::const_iterator,
346  CCopasiContainer::objectMap::const_iterator > range =
347  getObjects().equal_range(name);
348 
349  if (range.first == range.second) return NULL;
350 
351  return
352  dynamic_cast<CCopasiParameter *>(const_cast< CCopasiObject * >(range.first->second));
353 }
354 
355 const CCopasiParameter * CCopasiParameterGroup::getParameter(const std::string & name) const
356 {
357  std::pair < CCopasiContainer::objectMap::const_iterator,
358  CCopasiContainer::objectMap::const_iterator > range =
359  getObjects().equal_range(name);
360 
361  if (range.first == range.second) return NULL;
362 
363  return
364  dynamic_cast<CCopasiParameter *>(range.first->second);
365 }
366 
368 {
369  if (index < size())
370  return *(mValue.pGROUP->begin() + index);
371 
372  return NULL;
373 }
374 
375 const CCopasiParameter * CCopasiParameterGroup::getParameter(const size_t & index) const
376 {
377  if (index < size())
378  return *(mValue.pGROUP->begin() + index);
379 
380  return NULL;
381 }
382 
384 {return dynamic_cast<CCopasiParameterGroup *>(getParameter(name));}
385 
386 const CCopasiParameterGroup * CCopasiParameterGroup::getGroup(const std::string & name) const
387 {return dynamic_cast<const CCopasiParameterGroup *>(getParameter(name));}
388 
390 {return dynamic_cast<CCopasiParameterGroup *>(getParameter(index));}
391 
392 const CCopasiParameterGroup * CCopasiParameterGroup::getGroup(const size_t & index) const
393 {return dynamic_cast<const CCopasiParameterGroup *>(getParameter(index));}
394 
395 const CCopasiParameter::Value & CCopasiParameterGroup::getValue(const std::string & name) const
396 {
397  CCopasiParameter * pParameter =
398  const_cast< CCopasiParameterGroup * >(this)->getParameter(name);
399 
400  if (!pParameter)
401  fatalError();
402 
403  return pParameter->getValue();
404 }
405 
406 const CCopasiParameter::Value & CCopasiParameterGroup::getValue(const size_t & index) const
407 {
408  CCopasiParameter * pParameter =
409  const_cast< CCopasiParameterGroup * >(this)->getParameter(index);
410 
411  if (!pParameter)
412  fatalError();
413 
414  return pParameter->getValue();
415 }
416 
418 {
419  CCopasiParameter * pParameter = getParameter(name);
420 
421  if (!pParameter)
422  fatalError();
423 
424  return pParameter->getValue();
425 }
426 
428 {
429  CCopasiParameter * pParameter = getParameter(index);
430 
431  if (!pParameter)
432  fatalError();
433 
434  return pParameter->getValue();
435 }
436 
438 {
439  CCopasiParameter * pParameter =
440  const_cast< CCopasiParameterGroup * >(this)->getParameter(name);
441 
442  if (pParameter) return pParameter->getType();
443 
445 }
446 
448 {
449  CCopasiParameter * pParameter =
450  const_cast< CCopasiParameterGroup * >(this)->getParameter(index);
451 
452  if (pParameter) return pParameter->getType();
453 
455 }
456 
457 std::string CCopasiParameterGroup::getKey(const std::string & name) const
458 {
459  CCopasiParameter * pParameter =
460  const_cast< CCopasiParameterGroup * >(this)->getParameter(name);
461 
462  if (pParameter) return pParameter->getKey();
463 
464  return "Not Found";
465 }
466 
467 std::string CCopasiParameterGroup::getKey(const size_t & index) const
468 {
469  CCopasiParameter * pParameter =
470  const_cast< CCopasiParameterGroup * >(this)->getParameter(index);
471 
472  if (pParameter) return pParameter->getKey();
473 
474  return "Not Found";
475 }
476 
477 const std::string & CCopasiParameterGroup::getName(const size_t & index) const
478 {
479  static std::string Invalid("Invalid Index");
480 
481  CCopasiParameter * pParameter =
482  const_cast< CCopasiParameterGroup * >(this)->getParameter(index);
483 
484  if (pParameter) return pParameter->getObjectName();
485 
486  return Invalid;
487 }
488 
489 bool CCopasiParameterGroup::swap(const size_t & iFrom,
490  const size_t & iTo)
491 {
492  index_iterator from = beginIndex() + iFrom;
493  index_iterator to = beginIndex() + iTo;
494 
495  return swap(from, to);
496 }
497 
499  index_iterator & to)
500 {
501  if (from < beginIndex() || endIndex() <= from ||
502  to < beginIndex() || endIndex() <= to)
503  return false;
504 
505  CCopasiParameter *tmp = *from;
506  *from = *to;
507  *to = tmp;
508 
509  return true;
510 }
511 
513 {return mValue.pGROUP->size();}
514 
516 {
517  if (mValue.pGROUP)
518  {
519  index_iterator it = mValue.pGROUP->begin();
520  index_iterator end = mValue.pGROUP->end();
521 
522  for (; it != end; ++it) pdelete(*it);
523 
524  mValue.pGROUP->clear();
525  }
526 }
527 
528 size_t CCopasiParameterGroup::getIndex(const std::string & name) const
529 {
530  index_iterator it = mValue.pGROUP->begin();
531  index_iterator end = mValue.pGROUP->end();
532 
533  for (size_t i = 0; it != end; ++it, ++i)
534  if (name == (*it)->getObjectName()) return i;;
535 
536  return C_INVALID_INDEX;
537 }
538 
540 {
541  size_t counter = C_INVALID_INDEX;
542  size_t Index = C_INVALID_INDEX;
543 
544  std::string Name = pParameter->getObjectName();
545 
546  index_iterator it = mValue.pGROUP->begin();
547  index_iterator end = mValue.pGROUP->end();
548 
549  for (; it != end; ++it)
550  {
551  if ((*it)->getObjectName() == Name)
552  {
553  counter++;
554 
555  if (*it == pParameter)
556  {
557  Index = counter;
558  }
559  }
560  }
561 
562  if (counter == 0 || Index == C_INVALID_INDEX)
563  {
564  return Name;
565  }
566 
567  std::stringstream UniqueName;
568  UniqueName << Name << "[" << Index << "]";
569 
570  return UniqueName.str();
571 }
std::string getUniqueParameterName(const CCopasiParameter *pParameter) const
virtual const std::string & getName(const size_t &index) const
#define pdelete(p)
Definition: copasi.h:215
objectType
bool swap(const size_t &iFrom, const size_t &iTo)
size_t getIndex(const std::string &name) const
const std::string & getObjectName() const
CCopasiObjectName getRemainder() const
virtual const objectMap & getObjects() const
std::vector< CCopasiParameter * > * pGROUP
#define fatalError()
virtual void print(std::ostream *ostream) const
index_iterator endIndex() const
#define C_INVALID_INDEX
Definition: copasi.h:222
name_iterator beginName() const
bool operator==(const CCopasiParameterGroup &lhs, const CCopasiParameterGroup &rhs)
CCopasiContainer::objectMap::iterator name_iterator
std::multimap< std::string, CCopasiObject * > objectMap
bool removeParameter(const std::string &name)
const CCopasiParameter::Type & getType() const
std::ostream & operator<<(std::ostream &os, const CCopasiParameterGroup &o)
bool addGroup(const std::string &name)
virtual const std::string & getKey() const
index_iterator beginIndex() const
const Value & getValue() const
unsigned C_INT32 strToUnsignedInt(const char *str, char const **pTail)
Definition: utility.cpp:384
CCopasiParameterGroup * assertGroup(const std::string &name)
CCopasiParameter * getParameter(const std::string &name)
CCopasiParameterGroup & operator=(const CCopasiParameterGroup &rhs)
bool addParameter(const CCopasiParameter &parameter)
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
CCopasiParameterGroup * getGroup(const std::string &name)
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
name_iterator endName() const
bool setObjectName(const std::string &name)
parameterGroup::iterator index_iterator
std::string getObjectName() const