COPASI API  4.16.103
CCopasiParameter.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 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  * CCopasiParameter class.
17  * This class is used to describe method paramters. This class is intended
18  * to be used with integration or optimization methods.
19  *
20  * Created for Copasi by Stefan Hoops 2002
21  */
22 
23 #include <string>
24 
25 #include "copasi/copasi.h"
32 
33 const std::string CCopasiParameter::TypeName[] =
34 {
35  "float",
36  "unsigned float",
37  "integer",
38  "unsigned integer",
39  "bool",
40  "group",
41  "string",
42  "common name",
43  "key",
44  "file",
45  "expression",
46  ""
47 };
48 
49 const char* CCopasiParameter::XMLType[] =
50 {
51  "float",
52  "unsignedFloat",
53  "integer",
54  "unsignedInteger",
55  "bool",
56  "group",
57  "string",
58  "cn",
59  "key",
60  "file",
61  "expression",
62  NULL
63 };
64 
66  CCopasiContainer("NoName", NULL, "Parameter"),
67  mKey(CCopasiRootContainer::getKeyFactory()->add("Parameter", this)),
68  mType(INVALID),
69  mSize(0),
70  mValue(),
71  mpValueReference(NULL)
72 {mValue.pVOID = NULL;}
73 
75  const CCopasiContainer * pParent):
76  CCopasiContainer(src, pParent),
77  mKey(CCopasiRootContainer::getKeyFactory()->add(src.getObjectType(), this)),
78  mType(src.mType),
79  mSize(0),
80  mValue(),
81  mpValueReference(NULL)
82 {
83  createValue(src.mValue);
84 }
85 
86 CCopasiParameter::CCopasiParameter(const std::string & name,
87  const CCopasiParameter::Type & type,
88  const void * pValue,
89  const CCopasiContainer * pParent,
90  const std::string & objectType):
91  CCopasiContainer(name, pParent, objectType,
92  CCopasiObject::Container |
93  ((type == DOUBLE || type == UDOUBLE) ? CCopasiObject::ValueDbl :
94  ((type == INT || type == UINT) ? CCopasiObject::ValueInt :
95  ((type == STRING || type == CN || type == KEY || type == FILE || type == EXPRESSION) ? CCopasiObject::ValueString :
96  (type == BOOL) ? CCopasiObject::ValueBool : 0)))),
97  mKey(CCopasiRootContainer::getKeyFactory()->add(objectType, this)),
98  mType(type),
99  mSize(0),
100  mValue(),
101  mpValueReference(NULL)
102 {
104  value.pVOID = const_cast<void *>(pValue);
105  mValue = createValue(value);
106 }
107 
109 {
112 
113  deleteValue();
114 }
115 
117 {
118  if (getObjectName() != rhs.getObjectName())
120 
121  switch (rhs.mType)
122  {
125 
126  if (mType != DOUBLE &&
127  mType != UDOUBLE)
128  {
129  deleteValue();
130  mType = rhs.mType;
131  createValue(rhs.mValue);
132  }
133  else
134  {
135  mType = rhs.mType;
136  *mValue.pDOUBLE = *rhs.mValue.pDOUBLE;
137  }
138 
139  break;
140 
143 
144  if (mType != INT &&
145  mType != UINT)
146  {
147  deleteValue();
148  mType = rhs.mType;
149  createValue(rhs.mValue);
150  }
151  else
152  {
153  mType = rhs.mType;
154  *mValue.pINT = *rhs.mValue.pINT;
155  }
156 
157  break;
158 
160 
161  if (mType != BOOL)
162  {
163  deleteValue();
164  mType = BOOL;
165  createValue(rhs.mValue);
166  }
167  else
168  {
169  *mValue.pBOOL = *rhs.mValue.pBOOL;
170  }
171 
172  break;
173 
178 
179  if (mType != STRING &&
180  mType != KEY &&
181  mType != FILE &&
182  mType != EXPRESSION)
183  {
184  deleteValue();
185  mType = rhs.mType;
186  createValue(rhs.mValue);
187  }
188  else
189  {
190  mType = rhs.mType;
191  *mValue.pSTRING = *rhs.mValue.pSTRING;
192  }
193 
194  break;
195 
197 
198  if (mType != CN)
199  {
200  deleteValue();
201  mType = CN;
202  createValue(rhs.mValue);
203  }
204  else
205  {
206  *mValue.pCN = *rhs.mValue.pCN;
207  }
208 
209  break;
210 
212 
213  if (mType != GROUP)
214  {
215  deleteValue();
216  mType = GROUP;
217  createValue(rhs.mValue);
218  }
219 
220  *static_cast<CCopasiParameterGroup *>(this) =
221  *static_cast<const CCopasiParameterGroup *>(&rhs);
222 
223  break;
224 
226 
227  if (mType != INVALID)
228  {
229  deleteValue();
230  mType = INVALID;
231  createValue(rhs.mValue);
232  }
233 
234  break;
235  }
236 
237  return *this;
238 }
239 const std::string & CCopasiParameter::getKey() const {return mKey;}
240 
241 bool CCopasiParameter::setValue(const std::vector< CCopasiParameter * > & /* value */)
242 {
243  fatalError();
244  return false;
245 }
246 
248 
250 
252 {
253  return mpValueReference;
254 }
255 
257 {return mType;}
258 
259 bool CCopasiParameter::isValidValue(const C_FLOAT64 & value) const
260 {
264  value < 0.0)) return false;
265 
266  return true;
267 }
268 
269 bool CCopasiParameter::isValidValue(const C_INT32 & /* value */) const
270 {
271  if (mType != CCopasiParameter::INT) return false;
272 
273  return true;
274 }
275 
276 bool CCopasiParameter::isValidValue(const unsigned C_INT32 & /* value */) const
277 {
278  if (mType != CCopasiParameter::UINT) return false;
279 
280  return true;
281 }
282 
283 bool CCopasiParameter::isValidValue(const bool & C_UNUSED(value)) const
284 {
285  if (mType != CCopasiParameter::BOOL) return false;
286 
287  return true;
288 }
289 
290 bool CCopasiParameter::isValidValue(const std::string & value) const
291 {
293  return CKeyFactory::isValidKey(value);
294 
297  mType != CCopasiParameter::EXPRESSION) return false;
298 
299  return true;
300 }
301 
303 {
304  if (mType != CCopasiParameter::CN) return false;
305 
306  return true;
307 }
308 
310 {
311  if (mType != CCopasiParameter::GROUP) return false;
312 
313  return true;
314 }
315 
316 void CCopasiParameter::print(std::ostream * ostream) const
317 {*ostream << *this;}
318 
319 std::ostream &operator<<(std::ostream &os, const CCopasiParameter & o)
320 {
321  os << " " << o.getObjectName() << ": ";
322 
323  switch (o.mType)
324  {
327  os << * o.mValue.pDOUBLE;
328  break;
329 
331  os << * o.mValue.pINT;
332  break;
333 
335  os << * o.mValue.pUINT;
336  break;
337 
339  os << * o.mValue.pBOOL;
340  break;
341 
346  os << * o.mValue.pSTRING;
347  break;
348 
350  os << * o.mValue.pCN;
351  break;
352 
355  break;
356  }
357 
358  return os;
359 }
360 
361 bool operator==(const CCopasiParameter & lhs, const CCopasiParameter & rhs)
362 {
363  if (lhs.getObjectName() != rhs.getObjectName()) return false;
364 
365  if (lhs.mType != rhs.mType) return false;
366 
367  switch (lhs.mType)
368  {
371  return *lhs.mValue.pDOUBLE == *rhs.mValue.pDOUBLE;
372  break;
373 
375  return *lhs.mValue.pINT == *rhs.mValue.pINT;
376  break;
377 
379  return *lhs.mValue.pUINT == *rhs.mValue.pUINT;
380  break;
381 
383  return *lhs.mValue.pBOOL == *rhs.mValue.pBOOL;
384  break;
385 
390  return *lhs.mValue.pSTRING == *rhs.mValue.pSTRING;
391  break;
392 
394  return *lhs.mValue.pCN == *rhs.mValue.pCN;
395  break;
396 
398  return *static_cast<const CCopasiParameterGroup *>(&lhs) ==
399  *static_cast<const CCopasiParameterGroup *>(&rhs);
400  break;
401 
403  default:
404  return !memcmp(lhs.mValue.pVOID, rhs.mValue.pVOID, lhs.mSize);
405  break;
406  }
407 
408  return false;
409 }
410 
411 // virtual
413 {
414  CCopasiContainer * pObjectParent = getObjectParent();
415  CCopasiParameterGroup * pGroup;
416 
417  if (pObjectParent != NULL &&
418  (pGroup = dynamic_cast< CCopasiParameterGroup * >(pObjectParent)) != NULL)
419  {
420  return pObjectParent->getCN() + "," + CCopasiObjectName::escape(getObjectType()) + "=" + CCopasiObjectName::escape(pGroup->getUniqueParameterName(this));
421  }
422 
423  return CCopasiObject::getCN();
424 }
425 
427 {return const_cast<void *>(mValue.pVOID);}
428 
430 {
431  switch (mType)
432  {
435  mValue.pDOUBLE = new C_FLOAT64;
436 
437  if (value.pDOUBLE) * mValue.pDOUBLE = * value.pDOUBLE;
438 
439  mSize = sizeof(C_FLOAT64);
441  break;
442 
444  mValue.pINT = new C_INT32;
445 
446  if (value.pINT) * mValue.pINT = * value.pINT;
447 
448  mSize = sizeof(C_INT32);
450  break;
451 
453  mValue.pUINT = new unsigned C_INT32;
454 
455  if (value.pUINT) * mValue.pUINT = * value.pUINT;
456 
457  mSize = sizeof(unsigned C_INT32);
459  break;
460 
462  mValue.pBOOL = new bool;
463 
464  if (value.pBOOL) * mValue.pBOOL = * value.pBOOL;
465 
466  mSize = sizeof(bool);
468  break;
469 
474 
475  if (value.pSTRING)
476  mValue.pSTRING = new std::string(* value.pSTRING);
477  else
478  mValue.pSTRING = new std::string;
479 
480  mSize = sizeof(std::string);
482  break;
483 
485 
486  if (value.pCN)
487  mValue.pCN = new CRegisteredObjectName(* value.pCN);
488  else
490 
491  mSize = sizeof(CRegisteredObjectName);
493  break;
494 
496  mValue.pGROUP = new std::vector< CCopasiParameter * >;
497  mSize = sizeof(std::vector< CCopasiParameter * >);
498  break;
499 
501  mValue.pVOID = NULL;
502  mSize = 0;
503  break;
504  }
505 
506  return mValue;
507 }
508 
510 {
511  if (mValue.pVOID == NULL) return;
512 
513  switch (mType)
514  {
517  delete mValue.pDOUBLE;
518  break;
519 
521  delete mValue.pINT;
522  break;
523 
525  delete mValue.pUINT;
526  break;
527 
529  delete mValue.pBOOL;
530  break;
531 
536  delete mValue.pSTRING;
537  break;
538 
540  delete mValue.pCN;
541  break;
542 
544  delete mValue.pGROUP;
545  break;
546 
548  break;
549 
550  default:
551  fatalError();
552  break;
553  }
554 
555  mValue.pVOID = NULL;
556  return;
557 }
558 
559 // virtual
560 std::string CCopasiParameter::getObjectDisplayName(bool regular, bool richtext) const
561 {
562  // if one of the ancestors is a reaction and the parameter is not a group
563  // it is (hopefully) a kinetic parameter
564 
565  CCopasiObject* tmp = this->getObjectAncestor("Reaction");
566 
567  if (tmp && getType() != GROUP)
568  {
569  return tmp->getObjectDisplayName(regular, richtext) + "." + getObjectName();
570  }
571 
572  CCopasiContainer * pObjectParent = getObjectParent();
573  CCopasiParameterGroup * pGroup;
574 
575  if (pObjectParent != NULL &&
576  (pGroup = dynamic_cast< CCopasiParameterGroup * >(pObjectParent)) != NULL)
577  {
578  std::string DisplayName = pGroup->getObjectDisplayName(regular, richtext);
579 
580  if (DisplayName.length() >= 2 &&
581  (DisplayName.substr(DisplayName.length() - 2) == "[]"))
582  {
583  DisplayName.insert(DisplayName.length() - 1, pGroup->getUniqueParameterName(this));
584  }
585  else
586  {
587  if ((DisplayName.length() != 0) && (DisplayName[DisplayName.length() - 1] != '.'))
588  {
589  DisplayName += ".";
590  }
591 
592  DisplayName += pGroup->getUniqueParameterName(this);
593  }
594 
595  if (getType() == GROUP)
596  {
597  DisplayName += "[]";
598  }
599 
600  return DisplayName;
601  }
602 
603  return CCopasiObject::getObjectDisplayName(regular, richtext);
604 }
CCopasiContainer * getObjectAncestor(const std::string &type) const
bool remove(const std::string &key)
std::string getUniqueParameterName(const CCopasiParameter *pParameter) const
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
bool setValue(const CType &value)
objectType
static const std::string TypeName[]
bool isValidValue(const C_FLOAT64 &value) const
virtual CCopasiObjectName getCN() const
const std::string & getObjectName() const
CCopasiParameter::Type mType
CCopasiParameter & operator=(const CCopasiParameter &rhs)
CCopasiObject * mpValueReference
std::vector< CCopasiParameter * > * pGROUP
#define fatalError()
CCopasiObject * getValueReference() const
bool operator==(const CCopasiParameter &lhs, const CCopasiParameter &rhs)
virtual void * getValuePointer() const
const std::string & getObjectType() const
#define C_UNUSED(p)
Definition: copasi.h:220
static bool isValidKey(const std::string &key, const std::string &prefix="")
Definition: CKeyFactory.cpp:30
#define C_INT32
Definition: copasi.h:90
static std::string escape(const std::string &name)
CRegisteredObjectName * pCN
const CCopasiParameter::Type & getType() const
std::vector< CCopasiParameter * > parameterGroup
std::ostream & operator<<(std::ostream &os, const CCopasiParameter &o)
virtual const std::string & getKey() const
const Value & getValue() const
unsigned C_INT32 * pUINT
virtual void print(std::ostream *ostream) const
Value createValue(const Value &value)
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
virtual CCopasiObjectName getCN() const
static CKeyFactory * getKeyFactory()
#define C_FLOAT64
Definition: copasi.h:92
static const char * XMLType[]
static const CCopasiContainer * getRoot()
bool setObjectName(const std::string &name)
CCopasiObject * addObjectReference(const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
CCopasiContainer * getObjectParent() const