COPASI API  4.16.103
CCopasiParameterGroup.h
Go to the documentation of this file.
1 /* Begin CVS Header
2 $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/utilities/CCopasiParameterGroup.h,v $
3 $Revision: 1.32 $
4 $Name: $
5 $Author: shoops $
6 $Date: 2012/01/19 18:41:12 $
7 End CVS Header */
8 
9 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
11 // and The University of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc. and EML Research, gGmbH.
16 // All rights reserved.
17 
18 #ifndef COPASI_CCopasiParameterGroup
19 #define COPASI_CCopasiParameterGroup
20 
21 #include <string>
22 #include <vector>
23 
26 
27 /**
28  * CCopasiParameterGroup class.
29  * This class is used to describe parameters. This class is intended
30  * to be used with integration or optimization methods or reactions.
31  *
32  * Created for COPASI by Stefan Hoops 2002
33  */
35 {
36 public:
37  typedef std::vector< CCopasiParameter * > parameterGroup;
38  typedef parameterGroup::iterator index_iterator;
39  typedef CCopasiContainer::objectMap::iterator name_iterator;
40 
41  // Operations
42 protected:
43  /**
44  * Default constructor
45  */
47 
48 public:
49  /**
50  * Copy constructor
51  * @param "const CCopasiParameterGroup &" src
52  * @param const CCopasiContainer * pParent (default: NULL)
53  */
55  const CCopasiContainer * pParent = NULL);
56 
57  /**
58  * Specific constructor
59  * @param const string & name
60  * @param const CCopasiContainer * pParent (default: NULL)
61  * @param const std::string & objectType (default: "ParameterGroup")
62  */
63  CCopasiParameterGroup(const std::string & name,
64  const CCopasiContainer * pParent = NULL,
65  const std::string & objectType = "ParameterGroup");
66 
67  /**
68  * Destructor
69  */
70  virtual ~CCopasiParameterGroup();
71 
72  virtual const CObjectInterface * getObject(const CCopasiObjectName & cn) const;
73 
74  /**
75  * This methods must be called to elevate subgroups to
76  * derived objects. The default implementation does nothing.
77  * @return bool success
78  */
79  virtual bool elevateChildren();
80 
81  /**
82  * Assignment operator
83  * @param const CCopasiParameterGroup & rhs
84  * @return CCopasiParameterGroup & lhs
85  */
87 
88  /**
89  * Retrieve the begin of an alphabetically sorted iterator
90  * @return name_iterator begin
91  */
92  name_iterator beginName() const;
93 
94  /**
95  * Retrieve the end of an alphabetically sorted iterator
96  * @return name_iterator end
97  */
98  name_iterator endName() const;
99 
100  /**
101  * Retrieve the begin of unsorted iterator
102  * Note: the swap function may be used to change the order
103  * @return name_iterator begin
104  */
105  index_iterator beginIndex() const;
106 
107  /**
108  * Retrieve the end of unsorted iterator
109  * Note: the swap function may be used to change the order
110  * @return name_iterator end
111  */
112  index_iterator endIndex() const;
113 
114  /**
115  * Add a parameter
116  * @param const CCopasiParameter & parameter
117  * @return bool success
118  */
119  bool addParameter(const CCopasiParameter & parameter);
120 
121  /**
122  * Add a parameter to the group
123  * @param const std::string & name
124  * @param const CCopasiParameter::Type type
125  * @return bool success
126  */
127  bool addParameter(const std::string & name,
128  const CCopasiParameter::Type type);
129 
130  /**
131  * Add a parameter to the group
132  * @param const std::string & name
133  * @param const CCopasiParameter::Type type
134  * @param const CType & value
135  * @return bool success
136  */
137  template < class CType >
138  bool addParameter(const std::string & name,
139  const CCopasiParameter::Type type,
140  const CType & value)
141  {
142  CCopasiParameter * pParameter;
143 
144  if (type == GROUP)
145  {
147 
148  if (!tmp->isValidValue(value))
149  {
150  delete tmp;
151  return false;
152  }
153 
154  parameterGroup *pGroup = tmp->mValue.pGROUP;
155  tmp->mValue.pGROUP =
156  static_cast<parameterGroup *>(const_cast<void *>((const void *) & value));
157 
158  pParameter = new CCopasiParameterGroup(*tmp);
159 
160  tmp->mValue.pGROUP = pGroup;
161  delete tmp;
162  }
163  else
164  {
165  pParameter = new CCopasiParameter(name, type);
166 
167  if (!pParameter->isValidValue(value))
168  {
169  delete pParameter;
170  return false;
171  }
172 
173  pParameter->setValue(value);
174  }
175 
176  addParameter(pParameter);
177 
178  return true;
179  }
180 
181  /**
182  * Assert that a parameter with the given name and type is present.
183  * If not the parameter is created with the default value.
184  * @param const std::string & name
185  * @param const CCopasiParameter::Type type
186  * @param const CType & Value
187  * @return CCopasiParameter * pParameter
188  */
189  template < class CType >
190  CCopasiParameter * assertParameter(const std::string & name,
191  const CCopasiParameter::Type type,
192  const CType & defaultValue)
193  {
194  CCopasiParameter * pParm = getParameter(name);
195 
196  if (pParm && pParm->getType() == type) return pParm;
197 
198  if (pParm) removeParameter(name);
199 
200  addParameter(name, type, defaultValue);
201 
202  return getParameter(name);
203  }
204 
205  /**
206  * Add a subgroup to the group
207  * @param const std::string & name
208  * @return bool success
209  */
210  bool addGroup(const std::string & name);
211 
212  /**
213  * Assert that a group with the given name is present.
214  * If not the group is created as empty group.
215  * @param const std::string & name
216  * @return CCopasiParameterGroup * pGroup
217  */
218  CCopasiParameterGroup * assertGroup(const std::string & name);
219 
220  /**
221  * Remove a parameter or subgroup from the group
222  * @param const std::string & name
223  * @return bool success
224  */
225  bool removeParameter(const std::string & name);
226 
227  /**
228  * Remove a parameter or subgroup from the group
229  * @param const size_t & index
230  * @return bool success
231  */
232  bool removeParameter(const size_t & index);
233 
234  /**
235  * Retrieve a parameter or subgroup from the group
236  * @param const std::string & name
237  * @return CCopasiParameter * parameter
238  */
239  CCopasiParameter * getParameter(const std::string & name);
240 
241  /**
242  * Retrieve a parameter or subgroup from the group
243  * @param const std::string & name
244  * @return const CCopasiParameter * parameter
245  */
246  const CCopasiParameter * getParameter(const std::string & name) const;
247 
248  /**
249  * Retrieve a parameter or subgroup from the group
250  * @param const size_t & index
251  * @return CCopasiParameter * parameter
252  */
253  CCopasiParameter * getParameter(const size_t & index);
254 
255  /**
256  * Retrieve a parameter or subgroup from the group
257  * @param const size_t & index
258  * @return const CCopasiParameter * parameter
259  */
260  const CCopasiParameter * getParameter(const size_t & index) const;
261 
262  /**
263  * Retrieve a subgroup from the group
264  * @param const std::string & name
265  * @return CCopasiParameterGroup * group
266  */
267  CCopasiParameterGroup * getGroup(const std::string & name);
268 
269  /**
270  * Retrieve a subgroup from the group
271  * @param const std::string & name
272  * @return const CCopasiParameterGroup * group
273  */
274  const CCopasiParameterGroup * getGroup(const std::string & name) const;
275 
276  /**
277  * Retrieve a subgroup from the group
278  * @param const size_t & index
279  * @return CCopasiParameterGroup * parameter
280  */
281  CCopasiParameterGroup * getGroup(const size_t & index);
282 
283  /**
284  * Retrieve a subgroup from the group
285  * @param const size_t & index
286  * @return const CCopasiParameterGroup * parameter
287  */
288  const CCopasiParameterGroup * getGroup(const size_t & index) const;
289 
290  /**
291  * Retrieve a pointer to the value of a parameter or subgroup
292  * @param const std::string & name
293  * @return const CCopasiParameter::Value & Value
294  */
295  const CCopasiParameter::Value & getValue(const std::string & name) const;
296 
297  /**
298  * Retrieve a pointer to the value of a parameter or subgroup
299  * @param const size_t & index
300  * @return const CCopasiParameter::Value & Value
301  */
302  const CCopasiParameter::Value & getValue(const size_t & index) const;
303 
304  /**
305  * Retrieve a pointer to the value of a parameter or subgroup
306  * @param const std::string & name
307  * @return CCopasiParameter::Value & Value
308  */
309  CCopasiParameter::Value & getValue(const std::string & name);
310 
311  /**
312  * Retrieve a pointer to the value of a parameter or subgroup
313  * @param const size_t & index
314  * @return CCopasiParameter::Value & Value
315  */
316  CCopasiParameter::Value & getValue(const size_t & index);
317 
318  /**
319  * Retrieve the type of a parameter or subgroup
320  * @param const std::string & name
321  * @return CCopasiParameter::Type
322  */
323  CCopasiParameter::Type getType(const std::string & name) const;
324 
325  /**
326  * Retrieve the type of a parameter or subgroup
327  * @param const size_t & index
328  * @return CCopasiParameter::Type
329  */
330  CCopasiParameter::Type getType(const size_t & index) const;
331 
332  /**
333  * Retrieve the key of a parameter or subgroup
334  * @param const std::string & name
335  * @return std::string key
336  */
337  std::string getKey(const std::string & name) const;
338 
339  /**
340  * Retrieve the key of a parameter or subgroup
341  * @param const size_t & index
342  * @return std::string key
343  */
344  std::string getKey(const size_t & index) const;
345 
346  /**
347  * Retrieve the name of a parameter or subgroup
348  * @param const size_t & index
349  * @return std::string name
350  *
351  */
352  virtual const std::string & getName(const size_t & index) const;
353 
354  /**
355  * Set the value of a parameter or subgroup.
356  * @param const std::string & name
357  * @param const CType & value
358  * @return bool success
359  */
360  template <class CType>
361  bool setValue(const std::string & name,
362  const CType & value)
363  {
364  CCopasiParameter * pParameter =
365  const_cast< CCopasiParameterGroup * >(this)->getParameter(name);
366 
367  if (pParameter) return pParameter->setValue(value);
368 
369  return false;
370  }
371 
372  /**
373  * Set the value of a parameter or subgroup.
374  * @param const size_t & index
375  * @param const CType & value
376  * @return bool success
377  */
378  template <class CType>
379  bool setValue(const size_t & index,
380  const CType & value)
381  {
382  CCopasiParameter * pParameter =
383  const_cast< CCopasiParameterGroup * >(this)->getParameter(index);
384 
385  if (pParameter) return pParameter->setValue(value);
386 
387  return false;
388  }
389 
390  /**
391  * Swap two parameters
392  * Note: This has effect on the index_iterator and index access methods.
393  * @param const size_t & iFrom
394  * @param const size_t & iTo
395  * @return bool success
396  */
397  bool swap(const size_t & iFrom,
398  const size_t & iTo);
399 
400  /**
401  * Swap two parameters
402  * Note: This has effect on the iterator and index access methods.
403  * @param index_iterator & from
404  * @param index_iterator & to
405  * @return bool success
406  */
407  bool swap(index_iterator & from,
408  index_iterator & to);
409 
410  /**
411  * The size of the parameter group
412  * @ return size_t size
413  */
414  size_t size() const;
415 
416  /**
417  * Clear all parameters and subgroups
418  */
419  void clear();
420 
421  /**
422  * Retrieve the index of a parameter or subgroup with a given name
423  * @param const std::string & name
424  * @return size_t index
425  */
426  size_t getIndex(const std::string & name) const;
427 
428  /**
429  * Retrieve a unique name for the given parameter
430  * @param const CCopasiParameter * pParameter
431  * @return std::string uniqueParameterName
432  */
433  std::string getUniqueParameterName(const CCopasiParameter * pParameter) const;
434 
435  /**
436  * This is the output method for any object. The default implementation
437  * provided with CCopasiObject uses the ostream operator<< of the object
438  * to print the object.To override this default behavior one needs to
439  * reimplement the virtual print function.
440  * @param std::ostream * ostream
441  */
442  virtual void print(std::ostream * ostream) const;
443 
444  /**
445  * Output stream operator
446  * @param ostream & os
447  * @param const CCopasiParameterGroup & o
448  * @return ostream & os
449  */
450  friend std::ostream &operator<<(std::ostream &os, const CCopasiParameterGroup & o);
451 
452  /**
453  * Comparison operator
454  * @param const CCopasiParameterGroup & lhs
455  * @param const CCopasiParameterGroup & rhs
456  * @return bool equal
457  */
458  friend bool operator==(const CCopasiParameterGroup & lhs,
459  const CCopasiParameterGroup & rhs);
460 
461 public:
462  /**
463  * Add a parameter
464  * @param CCopasiParameter * pParameter
465  */
466  void addParameter(CCopasiParameter * pParameter);
467 };
468 
469 // :TODO: This should be a member function but Visual C++ 6.0
470 // can not deal with specifying template parameters correctly.
471 /**
472  * Elevate a CCopasiParameter pointer to ElevateTo. If pParm is NULL,
473  * not of type ElevateFrom, or the elevation fails a NULL pointer is
474  * returned. If the elevation is success full the parameter *pParm is
475  * replaced with the elevated object.
476  * @param CCopasiParameter * pParm
477  * @return ElevateTo * pTo
478  */
479 template <typename ElevateTo, typename ElevateFrom>
480 ElevateTo * elevate(CCopasiParameter * pParm)
481 {
482  if (!pParm)
483  {
485  return NULL;
486  }
487 
488  ElevateFrom * pFrom = dynamic_cast<ElevateFrom *>(pParm);
489 
490  if (!pFrom)
491  {
493  return NULL;
494  }
495 
496  // Even if the pParm is already of the desired type we still
497  // recreate it to assure that we have proper construction.
498  ElevateTo * pTo = NULL;
499 
500  CCopasiParameterGroup * pGrp =
501  dynamic_cast<CCopasiParameterGroup *>(pParm->getObjectParent());
502 
503  if (pGrp)
504  {
505  std::vector< CCopasiParameter * >::iterator it =
506  pGrp->CCopasiParameter::getValue().pGROUP->begin();
507  std::vector< CCopasiParameter * >::iterator end =
508  pGrp->CCopasiParameter::getValue().pGROUP->end();
509 
510  while (it != end && *it != pParm) ++it;
511 
512  if (it == end)
513  {
515  return NULL;
516  }
517 
518  pTo = new ElevateTo(*pFrom);
519  delete pParm;
520 
521  pGrp->CCopasiContainer::add(pTo, true);
522  *it = pTo;
523  }
524  else
525  pTo = new ElevateTo(*pFrom);
526 
527  return pTo;
528 }
529 #endif // COPASI_CCopasiParameterGroup
std::string getUniqueParameterName(const CCopasiParameter *pParameter) const
virtual const std::string & getName(const size_t &index) const
bool setValue(const CType &value)
friend std::ostream & operator<<(std::ostream &os, const CCopasiParameterGroup &o)
objectType
bool swap(const size_t &iFrom, const size_t &iTo)
bool isValidValue(const C_FLOAT64 &value) const
size_t getIndex(const std::string &name) const
friend bool operator==(const CCopasiParameterGroup &lhs, const CCopasiParameterGroup &rhs)
std::vector< CCopasiParameter * > * pGROUP
virtual void print(std::ostream *ostream) const
index_iterator endIndex() const
bool addParameter(const std::string &name, const CCopasiParameter::Type type, const CType &value)
name_iterator beginName() const
CCopasiContainer::objectMap::iterator name_iterator
bool removeParameter(const std::string &name)
ElevateTo * elevate(CCopasiParameter *pParm)
const CCopasiParameter::Type & getType() const
std::vector< CCopasiParameter * > parameterGroup
bool addGroup(const std::string &name)
virtual const std::string & getKey() const
index_iterator beginIndex() const
const Value & getValue() const
#define MCParameter
bool setValue(const size_t &index, const CType &value)
bool setValue(const std::string &name, const CType &value)
CCopasiParameterGroup * assertGroup(const std::string &name)
CCopasiParameter * getParameter(const std::string &name)
CCopasiParameterGroup & operator=(const CCopasiParameterGroup &rhs)
bool addParameter(const CCopasiParameter &parameter)
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
CCopasiParameter * assertParameter(const std::string &name, const CCopasiParameter::Type type, const CType &defaultValue)
CCopasiParameterGroup * getGroup(const std::string &name)
name_iterator endName() const
parameterGroup::iterator index_iterator
CCopasiContainer * getObjectParent() const